Skip to content

Commit c480f4b

Browse files
committed
Add kubernetes auth method
1 parent 5e5688a commit c480f4b

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
@@ -264,6 +264,41 @@ def gcp(role, jwt, path = 'gcp')
264264
return secret
265265
end
266266

267+
# Authenticate via the kubernetes authentication method. If authentication is
268+
# successful, the resulting token will be stored on the client and used
269+
# for future requests.
270+
#
271+
# @example
272+
# Vault.auth.kubernetes("default", "/var/run/secrets/kubernetes.io/serviceaccount/token")
273+
# #=> #<Vault::Secret lease_id="">
274+
#
275+
# @param [String] role
276+
# @param [String] service_account_path optional
277+
# Path on filesystem of service account token secret.
278+
# @param [String] route optional
279+
#
280+
# @return [Secret]
281+
def kubernetes(role, service_account_path = nil, route = nil)
282+
route ||= '/v1/auth/kubernetes/login'
283+
service_account_path ||=
284+
'/var/run/secrets/kubernetes.io/serviceaccount/token'
285+
286+
payload = {
287+
role: role,
288+
jwt: File.read(service_account_path)
289+
}
290+
291+
json = client.post(
292+
route,
293+
JSON.fast_generate(payload)
294+
)
295+
296+
secret = Secret.decode(json)
297+
client.token = secret.auth.client_token
298+
299+
return secret
300+
end
301+
267302
# Authenticate via a TLS authentication method. If authentication is
268303
# successful, the resulting token will be stored on the client and used
269304
# for future requests.

0 commit comments

Comments
 (0)