Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/auth/metrics.py: 62%
42 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:03 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:03 +0000
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# Auth request type
27REQUEST_TYPE_ACCESS_TOKEN = "auth-request-type/at"
28REQUEST_TYPE_ID_TOKEN = "auth-request-type/it"
29REQUEST_TYPE_MDS_PING = "auth-request-type/mds"
30REQUEST_TYPE_REAUTH_START = "auth-request-type/re-start"
31REQUEST_TYPE_REAUTH_CONTINUE = "auth-request-type/re-cont"
33# Credential type
34CRED_TYPE_USER = "cred-type/u"
35CRED_TYPE_SA_ASSERTION = "cred-type/sa"
36CRED_TYPE_SA_JWT = "cred-type/jwt"
37CRED_TYPE_SA_MDS = "cred-type/mds"
38CRED_TYPE_SA_IMPERSONATE = "cred-type/imp"
41# Versions
42def python_and_auth_lib_version():
43 return "gl-python/{} auth/{}".format(platform.python_version(), version.__version__)
46# Token request metric header values
48# x-goog-api-client header value for access token request via metadata server.
49# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/mds"
50def token_request_access_token_mds():
51 return "{} {} {}".format(
52 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_MDS
53 )
56# x-goog-api-client header value for ID token request via metadata server.
57# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/mds"
58def token_request_id_token_mds():
59 return "{} {} {}".format(
60 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_MDS
61 )
64# x-goog-api-client header value for impersonated credentials access token request.
65# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
66def token_request_access_token_impersonate():
67 return "{} {} {}".format(
68 python_and_auth_lib_version(),
69 REQUEST_TYPE_ACCESS_TOKEN,
70 CRED_TYPE_SA_IMPERSONATE,
71 )
74# x-goog-api-client header value for impersonated credentials ID token request.
75# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/imp"
76def token_request_id_token_impersonate():
77 return "{} {} {}".format(
78 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_IMPERSONATE
79 )
82# x-goog-api-client header value for service account credentials access token
83# request (assertion flow).
84# Example: "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/sa"
85def token_request_access_token_sa_assertion():
86 return "{} {} {}".format(
87 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_ASSERTION
88 )
91# x-goog-api-client header value for service account credentials ID token
92# request (assertion flow).
93# Example: "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/sa"
94def token_request_id_token_sa_assertion():
95 return "{} {} {}".format(
96 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_ASSERTION
97 )
100# x-goog-api-client header value for user credentials token request.
101# Example: "gl-python/3.7 auth/1.1 cred-type/u"
102def token_request_user():
103 return "{} {}".format(python_and_auth_lib_version(), CRED_TYPE_USER)
106# Miscellenous metrics
108# x-goog-api-client header value for metadata server ping.
109# Example: "gl-python/3.7 auth/1.1 auth-request-type/mds"
110def mds_ping():
111 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_MDS_PING)
114# x-goog-api-client header value for reauth start endpoint calls.
115# Example: "gl-python/3.7 auth/1.1 auth-request-type/re-start"
116def reauth_start():
117 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_START)
120# x-goog-api-client header value for reauth continue endpoint calls.
121# Example: "gl-python/3.7 auth/1.1 cred-type/re-cont"
122def reauth_continue():
123 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_CONTINUE)
126def add_metric_header(headers, metric_header_value):
127 """Add x-goog-api-client header with the given value.
129 Args:
130 headers (Mapping[str, str]): The headers to which we will add the
131 metric header.
132 metric_header_value (Optional[str]): If value is None, do nothing;
133 if headers already has a x-goog-api-client header, append the value
134 to the existing header; otherwise add a new x-goog-api-client
135 header with the given value.
136 """
137 if not metric_header_value:
138 return
139 if API_CLIENT_HEADER not in headers:
140 headers[API_CLIENT_HEADER] = metric_header_value
141 else:
142 headers[API_CLIENT_HEADER] += " " + metric_header_value