mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
fix(server): ask the issuer for the allowance of a key only when TREK_SERVICES_HOSTS lists it
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import { servicesConfig } from '../app-config/tokens';
|
||||
import { logDebug } from '../audit/audit-log.logger';
|
||||
import { IssuerClient } from './issuer.client';
|
||||
import { QuotaTracker } from './quota-tracker';
|
||||
import { ServiceClock } from './service-clock';
|
||||
import { SERVICE_ISSUER_ORIGIN } from './service-identity.constants';
|
||||
import { isAllowlisted } from './service-identity.helpers';
|
||||
import { ServiceKeyService, type KeySnapshot } from './service-key.service';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import type { ConfigType } from '@nestjs/config';
|
||||
import {
|
||||
serviceTierSchema,
|
||||
type ServiceIdentityStatus,
|
||||
@@ -23,6 +27,7 @@ export class ServiceIdentityService {
|
||||
private cached: { bearer: string | null; at: number; quota: ServiceQuota | null } | null = null;
|
||||
|
||||
constructor(
|
||||
@Inject(servicesConfig.KEY) private readonly config: ConfigType<typeof servicesConfig>,
|
||||
private readonly keys: ServiceKeyService,
|
||||
private readonly issuer: IssuerClient,
|
||||
private readonly tracker: QuotaTracker,
|
||||
@@ -49,8 +54,9 @@ export class ServiceIdentityService {
|
||||
return this.status();
|
||||
}
|
||||
|
||||
/** GET /v1/quota, which costs no allowance; null when the issuer cannot be asked right now. */
|
||||
/** GET /v1/quota, which costs no allowance; null when the issuer cannot or may not be asked right now. */
|
||||
private async freshQuota(bearer: string | null): Promise<ServiceQuota | null> {
|
||||
if (bearer && !this.issuerMaySeeKey()) return null;
|
||||
if (this.cached && this.cached.bearer === bearer && Date.now() - this.cached.at < QUOTA_CACHE_MS) {
|
||||
return this.cached.quota;
|
||||
}
|
||||
@@ -65,6 +71,11 @@ export class ServiceIdentityService {
|
||||
this.cached = { bearer, at: Date.now(), quota };
|
||||
return quota;
|
||||
}
|
||||
|
||||
/** Even the issuer sees the key only while TREK_SERVICES_HOSTS lists it. */
|
||||
private issuerMaySeeKey(): boolean {
|
||||
return isAllowlisted(new URL(SERVICE_ISSUER_ORIGIN), this.config.hosts);
|
||||
}
|
||||
}
|
||||
|
||||
function keyTier(key: KeySnapshot): ServiceTier | null {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* the issuer when it answers, the last one a liketrek answer stated when it
|
||||
* does not, nothing at all while the services are off.
|
||||
*/
|
||||
import { servicesConfig } from '../../../../src/nest/app-config/tokens';
|
||||
import { IssuerClient } from '../../../../src/nest/service-identity/issuer.client';
|
||||
import { QuotaTracker } from '../../../../src/nest/service-identity/quota-tracker';
|
||||
import { ServiceClock } from '../../../../src/nest/service-identity/service-clock';
|
||||
@@ -21,6 +22,7 @@ const QUOTA = { tier: 'std', limit: 10_000, used: 250, remaining: 9_750, reset_a
|
||||
|
||||
const keys = { snapshot: vi.fn(async (): Promise<KeySnapshot> => CONNECTED), regenerate: vi.fn(async () => undefined) };
|
||||
const issuer = { quota: vi.fn() };
|
||||
const config = { mode: 'auto' as const, key: null, hosts: ['places.liketrek.com'] };
|
||||
|
||||
const testDb = createSnapshotTestDb();
|
||||
let t: TestModule;
|
||||
@@ -28,6 +30,7 @@ let identity: ServiceIdentityService;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
config.hosts = ['places.liketrek.com'];
|
||||
issuer.quota.mockResolvedValue({ body: QUOTA, serverMs: null });
|
||||
t = await createTestModule({
|
||||
db: testDb,
|
||||
@@ -38,6 +41,7 @@ beforeEach(async () => {
|
||||
ServiceClock,
|
||||
{ provide: ServiceKeyService, useValue: keys },
|
||||
{ provide: IssuerClient, useValue: issuer },
|
||||
{ provide: servicesConfig.KEY, useValue: config },
|
||||
],
|
||||
});
|
||||
identity = t.get(ServiceIdentityService);
|
||||
@@ -105,4 +109,10 @@ describe('ServiceIdentityService.status', () => {
|
||||
expect(after.state).toBe('registering');
|
||||
expect(issuer.quota).toHaveBeenLastCalledWith(null);
|
||||
});
|
||||
|
||||
it('SISVC-006: never shows the key to the issuer when TREK_SERVICES_HOSTS leaves it out', async () => {
|
||||
config.hosts = ['gate.example.org'];
|
||||
expect(await identity.status()).toMatchObject({ state: 'connected', tier: 'hosted', quota: null });
|
||||
expect(issuer.quota).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user