Skip to content

Commit fcefc2a

Browse files
authored
Merge pull request #173 from wayfair/add-gcp-auth
Add gcp auth
2 parents b1a79c4 + aed1d73 commit fcefc2a

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.gem
33
*.rbc
44
/.config
5+
/.vscode
56
/coverage/
67
/InstalledFiles
78
/pkg/

lib/vault/api/auth.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,28 @@ def aws_iam(role, credentials_provider, iam_auth_header_value = nil, sts_endpoin
242242
return secret
243243
end
244244

245+
# Authenticate via the GCP authentication method. If authentication is
246+
# successful, the resulting token will be stored on the client and used
247+
# for future requests.
248+
#
249+
# @example
250+
# Vault.auth.gcp("read-only", "jwt", "gcp") #=> #<Vault::Secret lease_id="">
251+
#
252+
# @param [String] role
253+
# @param [String] jwt
254+
# jwt returned by the instance identity metadata, or iam api
255+
# @param [String] path optional
256+
# the path were the gcp auth backend is mounted
257+
#
258+
# @return [Secret]
259+
def gcp(role, jwt, path = 'gcp')
260+
payload = { role: role, jwt: jwt }
261+
json = client.post("/v1/auth/#{CGI.escape(path)}/login", JSON.fast_generate(payload))
262+
secret = Secret.decode(json)
263+
client.token = secret.auth.client_token
264+
return secret
265+
end
266+
245267
# Authenticate via a TLS authentication method. If authentication is
246268
# successful, the resulting token will be stored on the client and used
247269
# for future requests.

spec/integration/api/auth_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,42 @@ module Vault
258258
subject.auth.aws_iam('a_rolename', credentials_provider, 'iam_header_canary', 'https://sts.cn-north-1.amazonaws.com.cn')
259259
end
260260
end
261+
262+
describe "#gcp", vault: ">= 0.8.1" do
263+
before(:context) do
264+
vault_test_client.sys.enable_auth("gcp", "gcp", nil)
265+
vault_test_client.post("/v1/auth/gcp/config", JSON.fast_generate("service_account" => "rspec_service_account"))
266+
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"))
267+
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"))
268+
end
269+
270+
after(:context) do
271+
vault_test_client.sys.disable_auth("gcp")
272+
end
273+
274+
let!(:old_token) { subject.token }
275+
276+
let(:jwt) do
277+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJwcm9qZWN0X2lkIjoicHJvamVjdF9pZCJ9.TmuiSHtbLMZuw_LOzKWQ2vnC7BUvu2b4CeBXdxCDCXQ"
278+
end
279+
280+
after do
281+
subject.token = old_token
282+
end
283+
284+
it "does not authenticate if project_id does not match" do
285+
pending "gcp auth requires real resources and keys"
286+
287+
expect do
288+
subject.auth.gcp("rspec_wrong_role", jwt)
289+
end.to raise_error(Vault::HTTPClientError, /project_id doesn't match/)
290+
end
291+
292+
it "authenticates and saves the token on the client" do
293+
pending "gcp auth requires real resources and keys"
294+
295+
subject.auth.gcp("rspec_role", jwt)
296+
end
297+
end
261298
end
262299
end

0 commit comments

Comments
 (0)