ui: Detect token in a cookie and passthrough (#14495)

This commit is contained in:
John Cowen 2022-09-08 11:43:39 +01:00 committed by GitHub
parent 39439d07bd
commit efb2ecbb2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 4 deletions

3
.changelog/14495.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:feature
ui: Detect a TokenSecretID cookie and passthrough to localStorage
```

View File

@ -1,14 +1,44 @@
import Route from 'consul-ui/routing/route'; import Route from 'consul-ui/routing/route';
import { action } from '@ember/object'; import { action } from '@ember/object';
import { inject as service } from '@ember/service'; import { inject as service } from '@ember/service';
import { runInDebug } from '@ember/debug';
import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions'; import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions';
export default class ApplicationRoute extends Route.extend(WithBlockingActions) { export default class ApplicationRoute extends Route.extend(WithBlockingActions) {
@service('client/http') client; @service('client/http') client;
@service('env') env;
@service('repository/token') tokenRepo;
@service('settings') settings;
data; data;
async model() {
if(this.env.var('CONSUL_ACLS_ENABLED')) {
const secret = this.env.var('CONSUL_HTTP_TOKEN');
const existing = await this.settings.findBySlug('token');
if(!existing.AccessorID && secret) {
try {
const token = await this.tokenRepo.self({
secret: secret,
dc: this.env.var('CONSUL_DATACENTER_LOCAL')
});
await this.settings.persist({
token: {
AccessorID: token.AccessorID,
SecretID: token.SecretID,
Namespace: token.Namespace,
Partition: token.Partition,
}
});
} catch(e) {
runInDebug(_ => console.error(e));
}
}
}
return {};
}
@action @action
onClientChanged(e) { onClientChanged(e) {
let data = e.data; let data = e.data;

View File

@ -192,7 +192,7 @@ export default function(config = {}, win = window, doc = document) {
} }
}; };
const ui = function(key) { const ui = function(key) {
let $; let $ = {};
switch (config.environment) { switch (config.environment) {
case 'development': case 'development':
case 'staging': case 'staging':
@ -227,16 +227,29 @@ export default function(config = {}, win = window, doc = document) {
case 'CONSUL_UI_CONFIG': case 'CONSUL_UI_CONFIG':
prev['CONSUL_UI_CONFIG'] = JSON.parse(value); prev['CONSUL_UI_CONFIG'] = JSON.parse(value);
break; break;
case 'TokenSecretID':
prev['CONSUL_HTTP_TOKEN'] = value;
break;
default: default:
prev[key] = value; prev[key] = value;
} }
return prev; return prev;
}, {}); }, {});
break;
case 'production':
$ = dev().reduce(function(prev, [key, value]) {
switch (key) {
case 'TokenSecretID':
prev['CONSUL_HTTP_TOKEN'] = value;
break;
}
return prev;
}, {});
break;
}
if (typeof $[key] !== 'undefined') { if (typeof $[key] !== 'undefined') {
return $[key]; return $[key];
} }
break;
}
return config[key]; return config[key];
}; };
return function env(str) { return function env(str) {