Skip to content

Commit 3642c82

Browse files
authored
Merge pull request rails#55353 from drcapulet/alexc-gcp-metadata-server
Remove unnecessary calls to the GCP metadata server
2 parents 781394f + bd573b0 commit 3642c82

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

activestorage/CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
* Remove unnecessary calls to the GCP metadata server.
2+
3+
Calling Google::Auth.get_application_default triggers an explicit call to
4+
the metadata server - given it was being called for significant number of
5+
file operations, it can lead to considerable tail latencies and even metadata
6+
server overloads. Instead, it's preferable (and significantly more efficient)
7+
that applications use:
8+
9+
```ruby
10+
Google::Apis::RequestOptions.default.authorization = Google::Auth.get_application_default(...)
11+
```
12+
13+
In the cases applications do not set that, the GCP libraries automatically determine credentials.
14+
15+
This also enables using credentials other than those of the associated GCP
16+
service account like when using impersonation.
17+
18+
*Alex Coomans*
19+
120
* Direct upload progress accounts for server processing time.
221

322
*Jeremy Daer*

activestorage/lib/active_storage/service/gcs_service.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,16 @@ def signer
213213
lambda do |string_to_sign|
214214
iam_client = Google::Apis::IamcredentialsV1::IAMCredentialsService.new
215215

216-
scopes = ["https://www.googleapis.com/auth/iam"]
217-
iam_client.authorization = Google::Auth.get_application_default(scopes)
216+
# We explicitly do not set iam_client.authorization so that it uses the
217+
# credentials set by the application at Google::Apis::RequestOptions.default.authorization.
218+
# If the application does not set it, the GCP libraries will automatically
219+
# determine it on each call. This code previously explicitly set the
220+
# authorization to Google::Auth.get_application_default which triggers
221+
# an explicit call to the metadata server - given this lambda is called
222+
# for a significant number of file operations, it can lead to considerable
223+
# tail latencies and even metadata server overloads. Additionally, that
224+
# prevented applications from being able to configure the credentials
225+
# used to perform the signature operation.
218226

219227
request = Google::Apis::IamcredentialsV1::SignBlobRequest.new(
220228
payload: string_to_sign

0 commit comments

Comments
 (0)