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 { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { runInDebug } from '@ember/debug';
import WithBlockingActions from 'consul-ui/mixins/with-blocking-actions';
export default class ApplicationRoute extends Route.extend(WithBlockingActions) {
@service('client/http') client;
@service('env') env;
@service('repository/token') tokenRepo;
@service('settings') settings;
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
onClientChanged(e) {
let data = e.data;

View File

@ -192,7 +192,7 @@ export default function(config = {}, win = window, doc = document) {
}
};
const ui = function(key) {
let $;
let $ = {};
switch (config.environment) {
case 'development':
case 'staging':
@ -227,16 +227,29 @@ export default function(config = {}, win = window, doc = document) {
case 'CONSUL_UI_CONFIG':
prev['CONSUL_UI_CONFIG'] = JSON.parse(value);
break;
case 'TokenSecretID':
prev['CONSUL_HTTP_TOKEN'] = value;
break;
default:
prev[key] = value;
}
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') {
return $[key];
}
break;
}
return config[key];
};
return function env(str) {