googleauth 0.9.0 → 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CHANGELOG.md +113 -21
  4. data/README.md +13 -15
  5. data/SECURITY.md +7 -0
  6. data/lib/googleauth/application_default.rb +9 -9
  7. data/lib/googleauth/compute_engine.rb +55 -30
  8. data/lib/googleauth/credentials.rb +253 -64
  9. data/lib/googleauth/credentials_loader.rb +15 -16
  10. data/lib/googleauth/iam.rb +1 -1
  11. data/{spec/googleauth/stores/store_examples.rb → lib/googleauth/id_tokens/errors.rb} +36 -23
  12. data/lib/googleauth/id_tokens/key_sources.rb +396 -0
  13. data/lib/googleauth/id_tokens/verifier.rb +142 -0
  14. data/lib/googleauth/id_tokens.rb +233 -0
  15. data/lib/googleauth/json_key_reader.rb +6 -2
  16. data/lib/googleauth/scope_util.rb +1 -1
  17. data/lib/googleauth/service_account.rb +61 -36
  18. data/lib/googleauth/signet.rb +9 -7
  19. data/lib/googleauth/stores/file_token_store.rb +1 -0
  20. data/lib/googleauth/stores/redis_token_store.rb +1 -0
  21. data/lib/googleauth/user_authorizer.rb +8 -3
  22. data/lib/googleauth/user_refresh.rb +1 -1
  23. data/lib/googleauth/version.rb +1 -1
  24. data/lib/googleauth/web_user_authorizer.rb +5 -8
  25. data/lib/googleauth.rb +1 -0
  26. metadata +33 -76
  27. data/.github/CONTRIBUTING.md +0 -74
  28. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -36
  29. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -21
  30. data/.github/ISSUE_TEMPLATE/support_request.md +0 -7
  31. data/.gitignore +0 -36
  32. data/.kokoro/build.bat +0 -16
  33. data/.kokoro/build.sh +0 -4
  34. data/.kokoro/continuous/common.cfg +0 -24
  35. data/.kokoro/continuous/linux.cfg +0 -15
  36. data/.kokoro/continuous/osx.cfg +0 -3
  37. data/.kokoro/continuous/windows.cfg +0 -19
  38. data/.kokoro/osx.sh +0 -4
  39. data/.kokoro/presubmit/common.cfg +0 -24
  40. data/.kokoro/presubmit/linux.cfg +0 -14
  41. data/.kokoro/presubmit/osx.cfg +0 -3
  42. data/.kokoro/presubmit/windows.cfg +0 -19
  43. data/.kokoro/release.cfg +0 -53
  44. data/.kokoro/trampoline.bat +0 -10
  45. data/.kokoro/trampoline.sh +0 -4
  46. data/.rspec +0 -2
  47. data/.rubocop.yml +0 -42
  48. data/Gemfile +0 -25
  49. data/Rakefile +0 -89
  50. data/googleauth.gemspec +0 -35
  51. data/spec/googleauth/apply_auth_examples.rb +0 -148
  52. data/spec/googleauth/client_id_spec.rb +0 -160
  53. data/spec/googleauth/compute_engine_spec.rb +0 -122
  54. data/spec/googleauth/credentials_spec.rb +0 -459
  55. data/spec/googleauth/get_application_default_spec.rb +0 -286
  56. data/spec/googleauth/iam_spec.rb +0 -80
  57. data/spec/googleauth/scope_util_spec.rb +0 -77
  58. data/spec/googleauth/service_account_spec.rb +0 -482
  59. data/spec/googleauth/signet_spec.rb +0 -134
  60. data/spec/googleauth/stores/file_token_store_spec.rb +0 -57
  61. data/spec/googleauth/stores/redis_token_store_spec.rb +0 -50
  62. data/spec/googleauth/user_authorizer_spec.rb +0 -323
  63. data/spec/googleauth/user_refresh_spec.rb +0 -359
  64. data/spec/googleauth/web_user_authorizer_spec.rb +0 -172
  65. data/spec/spec_helper.rb +0 -92
  66. /data/{COPYING → LICENSE} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e97bb2d3af353d706c6b608e1743fb3f571e62530201ffa3bebcae2c7e6460bb
4
- data.tar.gz: 3c06cad2ea956d09a37783516292c61f754706366a2b8bf03ef7928769762070
3
+ metadata.gz: 1cc321053063d0fcbe9b0ac9cece62227049fca62bb4377161cd0679342ceee9
4
+ data.tar.gz: 635e4992df0bfc21fe3df120dc86347619324e685ca136aa6770c23b4104153a
5
5
  SHA512:
6
- metadata.gz: 68ad7978f7d5abcc14fbd4ca668ba24f69e4bef2c184427b1a70a8c57e2d742b636d5c0b8da10f4d62f8762adb0075d20c1eb75a76302f9f9b6e61cd1b989685
7
- data.tar.gz: 25333a0a26181c8f8f0464642eccfe0340ed591a4c11f9fe3ed4b85179524c37100431f686a16a75f2985ade010e3b488aeb60a6c1a06e0109db46efa9e1d620
6
+ metadata.gz: 19b49461310e8b41a4062005255d51c15792481183c6fc161baf36a13e40ba1528d604ef8c17048de1661a41dfe7de6867fab3b721cd1be3b148b1c5a15f8a97
7
+ data.tar.gz: 2ae55a1ad27def042196075cb8c5e46db5295797edc568126903ccd7e345a2b7400d5a30f3d79d7001588a1c25ec9fcb12ea128dfc06234dd67077a3c1aae0af
data/.yardopts ADDED
@@ -0,0 +1,11 @@
1
+ --no-private
2
+ --title=Google Auth
3
+ --markup markdown
4
+ --markup-provider redcarpet
5
+
6
+ ./lib/**/*.rb
7
+ -
8
+ README.md
9
+ CHANGELOG.md
10
+ CODE_OF_CONDUCT.md
11
+ LICENSE
data/CHANGELOG.md CHANGED
@@ -1,4 +1,95 @@
1
- ### 0.9.0 / 2019-08-05
1
+ # Release History
2
+
3
+ ### [0.17.1](https://www.github.com/googleapis/google-auth-library-ruby/compare/googleauth/v0.15.0...googleauth/v0.17.1) (2021-09-01)
4
+
5
+ ### Bug Fixes
6
+
7
+ * Updates to gem metadata ([fb5e56d](https://www.github.com/googleapis/google-auth-library-ruby/commit/fb5e56dad1e6ed6afd4f9b5c626e5e1495e48343))
8
+
9
+ ## [0.17.0](https://www.github.com/googleapis/google-auth-library-ruby/compare/google-auth-library-ruby/v0.16.2...google-auth-library-ruby/v0.17.0) (2021-07-30)
10
+
11
+
12
+ ### Features
13
+
14
+ * Allow scopes to be self-signed into jwts ([e67ce40](https://www.github.com/googleapis/google-auth-library-ruby/commit/e67ce40f919b7eb3723c2ec95f5b8d58315ab1ee))
15
+
16
+ ### [0.16.2](https://www.github.com/googleapis/google-auth-library-ruby/compare/google-auth-library-ruby/v0.16.1...google-auth-library-ruby/v0.16.2) (2021-04-28)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * Stop attempting to get the project from gcloud when applying self-signed JWTs ([#317](https://www.github.com/googleapis/google-auth-library-ruby/issues/317)) ([39258ca](https://www.github.com/googleapis/google-auth-library-ruby/commit/39258cacafa5c770fb40d99075a97b8e6427adba))
22
+
23
+ ### [0.16.1](https://www.github.com/googleapis/google-auth-library-ruby/compare/google-auth-library-ruby/v0.16.0...google-auth-library-ruby/v0.16.1) (2021-04-01)
24
+
25
+
26
+ ### Bug Fixes
27
+
28
+ * Accept application/text content-type for plain idtoken response ([4948ebb](https://www.github.com/googleapis/google-auth-library-ruby/commit/4948ebb3ca151e9f0433585a41bad6f415416b2d))
29
+
30
+ ## [0.16.0](https://www.github.com/googleapis/google-auth-library-ruby/compare/v0.15.1...v0.16.0) (2021-03-04)
31
+
32
+
33
+ ### Features
34
+
35
+ * Drop support for Ruby 2.4 and add support for Ruby 3.0 ([6644806](https://www.github.com/googleapis/google-auth-library-ruby/commit/6644806ab47cea6d08e1901c2ed808e53a579bc3))
36
+
37
+ ## [0.15.1](https://www.github.com/googleapis/google-auth-library-ruby/compare/v0.15.0...v0.15.1) (2021-02-08)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * Fix crash when using a client credential without any paths or env_vars set ([#296](https://www.github.com/googleapis/google-auth-library-ruby/issues/296)) ([c971c1a](https://www.github.com/googleapis/google-auth-library-ruby/commit/c971c1ad2d7730c0f5b389d533a972be32fbaf49))
43
+
44
+ ## [0.15.0](https://www.github.com/googleapis/google-auth-library-ruby/compare/v0.14.0...v0.15.0) (2021-01-26)
45
+
46
+
47
+ ### Features
48
+
49
+ * Credential parameters inherit from superclasses ([4fa4720](https://www.github.com/googleapis/google-auth-library-ruby/commit/4fa47206dbd62f8bbdd1b9d3721f6baee9fd1d62))
50
+ * Service accounts apply a self-signed JWT if scopes are marked as default ([d22acb8](https://www.github.com/googleapis/google-auth-library-ruby/commit/d22acb8a510e6711b5674545c31a4816e5a9168f))
51
+
52
+
53
+ ### Bug Fixes
54
+
55
+ * Retry fetch_access_token when GCE metadata server returns unexpected errors ([cd9b012](https://www.github.com/googleapis/google-auth-library-ruby/commit/cd9b0126d3419b9953982f71edc9e6ba3f640e3c))
56
+ * Support correct service account and user refresh behavior for custom credential env variables ([d2dffe5](https://www.github.com/googleapis/google-auth-library-ruby/commit/d2dffe592112b45006291ad9a57f56e00fb208c3))
57
+
58
+ ## 0.14.0 / 2020-10-09
59
+
60
+ * Honor GCE_METADATA_HOST environment variable
61
+ * Fix errors in some environments when requesting an access token for multiple scopes
62
+
63
+ ## 0.13.1 / 2020-07-30
64
+
65
+ * Support scopes when using GCE Metadata Server authentication ([@ball-hayden][])
66
+
67
+ ## 0.13.0 / 2020-06-17
68
+
69
+ * Support for validating ID tokens.
70
+ * Fixed header application of ID tokens from service accounts.
71
+
72
+ ## 0.12.0 / 2020-04-08
73
+
74
+ * Support for ID token credentials.
75
+ * Support reading quota_id_project from service account credentials.
76
+
77
+ ## 0.11.0 / 2020-02-24
78
+
79
+ * Support Faraday 1.x.
80
+ * Allow special "postmessage" value for redirect_uri.
81
+
82
+ ## 0.10.0 / 2019-10-09
83
+
84
+ Note: This release now requires Ruby 2.4 or later
85
+
86
+ * Increase metadata timeout to improve reliability in some hosting environments
87
+ * Support an environment variable to suppress Cloud SDK credentials warnings
88
+ * Make the header check case insensitive
89
+ * Set instance variables at initialization to avoid spamming warnings
90
+ * Pass "Metadata-Flavor" header to metadata server when checking for GCE
91
+
92
+ ## 0.9.0 / 2019-08-05
2
93
 
3
94
  * Restore compatibility with Ruby 2.0. This is the last release that will work on end-of-lifed versions of Ruby. The 0.10 release will require Ruby 2.4 or later.
4
95
  * Update Credentials to use methods for values that are intended to be changed by users, replacing constants.
@@ -7,79 +98,79 @@
7
98
  * Add verbosity none to gcloud command
8
99
  * Make arity of WebUserAuthorizer#get_credentials compatible with the base class
9
100
 
10
- ### 0.8.1 / 2019-03-27
101
+ ## 0.8.1 / 2019-03-27
11
102
 
12
103
  * Silence unnecessary gcloud warning
13
104
  * Treat empty credentials environment variables as unset
14
105
 
15
- ### 0.8.0 / 2019-01-02
106
+ ## 0.8.0 / 2019-01-02
16
107
 
17
108
  * Support connection options :default_connection and :connection_builder when creating credentials that need to refresh OAuth tokens. This lets clients provide connection objects with custom settings, such as proxies, needed for the client environment.
18
109
  * Removed an unnecessary warning about project IDs.
19
110
 
20
- ### 0.7.1 / 2018-10-25
111
+ ## 0.7.1 / 2018-10-25
21
112
 
22
113
  * Make load_gcloud_project_id module function.
23
114
 
24
- ### 0.7.0 / 2018-10-24
115
+ ## 0.7.0 / 2018-10-24
25
116
 
26
117
  * Add project_id instance variable to UserRefreshCredentials, ServiceAccountCredentials, and Credentials.
27
118
 
28
- ### 0.6.7 / 2018-10-16
119
+ ## 0.6.7 / 2018-10-16
29
120
 
30
121
  * Update memoist dependency to ~> 0.16.
31
122
 
32
- ### 0.6.6 / 2018-08-22
123
+ ## 0.6.6 / 2018-08-22
33
124
 
34
125
  * Remove ruby version warnings.
35
126
 
36
- ### 0.6.5 / 2018-08-16
127
+ ## 0.6.5 / 2018-08-16
37
128
 
38
129
  * Fix incorrect http verb when revoking credentials.
39
130
  * Warn on EOL ruby versions.
40
131
 
41
- ### 0.6.4 / 2018-08-03
132
+ ## 0.6.4 / 2018-08-03
42
133
 
43
134
  * Resolve issue where DefaultCredentials constant was undefined.
44
135
 
45
- ### 0.6.3 / 2018-08-02
136
+ ## 0.6.3 / 2018-08-02
46
137
 
47
138
  * Resolve issue where token_store was being written to twice
48
139
 
49
- ### 0.6.2 / 2018-08-01
140
+ ## 0.6.2 / 2018-08-01
50
141
 
51
142
  * Add warning when using cloud sdk credentials
52
143
 
53
- ### 0.6.1 / 2017-10-18
144
+ ## 0.6.1 / 2017-10-18
54
145
 
55
146
  * Fix file permissions
56
147
 
57
- ### 0.6.0 / 2017-10-17
148
+ ## 0.6.0 / 2017-10-17
58
149
 
59
150
  * Support ruby-jwt 2.0
60
151
  * Add simple credentials class
61
152
 
62
- ### 0.5.3 / 2017-07-21
153
+ ## 0.5.3 / 2017-07-21
63
154
 
64
155
  * Fix file permissions on the gem's `.rb` files.
65
156
 
66
- ### 0.5.2 / 2017-07-19
157
+ ## 0.5.2 / 2017-07-19
67
158
 
68
159
  * Add retry mechanism when fetching access tokens in `GCECredentials` and `UserRefreshCredentials` classes.
69
160
  * Update Google API OAuth2 token credential URI to v4.
70
161
 
71
- ### 0.5.1 / 2016-01-06
162
+ ## 0.5.1 / 2016-01-06
72
163
 
73
164
  * Change header name emitted by `Client#apply` from "Authorization" to "authorization" ([@murgatroid99][])
74
165
  * Fix ADC not working on some windows machines ([@vsubramani][])
75
166
  [#55](https://github.com/google/google-auth-library-ruby/issues/55)
76
167
 
77
- ### 0.5.0 / 2015-10-12
168
+ ## 0.5.0 / 2015-10-12
78
169
 
79
170
  * Initial support for user credentials ([@sqrrrl][])
80
171
  * Update Signet to 0.7
81
172
 
82
- ### 0.4.2 / 2015-08-05
173
+ ## 0.4.2 / 2015-08-05
83
174
 
84
175
  * Updated UserRefreshCredentials hash to use string keys ([@haabaato][])
85
176
  [#36](https://github.com/google/google-auth-library-ruby/issues/36)
@@ -96,16 +187,16 @@
96
187
  * Enables passing credentials via environment variables. ([@haabaato][])
97
188
  [#27](https://github.com/google/google-auth-library-ruby/issues/27)
98
189
 
99
- ### 0.4.1 / 2015-04-25
190
+ ## 0.4.1 / 2015-04-25
100
191
 
101
192
  * Improves handling of --no-scopes GCE authorization ([@tbetbetbe][])
102
193
  * Refactoring and cleanup ([@joneslee85][])
103
194
 
104
- ### 0.4.0 / 2015-03-25
195
+ ## 0.4.0 / 2015-03-25
105
196
 
106
197
  * Adds an implementation of JWT header auth ([@tbetbetbe][])
107
198
 
108
- ### 0.3.0 / 2015-03-23
199
+ ## 0.3.0 / 2015-03-23
109
200
 
110
201
  * makes the scope parameter's optional in all APIs. ([@tbetbetbe][])
111
202
  * changes the scope parameter's position in various constructors. ([@tbetbetbe][])
@@ -118,3 +209,4 @@
118
209
  [@tbetbetbe]: https://github.com/tbetbetbe
119
210
  [@murgatroid99]: https://github.com/murgatroid99
120
211
  [@vsubramani]: https://github.com/vsubramani
212
+ [@ball-hayden]: https://github.com/ball-hayden
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
1
  # Google Auth Library for Ruby
2
2
 
3
3
  <dl>
4
- <dt>Homepage</dt><dd><a href="http://www.github.com/google/google-auth-library-ruby">http://www.github.com/google/google-auth-library-ruby</a></dd>
4
+ <dt>Homepage</dt><dd><a href="http://www.github.com/googleapis/google-auth-library-ruby">http://www.github.com/googleapis/google-auth-library-ruby</a></dd>
5
5
  <dt>Authors</dt><dd><a href="mailto:temiola@google.com">Tim Emiola</a></dd>
6
6
  <dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd>
7
7
  <dt>License</dt><dd>Apache 2.0</dd>
8
8
  </dl>
9
9
 
10
10
  [![Gem Version](https://badge.fury.io/rb/googleauth.svg)](http://badge.fury.io/rb/googleauth)
11
- [![Coverage Status](https://coveralls.io/repos/google/google-auth-library-ruby/badge.svg)](https://coveralls.io/r/google/google-auth-library-ruby)
12
11
 
13
12
  ## Description
14
13
 
@@ -179,23 +178,23 @@ access and refresh tokens. Two storage implementations are included:
179
178
  * Google::Auth::Stores::RedisTokenStore
180
179
 
181
180
  Custom storage implementations can also be used. See
182
- [token_store.rb](lib/googleauth/token_store.rb) for additional details.
181
+ [token_store.rb](https://googleapis.dev/ruby/googleauth/latest/Google/Auth/TokenStore.html) for additional details.
183
182
 
184
183
  ## Supported Ruby Versions
185
184
 
186
- This library is currently supported on Ruby 2.3+.
185
+ This library is supported on Ruby 2.5+.
187
186
 
188
- However, Ruby 2.4 or later is strongly recommended, as earlier releases have
189
- reached or are nearing end-of-life. After March 31, 2019, Google will provide
190
- official support only for Ruby versions that are considered current and
191
- supported by Ruby Core (that is, Ruby versions that are either in normal
192
- maintenance or in security maintenance).
193
- See https://www.ruby-lang.org/en/downloads/branches/ for further details.
187
+ Google provides official support for Ruby versions that are actively supported
188
+ by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
189
+ security maintenance, and not end of life. Currently, this means Ruby 2.5 and
190
+ later. Older versions of Ruby _may_ still work, but are unsupported and not
191
+ recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
192
+ about the Ruby support schedule.
194
193
 
195
194
  ## License
196
195
 
197
196
  This library is licensed under Apache 2.0. Full license text is
198
- available in [COPYING][copying].
197
+ available in [LICENSE][license].
199
198
 
200
199
  ## Contributing
201
200
 
@@ -209,7 +208,6 @@ hesitate to
209
208
  [ask questions](http://stackoverflow.com/questions/tagged/google-auth-library-ruby)
210
209
  about the client or APIs on [StackOverflow](http://stackoverflow.com).
211
210
 
212
- [google-apis-ruby-client]: (https://github.com/google/google-api-ruby-client)
213
- [application default credentials]: (https://developers.google.com/accounts/docs/application-default-credentials)
214
- [contributing]: https://github.com/google/google-auth-library-ruby/tree/master/CONTRIBUTING.md
215
- [copying]: https://github.com/google/google-auth-library-ruby/tree/master/COPYING
211
+ [application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials
212
+ [contributing]: https://github.com/googleapis/google-auth-library-ruby/tree/master/.github/CONTRIBUTING.md
213
+ [license]: https://github.com/googleapis/google-auth-library-ruby/tree/master/LICENSE
data/SECURITY.md ADDED
@@ -0,0 +1,7 @@
1
+ # Security Policy
2
+
3
+ To report a security issue, please use [g.co/vulnz](https://g.co/vulnz).
4
+
5
+ The Google Security Team will respond within 5 working days of your report on g.co/vulnz.
6
+
7
+ We use g.co/vulnz for our intake, and do coordination and disclosure here using GitHub Security Advisory to privately discuss and fix the issue.
@@ -34,18 +34,20 @@ module Google
34
34
  # Module Auth provides classes that provide Google-specific authorization
35
35
  # used to access Google APIs.
36
36
  module Auth
37
- NOT_FOUND_ERROR = <<-ERROR_MESSAGE.freeze
38
- Could not load the default credentials. Browse to
39
- https://developers.google.com/accounts/docs/application-default-credentials
40
- for more information
41
- ERROR_MESSAGE
37
+ NOT_FOUND_ERROR = <<~ERROR_MESSAGE.freeze
38
+ Could not load the default credentials. Browse to
39
+ https://developers.google.com/accounts/docs/application-default-credentials
40
+ for more information
41
+ ERROR_MESSAGE
42
+
43
+ module_function
42
44
 
43
45
  # Obtains the default credentials implementation to use in this
44
46
  # environment.
45
47
  #
46
48
  # Use this to obtain the Application Default Credentials for accessing
47
49
  # Google APIs. Application Default Credentials are described in detail
48
- # at http://goo.gl/IUuyuX.
50
+ # at https://cloud.google.com/docs/authentication/production.
49
51
  #
50
52
  # If supplied, scope is used to create the credentials instance, when it can
51
53
  # be applied. E.g, on google compute engine and for user credentials the
@@ -73,9 +75,7 @@ ERROR_MESSAGE
73
75
  GCECredentials.unmemoize_all
74
76
  raise NOT_FOUND_ERROR
75
77
  end
76
- GCECredentials.new
78
+ GCECredentials.new scope: scope
77
79
  end
78
-
79
- module_function :get_application_default
80
80
  end
81
81
  end
@@ -35,46 +35,63 @@ module Google
35
35
  # Module Auth provides classes that provide Google-specific authorization
36
36
  # used to access Google APIs.
37
37
  module Auth
38
- NO_METADATA_SERVER_ERROR = <<-ERROR.freeze
39
- Error code 404 trying to get security access token
40
- from Compute Engine metadata for the default service account. This
41
- may be because the virtual machine instance does not have permission
42
- scopes specified.
43
- ERROR
44
- UNEXPECTED_ERROR_SUFFIX = <<-ERROR.freeze
45
- trying to get security access token from Compute Engine metadata for
46
- the default service account
47
- ERROR
38
+ NO_METADATA_SERVER_ERROR = <<~ERROR.freeze
39
+ Error code 404 trying to get security access token
40
+ from Compute Engine metadata for the default service account. This
41
+ may be because the virtual machine instance does not have permission
42
+ scopes specified.
43
+ ERROR
44
+ UNEXPECTED_ERROR_SUFFIX = <<~ERROR.freeze
45
+ trying to get security access token from Compute Engine metadata for
46
+ the default service account
47
+ ERROR
48
48
 
49
49
  # Extends Signet::OAuth2::Client so that the auth token is obtained from
50
50
  # the GCE metadata server.
51
51
  class GCECredentials < Signet::OAuth2::Client
52
52
  # The IP Address is used in the URIs to speed up failures on non-GCE
53
53
  # systems.
54
- COMPUTE_AUTH_TOKEN_URI = "http://169.254.169.254/computeMetadata/v1/"\
55
- "instance/service-accounts/default/token".freeze
54
+ DEFAULT_METADATA_HOST = "169.254.169.254".freeze
55
+
56
+ # @private Unused and deprecated
57
+ COMPUTE_AUTH_TOKEN_URI =
58
+ "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token".freeze
59
+ # @private Unused and deprecated
60
+ COMPUTE_ID_TOKEN_URI =
61
+ "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/identity".freeze
62
+ # @private Unused and deprecated
56
63
  COMPUTE_CHECK_URI = "http://169.254.169.254".freeze
57
64
 
58
65
  class << self
59
66
  extend Memoist
60
67
 
68
+ def metadata_host
69
+ ENV.fetch "GCE_METADATA_HOST", DEFAULT_METADATA_HOST
70
+ end
71
+
72
+ def compute_check_uri
73
+ "http://#{metadata_host}".freeze
74
+ end
75
+
76
+ def compute_auth_token_uri
77
+ "#{compute_check_uri}/computeMetadata/v1/instance/service-accounts/default/token".freeze
78
+ end
79
+
80
+ def compute_id_token_uri
81
+ "#{compute_check_uri}/computeMetadata/v1/instance/service-accounts/default/identity".freeze
82
+ end
83
+
61
84
  # Detect if this appear to be a GCE instance, by checking if metadata
62
- # is available
85
+ # is available.
63
86
  def on_gce? options = {}
87
+ # TODO: This should use google-cloud-env instead.
64
88
  c = options[:connection] || Faraday.default_connection
65
- resp = c.get COMPUTE_CHECK_URI do |req|
66
- # Comment from: oauth2client/client.py
67
- #
68
- # Note: the explicit `timeout` below is a workaround. The underlying
69
- # issue is that resolving an unknown host on some networks will take
70
- # 20-30 seconds; making this timeout short fixes the issue, but
71
- # could lead to false negatives in the event that we are on GCE, but
72
- # the metadata resolution was particularly slow. The latter case is
73
- # "unlikely".
74
- req.options.timeout = 0.1
89
+ headers = { "Metadata-Flavor" => "Google" }
90
+ resp = c.get compute_check_uri, nil, headers do |req|
91
+ req.options.timeout = 1.0
92
+ req.options.open_timeout = 0.1
75
93
  end
76
94
  return false unless resp.status == 200
77
- return false unless resp.headers.key? "Metadata-Flavor"
78
95
  resp.headers["Metadata-Flavor"] == "Google"
79
96
  rescue Faraday::TimeoutError, Faraday::ConnectionFailed
80
97
  false
@@ -88,17 +105,25 @@ ERROR
88
105
  def fetch_access_token options = {}
89
106
  c = options[:connection] || Faraday.default_connection
90
107
  retry_with_error do
91
- headers = { "Metadata-Flavor" => "Google" }
92
- resp = c.get COMPUTE_AUTH_TOKEN_URI, nil, headers
108
+ uri = target_audience ? GCECredentials.compute_id_token_uri : GCECredentials.compute_auth_token_uri
109
+ query = target_audience ? { "audience" => target_audience, "format" => "full" } : {}
110
+ query[:scopes] = Array(scope).join "," if scope
111
+ resp = c.get uri, query, "Metadata-Flavor" => "Google"
93
112
  case resp.status
94
113
  when 200
95
- Signet::OAuth2.parse_credentials(resp.body,
96
- resp.headers["content-type"])
114
+ content_type = resp.headers["content-type"]
115
+ if ["text/html", "application/text"].include? content_type
116
+ { (target_audience ? "id_token" : "access_token") => resp.body }
117
+ else
118
+ Signet::OAuth2.parse_credentials resp.body, content_type
119
+ end
120
+ when 403, 500
121
+ msg = "Unexpected error code #{resp.status} #{UNEXPECTED_ERROR_SUFFIX}"
122
+ raise Signet::UnexpectedStatusError, msg
97
123
  when 404
98
124
  raise Signet::AuthorizationError, NO_METADATA_SERVER_ERROR
99
125
  else
100
- msg = "Unexpected error code #{resp.status}" \
101
- "#{UNEXPECTED_ERROR_SUFFIX}"
126
+ msg = "Unexpected error code #{resp.status} #{UNEXPECTED_ERROR_SUFFIX}"
102
127
  raise Signet::AuthorizationError, msg
103
128
  end
104
129
  end