Skip to content

Commit ebb780b

Browse files
Add support for custom mount paths in AppRole authentication
Adds optional mount parameter to approle() method to support AppRole authentication against non-default mount points. Usage: # Default mount point Vault.auth.approle(role_id, secret_id) # Custom mount point Vault.auth.approle(role_id, secret_id, mount: "my-approle") The implementation uses an options hash (mount:) to maintain consistency with other auth methods like userpass and ldap, and to allow for future extensibility. Fully backward compatible - defaults to 'approle' mount. Changes: - Updated approle() to accept options hash with :mount parameter - Fixed deprecated JSON.fast_generate to JSON.generate - Added integration test for custom mount path - Added documentation examples for both default and custom mounts - Updated CHANGELOG Co-authored-by: Laurent Lafage <laurent.lafage@fr.clara.net>
1 parent 29a84fe commit ebb780b

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ IMPROVEMENTS
66

77
- Added `cluster_address` field to `LeaderStatus` response from `sys/leader` endpoint [GH-204]
88
- Updated AppRole `set_role` documentation to include modern parameters like `secret_id_bound_cidrs`, `token_bound_cidrs`, and `token_policies`. Added reference to official Vault API docs for complete parameter list. [GH-220]
9+
- Added support for custom mount paths in AppRole authentication via `mount:` option [GH-292]
910

1011
BUG FIXES
1112

lib/vault/api/auth.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,32 @@ def app_id(app_id, user_id, options = {})
8181
# successful, the resulting token will be stored on the client and used for
8282
# future requests.
8383
#
84-
# @example
84+
# @example Default mount point
8585
# Vault.auth.approle(
8686
# "db02de05-fa39-4855-059b-67221c5c2f63",
8787
# "6a174c20-f6de-a53c-74d2-6018fcceff64",
8888
# ) #=> #<Vault::Secret lease_id="">
8989
#
90+
# @example Custom mount point
91+
# Vault.auth.approle(
92+
# "db02de05-fa39-4855-059b-67221c5c2f63",
93+
# "6a174c20-f6de-a53c-74d2-6018fcceff64",
94+
# mount: "my-approle"
95+
# ) #=> #<Vault::Secret lease_id="">
96+
#
9097
# @param [String] role_id
9198
# @param [String] secret_id (default: nil)
9299
# It is required when `bind_secret_id` is enabled for the specified role_id
100+
# @param [Hash] options
101+
# @option options [String] :mount (default: "approle")
102+
# The path where the approle auth backend is mounted
93103
#
94104
# @return [Secret]
95-
def approle(role_id, secret_id=nil)
105+
def approle(role_id, secret_id=nil, options = {})
106+
mount = options[:mount] || 'approle'
96107
payload = { role_id: role_id }
97108
payload[:secret_id] = secret_id if secret_id
98-
json = client.post("/v1/auth/approle/login", JSON.generate(payload))
109+
json = client.post("/v1/auth/#{CGI.escape(mount)}/login", JSON.generate(payload))
99110
secret = Secret.decode(json)
100111
client.token = secret.auth.client_token
101112
return secret

spec/integration/api/auth_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ module Vault
8686
expect(subject.token).to eq(result.auth.client_token)
8787
end
8888
end
89+
90+
context "when using a custom mount path" do
91+
before(:context) do
92+
@custom_path = "custom-approle"
93+
@custom_approle = "custom-role"
94+
vault_test_client.sys.enable_auth(@custom_path, "approle", nil)
95+
96+
# Use logical.write to configure the role on the custom mount
97+
vault_test_client.logical.write("auth/#{@custom_path}/role/#{@custom_approle}", {
98+
secret_id_ttl: "10m",
99+
token_ttl: "20m",
100+
})
101+
102+
@role_id = vault_test_client.logical.read("auth/#{@custom_path}/role/#{@custom_approle}/role-id").data[:role_id]
103+
@secret_id = vault_test_client.logical.write("auth/#{@custom_path}/role/#{@custom_approle}/secret-id", {}).data[:secret_id]
104+
end
105+
106+
after(:context) do
107+
vault_test_client.logical.delete("auth/#{@custom_path}/role/#{@custom_approle}")
108+
vault_test_client.sys.disable_auth(@custom_path)
109+
end
110+
111+
it "authenticates using the custom mount path" do
112+
result = subject.auth.approle(@role_id, @secret_id, mount: @custom_path)
113+
expect(subject.token).to eq(result.auth.client_token)
114+
end
115+
end
89116
end
90117

91118
describe "#userpass" do

0 commit comments

Comments
 (0)