2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-15 15:34:54 +00:00
|
|
|
import { runInDebug } from '@ember/debug';
|
|
|
|
// 'environment' getter
|
|
|
|
// there are currently 3 levels of environment variables:
|
|
|
|
// 1. Those that can be set by the user by setting localStorage values
|
|
|
|
// 2. Those that can be set by the operator either via ui_config, or inferring
|
|
|
|
// from other server type properties (protocol)
|
|
|
|
// 3. Those that can be set only during development by adding cookie values
|
|
|
|
// via the browsers Web Inspector, or via the browsers hash (#COOKIE_NAME=1),
|
|
|
|
// which is useful for showing the UI with various settings enabled/disabled
|
2022-09-15 08:43:17 +00:00
|
|
|
export default function (config = {}, win = window, doc = document) {
|
2020-12-15 15:34:54 +00:00
|
|
|
// look at the hash in the URL and transfer anything after the hash into
|
|
|
|
// cookies to enable linking of the UI with various settings enabled
|
|
|
|
runInDebug(() => {
|
2022-09-15 08:43:17 +00:00
|
|
|
const cookies = function (str) {
|
2021-02-01 17:50:11 +00:00
|
|
|
return str
|
|
|
|
.split(';')
|
2022-09-15 08:43:17 +00:00
|
|
|
.map((item) => item.trim())
|
|
|
|
.filter((item) => item !== '')
|
|
|
|
.filter((item) => item.split('=').shift().startsWith('CONSUL_'));
|
2021-02-01 17:50:11 +00:00
|
|
|
};
|
2022-10-13 17:11:38 +00:00
|
|
|
|
|
|
|
// Define the function that reads in "Scenarios", parse and set cookies and set theme if specified.
|
|
|
|
// See https://github.com/hashicorp/consul/blob/main/ui/packages/consul-ui/docs/bookmarklets.mdx
|
2022-09-15 08:43:17 +00:00
|
|
|
win['Scenario'] = function (str = '') {
|
2021-02-01 17:50:11 +00:00
|
|
|
if (str.length > 0) {
|
2022-09-15 08:43:17 +00:00
|
|
|
cookies(str).forEach((item) => {
|
2022-01-21 12:19:03 +00:00
|
|
|
// this current outlier is the only one that
|
|
|
|
// 1. Toggles
|
|
|
|
// 2. Uses localStorage
|
|
|
|
// Once we have a user facing widget to do this, it can all go
|
|
|
|
if (item.startsWith('CONSUL_COLOR_SCHEME=')) {
|
|
|
|
const [, value] = item.split('=');
|
|
|
|
let current;
|
|
|
|
try {
|
|
|
|
current = JSON.parse(win.localStorage.getItem('consul:theme'));
|
|
|
|
} catch (e) {
|
|
|
|
current = {
|
|
|
|
'color-scheme': 'light',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
win.localStorage.setItem(
|
|
|
|
'consul:theme',
|
|
|
|
`{"color-scheme": "${
|
|
|
|
value === '!' ? (current['color-scheme'] === 'light' ? 'dark' : 'light') : value
|
|
|
|
}"}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
doc.cookie = `${item};Path=/`;
|
|
|
|
}
|
|
|
|
});
|
2021-02-01 17:50:11 +00:00
|
|
|
win.location.hash = '';
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
str = cookies(doc.cookie).join(';');
|
|
|
|
const tab = win.open('', '_blank');
|
|
|
|
tab.document.write(
|
|
|
|
`<body><pre>${location.href}#${str}</pre><br /><a href="javascript:Scenario('${str}')">Scenario</a></body>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2022-10-13 17:11:38 +00:00
|
|
|
|
2020-12-15 15:34:54 +00:00
|
|
|
if (
|
|
|
|
typeof win.location !== 'undefined' &&
|
|
|
|
typeof win.location.hash === 'string' &&
|
|
|
|
win.location.hash.length > 0
|
|
|
|
) {
|
2022-01-21 11:42:48 +00:00
|
|
|
win['Scenario'](win.location.hash.substr(1));
|
2020-12-15 15:34:54 +00:00
|
|
|
}
|
|
|
|
});
|
2022-10-13 17:11:38 +00:00
|
|
|
|
|
|
|
// Defines a function that reads in the cookies and parses the cookie keys.
|
2022-09-15 08:43:17 +00:00
|
|
|
const dev = function (str = doc.cookie) {
|
2020-12-15 15:34:54 +00:00
|
|
|
return str
|
2020-01-21 17:52:40 +00:00
|
|
|
.split(';')
|
2022-09-15 08:43:17 +00:00
|
|
|
.filter((item) => item !== '')
|
|
|
|
.map((item) => {
|
2020-12-17 16:35:01 +00:00
|
|
|
const [key, ...rest] = item.trim().split('=');
|
|
|
|
return [key, rest.join('=')];
|
|
|
|
});
|
2020-01-21 17:52:40 +00:00
|
|
|
};
|
2022-10-13 17:11:38 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const user = function (str) {
|
2020-01-21 17:52:40 +00:00
|
|
|
const item = win.localStorage.getItem(str);
|
|
|
|
return item === null ? undefined : item;
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const getResourceFor = function (src) {
|
2020-01-21 17:52:40 +00:00
|
|
|
try {
|
2020-04-15 13:42:55 +00:00
|
|
|
return (
|
2022-09-15 08:43:17 +00:00
|
|
|
win.performance.getEntriesByType('resource').find((item) => {
|
2020-04-15 13:42:55 +00:00
|
|
|
return item.initiatorType === 'script' && src === item.name;
|
|
|
|
}) || {}
|
|
|
|
);
|
2020-01-21 17:52:40 +00:00
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
2021-10-26 14:30:17 +00:00
|
|
|
const operatorConfig = {
|
|
|
|
...config.operatorConfig,
|
2022-01-21 12:19:03 +00:00
|
|
|
...JSON.parse(doc.querySelector(`[data-${config.modulePrefix}-config]`).textContent),
|
2021-10-26 14:30:17 +00:00
|
|
|
};
|
2021-01-20 18:40:46 +00:00
|
|
|
const ui_config = operatorConfig.UIConfig || {};
|
2020-04-15 13:42:55 +00:00
|
|
|
const scripts = doc.getElementsByTagName('script');
|
2020-04-29 08:25:20 +00:00
|
|
|
// we use the currently executing script as a reference
|
|
|
|
// to figure out where we are for other things such as
|
|
|
|
// base url, api url etc
|
2020-04-15 13:42:55 +00:00
|
|
|
const currentSrc = scripts[scripts.length - 1].src;
|
|
|
|
let resource;
|
2020-04-29 08:25:20 +00:00
|
|
|
// TODO: Potentially use ui_config {}, for example
|
|
|
|
// turning off blocking queries if its a busy cluster
|
|
|
|
// forcing/providing amount of possible HTTP connections
|
|
|
|
// re-setting the base url for the API etc
|
2022-09-15 08:43:17 +00:00
|
|
|
const operator = function (str, env) {
|
2020-12-15 18:14:25 +00:00
|
|
|
let protocol, dashboards, provider, proxy;
|
2020-01-21 17:52:40 +00:00
|
|
|
switch (str) {
|
2021-01-20 18:40:46 +00:00
|
|
|
case 'CONSUL_NSPACES_ENABLED':
|
|
|
|
return typeof operatorConfig.NamespacesEnabled === 'undefined'
|
|
|
|
? false
|
|
|
|
: operatorConfig.NamespacesEnabled;
|
|
|
|
case 'CONSUL_SSO_ENABLED':
|
|
|
|
return typeof operatorConfig.SSOEnabled === 'undefined' ? false : operatorConfig.SSOEnabled;
|
|
|
|
case 'CONSUL_ACLS_ENABLED':
|
|
|
|
return typeof operatorConfig.ACLsEnabled === 'undefined'
|
|
|
|
? false
|
|
|
|
: operatorConfig.ACLsEnabled;
|
2021-04-22 11:22:40 +00:00
|
|
|
case 'CONSUL_PARTITIONS_ENABLED':
|
|
|
|
return typeof operatorConfig.PartitionsEnabled === 'undefined'
|
|
|
|
? false
|
|
|
|
: operatorConfig.PartitionsEnabled;
|
2022-07-14 10:22:45 +00:00
|
|
|
case 'CONSUL_PEERINGS_ENABLED':
|
2022-07-12 11:02:45 +00:00
|
|
|
return typeof operatorConfig.PeeringEnabled === 'undefined'
|
|
|
|
? false
|
|
|
|
: operatorConfig.PeeringEnabled;
|
2022-09-14 18:39:50 +00:00
|
|
|
case 'CONSUL_HCP_ENABLED':
|
2022-09-20 14:34:08 +00:00
|
|
|
return typeof operatorConfig.HCPEnabled === 'undefined' ? false : operatorConfig.HCPEnabled;
|
2021-01-20 18:40:46 +00:00
|
|
|
case 'CONSUL_DATACENTER_LOCAL':
|
|
|
|
return operatorConfig.LocalDatacenter;
|
2021-10-26 14:30:17 +00:00
|
|
|
case 'CONSUL_DATACENTER_PRIMARY':
|
|
|
|
return operatorConfig.PrimaryDatacenter;
|
2022-09-08 10:45:20 +00:00
|
|
|
case 'CONSUL_HCP_MANAGED_RUNTIME':
|
|
|
|
return operatorConfig.HCPManagedRuntime;
|
2022-09-06 10:13:51 +00:00
|
|
|
case 'CONSUL_API_PREFIX':
|
|
|
|
// we want API prefix to look like an env var for if we ever change
|
|
|
|
// operator config to be an API request, we need this variable before we
|
2024-01-29 23:38:21 +00:00
|
|
|
// make and API request so this specific variable should never be
|
2022-09-06 10:13:51 +00:00
|
|
|
// retrived via an API request
|
|
|
|
return operatorConfig.APIPrefix;
|
2022-09-07 15:43:42 +00:00
|
|
|
case 'CONSUL_HCP_URL':
|
|
|
|
return operatorConfig.HCPURL;
|
2020-12-15 15:34:54 +00:00
|
|
|
case 'CONSUL_UI_CONFIG':
|
2022-01-21 11:42:48 +00:00
|
|
|
dashboards = {
|
2022-07-12 11:02:45 +00:00
|
|
|
service: undefined,
|
2022-01-21 11:42:48 +00:00
|
|
|
};
|
2020-12-15 18:14:25 +00:00
|
|
|
provider = env('CONSUL_METRICS_PROVIDER');
|
|
|
|
proxy = env('CONSUL_METRICS_PROXY_ENABLED');
|
2020-12-15 15:34:54 +00:00
|
|
|
dashboards.service = env('CONSUL_SERVICE_DASHBOARD_URL');
|
|
|
|
if (provider) {
|
|
|
|
ui_config.metrics_provider = provider;
|
|
|
|
}
|
|
|
|
if (proxy) {
|
|
|
|
ui_config.metrics_proxy_enabled = proxy;
|
|
|
|
}
|
|
|
|
if (dashboards.service) {
|
|
|
|
ui_config.dashboard_url_templates = dashboards;
|
|
|
|
}
|
|
|
|
return ui_config;
|
2020-04-29 08:25:20 +00:00
|
|
|
case 'CONSUL_BASE_UI_URL':
|
2022-09-15 08:43:17 +00:00
|
|
|
return currentSrc.split('/').slice(0, -2).join('/');
|
2020-01-21 17:52:40 +00:00
|
|
|
case 'CONSUL_HTTP_PROTOCOL':
|
2020-04-15 13:42:55 +00:00
|
|
|
if (typeof resource === 'undefined') {
|
|
|
|
// resource needs to be retrieved lazily as entries aren't guaranteed
|
|
|
|
// to be available at script execution time (caching seems to affect this)
|
|
|
|
// waiting until we retrieve this value lazily at runtime means that
|
|
|
|
// the entries are always available as these values are only retrieved
|
|
|
|
// after initialization
|
|
|
|
// current is based on the assumption that whereever this script is it's
|
|
|
|
// likely to be the same as the xmlhttprequests
|
|
|
|
resource = getResourceFor(currentSrc);
|
|
|
|
}
|
2020-01-21 17:52:40 +00:00
|
|
|
return resource.nextHopProtocol || 'http/1.1';
|
|
|
|
case 'CONSUL_HTTP_MAX_CONNECTIONS':
|
|
|
|
protocol = env('CONSUL_HTTP_PROTOCOL');
|
|
|
|
// http/2, http2+QUIC/39 and SPDY don't have connection limits
|
|
|
|
switch (true) {
|
|
|
|
case protocol.indexOf('h2') === 0:
|
|
|
|
case protocol.indexOf('hq') === 0:
|
|
|
|
case protocol.indexOf('spdy') === 0:
|
|
|
|
// TODO: Change this to return -1 so we try to consistently
|
|
|
|
// return a value from env vars
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// generally 6 are available
|
|
|
|
// reserve 1 for traffic that we can't manage
|
|
|
|
return 5;
|
|
|
|
}
|
2024-01-26 21:38:51 +00:00
|
|
|
case 'CONSUL_V2_CATALOG_ENABLED':
|
|
|
|
return operatorConfig.V2CatalogEnabled === 'undefined'
|
|
|
|
? false
|
|
|
|
: operatorConfig.V2CatalogEnabled;
|
2020-01-21 17:52:40 +00:00
|
|
|
}
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const ui = function (key) {
|
2022-09-08 10:43:39 +00:00
|
|
|
let $ = {};
|
2020-01-21 17:52:40 +00:00
|
|
|
switch (config.environment) {
|
|
|
|
case 'development':
|
|
|
|
case 'staging':
|
|
|
|
case 'coverage':
|
|
|
|
case 'test':
|
2022-09-15 08:43:17 +00:00
|
|
|
$ = dev().reduce(function (prev, [key, value]) {
|
2020-01-21 17:52:40 +00:00
|
|
|
switch (key) {
|
2021-07-06 16:01:08 +00:00
|
|
|
case 'CONSUL_INTL_LOCALE':
|
|
|
|
prev['CONSUL_INTL_LOCALE'] = String(value).toLowerCase();
|
|
|
|
break;
|
|
|
|
case 'CONSUL_INTL_DEBUG':
|
|
|
|
prev['CONSUL_INTL_DEBUG'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2020-01-21 17:52:40 +00:00
|
|
|
case 'CONSUL_ACLS_ENABLE':
|
|
|
|
prev['CONSUL_ACLS_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2022-10-13 17:11:38 +00:00
|
|
|
case 'CONSUL_AGENTLESS_ENABLE':
|
|
|
|
prev['CONSUL_AGENTLESS_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2020-01-21 17:52:40 +00:00
|
|
|
case 'CONSUL_NSPACES_ENABLE':
|
|
|
|
prev['CONSUL_NSPACES_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2020-05-11 15:37:11 +00:00
|
|
|
case 'CONSUL_SSO_ENABLE':
|
|
|
|
prev['CONSUL_SSO_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2021-04-22 11:22:40 +00:00
|
|
|
case 'CONSUL_PARTITIONS_ENABLE':
|
|
|
|
prev['CONSUL_PARTITIONS_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2020-12-15 15:34:54 +00:00
|
|
|
case 'CONSUL_METRICS_PROXY_ENABLE':
|
|
|
|
prev['CONSUL_METRICS_PROXY_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2022-07-14 10:22:45 +00:00
|
|
|
case 'CONSUL_PEERINGS_ENABLE':
|
|
|
|
prev['CONSUL_PEERINGS_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
2022-07-12 11:02:45 +00:00
|
|
|
break;
|
2022-09-14 18:39:50 +00:00
|
|
|
case 'CONSUL_HCP_ENABLE':
|
|
|
|
prev['CONSUL_HCP_ENABLED'] = !!JSON.parse(String(value).toLowerCase());
|
|
|
|
break;
|
2020-12-15 15:34:54 +00:00
|
|
|
case 'CONSUL_UI_CONFIG':
|
|
|
|
prev['CONSUL_UI_CONFIG'] = JSON.parse(value);
|
|
|
|
break;
|
2022-09-08 10:43:39 +00:00
|
|
|
case 'TokenSecretID':
|
|
|
|
prev['CONSUL_HTTP_TOKEN'] = value;
|
|
|
|
break;
|
2024-01-26 21:38:51 +00:00
|
|
|
case 'CONSUL_V2_CATALOG_ENABLE':
|
|
|
|
prev['CONSUL_V2_CATALOG_ENABLED'] = JSON.parse(value);
|
|
|
|
break;
|
2020-01-21 17:52:40 +00:00
|
|
|
default:
|
|
|
|
prev[key] = value;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
break;
|
2022-09-08 10:43:39 +00:00
|
|
|
case 'production':
|
2022-09-15 08:43:17 +00:00
|
|
|
$ = dev().reduce(function (prev, [key, value]) {
|
2022-09-08 10:43:39 +00:00
|
|
|
switch (key) {
|
|
|
|
case 'TokenSecretID':
|
|
|
|
prev['CONSUL_HTTP_TOKEN'] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (typeof $[key] !== 'undefined') {
|
|
|
|
return $[key];
|
2020-01-21 17:52:40 +00:00
|
|
|
}
|
|
|
|
return config[key];
|
|
|
|
};
|
|
|
|
return function env(str) {
|
|
|
|
switch (str) {
|
|
|
|
// All user providable values should start with CONSUL_UI
|
|
|
|
// We allow the user to set these ones via localStorage
|
|
|
|
// user value is preferred.
|
|
|
|
case 'CONSUL_UI_DISABLE_REALTIME':
|
|
|
|
case 'CONSUL_UI_DISABLE_ANCHOR_SELECTION':
|
|
|
|
// these are booleans cast things out
|
|
|
|
return !!JSON.parse(String(user(str) || 0).toLowerCase()) || ui(str);
|
|
|
|
case 'CONSUL_UI_REALTIME_RUNNER':
|
|
|
|
// these are strings
|
|
|
|
return user(str) || ui(str);
|
2020-12-15 15:34:54 +00:00
|
|
|
case 'CONSUL_UI_CONFIG':
|
2021-01-20 18:40:46 +00:00
|
|
|
case 'CONSUL_DATACENTER_LOCAL':
|
2021-10-26 14:30:17 +00:00
|
|
|
case 'CONSUL_DATACENTER_PRIMARY':
|
2022-09-08 10:45:20 +00:00
|
|
|
case 'CONSUL_HCP_MANAGED_RUNTIME':
|
2022-09-06 10:13:51 +00:00
|
|
|
case 'CONSUL_API_PREFIX':
|
2022-09-07 15:43:42 +00:00
|
|
|
case 'CONSUL_HCP_URL':
|
2021-01-20 18:40:46 +00:00
|
|
|
case 'CONSUL_ACLS_ENABLED':
|
|
|
|
case 'CONSUL_NSPACES_ENABLED':
|
2022-07-14 10:22:45 +00:00
|
|
|
case 'CONSUL_PEERINGS_ENABLED':
|
2022-10-13 17:11:38 +00:00
|
|
|
case 'CONSUL_AGENTLESS_ENABLED':
|
2022-09-14 18:39:50 +00:00
|
|
|
case 'CONSUL_HCP_ENABLED':
|
2021-01-20 18:40:46 +00:00
|
|
|
case 'CONSUL_SSO_ENABLED':
|
2021-04-22 11:22:40 +00:00
|
|
|
case 'CONSUL_PARTITIONS_ENABLED':
|
2020-12-15 15:34:54 +00:00
|
|
|
case 'CONSUL_METRICS_PROVIDER':
|
|
|
|
case 'CONSUL_METRICS_PROXY_ENABLE':
|
|
|
|
case 'CONSUL_SERVICE_DASHBOARD_URL':
|
2020-04-29 08:25:20 +00:00
|
|
|
case 'CONSUL_BASE_UI_URL':
|
2024-01-26 21:38:51 +00:00
|
|
|
case 'CONSUL_V2_CATALOG_ENABLED':
|
2020-01-21 17:52:40 +00:00
|
|
|
case 'CONSUL_HTTP_PROTOCOL':
|
2022-09-20 14:49:31 +00:00
|
|
|
case 'CONSUL_HTTP_MAX_CONNECTIONS': {
|
2020-01-21 17:52:40 +00:00
|
|
|
// We allow the operator to set these ones via various methods
|
|
|
|
// although UI developer config is preferred
|
2022-09-20 14:49:31 +00:00
|
|
|
const _ui = ui(str);
|
|
|
|
return typeof _ui !== 'undefined' ? _ui : operator(str, env);
|
|
|
|
}
|
2020-01-21 17:52:40 +00:00
|
|
|
default:
|
|
|
|
return ui(str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|