Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/auth/VaultAppRoleAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ class VaultAppRoleAuth extends VaultBaseAuth {

this.__roleId = config.role_id;
this.__secretId = config.secret_id;
this.__namespace = config.namespace;
}

_authenticate() {
this._log.info(
'making authentication request: role_id=%s',
this.__roleId
);

const headers = {};
if (this.__namespace) {
headers['X-Vault-Namespace'] = this.__namespace;
}

return this.__apiClient.makeRequest('POST', `/auth/${this._mount}/login`, {
role_id: this.__roleId,
secret_id: this.__secretId,
}).then(res => {
}, headers).then(res => {
this._log.debug(
'receive token: %s',
res.auth.client_token
Expand Down
91 changes: 91 additions & 0 deletions test/auth.appRole.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"use strict";

require("co-mocha");

const _ = require("lodash");
const sinon = require("sinon");
const chai = require("chai");
const expect = chai.expect;
chai.use(require("sinon-chai"));

const VaultApiClient = require("../src/VaultApiClient");
const VaultAppRoleAuth = require("../src/auth/VaultAppRoleAuth");
const errors = require("../src/errors");

const logger = _.fromPairs(
_.map(["error", "warn", "info", "debug", "trace"], (prop) => [prop, _.noop]),
);

describe("AppRole auth backend", function () {
/**
* @returns {VaultApiClient}
*/
function getApiStub() {
return sinon.createStubInstance(VaultApiClient);
}

describe("Vault Request", function () {
const mount = "approle";

it("Should make a correct vault login request with namespace", async () => {
const api = getApiStub();

const auth = new VaultAppRoleAuth(
api,
logger,
{
role_id: "role123",
secret_id: "secret456",
namespace: "ns1",
},
mount,
);

api.makeRequest
.withArgs("POST")
.resolves({ auth: { client_token: "fake_token" } });
sinon.stub(auth, "_getTokenEntity");

await auth._authenticate();

expect(
api.makeRequest.calledWith(
"POST",
"/auth/approle/login",
{ role_id: "role123", secret_id: "secret456" },
{ "X-Vault-Namespace": "ns1" },
),
).to.be.true;
});

it("Should not set namespace header if not provided", async () => {
const api = getApiStub();

const auth = new VaultAppRoleAuth(
api,
logger,
{
role_id: "role123",
secret_id: "secret456",
},
mount,
);

api.makeRequest
.withArgs("POST")
.resolves({ auth: { client_token: "fake_token" } });
sinon.stub(auth, "_getTokenEntity");

await auth._authenticate();

expect(
api.makeRequest.calledWith(
"POST",
"/auth/approle/login",
{ role_id: "role123", secret_id: "secret456" },
{},
),
).to.be.true;
});
});
});