Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Exec not working with reverse proxy X-Nomad-Token #12925

Merged
merged 3 commits into from
May 10, 2022
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
4 changes: 3 additions & 1 deletion ui/app/services/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export default class TokenService extends Service {
@task(function* () {
const TokenAdapter = getOwner(this).lookup('adapter:token');
try {
return yield TokenAdapter.findSelf();
var token = yield TokenAdapter.findSelf();
this.secret = token.secret;
return token;
} catch (e) {
const errors = e.errors ? e.errors.mapBy('detail') : [];
if (errors.find((error) => error === 'ACL support disabled')) {
Expand Down
27 changes: 24 additions & 3 deletions ui/tests/acceptance/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,38 @@ module('Acceptance | reverse proxy', function (hooks) {
server.create('agent');
managementToken = server.create('token');

// Prepare a setRequestHeader that accumulate headers already set. This is to avoid double setting X-Nomad-Token
this._originalXMLHttpRequestSetRequestHeader =
XMLHttpRequest.prototype.setRequestHeader;
(function (setRequestHeader) {
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
if (!this.headers) {
this.headers = {};
}
if (!this.headers[header]) {
this.headers[header] = [];
}
// Add the value to the header
this.headers[header].push(value);
setRequestHeader.call(this, header, value);
};
})(this._originalXMLHttpRequestSetRequestHeader);

// Simulate a reverse proxy injecting X-Nomad-Token header for all requests
this._originalXMLHttpRequestSend = XMLHttpRequest.prototype.send;
(function (send) {
XMLHttpRequest.prototype.send = function (data) {
this.setRequestHeader('X-Nomad-Token', managementToken.secretId);
if (!this.headers || !('X-Nomad-Token' in this.headers)) {
this.setRequestHeader('X-Nomad-Token', managementToken.secretId);
}
send.call(this, data);
};
})(this._originalXMLHttpRequestSend);
});

hooks.afterEach(function () {
XMLHttpRequest.prototype.setRequestHeader =
this._originalXMLHttpRequestSetRequestHeader;
XMLHttpRequest.prototype.send = this._originalXMLHttpRequestSend;
});

Expand All @@ -38,8 +59,8 @@ module('Acceptance | reverse proxy', function (hooks) {
await Jobs.visit();
assert.equal(
window.localStorage.nomadTokenSecret,
null,
'No token secret set'
secretId,
'Token secret was set'
);

// Make sure that server received the header
Expand Down