open-consul/ui-v2/app/utils/acls-status.js
John Cowen d1b3a63b2f
UI: Catch 500 error on token endpoint and revert to legacy tokens (#4874)
In some circumstances a consul 1.4 client could be running in an
un-upgraded 1.3 or lower cluster. Currently this gives a 500 error on
the new ACL token endpoint. Here we catch this specific 500 error/message
and set the users AccessorID to null. Elsewhere in the frontend we use
this fact (AccessorID being null) to decide whether to present the
legacy or the new ACL UI to the user.

Also:
- Re-adds in most of the old style ACL acceptance tests, now that we are keeping the old style UI
- Restricts code editors to HCL only mode for all `Rules` editing (legacy/'half legacy'/new style)
- Adds a [Stop using] button to the old style ACL rows so its possible to logout.
- Updates copy and documentation links for the upgrade notices
2018-11-02 14:44:36 +00:00

56 lines
1.4 KiB
JavaScript

// This is used by all acl routes to check whether
// acls are enabled on the server, and whether the user
// has a valid token
// Right now this is very acl specific, but is likely to be
// made a bit more less specific
export default function(isValidServerError, P = Promise) {
return function(obj) {
const propName = Object.keys(obj)[0];
const p = obj[propName];
let authorize;
let enable;
return {
isAuthorized: new P(function(resolve) {
authorize = function(bool) {
resolve(bool);
};
}),
isEnabled: new P(function(resolve) {
enable = function(bool) {
resolve(bool);
};
}),
[propName]: p
.catch(function(e) {
switch (e.errors[0].status) {
case '500':
if (isValidServerError(e)) {
enable(true);
authorize(false);
}
break;
case '403':
enable(true);
authorize(false);
break;
case '401':
enable(false);
authorize(false);
break;
default:
enable(false);
authorize(false);
throw e;
}
return [];
})
.then(function(res) {
enable(true);
authorize(true);
return res;
}),
};
};
}