Skip to content

Commit

Permalink
PIN-5931: added SH feature flag and whitelist (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
borgesis95 authored Feb 3, 2025
1 parent 7ded6c9 commit 9fd0fe7
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/catalog-process/.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ READMODEL_DB_NAME=readmodel
READMODEL_DB_USERNAME=root
READMODEL_DB_PASSWORD=example
READMODEL_DB_PORT=27017

WELL_KNOWN_URLS="http://127.0.0.1:4500/jwks.json"
ACCEPTED_AUDIENCES="dev.interop.pagopa.it/ui,refactor.dev.interop.pagopa.it/ui,refactor.dev.interop.pagopa.it/internal,dev.interop.pagopa.it/m2m,refactor.dev.interop.pagopa.it/m2m"

Expand All @@ -27,3 +26,6 @@ S3_SERVER_PORT=9000
ESERVICE_DOCUMENTS_PATH="interop-eservice-documents"

PRODUCER_ALLOWED_ORIGINS="IPA"

FEATURE_FLAG_SIGNALHUB_WHITELIST=true
SIGNALHUB_WHITELIST=cec6866f-1561-4c83-a230-c91b98c2a890
2 changes: 2 additions & 0 deletions packages/catalog-process/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
FileManagerConfig,
EventStoreConfig,
S3Config,
FeatureFlagsConfig,
} from "pagopa-interop-commons";
import { z } from "zod";

const CatalogProcessConfig = CommonHTTPServiceConfig.and(ReadModelDbConfig)
.and(FileManagerConfig)
.and(S3Config)
.and(EventStoreConfig)
.and(FeatureFlagsConfig)
.and(
z
.object({
Expand Down
23 changes: 21 additions & 2 deletions packages/catalog-process/src/services/catalogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ async function parseAndCheckAttributes(
};
}

function isTenantInSignalHubWhitelist(
organizationId: TenantId,
isSignalubEnabled: boolean | undefined
): boolean | undefined {
return config.signalhubWhitelist?.includes(organizationId)
? isSignalubEnabled
: false;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function catalogServiceBuilder(
dbInstance: DB,
Expand Down Expand Up @@ -444,7 +453,12 @@ export function catalogServiceBuilder(
descriptors: [],
createdAt: creationDate,
riskAnalysis: [],
isSignalHubEnabled: seed.isSignalHubEnabled,
isSignalHubEnabled: config.featureFlagSignalhubWhitelist
? isTenantInSignalHubWhitelist(
authData.organizationId,
seed.isSignalHubEnabled
)
: seed.isSignalHubEnabled,
};

const eserviceCreationEvent = toCreateEventEServiceAdded(
Expand Down Expand Up @@ -565,7 +579,12 @@ export function catalogServiceBuilder(
serverUrls: [],
}))
: eservice.data.descriptors,
isSignalHubEnabled: eserviceSeed.isSignalHubEnabled,
isSignalHubEnabled: config.featureFlagSignalhubWhitelist
? isTenantInSignalHubWhitelist(
authData.organizationId,
eservice.data.isSignalHubEnabled
)
: eservice.data.isSignalHubEnabled,
};

const event = toCreateEventEServiceUpdated(
Expand Down
79 changes: 79 additions & 0 deletions packages/catalog-process/test/createEService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
inconsistentDailyCalls,
originNotCompliant,
} from "../src/model/domain/errors.js";
import { config } from "../src/config/config.js";
import {
addOneEService,
buildDescriptorSeedForEserviceCreation,
Expand All @@ -40,6 +41,9 @@ describe("create eservice", () => {
vi.useRealTimers();
});
it("should write on event-store for the creation of an eservice", async () => {
config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [mockEService.producerId];

const isSignalHubEnabled = randomArrayItem([false, true, undefined]);
const eservice = await catalogService.createEService(
{
Expand Down Expand Up @@ -119,6 +123,81 @@ describe("create eservice", () => {
toEServiceV2(expectedEserviceWithDescriptor)
);
});
it("should assign value inherit from request to isSignalhubEnabled field if signalhub whitelist feature flag is not enabled", async () => {
config.featureFlagSignalhubWhitelist = false;
const isSignalHubEnabled = randomArrayItem([false, true, undefined]);
const eservice = await catalogService.createEService(
{
name: mockEService.name,
description: mockEService.description,
technology: "REST",
mode: "DELIVER",
descriptor: buildDescriptorSeedForEserviceCreation(mockDescriptor),
isSignalHubEnabled,
},
{
authData: getMockAuthData(mockEService.producerId),
correlationId: generateId(),
serviceName: "",
logger: genericLogger,
}
);

expect(eservice).toBeDefined();
expect(eservice.isSignalHubEnabled).toBe(isSignalHubEnabled);
});

it("should assign false to isSignalhubEnabled field if signalhub whitelist feature flag is enabled but the organization is not in whitelist", async () => {
config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [generateId()];
const isSignalHubEnabled = true;

const eservice = await catalogService.createEService(
{
name: mockEService.name,
description: mockEService.description,
technology: "REST",
mode: "DELIVER",
descriptor: buildDescriptorSeedForEserviceCreation(mockDescriptor),
isSignalHubEnabled,
},
{
authData: getMockAuthData(mockEService.producerId),
correlationId: generateId(),
serviceName: "",
logger: genericLogger,
}
);

expect(eservice).toBeDefined();
expect(eservice.isSignalHubEnabled).toBe(false);
});

it("should assign value inherit from request to isSignalhubEnabled field if signalhub whitelist feature flag is enabled and the organization is in whitelist", async () => {
config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [mockEService.producerId];
const isSignalHubEnabled = randomArrayItem([false, true, undefined]);

const eservice = await catalogService.createEService(
{
name: mockEService.name,
description: mockEService.description,
technology: "REST",
mode: "DELIVER",
descriptor: buildDescriptorSeedForEserviceCreation(mockDescriptor),
isSignalHubEnabled,
},
{
authData: getMockAuthData(mockEService.producerId),
correlationId: generateId(),
serviceName: "",
logger: genericLogger,
}
);

expect(eservice).toBeDefined();
expect(eservice.isSignalHubEnabled).toBe(isSignalHubEnabled);
});

it("should throw eServiceDuplicate if an eservice with the same name already exists", async () => {
await addOneEService(mockEService);
Expand Down
15 changes: 13 additions & 2 deletions packages/catalog-process/test/updateEservice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe("update eService", () => {
const mockDocument = getMockDocument();
it("should write on event-store for the update of an eService (no technology change)", async () => {
vi.spyOn(fileManager, "delete");

config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [mockEService.producerId];
const isSignalHubEnabled = randomArrayItem([false, true, undefined]);
const descriptor: Descriptor = {
...getMockDescriptor(),
Expand All @@ -48,6 +49,7 @@ describe("update eService", () => {
const eservice: EService = {
...mockEService,
descriptors: [descriptor],
isSignalHubEnabled,
};
const updatedName = "eservice new name";
await addOneEService(eservice);
Expand All @@ -71,7 +73,6 @@ describe("update eService", () => {
const updatedEService: EService = {
...eservice,
name: updatedName,
isSignalHubEnabled,
};

const writtenEvent = await readLastEserviceEvent(mockEService.id);
Expand All @@ -92,6 +93,9 @@ describe("update eService", () => {
it("should write on event-store for the update of an eService (technology change: interface has to be deleted)", async () => {
vi.spyOn(fileManager, "delete");

config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [mockEService.producerId];

const interfaceDocument = {
...mockDocument,
name: `${mockDocument.name}`,
Expand Down Expand Up @@ -217,6 +221,10 @@ describe("update eService", () => {
});
it("should write on event-store for the update of an eService (update description only)", async () => {
const updatedDescription = "eservice new description";

config.featureFlagSignalhubWhitelist = true;
config.signalhubWhitelist = [mockEService.producerId];

await addOneEService(mockEService);
const returnedEService = await catalogService.updateEService(
mockEService.id,
Expand Down Expand Up @@ -256,6 +264,7 @@ describe("update eService", () => {
});

it("should write on event-store for the update of an eService (update mode to DELIVER so risk analysis has to be deleted)", async () => {
config.featureFlagSignalhubWhitelist = true;
const riskAnalysis = getMockValidRiskAnalysis("PA");
const eservice: EService = {
...mockEService,
Expand All @@ -265,6 +274,8 @@ describe("update eService", () => {
};
await addOneEService(eservice);

config.signalhubWhitelist = [eservice.producerId];

const returnedEService = await catalogService.updateEService(
eservice.id,
{
Expand Down
20 changes: 20 additions & 0 deletions packages/commons/src/config/featureFlagsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { z } from "zod";

export const FeatureFlagsConfig = z
.object({
FEATURE_FLAG_SIGNALHUB_WHITELIST: z
.enum(["true", "false"])
.default("false")
.transform((value) => value === "true"),
SIGNALHUB_WHITELIST: z
.string()
.transform((value) => value.split(","))
.pipe(z.array(z.string().uuid()))
.optional(),
})
.transform((c) => ({
featureFlagSignalhubWhitelist: c.FEATURE_FLAG_SIGNALHUB_WHITELIST,
signalhubWhitelist: c.SIGNALHUB_WHITELIST,
}));

export type FeatureFlagsConfig = z.infer<typeof FeatureFlagsConfig>;
1 change: 1 addition & 0 deletions packages/commons/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./sessionTokenGenerationConfig.js";
export * from "./redisRateLimiterConfig.js";
export * from "./pecEmailManagerConfig.js";
export * from "./selfcareConfig.js";
export * from "./featureFlagsConfig.js";

0 comments on commit 9fd0fe7

Please sign in to comment.