-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathauth_spec.rb
More file actions
352 lines (282 loc) · 12.5 KB
/
auth_spec.rb
File metadata and controls
352 lines (282 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require "spec_helper"
require "aws-sigv4"
module Vault
describe Auth do
subject { vault_test_client }
describe "#token" do
before do
subject.token = nil
end
it "verifies the token and saves it on the client" do
token = RSpec::VaultServer.token
subject.auth.token(token)
expect(subject.token).to eq(token)
end
it "raises an error if the token is invalid" do
expect {
expect {
subject.auth.token("nope-not-real")
}.to raise_error(HTTPError)
}.to_not change(subject, :token)
end
end
describe "#app_id" do
before(:context) do
@app_id = "aeece56e-3f9b-40c3-8f85-781d3e9a8f68"
@user_id = "3b87be76-95cf-493a-a61b-7d5fc70870ad"
vault_test_client.sys.enable_auth("app-id", "app-id", nil)
vault_test_client.logical.write("auth/app-id/map/app-id/#{@app_id}", { value: "default" })
vault_test_client.logical.write("auth/app-id/map/user-id/#{@user_id}", { value: @app_id })
vault_test_client.sys.enable_auth("new-app-id", "app-id", nil)
vault_test_client.logical.write("auth/new-app-id/map/app-id/#{@app_id}", { value: "default" })
vault_test_client.logical.write("auth/new-app-id/map/user-id/#{@user_id}", { value: @app_id })
end
before do
subject.token = nil
end
it "authenticates and saves the token on the client" do
result = subject.auth.app_id(@app_id, @user_id)
expect(subject.token).to eq(result.auth.client_token)
end
it "authenticates with custom options" do
result = subject.auth.app_id(@app_id, @user_id, mount: "new-app-id")
expect(subject.token).to eq(result.auth.client_token)
end
it "raises an error if the authentication is bad" do
expect {
expect {
subject.auth.app_id("nope", "bad")
}.to raise_error(HTTPError)
}.to_not change(subject, :token)
end
end
describe "#approle", vault: ">= 0.6.1" do
before(:context) do
@approle = "sample-role-name"
vault_test_client.sys.enable_auth("approle", "approle", nil)
end
after(:context) do
vault_test_client.sys.disable_auth("approle")
end
before do
subject.token = nil
end
context "when approle has default settings" do
before(:context) do
vault_test_client.approle.set_role(@approle)
@role_id = vault_test_client.approle.role_id(@approle)
@secret_id = vault_test_client.approle.create_secret_id(@approle).data[:secret_id]
end
after(:context) do
vault_test_client.approle.delete_role(@approle)
end
it "authenticates and saves the token on the client" do
result = subject.auth.approle(@role_id, @secret_id)
expect(subject.token).to eq(result.auth.client_token)
end
it "raises an error if the authentication is bad" do
expect {
expect {
subject.auth.approle("nope", "bad")
}.to raise_error(HTTPError)
}.to_not change(subject, :token)
end
end
context "when approle has 'bind_secret_id' disabled" do
before(:context) do
opts = {
bind_secret_id: false,
bound_cidr_list: "127.0.0.1/32"
}
vault_test_client.approle.set_role(@approle, opts)
@role_id = vault_test_client.approle.role_id(@approle)
end
after(:context) do
vault_test_client.approle.delete_role(@approle)
end
it "authenticates w/o secret_id and saves the token on the client" do
result = subject.auth.approle(@role_id)
expect(subject.token).to eq(result.auth.client_token)
end
end
end
describe "#userpass" do
before(:context) do
@username = "sethvargo"
@password = "s3kr3t"
vault_test_client.sys.enable_auth("userpass", "userpass", nil)
vault_test_client.logical.write("auth/userpass/users/#{@username}", { password: @password, policies: "default" })
vault_test_client.sys.enable_auth("new-userpass", "userpass", nil)
vault_test_client.logical.write("auth/new-userpass/users/#{@username}", { password: @password, policies: "default" })
end
before do
subject.token = nil
end
it "authenticates and saves the token on the client" do
result = subject.auth.userpass(@username, @password)
expect(subject.token).to eq(result.auth.client_token)
end
it "authenticates with custom options" do
result = subject.auth.userpass(@username, @password, mount: "new-userpass")
expect(subject.token).to eq(result.auth.client_token)
end
it "raises an error if the authentication is bad" do
expect {
expect {
subject.auth.userpass("nope", "bad")
}.to raise_error(HTTPError)
}.to_not change(subject, :token)
end
end
describe "#tls" do
before(:context) do
vault_test_client.sys.enable_auth("cert", "cert", nil)
end
after(:context) do
vault_test_client.sys.disable_auth("cert")
end
let!(:old_token) { subject.token }
let(:certificate) do
{
display_name: "sample-cert",
certificate: RSpec::SampleCertificate.cert,
policies: "default",
ttl: 3600,
}
end
let(:auth_cert) { RSpec::SampleCertificate.cert << RSpec::SampleCertificate.key }
after do
subject.token = old_token
end
it "authenticates and saves the token on the client" do
pending "dev server does not support tls"
subject.auth_tls.set_certificate("kaelumania", certificate)
result = subject.auth.tls(auth_cert)
expect(subject.token).to eq(result.auth.client_token)
end
it "authenticates with default ssl_pem_file" do
pending "dev server does not support tls"
subject.auth_tls.set_certificate("kaelumania", certificate)
subject.ssl_pem_file = auth_cert
result = subject.auth.tls
expect(subject.token).to eq(result.auth.client_token)
end
it "returns nil if a certificate name is provided that doesn't exist" do
pending "dev server does not support tls"
secret = subject.auth.tls(auth_cert, name: "not_here")
expect(secret).to_be nil
end
it "raises an error if the authentication is bad", vault: "> 0.6.1" do
subject.sys.disable_auth("cert")
expect {
expect {
subject.auth.tls(auth_cert)
}.to raise_error(HTTPError)
}.to_not change { subject.token }
end
end
describe "#aws_iam", vault: "> 0.7.3" do
before(:context) do
vault_test_client.sys.enable_auth("aws", "aws", nil)
vault_test_client.post("/v1/auth/aws/config/client", JSON.fast_generate("iam_server_id_header_value" => "iam_header_canary"))
end
after(:context) do
vault_test_client.sys.disable_auth("aws")
end
let!(:old_token) { subject.token }
let(:credentials_provider) do
instance_double(
Aws::Sigv4::StaticCredentialsProvider,
credentials: instance_double(
Aws::Sigv4::Credentials,
access_key_id: 'very',
secret_access_key: 'secure',
session_token: 'thing'
)
)
end
let(:secret) { double(auth: double(client_token: 'a great token')) }
after do
subject.token = old_token
end
it "does not authenticate if iam_server_id_header_value does not match" do
expect(::Aws::Sigv4::Signer).to(
receive(:new).with(
service: 'sts', region: 'cn-north-1', credentials_provider: credentials_provider
).and_call_original
)
expect do
subject.auth.aws_iam('a_rolename', credentials_provider, 'mismatched_iam_header', 'https://sts.cn-north-1.amazonaws.com.cn')
end.to raise_error(Vault::HTTPClientError, /expected "?iam_header_canary"? but got "?mismatched_iam_header"?/)
end
it "authenticates and saves the token on the client" do
expect(subject).to receive(:post).and_return 'huzzah!'
expect(Secret).to receive(:decode).and_return secret
expect(::Aws::Sigv4::Signer).to(
receive(:new).with(
service: 'sts', region: 'cn-north-1', credentials_provider: credentials_provider
).and_call_original
)
subject.auth.aws_iam('a_rolename', credentials_provider, 'iam_header_canary', 'https://sts.cn-north-1.amazonaws.com.cn')
end
end
describe "#gcp", vault: ">= 0.8.1" do
before(:context) do
skip "gcp auth requires real resources and keys"
vault_test_client.sys.enable_auth("gcp", "gcp", nil)
vault_test_client.post("/v1/auth/gcp/config", JSON.fast_generate("service_account" => "rspec_service_account"))
vault_test_client.post("/v1/auth/gcp/role/rspec_wrong_role", JSON.fast_generate("name" => "rspec_role", "project_id" => "wrong_project_id", "bound_service_accounts" => "\*", "type" => "iam"))
vault_test_client.post("/v1/auth/gcp/role/rspec_role", JSON.fast_generate("name" => "rspec_role", "project_id" => "project_id", "bound_service_accounts" => "\*", "type" => "iam"))
end
after(:context) do
vault_test_client.sys.disable_auth("gcp")
end
let!(:old_token) { subject.token }
let(:jwt) do
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJwcm9qZWN0X2lkIjoicHJvamVjdF9pZCJ9.TmuiSHtbLMZuw_LOzKWQ2vnC7BUvu2b4CeBXdxCDCXQ"
end
after do
subject.token = old_token
end
it "does not authenticate if project_id does not match" do
pending "gcp auth requires real resources and keys"
expect do
subject.auth.gcp("rspec_wrong_role", jwt)
end.to raise_error(Vault::HTTPClientError, /project_id doesn't match/)
end
it "authenticates and saves the token on the client" do
pending "gcp auth requires real resources and keys"
subject.auth.gcp("rspec_role", jwt)
end
end
describe "#azure", vault: ">= 0.8.1" do
before(:context) do
skip "azure auth requires real resources and keys"
vault_test_client.sys.enable_auth("azure", "azure", nil)
vault_test_client.post("/v1/auth/azure/config", JSON.fast_generate("tenant_id" => "rspec_tenant_id", "resource" => "rspec_resource", "client_id" => "rspec_client_id", "client_secret" => "rspec_client_secret"))
vault_test_client.post("/v1/auth/azure/role/rspec_wrong_role", JSON.fast_generate("name" => "rspec_role", "bound_resource_groups" => "wrong_bound_resource_groups", "bound_subscription_ids" => "wrong_bound_subscription_ids"))
vault_test_client.post("/v1/auth/azure/role/rspec_role", JSON.fast_generate("name" => "rspec_role", "bound_resource_groups" => "bound_resource_groups", "bound_subscription_ids" => "bound_subscription_ids"))
end
after(:context) do
vault_test_client.sys.disable_auth("azure")
end
let!(:old_token) { subject.token }
let(:jwt) do
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1yNS1BVWliZkJpaTdOZDFqQmViYXhib1hXMCIsImtpZCI6Ik1yNS1BVWliZkJpaTdOZDFqQmViYXhib1hXMCJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuYXp1cmUuY29tIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvOTJkMTEzMTEtZmVhYy00ODk4LWEwMTItMWU0NTY1ZDUyNTg2LyIsImlhdCI6MTY0NTYxMDczMywibmJmIjoxNjQ1NjEwNzMzLCJleHAiOjE2NDU2OTc0MzMsImFpbyI6IkUyWmdZRGl6bWp0UXZNbnYwSGRad1hVZkJPWWVBUUE9IiwiYXBwaWQiOiJlZjEzYjJlMS05OGRkLTQxOTEtYjVlMy0wMDZkOTdiYjBhNjgiLCJhcHBpZGFjciI6IjIiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC85MmQxMTMxMS1mZWFjLTQ4OTgtYTAxMi0xZTQ1NjVkNTI1ODYvIiwiaWR0eXAiOiJhcHAiLCJvaWQiOiJiMGY2YTJmNi1iNTZjLTQyNTAtODY5Ni1jNzJmYjQ1NDgxYmUiLCJyaCI6IjAuQVN3QUVSUFJrcXotbUVpZ0VoNUZaZFVsaGtaSWYza0F1dGRQdWtQYXdmajJNQk1zQUFBLiIsInN1YiI6ImIwZjZhMmY2LWI1NmMtNDI1MC04Njk2LWM3MmZiNDU0OD"
end
after do
subject.token = old_token
end
it "does not authenticate if resource_groups does not match" do
pending "azure auth requires real resources and keys"
expect do
subject.auth.azure("rspec_wrong_role", jwt)
end.to raise_error(Vault::HTTPClientError, /resource_groups doesn't match/)
end
it "authenticates and saves the token on the client" do
pending "azure auth requires real resources and keys"
subject.auth.azure("rspec_role", jwt)
end
end
end
end