Skip to content

Commit 2f64a97

Browse files
authored
make "role" input optional (#291)
* make "role" input optional Per Vault documentation it doesn't have to be provided, and the auth provider's "default_role" parameter is required precisely for this case. https://www.vaultproject.io/api/auth/jwt
1 parent 25c4aec commit 2f64a97

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

integrationTests/basic/jwt_auth.test.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ function mockGithubOIDCResponse(aud= "https://github.com/hashicorp/vault-action"
5151
return rsasign.KJUR.jws.JWS.sign(alg, JSON.stringify(header), JSON.stringify(payload), decryptedKey);
5252
}
5353

54+
// The sign call inside this function takes a while to run, so cache the default JWT in a constant.
55+
const defaultGithubJwt = mockGithubOIDCResponse();
56+
5457
describe('jwt auth', () => {
5558
beforeAll(async () => {
5659
// Verify Connection
@@ -99,7 +102,8 @@ describe('jwt auth', () => {
99102
'X-Vault-Token': 'testtoken',
100103
},
101104
json: {
102-
jwt_validation_pubkeys: publicRsaKey
105+
jwt_validation_pubkeys: publicRsaKey,
106+
default_role: "default"
103107
}
104108
});
105109

@@ -198,20 +202,20 @@ describe('jwt auth', () => {
198202
.calledWith('jwtPrivateKey')
199203
.mockReturnValueOnce('');
200204

201-
when(core.getInput)
202-
.calledWith('role')
203-
.mockReturnValueOnce('default');
204-
205205
when(core.getInput)
206206
.calledWith('secrets')
207207
.mockReturnValueOnce('secret/data/test secret');
208+
});
209+
210+
it('successfully authenticates', async () => {
211+
when(core.getInput)
212+
.calledWith('role')
213+
.mockReturnValueOnce('default');
208214

209215
when(core.getIDToken)
210216
.calledWith()
211-
.mockReturnValueOnce(mockGithubOIDCResponse());
212-
});
217+
.mockReturnValueOnce(defaultGithubJwt);
213218

214-
it('successfully authenticates', async () => {
215219
await exportSecrets();
216220
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
217221
});
@@ -233,6 +237,19 @@ describe('jwt auth', () => {
233237
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
234238
})
235239

240+
it('successfully authenticates as default role without specifying it', async () => {
241+
when(core.getInput)
242+
.calledWith('role')
243+
.mockReturnValueOnce(null);
244+
245+
when(core.getIDToken)
246+
.calledWith()
247+
.mockReturnValueOnce(defaultGithubJwt);
248+
249+
await exportSecrets();
250+
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
251+
})
252+
236253
});
237254

238255
});

src/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function retrieveToken(method, client) {
2525
case 'jwt': {
2626
/** @type {string} */
2727
let jwt;
28-
const role = core.getInput('role', { required: true });
28+
const role = core.getInput('role', { required: false });
2929
const privateKeyRaw = core.getInput('jwtPrivateKey', { required: false });
3030
const privateKey = Buffer.from(privateKeyRaw, 'base64').toString();
3131
const keyPassword = core.getInput('jwtKeyPassword', { required: false });

0 commit comments

Comments
 (0)