Normalize live policy simulation tenant routing

This commit is contained in:
master
2026-03-10 02:14:29 +02:00
parent 72084355a6
commit c0c0267ac9
4 changed files with 82 additions and 3 deletions

View File

@@ -30,7 +30,9 @@ describe('PolicySimulationHttpClient', () => {
beforeEach(() => {
authSessionStoreMock = jasmine.createSpyObj('AuthSessionStore', ['getActiveTenantId']);
tenantServiceMock = jasmine.createSpyObj('TenantActivationService', ['getActiveTenant']);
tenantServiceMock = {
activeTenantId: jasmine.createSpy('activeTenantId').and.returnValue(null),
};
authSessionStoreMock.getActiveTenantId.and.returnValue('tenant-001');
TestBed.configureTestingModule({
@@ -726,6 +728,7 @@ describe('PolicySimulationHttpClient', () => {
it('should use provided tenant over active tenant', async () => {
const customTenant = 'custom-tenant-001';
tenantServiceMock.activeTenantId.and.returnValue('tenant-context-001');
const promise = firstValueFrom(httpClient.getShadowModeConfig({ tenantId: customTenant }));
const req = httpMock.expectOne(`${baseUrl}/policy/shadow/config`);
@@ -734,6 +737,23 @@ describe('PolicySimulationHttpClient', () => {
await promise;
});
it('should map legacy default placeholder tenant to the active tenant context', async () => {
tenantServiceMock.activeTenantId.and.returnValue('demo-prod');
const promise = firstValueFrom(
httpClient.getSimulationHistory({
tenantId: 'default',
page: 1,
pageSize: 20,
}),
);
const req = httpMock.expectOne((request) => request.url === `${baseUrl}/policy/simulations/history`);
expect(req.request.headers.get('X-StellaOps-Tenant')).toBe('demo-prod');
req.flush({ items: [], total: 0, hasMore: false });
await promise;
});
});
});

View File

@@ -110,6 +110,8 @@ export interface PolicySimulationApi {
export const POLICY_SIMULATION_API = new InjectionToken<PolicySimulationApi>('POLICY_SIMULATION_API');
export const POLICY_SIMULATION_API_BASE_URL = new InjectionToken<string>('POLICY_SIMULATION_API_BASE_URL');
const LEGACY_PLACEHOLDER_TENANTS = new Set(['default']);
// ============================================================================
// HTTP Implementation
// ============================================================================
@@ -426,7 +428,15 @@ export class PolicySimulationHttpClient implements PolicySimulationApi {
}
private resolveTenant(tenantId?: string): string {
const tenant = (tenantId && tenantId.trim()) || this.authSession.getActiveTenantId();
const requestedTenant = tenantId?.trim() || null;
const activeTenant =
this.tenantService.activeTenantId?.() ??
this.authSession.getActiveTenantId();
const tenant =
!requestedTenant || (activeTenant && LEGACY_PLACEHOLDER_TENANTS.has(requestedTenant.toLowerCase()))
? activeTenant ?? requestedTenant
: requestedTenant;
if (!tenant) {
throw new Error('PolicySimulationHttpClient requires an active tenant identifier.');
}