Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/google/auth/metrics.py: 57%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2023 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15""" We use x-goog-api-client header to report metrics. This module provides
16the constants and helper methods to construct x-goog-api-client header.
17"""
19import platform
21from google.auth import version
24API_CLIENT_HEADER = "x-goog-api-client"
26# BYOID Specific consts
27BYOID_HEADER_SECTION = "google-byoid-sdk"
29# Auth request type
30REQUEST_TYPE_ACCESS_TOKEN = "auth-request-type/at"
31REQUEST_TYPE_ID_TOKEN = "auth-request-type/it"
32REQUEST_TYPE_MDS_PING = "auth-request-type/mds"
33REQUEST_TYPE_REAUTH_START = "auth-request-type/re-start"
34REQUEST_TYPE_REAUTH_CONTINUE = "auth-request-type/re-cont"
36# Credential type
37CRED_TYPE_USER = "cred-type/u"
38CRED_TYPE_SA_ASSERTION = "cred-type/sa"
39CRED_TYPE_SA_JWT = "cred-type/jwt"
40CRED_TYPE_SA_MDS = "cred-type/mds"
41CRED_TYPE_SA_IMPERSONATE = "cred-type/imp"
44# Versions
45def python_and_auth_lib_version():
46 return "gl-python/{} auth/{}".format(platform.python_version(), version.__version__)
49# Token request metric header values
52# x-goog-api-client header value for access token request via metadata server.
53# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/mds"
54def token_request_access_token_mds():
55 return "{} {} {}".format(
56 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_MDS
57 )
60# x-goog-api-client header value for ID token request via metadata server.
61# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/mds"
62def token_request_id_token_mds():
63 return "{} {} {}".format(
64 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_MDS
65 )
68# x-goog-api-client header value for impersonated credentials access token request.
69# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
70def token_request_access_token_impersonate():
71 return "{} {} {}".format(
72 python_and_auth_lib_version(),
73 REQUEST_TYPE_ACCESS_TOKEN,
74 CRED_TYPE_SA_IMPERSONATE,
75 )
78# x-goog-api-client header value for impersonated credentials ID token request.
79# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/imp"
80def token_request_id_token_impersonate():
81 return "{} {} {}".format(
82 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_IMPERSONATE
83 )
86# x-goog-api-client header value for service account credentials access token
87# request (assertion flow).
88# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/sa"
89def token_request_access_token_sa_assertion():
90 return "{} {} {}".format(
91 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_ASSERTION
92 )
95# x-goog-api-client header value for service account credentials ID token
96# request (assertion flow).
97# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/sa"
98def token_request_id_token_sa_assertion():
99 return "{} {} {}".format(
100 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_ASSERTION
101 )
104# x-goog-api-client header value for user credentials token request.
105# Example: "gl-python/3.7 auth/1.1 cred-type/u"
106def token_request_user():
107 return "{} {}".format(python_and_auth_lib_version(), CRED_TYPE_USER)
110# Miscellenous metrics
113# x-goog-api-client header value for metadata server ping.
114# Example: "gl-python/3.7 auth/1.1 auth-request-type/mds"
115def mds_ping():
116 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_MDS_PING)
119# x-goog-api-client header value for reauth start endpoint calls.
120# Example: "gl-python/3.7 auth/1.1 auth-request-type/re-start"
121def reauth_start():
122 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_START)
125# x-goog-api-client header value for reauth continue endpoint calls.
126# Example: "gl-python/3.7 auth/1.1 cred-type/re-cont"
127def reauth_continue():
128 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_CONTINUE)
131# x-goog-api-client header value for BYOID calls to the Security Token Service exchange token endpoint.
132# Example: "gl-python/3.7 auth/1.1 google-byoid-sdk source/aws sa-impersonation/true sa-impersonation/true"
133def byoid_metrics_header(metrics_options):
134 header = "{} {}".format(python_and_auth_lib_version(), BYOID_HEADER_SECTION)
135 for key, value in metrics_options.items():
136 header = "{} {}/{}".format(header, key, value)
137 return header
140def add_metric_header(headers, metric_header_value):
141 """Add x-goog-api-client header with the given value.
143 Args:
144 headers (Mapping[str, str]): The headers to which we will add the
145 metric header.
146 metric_header_value (Optional[str]): If value is None, do nothing;
147 if headers already has a x-goog-api-client header, append the value
148 to the existing header; otherwise add a new x-goog-api-client
149 header with the given value.
150 """
151 if not metric_header_value:
152 return
153 if API_CLIENT_HEADER not in headers:
154 headers[API_CLIENT_HEADER] = metric_header_value
155 else:
156 headers[API_CLIENT_HEADER] += " " + metric_header_value