in nl2sql_library/nl2sql/commons/reporting/fingerprint.py [0:0]
def user_info() -> dict[str, Any]:
"""
Provides an ID of the currently logged in user
"""
user_info_gathered: dict[str, Any] = {}
try:
user_info_gathered["userid_gcloud_cli"] = subprocess.run(
"gcloud config get-value account",
shell=True,
capture_output=True,
text=True,
check=False,
).stdout.strip()
except Exception as exc: # pylint: disable=broad-exception-caught
logger.warning(str(exc))
user_info_gathered["userid_gcloud_cli"] = None
try:
creds, _ = google.auth.default()
creds.refresh(GoogleAuthRequest())
user_info_gathered["userid_google_auth_sdk"] = getattr(
creds, "service_account_email", None
)
except Exception as exc: # pylint: disable=broad-exception-caught
logger.warning(str(exc))
user_info_gathered["userid_google_auth_sdk"] = None
try:
user_info_gathered["userid_computeMetadata"] = requests.get(
(
"http://metadata.google.internal/computeMetadata/"
"v1/instance/service-accounts/default/email"
),
headers={"Metadata-Flavor": "Google"},
timeout=60,
).text
except Exception as exc: # pylint: disable=broad-exception-caught
logger.warning(str(exc))
user_info_gathered["userid_computeMetadata"] = None
try:
user_info_gathered["userid_tokeninfo"] = requests.get(
"https://www.googleapis.com/oauth2/v3/tokeninfo",
params={
"access_token": subprocess.run(
"gcloud auth print-access-token",
shell=True,
capture_output=True,
text=True,
check=False,
).stdout.strip()
},
timeout=60,
).json()["email"]
except Exception as exc: # pylint: disable=broad-exception-caught
logger.warning(str(exc))
user_info_gathered["userid_tokeninfo"] = None
userids = set(user_info_gathered.values())
userids.discard(None)
user_info_gathered["userid"] = (userids or {None}).pop()
return user_info_gathered