Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#IOPAE-200] update function services with authorized cidrs on manage APIs #268

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions CreateService/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
AzureUserAttributesManageMiddleware,
IAzureUserAttributesManage
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes_manage";
import { SubscriptionCIDRsModel } from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { APIClient } from "../clients/admin";
import { ServicePayload } from "../generated/definitions/ServicePayload";
import { ServiceWithSubscriptionKeys } from "../generated/definitions/ServiceWithSubscriptionKeys";
Expand Down Expand Up @@ -243,13 +244,15 @@ export function CreateServiceHandler(
* Wraps a CreateService handler inside an Express request handler.
*/
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function CreateService(
export const CreateService = (
telemetryClient: ReturnType<typeof initAppInsights>,
serviceModel: ServiceModel,
client: APIClient,
client: APIClient
) => (
productName: NonEmptyString,
sandboxFiscalCode: NonEmptyString
): express.RequestHandler {
sandboxFiscalCode: NonEmptyString,
serviceModel: ServiceModel,
subscriptionCIDRsModel: SubscriptionCIDRsModel
): express.RequestHandler => {
const handler = CreateServiceHandler(
telemetryClient,
client,
Expand All @@ -263,7 +266,7 @@ export function CreateService(
ClientIpMiddleware,
SequenceMiddleware(ResponseErrorForbiddenNotAuthorized)(
AzureUserAttributesMiddleware(serviceModel),
AzureUserAttributesManageMiddleware()
AzureUserAttributesManageMiddleware(subscriptionCIDRsModel)
),
RequiredBodyPayloadMiddleware(ServicePayload)
);
Expand All @@ -272,4 +275,4 @@ export function CreateService(
checkSourceIpForHandler(handler, (_, __, c, u, ___) => ipTuple(c, u))
)
);
}
};
17 changes: 12 additions & 5 deletions CreateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import {
SubscriptionCIDRsModel,
SUBSCRIPTION_CIDRS_COLLECTION_NAME
} from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { cosmosdbInstance } from "../utils/cosmosdb";

import { apiClient } from "../clients/admin";
Expand All @@ -31,18 +35,21 @@ const serviceModel = new ServiceModel(
cosmosdbInstance.container(SERVICE_COLLECTION_NAME)
);

const subscriptionCIDRsModel = new SubscriptionCIDRsModel(
cosmosdbInstance.container(SUBSCRIPTION_CIDRS_COLLECTION_NAME)
);

const telemetryClient = initTelemetryClient(
config.APPINSIGHTS_INSTRUMENTATIONKEY
);

app.post(
"/api/v1/services",
CreateService(
telemetryClient,
serviceModel,
apiClient,
CreateService(telemetryClient, apiClient)(
config.DEFAULT_SUBSCRIPTION_PRODUCT_NAME,
config.SANDBOX_FISCAL_CODE
config.SANDBOX_FISCAL_CODE,
serviceModel,
subscriptionCIDRsModel
)
);

Expand Down
6 changes: 4 additions & 2 deletions RegenerateServiceKey/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
AzureUserAttributesManageMiddleware,
IAzureUserAttributesManage
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes_manage";
import { SubscriptionCIDRsModel } from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { APIClient } from "../clients/admin";
import { SubscriptionKeys } from "../generated/definitions/SubscriptionKeys";
import { SubscriptionKeyTypePayload } from "../generated/definitions/SubscriptionKeyTypePayload";
Expand Down Expand Up @@ -141,7 +142,8 @@ export function RegenerateServiceKeyHandler(
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function RegenerateServiceKey(
serviceModel: ServiceModel,
client: APIClient
client: APIClient,
subscriptionCIDRsModel: SubscriptionCIDRsModel
): express.RequestHandler {
const handler = RegenerateServiceKeyHandler(client);
const middlewaresWrap = withRequestMiddlewares(
Expand All @@ -150,7 +152,7 @@ export function RegenerateServiceKey(
ClientIpMiddleware,
SequenceMiddleware(ResponseErrorForbiddenNotAuthorized)(
AzureUserAttributesMiddleware(serviceModel),
AzureUserAttributesManageMiddleware()
AzureUserAttributesManageMiddleware(subscriptionCIDRsModel)
),
RequiredParamMiddleware("service_id", NonEmptyString),
RequiredBodyPayloadMiddleware(SubscriptionKeyTypePayload)
Expand Down
10 changes: 9 additions & 1 deletion RegenerateServiceKey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import {
SubscriptionCIDRsModel,
SUBSCRIPTION_CIDRS_COLLECTION_NAME
} from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { cosmosdbInstance } from "../utils/cosmosdb";

import { apiClient } from "../clients/admin";
Expand All @@ -22,9 +26,13 @@ const serviceModel = new ServiceModel(
cosmosdbInstance.container(SERVICE_COLLECTION_NAME)
);

const subscriptionCIDRsModel = new SubscriptionCIDRsModel(
cosmosdbInstance.container(SUBSCRIPTION_CIDRS_COLLECTION_NAME)
);

app.put(
"/api/v1/services/:service_id/keys",
RegenerateServiceKey(serviceModel, apiClient)
RegenerateServiceKey(serviceModel, apiClient, subscriptionCIDRsModel)
);

const azureFunctionHandler = createAzureFunctionHandler(app);
Expand Down
6 changes: 4 additions & 2 deletions UpdateService/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
AzureUserAttributesManageMiddleware,
IAzureUserAttributesManage
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes_manage";
import { SubscriptionCIDRsModel } from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { APIClient } from "../clients/admin";
import { ServicePayload } from "../generated/definitions/ServicePayload";
import { ServiceWithSubscriptionKeys } from "../generated/definitions/ServiceWithSubscriptionKeys";
Expand Down Expand Up @@ -259,7 +260,8 @@ export function UpdateServiceHandler(
export function UpdateService(
telemetryClient: ReturnType<typeof initAppInsights>,
serviceModel: ServiceModel,
client: APIClient
client: APIClient,
subscriptionCIDRsModel: SubscriptionCIDRsModel
): express.RequestHandler {
const handler = UpdateServiceHandler(telemetryClient, client);
const middlewaresWrap = withRequestMiddlewares(
Expand All @@ -268,7 +270,7 @@ export function UpdateService(
ClientIpMiddleware,
SequenceMiddleware(ResponseErrorForbiddenNotAuthorized)(
AzureUserAttributesMiddleware(serviceModel),
AzureUserAttributesManageMiddleware()
AzureUserAttributesManageMiddleware(subscriptionCIDRsModel)
),
RequiredParamMiddleware("service_id", NonEmptyString),
RequiredBodyPayloadMiddleware(ServicePayload)
Expand Down
15 changes: 14 additions & 1 deletion UpdateService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import {
SubscriptionCIDRsModel,
SUBSCRIPTION_CIDRS_COLLECTION_NAME
} from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { cosmosdbInstance } from "../utils/cosmosdb";

import { apiClient } from "../clients/admin";
Expand All @@ -30,13 +34,22 @@ const serviceModel = new ServiceModel(
cosmosdbInstance.container(SERVICE_COLLECTION_NAME)
);

const subscriptionCIDRsModel = new SubscriptionCIDRsModel(
cosmosdbInstance.container(SUBSCRIPTION_CIDRS_COLLECTION_NAME)
);

const telemetryClient = initTelemetryClient(
config.APPINSIGHTS_INSTRUMENTATIONKEY
);

app.put(
"/api/v1/services/:service_id",
UpdateService(telemetryClient, serviceModel, apiClient)
UpdateService(
telemetryClient,
serviceModel,
apiClient,
subscriptionCIDRsModel
)
);

const azureFunctionHandler = createAzureFunctionHandler(app);
Expand Down
6 changes: 4 additions & 2 deletions UploadServiceLogo/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
AzureUserAttributesManageMiddleware,
IAzureUserAttributesManage
} from "@pagopa/io-functions-commons/dist/src/utils/middlewares/azure_user_attributes_manage";
import { SubscriptionCIDRsModel } from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { APIClient } from "../clients/admin";
import { withApiRequestWrapper } from "../utils/api";
import { getLogger, ILogger } from "../utils/logging";
Expand Down Expand Up @@ -142,7 +143,8 @@ export function UploadServiceLogoHandler(
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function UploadServiceLogo(
serviceModel: ServiceModel,
client: APIClient
client: APIClient,
subscriptionCIDRsModel: SubscriptionCIDRsModel
): express.RequestHandler {
const handler = UploadServiceLogoHandler(client);
const middlewaresWrap = withRequestMiddlewares(
Expand All @@ -151,7 +153,7 @@ export function UploadServiceLogo(
ClientIpMiddleware,
SequenceMiddleware(ResponseErrorForbiddenNotAuthorized)(
AzureUserAttributesMiddleware(serviceModel),
AzureUserAttributesManageMiddleware()
AzureUserAttributesManageMiddleware(subscriptionCIDRsModel)
),
RequiredParamMiddleware("service_id", NonEmptyString),
RequiredBodyPayloadMiddleware(Logo)
Expand Down
10 changes: 9 additions & 1 deletion UploadServiceLogo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { secureExpressApp } from "@pagopa/io-functions-commons/dist/src/utils/ex
import { setAppContext } from "@pagopa/io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "@pagopa/express-azure-functions/dist/src/createAzureFunctionsHandler";
import {
SubscriptionCIDRsModel,
SUBSCRIPTION_CIDRS_COLLECTION_NAME
} from "@pagopa/io-functions-commons/dist/src/models/subscription_cidrs";
import { cosmosdbInstance } from "../utils/cosmosdb";

import { apiClient } from "../clients/admin";
Expand All @@ -22,9 +26,13 @@ const serviceModel = new ServiceModel(
cosmosdbInstance.container(SERVICE_COLLECTION_NAME)
);

const subscriptionCIDRsModel = new SubscriptionCIDRsModel(
cosmosdbInstance.container(SUBSCRIPTION_CIDRS_COLLECTION_NAME)
);

app.put(
"/api/v1/services/:service_id/logo",
UploadServiceLogo(serviceModel, apiClient)
UploadServiceLogo(serviceModel, apiClient, subscriptionCIDRsModel)
);

const azureFunctionHandler = createAzureFunctionHandler(app);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@pagopa/express-azure-functions": "^2.0.0",
"@pagopa/io-backend-notifications-sdk": "x",
"@pagopa/io-functions-admin-sdk": "x",
"@pagopa/io-functions-commons": "^26.6.0",
"@pagopa/io-functions-commons": "^27.4.0",
"@pagopa/ts-commons": "^10.14.0",
"applicationinsights": "^1.7.4",
"azure-storage": "^2.10.4",
Expand Down
2 changes: 2 additions & 0 deletions utils/domain-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export const cosmosErrorsToString = (errs: CosmosErrors): NonEmptyString =>
pipe(
errs.kind === "COSMOS_EMPTY_RESPONSE"
? "Empty response"
: errs.kind === "COSMOS_CONFLICT_RESPONSE"
? "Conflict response"
: errs.kind === "COSMOS_DECODING_ERROR"
? "Decoding error: " + errorsToReadableMessages(errs.error).join("/")
: "Generic error: " + JSON.stringify(errs.error),
Expand Down
3 changes: 2 additions & 1 deletion utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const getLogger = (
logCosmosErrors: (errs: CosmosErrors): void =>
context.log.error(
`${logPrefix}|${name}|COSMOS_ERROR|ERROR_DETAILS=${
errs.kind === "COSMOS_EMPTY_RESPONSE"
errs.kind === "COSMOS_EMPTY_RESPONSE" ||
errs.kind === "COSMOS_CONFLICT_RESPONSE"
? errs.kind
: errs.kind === "COSMOS_DECODING_ERROR"
? errorsToReadableMessages(errs.error).join("/")
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,10 @@
fp-ts "^2.10.5"
io-ts "^2.2.16"

"@pagopa/io-functions-commons@^26.6.0":
version "26.6.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-26.6.0.tgz#a51b16708393d86104dc980ca05c4bbcb3c59f67"
integrity sha512-U40ckW7+Re6slOMY0u9rpwzTjZeYRkDn54bc2/Pl4eDFgOmKjUMlPvd8qergfeq+MsBVfY5tIS6cQ4H4OqfWcw==
"@pagopa/io-functions-commons@^27.4.0":
version "27.4.0"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-27.4.0.tgz#fe0fa128954286a7aec665aeeb21e73d6e54d02b"
integrity sha512-M8g89OM1w4KB7VLPntyc+vRO+tuZRXlKK6GtI8+4G2H3XihiidlXdia+CUv3sqKs4FhwWfnY8GFrc75nt1GcYQ==
dependencies:
"@azure/cosmos" "^3.17.1"
"@pagopa/ts-commons" "^10.13.0"
Expand Down