Refactor origin to cluster (#1210)

This commit is contained in:
Carlos Alcaide Corvo authored and GitHub committed 2022-07-27 10:25:01 -07:00
1 parent 40ba4286d3
commit 4fcb005a1f
43 files changed
+492 -609

No files matched your search

@@ -1,35 +1,35 @@
const netRequestPromise = require('../helpers/net-request-promise.js');
// Runtime settings are any configuration, especially user-specified,
// set up at runtime. For example, the Boundary origin is a runtime setting.
// set up at runtime. For example, the Boundary clusterUrl is a runtime setting.
class RuntimeSettings {
// Internal private origin is exposed via getter/setter below.
#origin = undefined;
// Internal private clusterUrl is exposed via getter/setter below.
#clusterUrl = undefined;
/**
* The user-specified Boundary origin, which should be allowed by CSP.
* The user-specified Boundary clusterUrl, which should be allowed by CSP.
* @type {?string}
*/
get origin() {
return this.#origin;
get clusterUrl() {
return this.#clusterUrl;
}
set origin(origin) {
if (this.#origin !== origin) {
this.#origin = origin;
this.triggerOriginChanged();
set clusterUrl(clusterUrl) {
if (this.#clusterUrl !== clusterUrl) {
this.#clusterUrl = clusterUrl;
this.triggerClusterUrlChanged();
}
}
/**
* Validates the origin is reachable and has a scopes endpoint.
* Validates the clusterUrl is reachable and has a scopes endpoint.
*/
async validateOrigin(origin) {
// If the origin is the Electron origin, it is automatically valid.
if (origin === 'serve://boundary') return true;
// Otherwise, check if scopes can be loaded from the specified origin.
async validateClusterUrl(clusterUrl) {
// If the clusterUrl is the Electron clusterUrl, it is automatically valid.
if (clusterUrl === 'serve://boundary') return true;
// Otherwise, check if scopes can be loaded from the specified clusterUrl.
// Scopes are required by the desktop client, so this is a simple smoke
// test to see if the origin is API-compatible with the desktop client.
const scopesEndpoint = `${origin}/v1/scopes`;
// test to see if the clusterUrl is API-compatible with the desktop client.
const scopesEndpoint = `${clusterUrl}/v1/scopes`;
try {
const result = await netRequestPromise(scopesEndpoint);
// Redirects (3xx) are handled in the netRequest Promise, so we only
@@ -38,27 +38,27 @@ class RuntimeSettings {
throw new Error();
}
} catch (e) {
throw new Error(`Origin ${origin} could not be validated.`);
throw new Error(`Cluster URL ${clusterUrl} could not be validated.`);
}
}
/**
* Sets the origin to null.
* Sets the clusterUrl to null.
*/
async resetOrigin() {
this.origin = null;
async resetClusterUrl() {
this.#clusterUrl = null;
}
// Quick and dirty event handler pattern to enable the application to respond
// when the origin is changed.
#originWatchers = [];
// when the clusterUrl is changed.
#clusterUrlWatchers = [];
onOriginChange(fn) {
this.#originWatchers.push(fn);
onClusterUrlChange(fn) {
this.#clusterUrlWatchers.push(fn);
}
triggerOriginChanged() {
this.#originWatchers.forEach((fn) => fn(this.#origin));
triggerClusterUrlChanged() {
this.#clusterUrlWatchers.forEach((fn) => fn(this.#clusterUrl));
}
}