Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kubernetes auth method #202

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/vault/api/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,41 @@ def gcp(role, jwt, path = 'gcp')
return secret
end

# Authenticate via the kubernetes authentication method. If authentication is
# successful, the resulting token will be stored on the client and used
# for future requests.
#
# @example
# Vault.auth.kubernetes("default", "/var/run/secrets/kubernetes.io/serviceaccount/token")
# #=> #<Vault::Secret lease_id="">
#
# @param [String] role
# @param [String] service_account_path optional
# Path on filesystem of service account token secret.
# @param [String] route optional
#
# @return [Secret]
def kubernetes(role, service_account_path = nil, route = nil)
route ||= '/v1/auth/kubernetes/login'
service_account_path ||=
'/var/run/secrets/kubernetes.io/serviceaccount/token'

payload = {
role: role,
jwt: File.read(service_account_path)
}

json = client.post(
route,
JSON.fast_generate(payload)
)

secret = Secret.decode(json)
client.token = secret.auth.client_token

return secret
end

# Authenticate via a TLS authentication method. If authentication is
# successful, the resulting token will be stored on the client and used
# for future requests.
Expand Down
17 changes: 16 additions & 1 deletion spec/unit/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

module Vault
describe Authenticate do
let(:auth) { Authenticate.new(client: nil) }
let(:client) { double('client') }
let(:auth) { Authenticate.new(client: client) }

describe '#kubernetes' do
before do
allow(::File).to receive(:read).with(
'/var/run/secrets/kubernetes.io/serviceaccount/token'
).and_return('abc123')
end

it 'authenticates with Kubernetes Auth method' do
expect(client).to receive(:post).with('/v1/auth/kubernetes/login', '{"blah": "wrong"}')
expect(auth.kubernetes('test-role')).to eq('secret')
end
end

describe "#region_from_sts_endpoint" do
subject { auth.send(:region_from_sts_endpoint, sts_endpoint) }

Expand Down
Loading