Skip to content

Commit ee1ef2c

Browse files
committed
Add kubernetes auth method
1 parent b1a79c4 commit ee1ef2c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

lib/vault/api/auth.rb

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

245+
# Authenticate via the kubernetes 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.aws_ec2("default", "/var/run/secrets/kubernetes.io/serviceaccount/token")
251+
# #=> #<Vault::Secret lease_id="">
252+
#
253+
# @param [String] role
254+
# @param [String] service_account_path optional
255+
# Path on filesystem of service account token secret.
256+
# @param [String] route optional
257+
#
258+
# @return [Secret]
259+
def kubernetes(role, service_account_path = nil, route = nil)
260+
route ||= '/v1/auth/kubernetes/login'
261+
service_account_path ||=
262+
'/var/run/secrets/kubernetes.io/serviceaccount/token'
263+
264+
payload = {
265+
role: role,
266+
jwt: File.read(service_account_path)
267+
}
268+
269+
json = client.post(
270+
route,
271+
JSON.fast_generate(payload)
272+
)
273+
274+
secret = Secret.decode(json)
275+
client.token = secret.auth.client_token
276+
277+
return secret
278+
end
279+
245280
# Authenticate via a TLS authentication method. If authentication is
246281
# successful, the resulting token will be stored on the client and used
247282
# for future requests.

0 commit comments

Comments
 (0)