@@ -10,16 +10,12 @@ class Real
10
10
11
11
def initialize ( options = { } )
12
12
shared_initialize ( options [ :google_project ] , GOOGLE_STORAGE_JSON_API_VERSION , GOOGLE_STORAGE_JSON_BASE_URL )
13
+ @options = options . dup
13
14
options [ :google_api_scope_url ] = GOOGLE_STORAGE_JSON_API_SCOPE_URLS . join ( " " )
14
15
@host = options [ :host ] || "storage.googleapis.com"
15
16
16
17
# TODO(temikus): Do we even need this client?
17
18
@client = initialize_google_client ( options )
18
- # IAM client used for SignBlob API
19
- @iam_service = ::Google ::Apis ::IamcredentialsV1 ::IAMCredentialsService . new
20
- apply_client_options ( @iam_service , {
21
- google_api_scope_url : GOOGLE_STORAGE_JSON_IAM_API_SCOPE_URLS . join ( " " )
22
- } )
23
19
24
20
@storage_json = ::Google ::Apis ::StorageV1 ::StorageService . new
25
21
apply_client_options ( @storage_json , options )
@@ -141,6 +137,18 @@ def default_signer(string_to_sign)
141
137
return key . sign ( digest , string_to_sign )
142
138
end
143
139
140
+ # IAM client used for SignBlob API.
141
+ # Lazily initialize this since it requires another authorization request.
142
+ def iam_service
143
+ return @iam_service if defined? ( @iam_service )
144
+
145
+ @iam_service = ::Google ::Apis ::IamcredentialsV1 ::IAMCredentialsService . new
146
+ apply_client_options ( @iam_service , @options )
147
+ iam_options = @options . merge ( google_api_scope_url : GOOGLE_STORAGE_JSON_IAM_API_SCOPE_URLS . join ( " " ) )
148
+ @iam_service . authorization = initialize_auth ( iam_options )
149
+ @iam_service
150
+ end
151
+
144
152
##
145
153
# Fallback URL signer using the IAM SignServiceAccountBlob API, see
146
154
# Google::Apis::IamcredentialsV1::IAMCredentialsService#sign_service_account_blob
@@ -162,7 +170,7 @@ def iam_signer(string_to_sign)
162
170
)
163
171
164
172
resource = "projects/-/serviceAccounts/#{ google_access_id } "
165
- response = @ iam_service. sign_service_account_blob ( resource , request )
173
+ response = iam_service . sign_service_account_blob ( resource , request )
166
174
167
175
return response . signed_blob
168
176
end
0 commit comments