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

49 statements  

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. 

14 

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""" 

18 

19import platform 

20 

21from google.auth import version 

22 

23API_CLIENT_HEADER = "x-goog-api-client" 

24 

25# BYOID Specific consts 

26BYOID_HEADER_SECTION = "google-byoid-sdk" 

27 

28# Auth request type 

29REQUEST_TYPE_ACCESS_TOKEN = "auth-request-type/at" 

30REQUEST_TYPE_ID_TOKEN = "auth-request-type/it" 

31REQUEST_TYPE_MDS_PING = "auth-request-type/mds" 

32REQUEST_TYPE_REAUTH_START = "auth-request-type/re-start" 

33REQUEST_TYPE_REAUTH_CONTINUE = "auth-request-type/re-cont" 

34 

35# Credential type 

36CRED_TYPE_USER = "cred-type/u" 

37CRED_TYPE_SA_ASSERTION = "cred-type/sa" 

38CRED_TYPE_SA_JWT = "cred-type/jwt" 

39CRED_TYPE_SA_MDS = "cred-type/mds" 

40CRED_TYPE_SA_IMPERSONATE = "cred-type/imp" 

41 

42 

43# Versions 

44def python_and_auth_lib_version(): 

45 return "gl-python/{} auth/{}".format(platform.python_version(), version.__version__) 

46 

47 

48# Token request metric header values 

49 

50 

51# x-goog-api-client header value for access token request via metadata server. 

52# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/at cred-type/mds" 

53def token_request_access_token_mds(): 

54 return "{} {} {}".format( 

55 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_MDS 

56 ) 

57 

58 

59# x-goog-api-client header value for ID token request via metadata server. 

60# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/it cred-type/mds" 

61def token_request_id_token_mds(): 

62 return "{} {} {}".format( 

63 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_MDS 

64 ) 

65 

66 

67# x-goog-api-client header value for impersonated credentials access token request. 

68# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/at cred-type/imp" 

69def token_request_access_token_impersonate(): 

70 return "{} {} {}".format( 

71 python_and_auth_lib_version(), 

72 REQUEST_TYPE_ACCESS_TOKEN, 

73 CRED_TYPE_SA_IMPERSONATE, 

74 ) 

75 

76 

77# x-goog-api-client header value for impersonated credentials ID token request. 

78# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/it cred-type/imp" 

79def token_request_id_token_impersonate(): 

80 return "{} {} {}".format( 

81 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_IMPERSONATE 

82 ) 

83 

84 

85# x-goog-api-client header value for service account credentials access token 

86# request (assertion flow). 

87# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/at cred-type/sa" 

88def token_request_access_token_sa_assertion(): 

89 return "{} {} {}".format( 

90 python_and_auth_lib_version(), REQUEST_TYPE_ACCESS_TOKEN, CRED_TYPE_SA_ASSERTION 

91 ) 

92 

93 

94# x-goog-api-client header value for service account credentials ID token 

95# request (assertion flow). 

96# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/it cred-type/sa" 

97def token_request_id_token_sa_assertion(): 

98 return "{} {} {}".format( 

99 python_and_auth_lib_version(), REQUEST_TYPE_ID_TOKEN, CRED_TYPE_SA_ASSERTION 

100 ) 

101 

102 

103# x-goog-api-client header value for user credentials token request. 

104# Example: "gl-python/<python-version> auth/<library-version> cred-type/u" 

105def token_request_user(): 

106 return "{} {}".format(python_and_auth_lib_version(), CRED_TYPE_USER) 

107 

108 

109# Miscellenous metrics 

110 

111 

112# x-goog-api-client header value for metadata server ping. 

113# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/mds" 

114def mds_ping(): 

115 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_MDS_PING) 

116 

117 

118# x-goog-api-client header value for reauth start endpoint calls. 

119# Example: "gl-python/<python-version> auth/<library-version> auth-request-type/re-start" 

120def reauth_start(): 

121 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_START) 

122 

123 

124# x-goog-api-client header value for reauth continue endpoint calls. 

125# Example: "gl-python/<python-version> auth/<library-version> cred-type/re-cont" 

126def reauth_continue(): 

127 return "{} {}".format(python_and_auth_lib_version(), REQUEST_TYPE_REAUTH_CONTINUE) 

128 

129 

130# x-goog-api-client header value for BYOID calls to the Security Token Service exchange token endpoint. 

131# Example: "gl-python/<python-version> auth/<library-version> google-byoid-sdk source/aws sa-impersonation/true sa-impersonation/true" 

132def byoid_metrics_header(metrics_options): 

133 header = "{} {}".format(python_and_auth_lib_version(), BYOID_HEADER_SECTION) 

134 for key, value in metrics_options.items(): 

135 header = "{} {}/{}".format(header, key, value) 

136 return header 

137 

138 

139def add_metric_header(headers, metric_header_value): 

140 """Add x-goog-api-client header with the given value. 

141 

142 Args: 

143 headers (Mapping[str, str]): The headers to which we will add the 

144 metric header. 

145 metric_header_value (Optional[str]): If value is None, do nothing; 

146 if headers already has a x-goog-api-client header, append the value 

147 to the existing header; otherwise add a new x-goog-api-client 

148 header with the given value. 

149 """ 

150 if not metric_header_value: 

151 return 

152 if API_CLIENT_HEADER not in headers: 

153 headers[API_CLIENT_HEADER] = metric_header_value 

154 else: 

155 headers[API_CLIENT_HEADER] += " " + metric_header_value