|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" We use x-goog-api-client header to report metrics. This module provides |
|
|
the constants and helper methods to construct x-goog-api-client header. |
|
|
""" |
|
|
|
|
|
import platform |
|
|
|
|
|
from google.auth import version |
|
|
|
|
|
|
|
|
API_CLIENT_HEADER = "x-goog-api-client" |
|
|
|
|
|
|
|
|
BYOID_HEADER_SECTION = "google-byoid-sdk" |
|
|
|
|
|
|
|
|
REQUEST_TYPE_ACCESS_TOKEN = "auth-request-type/at" |
|
|
REQUEST_TYPE_ID_TOKEN = "auth-request-type/it" |
|
|
REQUEST_TYPE_MDS_PING = "auth-request-type/mds" |
|
|
REQUEST_TYPE_REAUTH_START = "auth-request-type/re-start" |
|
|
REQUEST_TYPE_REAUTH_CONTINUE = "auth-request-type/re-cont" |
|
|
|
|
|
|
|
|
CRED_TYPE_USER = "cred-type/u" |
|
|
CRED_TYPE_SA_ASSERTION = "cred-type/sa" |
|
|
CRED_TYPE_SA_JWT = "cred-type/jwt" |
|
|
CRED_TYPE_SA_MDS = "cred-type/mds" |
|
|
CRED_TYPE_SA_IMPERSONATE = "cred-type/imp" |
|
|
|
|
|
|
|
|
|
|
|
def python_and_auth_lib_version(): |
|
|
return "gl-python/{} auth/{}".format(platform.python_version(), version.__version__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_access_token_mds(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_MDS |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_id_token_mds(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_MDS |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_access_token_impersonate(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), |
|
|
REQUEST_TYPE_ACCESS_TOKEN, |
|
|
CRED_TYPE_SA_IMPERSONATE, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_id_token_impersonate(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_IMPERSONATE |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_access_token_sa_assertion(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_ASSERTION |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_id_token_sa_assertion(): |
|
|
return "{} {} {}".format( |
|
|
python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_ASSERTION |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def token_request_user(): |
|
|
return "{} {}".format(python_and_auth_lib_version(), CRED_TYPE_USER) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mds_ping(): |
|
|
return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_MDS_PING) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reauth_start(): |
|
|
return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_START) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reauth_continue(): |
|
|
return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_CONTINUE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def byoid_metrics_header(metrics_options): |
|
|
header = "{} {}".format(python_and_auth_lib_version(), BYOID_HEADER_SECTION) |
|
|
for key, value in metrics_options.items(): |
|
|
header = "{} {}/{}".format(header, key, value) |
|
|
return header |
|
|
|
|
|
|
|
|
def add_metric_header(headers, metric_header_value): |
|
|
"""Add x-goog-api-client header with the given value. |
|
|
|
|
|
Args: |
|
|
headers (Mapping[str, str]): The headers to which we will add the |
|
|
metric header. |
|
|
metric_header_value (Optional[str]): If value is None, do nothing; |
|
|
if headers already has a x-goog-api-client header, append the value |
|
|
to the existing header; otherwise add a new x-goog-api-client |
|
|
header with the given value. |
|
|
""" |
|
|
if not metric_header_value: |
|
|
return |
|
|
if API_CLIENT_HEADER not in headers: |
|
|
headers[API_CLIENT_HEADER] = metric_header_value |
|
|
else: |
|
|
headers[API_CLIENT_HEADER] += " " + metric_header_value |
|
|
|