Make AuthGuard integration decouple from knowing the URL at startup
Every language's AuthGuard client/service used "no URL at construction" to mean "auth disabled forever" - a live SetBaseUrl/setBaseUrl push from ProjectConfigurator could only ever retarget an *already-enabled* client, never turn one on for the first time. That made AUTHGUARD_URL a de-facto required env var everywhere, even though ProjectConfigurator can push it live after startup. - Go: shared/backend/auth's New/NewWithStore now default to a "pending" Service (still enabled, fails closed until a URL arrives) instead of disabled when authGuardURL is empty; add an explicit Disabled() option for the genuine standalone/local-dev opt-out. - Python/Node/Java: add setBaseUrl/set_base_url to each AuthClient so a client built with an empty base URL can be retargeted in place once a URL is pushed, the same as the Go client already supported. - Python/Node: add a coreconfig module (Info/Store), the framework-agnostic equivalent of shared/backend/coreconfig (Go), for services in those languages to receive ProjectConfigurator's core-services push at all. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c09957189f
commit
dcfdb3d7af
18 files changed
+747
-46
No files matched your search
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Lets any Node service in this workspace receive AuthGuard's and
|
||||
* CarrierNode's current base URL(s) pushed from ProjectConfigurator, instead
|
||||
* of hardcoding them in its own env vars. Mirrors shared/backend/coreconfig
|
||||
* (Go) and shared/packages/python-backend/src/shared_backend/coreconfig.py
|
||||
* (Python) exactly - same Info shape, same Store semantics - translated to
|
||||
* Node idioms; keep all three in sync if any changes shape. See
|
||||
* ProjectConfigurator/README.md's "Pushing core service info to other
|
||||
* projects" section for the wire contract this module implements the
|
||||
* receiving end of.
|
||||
*
|
||||
* Framework-agnostic: this module has no HTTP server of its own. A service
|
||||
* wires the actual `PUT /api/core-services/internal` route in its own
|
||||
* framework (Express, plain `http`, ...), gated behind its own
|
||||
* INTERNAL_SERVICE_SECRET check the same way its other internal-service
|
||||
* routes already are, decodes the JSON body with `Info.fromJSON`, and calls
|
||||
* `store.set(info)` - see `Store.set`'s doc comment for a minimal Express
|
||||
* example.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{authGuardUrl?: string, carrierNodeUrl?: string, carrierNodeUrls?: string[]}} InfoLike
|
||||
*/
|
||||
|
||||
export class Info {
|
||||
/**
|
||||
* @param {InfoLike} [fields]
|
||||
*/
|
||||
constructor({ authGuardUrl = '', carrierNodeUrl = '', carrierNodeUrls = [] } = {}) {
|
||||
this.authGuardUrl = authGuardUrl;
|
||||
this.carrierNodeUrl = carrierNodeUrl;
|
||||
this.carrierNodeUrls = carrierNodeUrls;
|
||||
}
|
||||
|
||||
/** Decodes the JSON body ProjectConfigurator's push carries (coreServiceInfo). */
|
||||
static fromJSON(data) {
|
||||
const raw = data ?? {};
|
||||
let urls = Array.isArray(raw.carrierNodeUrls)
|
||||
? raw.carrierNodeUrls.map((u) => String(u ?? '').trim()).filter(Boolean)
|
||||
: [];
|
||||
if (urls.length === 0) {
|
||||
const single = String(raw.carrierNodeUrl ?? '').trim();
|
||||
if (single) urls = [single];
|
||||
}
|
||||
return new Info({
|
||||
authGuardUrl: String(raw.authGuardUrl ?? '').trim(),
|
||||
carrierNodeUrl: urls[0] ?? '',
|
||||
carrierNodeUrls: urls,
|
||||
});
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
authGuardUrl: this.authGuardUrl,
|
||||
carrierNodeUrl: this.carrierNodeUrl,
|
||||
carrierNodeUrls: [...this.carrierNodeUrls],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function normalize(info) {
|
||||
let cleaned = (info.carrierNodeUrls ?? []).map((u) => String(u ?? '').trim()).filter(Boolean);
|
||||
if (cleaned.length === 0 && String(info.carrierNodeUrl ?? '').trim()) {
|
||||
cleaned = [String(info.carrierNodeUrl).trim()];
|
||||
}
|
||||
return new Info({
|
||||
authGuardUrl: String(info.authGuardUrl ?? '').trim(),
|
||||
carrierNodeUrl: cleaned[0] ?? '',
|
||||
carrierNodeUrls: cleaned,
|
||||
});
|
||||
}
|
||||
|
||||
export class Store {
|
||||
/**
|
||||
* Holds the most recently pushed Info, defaulting to whatever fallback was
|
||||
* given (typically this service's own AUTHGUARD_URL/CARRIERNODE_URL env
|
||||
* vars, or "" now that they're optional) until ProjectConfigurator's first
|
||||
* push arrives - or forever, if this deployment isn't registered with a
|
||||
* ProjectConfigurator instance at all, in which case those env vars (if
|
||||
* set) keep working exactly as before.
|
||||
* @param {InfoLike} [fallback]
|
||||
*/
|
||||
constructor(fallback = {}) {
|
||||
this._info = normalize(fallback instanceof Info ? fallback : new Info(fallback));
|
||||
this._onUpdate = null;
|
||||
}
|
||||
|
||||
get() {
|
||||
return this._info;
|
||||
}
|
||||
|
||||
get authGuardUrl() {
|
||||
return this._info.authGuardUrl;
|
||||
}
|
||||
|
||||
get carrierNodeUrl() {
|
||||
return this._info.carrierNodeUrl;
|
||||
}
|
||||
|
||||
get carrierNodeUrls() {
|
||||
return this._info.carrierNodeUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers fn to be called (synchronously, so keep it fast) with the new
|
||||
* Info after every set() - e.g. an AuthClient's setBaseUrl for a client
|
||||
* that isn't already reading through Store on every call. Only one
|
||||
* callback is kept; a second registration replaces the first.
|
||||
* @param {(info: Info) => void} fn
|
||||
*/
|
||||
onUpdate(fn) {
|
||||
this._onUpdate = fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the current Info, field by field, ignoring a blank/empty field
|
||||
* so a partial push (e.g. only carrierNodeUrl(s) changed) never clobbers
|
||||
* the other value. Returns the resulting Info. Minimal Express usage:
|
||||
*
|
||||
* app.put('/api/core-services/internal', (req, res) => {
|
||||
* if (req.get('X-Internal-Auth') !== process.env.INTERNAL_SERVICE_SECRET) {
|
||||
* return res.status(401).json({ error: 'internal service authentication required' });
|
||||
* }
|
||||
* const info = coreConfigStore.set(Info.fromJSON(req.body));
|
||||
* res.json({ ok: true, info: info.toJSON() });
|
||||
* });
|
||||
*
|
||||
* @param {InfoLike | Info} nextInfo
|
||||
* @returns {Info}
|
||||
*/
|
||||
set(nextInfo) {
|
||||
const next = normalize(nextInfo instanceof Info ? nextInfo : new Info(nextInfo));
|
||||
if (next.authGuardUrl) {
|
||||
this._info.authGuardUrl = next.authGuardUrl;
|
||||
}
|
||||
if (next.carrierNodeUrls.length > 0) {
|
||||
this._info.carrierNodeUrls = next.carrierNodeUrls;
|
||||
this._info.carrierNodeUrl = next.carrierNodeUrls[0];
|
||||
}
|
||||
if (this._onUpdate) {
|
||||
this._onUpdate(this._info);
|
||||
}
|
||||
return this._info;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user