googleauth 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CHANGELOG.md +6 -0
  4. data/lib/googleauth/version.rb +1 -1
  5. metadata +14 -88
  6. data/.github/CODEOWNERS +0 -7
  7. data/.github/CONTRIBUTING.md +0 -74
  8. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -36
  9. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -21
  10. data/.github/ISSUE_TEMPLATE/support_request.md +0 -7
  11. data/.github/renovate.json +0 -6
  12. data/.github/sync-repo-settings.yaml +0 -18
  13. data/.github/workflows/ci.yml +0 -55
  14. data/.github/workflows/release-please.yml +0 -39
  15. data/.gitignore +0 -39
  16. data/.kokoro/populate-secrets.sh +0 -76
  17. data/.kokoro/release.cfg +0 -52
  18. data/.kokoro/release.sh +0 -18
  19. data/.kokoro/trampoline_v2.sh +0 -489
  20. data/.repo-metadata.json +0 -5
  21. data/.rspec +0 -2
  22. data/.rubocop.yml +0 -17
  23. data/.toys/.toys.rb +0 -45
  24. data/.toys/ci.rb +0 -43
  25. data/.toys/kokoro/.toys.rb +0 -66
  26. data/.toys/kokoro/publish-docs.rb +0 -67
  27. data/.toys/kokoro/publish-gem.rb +0 -53
  28. data/.toys/linkinator.rb +0 -43
  29. data/.trampolinerc +0 -48
  30. data/Gemfile +0 -25
  31. data/googleauth.gemspec +0 -39
  32. data/integration/helper.rb +0 -31
  33. data/integration/id_tokens/key_source_test.rb +0 -74
  34. data/spec/googleauth/apply_auth_examples.rb +0 -171
  35. data/spec/googleauth/client_id_spec.rb +0 -160
  36. data/spec/googleauth/compute_engine_spec.rb +0 -178
  37. data/spec/googleauth/credentials_spec.rb +0 -600
  38. data/spec/googleauth/get_application_default_spec.rb +0 -286
  39. data/spec/googleauth/iam_spec.rb +0 -80
  40. data/spec/googleauth/scope_util_spec.rb +0 -77
  41. data/spec/googleauth/service_account_spec.rb +0 -511
  42. data/spec/googleauth/signet_spec.rb +0 -142
  43. data/spec/googleauth/stores/file_token_store_spec.rb +0 -57
  44. data/spec/googleauth/stores/redis_token_store_spec.rb +0 -50
  45. data/spec/googleauth/stores/store_examples.rb +0 -58
  46. data/spec/googleauth/user_authorizer_spec.rb +0 -343
  47. data/spec/googleauth/user_refresh_spec.rb +0 -359
  48. data/spec/googleauth/web_user_authorizer_spec.rb +0 -172
  49. data/spec/spec_helper.rb +0 -92
  50. data/test/helper.rb +0 -33
  51. data/test/id_tokens/key_sources_test.rb +0 -240
  52. data/test/id_tokens/verifier_test.rb +0 -269
@@ -1,31 +0,0 @@
1
- # Copyright 2020 Google LLC
2
- #
3
- # Redistribution and use in source and binary forms, with or without
4
- # modification, are permitted provided that the following conditions are
5
- # met:
6
- #
7
- # * Redistributions of source code must retain the above copyright
8
- # notice, this list of conditions and the following disclaimer.
9
- # * Redistributions in binary form must reproduce the above
10
- # copyright notice, this list of conditions and the following disclaimer
11
- # in the documentation and/or other materials provided with the
12
- # distribution.
13
- # * Neither the name of Google Inc. nor the names of its
14
- # contributors may be used to endorse or promote products derived from
15
- # this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- require "minitest/autorun"
30
- require "minitest/focus"
31
- require "googleauth"
@@ -1,74 +0,0 @@
1
- # Copyright 2020 Google LLC
2
- #
3
- # Redistribution and use in source and binary forms, with or without
4
- # modification, are permitted provided that the following conditions are
5
- # met:
6
- #
7
- # * Redistributions of source code must retain the above copyright
8
- # notice, this list of conditions and the following disclaimer.
9
- # * Redistributions in binary form must reproduce the above
10
- # copyright notice, this list of conditions and the following disclaimer
11
- # in the documentation and/or other materials provided with the
12
- # distribution.
13
- # * Neither the name of Google Inc. nor the names of its
14
- # contributors may be used to endorse or promote products derived from
15
- # this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- require "helper"
30
-
31
- describe Google::Auth::IDTokens do
32
- describe "key source" do
33
- let(:legacy_oidc_key_source) {
34
- Google::Auth::IDTokens::X509CertHttpKeySource.new "https://www.googleapis.com/oauth2/v1/certs"
35
- }
36
- let(:oidc_key_source) { Google::Auth::IDTokens.oidc_key_source }
37
- let(:iap_key_source) { Google::Auth::IDTokens.iap_key_source }
38
-
39
- it "Gets real keys from the OAuth2 V1 cert URL" do
40
- keys = legacy_oidc_key_source.refresh_keys
41
- refute_empty keys
42
- keys.each do |key|
43
- assert_kind_of OpenSSL::PKey::RSA, key.key
44
- refute key.key.private?
45
- assert_equal "RS256", key.algorithm
46
- end
47
- end
48
-
49
- it "Gets real keys from the OAuth2 V3 cert URL" do
50
- keys = oidc_key_source.refresh_keys
51
- refute_empty keys
52
- keys.each do |key|
53
- assert_kind_of OpenSSL::PKey::RSA, key.key
54
- refute key.key.private?
55
- assert_equal "RS256", key.algorithm
56
- end
57
- end
58
-
59
- it "Gets the same keys from the OAuth2 V1 and V3 cert URLs" do
60
- keys_v1 = legacy_oidc_key_source.refresh_keys.map(&:key).map(&:export).sort
61
- keys_v3 = oidc_key_source.refresh_keys.map(&:key).map(&:export).sort
62
- assert_equal keys_v1, keys_v3
63
- end
64
-
65
- it "Gets real keys from the IAP public key URL" do
66
- keys = iap_key_source.refresh_keys
67
- refute_empty keys
68
- keys.each do |key|
69
- assert_kind_of OpenSSL::PKey::EC, key.key
70
- assert_equal "ES256", key.algorithm
71
- end
72
- end
73
- end
74
- end
@@ -1,171 +0,0 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
3
- #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
7
- #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
17
- #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- spec_dir = File.expand_path File.join(File.dirname(__FILE__))
31
- $LOAD_PATH.unshift spec_dir
32
- $LOAD_PATH.uniq!
33
-
34
- require "faraday"
35
- require "spec_helper"
36
-
37
- shared_examples "apply/apply! are OK" do
38
- let(:auth_key) { :authorization }
39
-
40
- # tests that use these examples need to define
41
- #
42
- # @client which should be an auth client
43
- #
44
- # @make_auth_stubs, which should stub out the expected http behaviour of the
45
- # auth client
46
- describe "#fetch_access_token" do
47
- let(:token) { "1/abcdef1234567890" }
48
- let :access_stub do
49
- make_auth_stubs access_token: token
50
- end
51
- let :id_stub do
52
- make_auth_stubs id_token: token
53
- end
54
-
55
- it "should set access_token to the fetched value" do
56
- access_stub
57
- @client.fetch_access_token!
58
- expect(@client.access_token).to eq(token)
59
- expect(access_stub).to have_been_requested
60
- end
61
-
62
- it "should set id_token to the fetched value" do
63
- skip unless @id_client
64
- id_stub
65
- @id_client.fetch_access_token!
66
- expect(@id_client.id_token).to eq(token)
67
- expect(id_stub).to have_been_requested
68
- end
69
-
70
- it "should notify refresh listeners after updating" do
71
- access_stub
72
- expect do |b|
73
- @client.on_refresh(&b)
74
- @client.fetch_access_token!
75
- end.to yield_with_args(have_attributes(
76
- access_token: "1/abcdef1234567890"
77
- ))
78
- expect(access_stub).to have_been_requested
79
- end
80
- end
81
-
82
- describe "#apply!" do
83
- it "should update the target hash with fetched access token" do
84
- token = "1/abcdef1234567890"
85
- stub = make_auth_stubs access_token: token
86
-
87
- md = { foo: "bar" }
88
- @client.apply! md
89
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
90
- expect(md).to eq(want)
91
- expect(stub).to have_been_requested
92
- end
93
-
94
- it "should update the target hash with fetched ID token" do
95
- skip unless @id_client
96
- token = "1/abcdef1234567890"
97
- stub = make_auth_stubs id_token: token
98
-
99
- md = { foo: "bar" }
100
- @id_client.apply! md
101
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
102
- expect(md).to eq(want)
103
- expect(stub).to have_been_requested
104
- end
105
- end
106
-
107
- describe "updater_proc" do
108
- it "should provide a proc that updates a hash with the access token" do
109
- token = "1/abcdef1234567890"
110
- stub = make_auth_stubs access_token: token
111
- md = { foo: "bar" }
112
- the_proc = @client.updater_proc
113
- got = the_proc.call md
114
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
115
- expect(got).to eq(want)
116
- expect(stub).to have_been_requested
117
- end
118
- end
119
-
120
- describe "#apply" do
121
- it "should not update the original hash with the access token" do
122
- token = "1/abcdef1234567890"
123
- stub = make_auth_stubs access_token: token
124
-
125
- md = { foo: "bar" }
126
- @client.apply md
127
- want = { foo: "bar" }
128
- expect(md).to eq(want)
129
- expect(stub).to have_been_requested
130
- end
131
-
132
- it "should add the token to the returned hash" do
133
- token = "1/abcdef1234567890"
134
- stub = make_auth_stubs access_token: token
135
-
136
- md = { foo: "bar" }
137
- got = @client.apply md
138
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
139
- expect(got).to eq(want)
140
- expect(stub).to have_been_requested
141
- end
142
-
143
- it "should not fetch a new token if the current is not expired" do
144
- token = "1/abcdef1234567890"
145
- stub = make_auth_stubs access_token: token
146
-
147
- n = 5 # arbitrary
148
- n.times do |_t|
149
- md = { foo: "bar" }
150
- got = @client.apply md
151
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
152
- expect(got).to eq(want)
153
- end
154
- expect(stub).to have_been_requested
155
- end
156
-
157
- it "should fetch a new token if the current one is expired" do
158
- token1 = "1/abcdef1234567890"
159
- token2 = "2/abcdef1234567891"
160
-
161
- [token1, token2].each do |t|
162
- make_auth_stubs access_token: t
163
- md = { foo: "bar" }
164
- got = @client.apply md
165
- want = { :foo => "bar", auth_key => "Bearer #{t}" }
166
- expect(got).to eq(want)
167
- @client.expires_at -= 3601 # default is to expire in 1hr
168
- end
169
- end
170
- end
171
- end
@@ -1,160 +0,0 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
3
- #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
7
- #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
17
- #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- spec_dir = File.expand_path File.join(File.dirname(__FILE__))
31
- $LOAD_PATH.unshift spec_dir
32
- $LOAD_PATH.uniq!
33
-
34
- require "spec_helper"
35
- require "fakefs/safe"
36
- require "googleauth"
37
-
38
- describe Google::Auth::ClientId do
39
- shared_examples "it has a valid config" do
40
- it "should include a valid id" do
41
- expect(client_id.id).to eql "abc@example.com"
42
- end
43
-
44
- it "should include a valid secret" do
45
- expect(client_id.secret).to eql "notasecret"
46
- end
47
- end
48
-
49
- shared_examples "it can successfully load client_id" do
50
- context "loaded from hash" do
51
- let(:client_id) { Google::Auth::ClientId.from_hash config }
52
-
53
- it_behaves_like "it has a valid config"
54
- end
55
-
56
- context "loaded from file" do
57
- file_path = "/client_secrets.json"
58
-
59
- let :client_id do
60
- FakeFS do
61
- content = MultiJson.dump config
62
- File.write file_path, content
63
- Google::Auth::ClientId.from_file file_path
64
- end
65
- end
66
-
67
- it_behaves_like "it has a valid config"
68
- end
69
- end
70
-
71
- describe "with web config" do
72
- let :config do
73
- {
74
- "web" => {
75
- "client_id" => "abc@example.com",
76
- "client_secret" => "notasecret"
77
- }
78
- }
79
- end
80
- it_behaves_like "it can successfully load client_id"
81
- end
82
-
83
- describe "with installed app config" do
84
- let :config do
85
- {
86
- "installed" => {
87
- "client_id" => "abc@example.com",
88
- "client_secret" => "notasecret"
89
- }
90
- }
91
- end
92
- it_behaves_like "it can successfully load client_id"
93
- end
94
-
95
- context "with missing top level property" do
96
- let :config do
97
- {
98
- "notvalid" => {
99
- "client_id" => "abc@example.com",
100
- "client_secret" => "notasecret"
101
- }
102
- }
103
- end
104
-
105
- it "should raise error" do
106
- expect { Google::Auth::ClientId.from_hash config }.to raise_error(
107
- /Expected top level property/
108
- )
109
- end
110
- end
111
-
112
- context "with missing client id" do
113
- let :config do
114
- {
115
- "web" => {
116
- "client_secret" => "notasecret"
117
- }
118
- }
119
- end
120
-
121
- it "should raise error" do
122
- expect { Google::Auth::ClientId.from_hash config }.to raise_error(
123
- /Client id can not be nil/
124
- )
125
- end
126
- end
127
-
128
- context "with missing client secret" do
129
- let :config do
130
- {
131
- "web" => {
132
- "client_id" => "abc@example.com"
133
- }
134
- }
135
- end
136
-
137
- it "should raise error" do
138
- expect { Google::Auth::ClientId.from_hash config }.to raise_error(
139
- /Client secret can not be nil/
140
- )
141
- end
142
- end
143
-
144
- context "with cloud sdk credentials" do
145
- let :config do
146
- {
147
- "web" => {
148
- "client_id" => Google::Auth::CredentialsLoader::CLOUD_SDK_CLIENT_ID,
149
- "client_secret" => "notasecret"
150
- }
151
- }
152
- end
153
-
154
- it "should raise warning" do
155
- expect { Google::Auth::ClientId.from_hash config }.to output(
156
- Google::Auth::CredentialsLoader::CLOUD_SDK_CREDENTIALS_WARNING + "\n"
157
- ).to_stderr
158
- end
159
- end
160
- end
@@ -1,178 +0,0 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
3
- #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
7
- #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
17
- #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- spec_dir = File.expand_path File.join(File.dirname(__FILE__))
31
- $LOAD_PATH.unshift spec_dir
32
- $LOAD_PATH.uniq!
33
-
34
- require "apply_auth_examples"
35
- require "faraday"
36
- require "googleauth/compute_engine"
37
- require "spec_helper"
38
-
39
- describe Google::Auth::GCECredentials do
40
- MD_ACCESS_URI = "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token".freeze
41
- MD_ID_URI = "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/identity?audience=https://pubsub.googleapis.com/&format=full".freeze
42
- GCECredentials = Google::Auth::GCECredentials
43
-
44
- before :example do
45
- @client = GCECredentials.new
46
- @id_client = GCECredentials.new target_audience: "https://pubsub.googleapis.com/"
47
- end
48
-
49
- def make_auth_stubs opts
50
- if opts[:access_token]
51
- body = MultiJson.dump("access_token" => opts[:access_token],
52
- "token_type" => "Bearer",
53
- "expires_in" => 3600)
54
-
55
- uri = MD_ACCESS_URI
56
- uri += "?scopes=#{Array(opts[:scope]).join ','}" if opts[:scope]
57
-
58
- stub_request(:get, uri)
59
- .with(headers: { "Metadata-Flavor" => "Google" })
60
- .to_return(body: body,
61
- status: 200,
62
- headers: { "Content-Type" => "application/json" })
63
- elsif opts[:id_token]
64
- stub_request(:get, MD_ID_URI)
65
- .with(headers: { "Metadata-Flavor" => "Google" })
66
- .to_return(body: opts[:id_token],
67
- status: 200,
68
- headers: { "Content-Type" => "text/html" })
69
- end
70
- end
71
-
72
- it_behaves_like "apply/apply! are OK"
73
-
74
- context "metadata is unavailable" do
75
- describe "#fetch_access_token" do
76
- it "should pass scopes when requesting an access token" do
77
- scopes = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/bigtable.data"]
78
- stub = make_auth_stubs access_token: "1/abcdef1234567890", scope: scopes
79
- @client = GCECredentials.new(scope: scopes)
80
- @client.fetch_access_token!
81
- expect(stub).to have_been_requested
82
- end
83
-
84
- it "should fail if the metadata request returns a 404" do
85
- stub = stub_request(:get, MD_ACCESS_URI)
86
- .to_return(status: 404,
87
- headers: { "Metadata-Flavor" => "Google" })
88
- expect { @client.fetch_access_token! }
89
- .to raise_error Signet::AuthorizationError
90
- expect(stub).to have_been_requested
91
- end
92
-
93
- it "should fail if the metadata request returns a 403" do
94
- stub = stub_request(:get, MD_ACCESS_URI)
95
- .to_return(status: 403,
96
- headers: { "Metadata-Flavor" => "Google" })
97
- expect { @client.fetch_access_token! }
98
- .to raise_error Signet::AuthorizationError
99
- expect(stub).to have_been_requested.times(6)
100
- end
101
-
102
- it "should fail if the metadata request returns a 500" do
103
- stub = stub_request(:get, MD_ACCESS_URI)
104
- .to_return(status: 500,
105
- headers: { "Metadata-Flavor" => "Google" })
106
- expect { @client.fetch_access_token! }
107
- .to raise_error Signet::AuthorizationError
108
- expect(stub).to have_been_requested.times(6)
109
- end
110
-
111
- it "should fail if the metadata request returns an unexpected code" do
112
- stub = stub_request(:get, MD_ACCESS_URI)
113
- .to_return(status: 503,
114
- headers: { "Metadata-Flavor" => "Google" })
115
- expect { @client.fetch_access_token! }
116
- .to raise_error Signet::AuthorizationError
117
- expect(stub).to have_been_requested
118
- end
119
-
120
- it "should fail with Signet::AuthorizationError if request times out" do
121
- allow_any_instance_of(Faraday::Connection).to receive(:get)
122
- .and_raise(Faraday::TimeoutError)
123
- expect { @client.fetch_access_token! }
124
- .to raise_error Signet::AuthorizationError
125
- end
126
-
127
- it "should fail with Signet::AuthorizationError if request fails" do
128
- allow_any_instance_of(Faraday::Connection).to receive(:get)
129
- .and_raise(Faraday::ConnectionFailed, nil)
130
- expect { @client.fetch_access_token! }
131
- .to raise_error Signet::AuthorizationError
132
- end
133
- end
134
- end
135
-
136
- describe "#on_gce?" do
137
- it "should be true when Metadata-Flavor is Google" do
138
- stub = stub_request(:get, "http://169.254.169.254")
139
- .with(headers: { "Metadata-Flavor" => "Google" })
140
- .to_return(status: 200,
141
- headers: { "Metadata-Flavor" => "Google" })
142
- expect(GCECredentials.on_gce?({}, true)).to eq(true)
143
- expect(stub).to have_been_requested
144
- end
145
-
146
- it "should be false when Metadata-Flavor is not Google" do
147
- stub = stub_request(:get, "http://169.254.169.254")
148
- .with(headers: { "Metadata-Flavor" => "Google" })
149
- .to_return(status: 200,
150
- headers: { "Metadata-Flavor" => "NotGoogle" })
151
- expect(GCECredentials.on_gce?({}, true)).to eq(false)
152
- expect(stub).to have_been_requested
153
- end
154
-
155
- it "should be false if the response is not 200" do
156
- stub = stub_request(:get, "http://169.254.169.254")
157
- .with(headers: { "Metadata-Flavor" => "Google" })
158
- .to_return(status: 404,
159
- headers: { "Metadata-Flavor" => "NotGoogle" })
160
- expect(GCECredentials.on_gce?({}, true)).to eq(false)
161
- expect(stub).to have_been_requested
162
- end
163
-
164
- it "should honor GCE_METADATA_HOST environment variable" do
165
- ENV["GCE_METADATA_HOST"] = "mymetadata.example.com"
166
- begin
167
- stub = stub_request(:get, "http://mymetadata.example.com")
168
- .with(headers: { "Metadata-Flavor" => "Google" })
169
- .to_return(status: 200,
170
- headers: { "Metadata-Flavor" => "Google" })
171
- expect(GCECredentials.on_gce?({}, true)).to eq(true)
172
- expect(stub).to have_been_requested
173
- ensure
174
- ENV.delete "GCE_METADATA_HOST"
175
- end
176
- end
177
- end
178
- end