Class: Vault::Authenticate
Instance Attribute Summary
Attributes inherited from Request
Instance Method Summary collapse
-
#app_id(app_id, user_id, options = {}) ⇒ Secret
Authenticate via the “app-id” authentication method.
-
#approle(role_id, secret_id = nil) ⇒ Secret
Authenticate via the “approle” authentication method.
-
#aws_ec2(role, pkcs7, nonce = nil, route = nil) ⇒ Secret
Authenticate via the AWS EC2 authentication method.
-
#aws_iam(role, credentials_provider, iam_auth_header_value = nil, sts_endpoint = 'https://sts.amazonaws.com', route = nil) ⇒ Secret
Authenticate via AWS IAM auth method by providing a AWS CredentialProvider (either ECS, AssumeRole, etc.) If authentication is successful, the resulting token will be stored on the client and used for future requests.
-
#gcp(role, jwt, path = 'gcp') ⇒ Secret
Authenticate via the GCP authentication method.
-
#github(github_token, path = "/v1/auth/github/login") ⇒ Secret
Authenticate via the GitHub authentication method.
-
#ldap(username, password, options = {}) ⇒ Secret
Authenticate via the “ldap” authentication method.
-
#tls(pem = nil, path = 'cert', name: nil) ⇒ Secret
Authenticate via a TLS authentication method.
-
#token(new_token) ⇒ Secret
Authenticate via the “token” authentication method.
-
#userpass(username, password, options = {}) ⇒ Secret
Authenticate via the “userpass” authentication method.
Methods inherited from Request
Methods included from EncodePath
Constructor Details
This class inherits a constructor from Vault::Request
Instance Method Details
#app_id(app_id, user_id, options = {}) ⇒ Secret
Authenticate via the “app-id” authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
72 73 74 75 76 77 78 |
# File 'lib/vault/api/auth.rb', line 72 def app_id(app_id, user_id, = {}) payload = { app_id: app_id, user_id: user_id }.merge() json = client.post("/v1/auth/app-id/login", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#approle(role_id, secret_id = nil) ⇒ Secret
Authenticate via the “approle” authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
95 96 97 98 99 100 101 102 |
# File 'lib/vault/api/auth.rb', line 95 def approle(role_id, secret_id=nil) payload = { role_id: role_id } payload[:secret_id] = secret_id if secret_id json = client.post("/v1/auth/approle/login", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#aws_ec2(role, pkcs7, nonce = nil, route = nil) ⇒ Secret
Authenticate via the AWS EC2 authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/vault/api/auth.rb', line 183 def aws_ec2(role, pkcs7, nonce = nil, route = nil) route ||= '/v1/auth/aws-ec2/login' payload = { role: role, pkcs7: pkcs7 } # Set a custom nonce if client is providing one payload[:nonce] = nonce if nonce json = client.post(route, JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#aws_iam(role, credentials_provider, iam_auth_header_value = nil, sts_endpoint = 'https://sts.amazonaws.com', route = nil) ⇒ Secret
Authenticate via AWS IAM auth method by providing a AWS CredentialProvider (either ECS, AssumeRole, etc.) If authentication is successful, the resulting token will be stored on the client and used for future requests.
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 |
# File 'lib/vault/api/auth.rb', line 210 def aws_iam(role, credentials_provider, iam_auth_header_value = nil, sts_endpoint = 'https://sts.amazonaws.com', route = nil) require "aws-sigv4" require "base64" request_body = 'Action=GetCallerIdentity&Version=2011-06-15' request_method = 'POST' route ||= '/v1/auth/aws/login' vault_headers = { 'User-Agent' => Vault::Client::USER_AGENT, 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' } vault_headers['X-Vault-AWS-IAM-Server-ID'] = iam_auth_header_value if iam_auth_header_value sig4_headers = Aws::Sigv4::Signer.new( service: 'sts', region: region_from_sts_endpoint(sts_endpoint), credentials_provider: credentials_provider ).sign_request( http_method: request_method, url: sts_endpoint, headers: vault_headers, body: request_body ).headers payload = { role: role, iam_http_request_method: request_method, iam_request_url: Base64.strict_encode64(sts_endpoint), iam_request_headers: Base64.strict_encode64(vault_headers.merge(sig4_headers).to_json), iam_request_body: Base64.strict_encode64(request_body) } json = client.post(route, JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#gcp(role, jwt, path = 'gcp') ⇒ Secret
Authenticate via the GCP authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
265 266 267 268 269 270 271 |
# File 'lib/vault/api/auth.rb', line 265 def gcp(role, jwt, path = 'gcp') payload = { role: role, jwt: jwt } json = client.post("/v1/auth/#{CGI.escape(path)}/login", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#github(github_token, path = "/v1/auth/github/login") ⇒ Secret
Authenticate via the GitHub authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
161 162 163 164 165 166 167 |
# File 'lib/vault/api/auth.rb', line 161 def github(github_token, path="/v1/auth/github/login") payload = {token: github_token} json = client.post(path, JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#ldap(username, password, options = {}) ⇒ Secret
Authenticate via the “ldap” authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
143 144 145 146 147 148 149 |
# File 'lib/vault/api/auth.rb', line 143 def ldap(username, password, = {}) payload = { password: password }.merge() json = client.post("/v1/auth/ldap/login/#{encode_path(username)}", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#tls(pem = nil, path = 'cert', name: nil) ⇒ Secret
Authenticate via a TLS authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/vault/api/auth.rb', line 296 def tls(pem = nil, path = 'cert', name: nil) new_client = client.dup new_client.ssl_pem_contents = pem if !pem.nil? opts = {} opts[:name] = name if name json = new_client.post("/v1/auth/#{CGI.escape(path)}/login", opts) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |
#token(new_token) ⇒ Secret
Authenticate via the “token” authentication method. This authentication method is a bit bizarre because you already have a token, but hey, whatever floats your boat.
This method hits the ‘/v1/auth/token/lookup-self` endpoint after setting the Vault client’s token to the given token parameter. If the self lookup succeeds, the token is persisted onto the client for future requests. If the lookup fails, the old token (which could be unset) is restored on the client.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vault/api/auth.rb', line 37 def token(new_token) old_token = client.token client.token = new_token json = client.get("/v1/auth/token/lookup-self") secret = Secret.decode(json) return secret rescue client.token = old_token raise end |
#userpass(username, password, options = {}) ⇒ Secret
Authenticate via the “userpass” authentication method. If authentication is successful, the resulting token will be stored on the client and used for future requests.
121 122 123 124 125 126 127 |
# File 'lib/vault/api/auth.rb', line 121 def userpass(username, password, = {}) payload = { password: password }.merge() json = client.post("/v1/auth/userpass/login/#{encode_path(username)}", JSON.fast_generate(payload)) secret = Secret.decode(json) client.token = secret.auth.client_token return secret end |