Skip to content

Commit a660d8a

Browse files
Pankaj Jainaviadhahami
authored andcommitted
fix for #143
kubernetes path can be provided in vault when creating the auth when running a multicluster setup. defaults to kubernetes.
1 parent 81f8f8b commit a660d8a

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ vault.write('secret/hello', { value: 'world', lease: '1s' })
6464
.then( () => vault.delete('secret/hello'))
6565
.catch(console.error);
6666
```
67+
### Kubernetes Auth Example
68+
```javascript
69+
70+
//if vault kubernets endpoint is /auth/example-cluster/login and role is example-role
71+
//read token from default token mount path
72+
const token = await fs.readFileSync('/var/run/secrets/kubernetes.io/serviceaccount/token', { encoding: 'utf8' });
73+
vault.kubernetesLogin({role: 'example-role' ,
74+
jwt: token,
75+
kubernetesPath: 'example-cluster'})
76+
```
6777

6878
## Docs
6979
Just generate [docco] docs via `npm run docs`.

example/auth_kubernetes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const kubernetesCaCert = process.env.K8S_CA_CERT || 'k8s-ca-certificate-data';
99

1010
const appName = process.env.APP_NAME || 'some-app';
1111
const appServiceAccountSecretToken = process.env.APP_SVC_ACCT_SECRET_TOKEN || 'app-k8s-token';
12+
const kubernetesPath = process.env.APP_SVC_ACCT_SECRET_TOKEN || 'kubernetes';
1213

1314
vault.auths()
1415
.then((result) => {
@@ -19,7 +20,7 @@ vault.auths()
1920
description: 'Kubernetes auth',
2021
});
2122
})
22-
.then(() => vault.write('auth/kubernetes/config', {
23+
.then(() => vault.write('auth/${kubernetesPath}/config', {
2324
token_reviewer_jwt: vaultServicAccountSecretToken,
2425
kubernetes_host: kubernetesHostUrl,
2526
kubernetes_ca_cert: kubernetesCaCert,
@@ -28,12 +29,12 @@ vault.auths()
2829
name: appName,
2930
rules: `path "secret/${appName}/*" { capabilities = ["read"] }`,
3031
}))
31-
.then(() => vault.write(`auth/kubernetes/role/${appName}`, {
32+
.then(() => vault.write(`auth/${kubernetesPath}/role/${appName}`, {
3233
bound_service_account_names: appName,
3334
bound_service_account_namespaces: 'default',
3435
policies: appName,
3536
ttl: '1h',
3637
}))
37-
.then(() => vault.kubernetesLogin({ role: appName, jwt: appServiceAccountSecretToken }))
38+
.then(() => vault.kubernetesLogin({ role: appName, jwt: appServiceAccountSecretToken, kubernetesPath: kubernetesPath }))
3839
.then(console.log)
3940
.catch((err) => console.error(err.message));

src/commands.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module.exports = {
205205
},
206206
addKubernetesRole: {
207207
method: 'POST',
208-
path: '/auth/{{mount_point}}{{^mount_point}}kubernetes{{/mount_point}}/role/{{ role_name }}',
208+
path: '/auth/{{mount_point}}{{^mount_point}}{{kubernetesPath}}{{/mount_point}}/role/{{ role_name }}',
209209
schema: {
210210
req: {
211211
name: {
@@ -240,14 +240,14 @@ module.exports = {
240240
},
241241
getKubernetesRole: {
242242
method: 'GET',
243-
path: '/auth/{{mount_point}}{{^mount_point}}kubernetes{{/mount_point}}/role/{{ role_name }}',
243+
path: '/auth/{{mount_point}}{{^mount_point}}{{kubernetesPath}}{{/mount_point}}/role/{{ role_name }}',
244244
schema: {
245245
res: kubernetesRoleResponse,
246246
},
247247
},
248248
deleteKubernetesRole: {
249249
method: 'DELETE',
250-
path: '/auth/{{mount_point}}{{^mount_point}}kubernetes{{/mount_point}}/role/{{ role_name }}',
250+
path: '/auth/{{mount_point}}{{^mount_point}}{{kubernetesPath}}{{/mount_point}}/role/{{ role_name }}',
251251
},
252252
addApproleRole: {
253253
method: 'POST',
@@ -611,7 +611,7 @@ module.exports = {
611611
},
612612
kubernetesLogin: {
613613
method: 'POST',
614-
path: '/auth/{{mount_point}}{{^mount_point}}kubernetes{{/mount_point}}/login',
614+
path: '/auth/{{mount_point}}{{^mount_point}}{{kubernetesPath}}{{/mount_point}}/login',
615615
tokenSource: true,
616616
schema: {
617617
req: {
@@ -742,7 +742,7 @@ module.exports = {
742742
},
743743
res: tokenResponse,
744744
},
745-
},
745+
},
746746
tokenAccessors: {
747747
method: 'LIST',
748748
path: '/auth/token/accessors',

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = (config = {}) => {
7575
client.token = config.token || process.env.VAULT_TOKEN;
7676
client.noCustomHTTPVerbs = config.noCustomHTTPVerbs || false;
7777
client.namespace = config.namespace || process.env.VAULT_NAMESPACE;
78+
client.kubernetesPath = config.kubernetesPath || 'kubernetes';
7879

7980
const requestSchema = {
8081
type: 'object',

0 commit comments

Comments
 (0)