Skip to content

Commit

Permalink
[#175119553] use config module for configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
infantesimone committed Oct 6, 2020
1 parent c90239f commit 3d56063
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 47 deletions.
7 changes: 5 additions & 2 deletions CreateValidationTokenActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { createTableService } from "azure-storage";
import { Millisecond } from "italia-ts-commons/lib/units";

import { VALIDATION_TOKEN_TABLE_NAME } from "io-functions-commons/dist/src/entities/validation_token";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { ulidGenerator } from "io-functions-commons/dist/src/utils/strings";

import { getCreateValidationTokenActivityHandler } from "./handler";

import { getConfigOrThrow } from "../utils/config";

const config = getConfigOrThrow();

const TOKEN_INVALID_AFTER_MS = (1000 * 60 * 60 * 24 * 30) as Millisecond; // 30 days

// TODO: Rename this env to `StorageConnection`
// https://www.pivotaltracker.com/story/show/169591817
const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
const storageConnectionString = config.QueueStorageConnection;

const tableService = createTableService(storageConnectionString);

Expand Down
9 changes: 6 additions & 3 deletions GetMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Context } from "@azure/functions";

import * as express from "express";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

Expand All @@ -18,18 +17,22 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur
import { cosmosdbInstance } from "../utils/cosmosdb";
import { GetMessage } from "./handler";

import { getConfigOrThrow } from "../utils/config";

// Setup Express
const app = express();
secureExpressApp(app);

const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");
const config = getConfigOrThrow();

const messageContainerName = config.MESSAGE_CONTAINER_NAME;

const messageModel = new MessageModel(
cosmosdbInstance.container(MESSAGE_COLLECTION_NAME),
messageContainerName
);

const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
const storageConnectionString = config.QueueStorageConnection;
const blobService = createBlobService(storageConnectionString);

app.get(
Expand Down
7 changes: 5 additions & 2 deletions GetMessages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Context } from "@azure/functions";

import * as express from "express";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

Expand All @@ -16,11 +15,15 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur
import { cosmosdbInstance } from "../utils/cosmosdb";
import { GetMessages } from "./handler";

import { getConfigOrThrow } from "../utils/config";

// Setup Express
const app = express();
secureExpressApp(app);

const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");
const config = getConfigOrThrow();

const messageContainerName = config.MESSAGE_CONTAINER_NAME;

const messageModel = new MessageModel(
cosmosdbInstance.container(MESSAGE_COLLECTION_NAME),
Expand Down
7 changes: 5 additions & 2 deletions GetVisibleServices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { createBlobService } from "azure-storage";

import * as express from "express";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { GetVisibleServices } from "./handler";

import { getConfigOrThrow } from "../utils/config";

// Setup Express
const app = express();
secureExpressApp(app);

const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
const config = getConfigOrThrow();

const storageConnectionString = config.QueueStorageConnection;
const blobService = createBlobService(storageConnectionString);

app.get("/api/v1/services", GetVisibleServices(blobService));
Expand Down
18 changes: 17 additions & 1 deletion HandleNHNotificationCallActivity/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@
// tslint:disable-next-line: no-object-mutation
process.env = {
...process.env,
MESSAGE_CONTAINER_NAME: "msg",
QueueStorageConnection: "foobar",
AZURE_NH_ENDPOINT:
"Endpoint=sb://anendpoint.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=XXX",
AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME"
AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME",
SUBSCRIPTIONS_FEED_TABLE: "feed",
CUSTOMCONNSTR_COSMOSDB_KEY: "key",
CUSTOMCONNSTR_COSMOSDB_URI: "uri",
COSMOSDB_NAME: "cosmoname",
COSMOSDB_URI: "uri",
COSMOSDB_KEY: "key",
FUNCTIONS_PUBLIC_URL: "url",
PUBLIC_API_URL: "url",
PUBLIC_API_KEY: "key",
MAILHOG_HOSTNAME: "mailhog",
MAIL_FROM: "mail@example.it",
NODE_ENV: "dev",
REQ_SERVICE_ID: "req_id_dev",
SPID_LOGS_PUBLIC_KEY: "key"
};

import { NonEmptyString } from "italia-ts-commons/lib/strings";
Expand Down
18 changes: 17 additions & 1 deletion HandleNHNotificationCallOrchestrator/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@
// tslint:disable-next-line: no-object-mutation
process.env = {
...process.env,
QueueStorageConnection: "foobar",
MESSAGE_CONTAINER_NAME: "msg",
SUBSCRIPTIONS_FEED_TABLE: "feed",
CUSTOMCONNSTR_COSMOSDB_KEY: "key",
CUSTOMCONNSTR_COSMOSDB_URI: "uri",
COSMOSDB_NAME: "cosmoname",
COSMOSDB_URI: "uri",
AZURE_NH_ENDPOINT:
"Endpoint=sb://anendpoint.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=C4xIzNZv4VrUnu5jkmPH635MApRUj8wABky8VfduYqg=",
AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME"
AZURE_NH_HUB_NAME: "AZURE_NH_HUB_NAME",
COSMOSDB_KEY: "key",
FUNCTIONS_PUBLIC_URL: "url",
PUBLIC_API_URL: "url",
PUBLIC_API_KEY: "key",
MAILHOG_HOSTNAME: "mailhog",
MAIL_FROM: "mail@example.it",
NODE_ENV: "dev",
REQ_SERVICE_ID: "req_id_dev",
SPID_LOGS_PUBLIC_KEY: "key"
};
import { NonEmptyString } from "italia-ts-commons/lib/strings";
import { context as contextMock } from "../../__mocks__/durable-functions";
Expand Down
21 changes: 12 additions & 9 deletions SendValidationEmailActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { MailMultiTransportConnectionsFromString } from "io-functions-commons/dist/src/utils/multi_transport_connection";
import { MailMultiTransportConnectionsFromString } from "io-functions-commons/dist/src/utils/multi_transport_connection";
import { MultiTransport } from "io-functions-commons/dist/src/utils/nodemailer";
import { NonEmptyString } from "italia-ts-commons/lib/strings";

Expand All @@ -14,24 +13,28 @@ import {

import { initTelemetryClient } from "../utils/appinsights";

import { getConfigOrThrow } from "../utils/config";

const config = getConfigOrThrow();

// Whether we're in a production environment
const isProduction = process.env.NODE_ENV === "production";
const isProduction = config.NODE_ENV === "production";

// Optional SendGrid key
const sendgridApiKey = NonEmptyString.decode(
process.env.SENDGRID_API_KEY
config.SENDGRID_API_KEY
).getOrElse(undefined);

// Mailup
const mailupUsername = getRequiredStringEnv("MAILUP_USERNAME");
const mailupSecret = getRequiredStringEnv("MAILUP_SECRET");
const mailupUsername = config.MAILUP_USERNAME;
const mailupSecret = config.MAILUP_SECRET;

// Email data
const EMAIL_TITLE = "Valida l’indirizzo email che usi su IO";
const mailFrom = getRequiredStringEnv("MAIL_FROM");
const mailFrom = config.MAIL_FROM;

// Needed to construct the email validation url
const functionsPublicUrl = getRequiredStringEnv("FUNCTIONS_PUBLIC_URL");
const functionsPublicUrl = config.FUNCTIONS_PUBLIC_URL;

const HTML_TO_TEXT_OPTIONS: HtmlToTextOptions = {
ignoreImage: true, // Ignore all document images
Expand All @@ -51,7 +54,7 @@ export type EmailDefaults = typeof emailDefaults;
// [mailup:username:password;][sendgrid:apikey:;]
// Note that multiple instances of the same provider can be provided.
const transports = MailMultiTransportConnectionsFromString.decode(
process.env.MAIL_TRANSPORTS
config.MAIL_TRANSPORTS
)
.map(getTransportsForConnections)
.getOrElse([]);
Expand Down
9 changes: 6 additions & 3 deletions SendWelcomeMessagesActivity/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { agent } from "italia-ts-commons";
import {
AbortableFetch,
Expand All @@ -8,12 +7,16 @@ import {
import { Millisecond } from "italia-ts-commons/lib/units";
import { getActivityFunction } from "./handler";

import { getConfigOrThrow } from "../utils/config";

// HTTP external requests timeout in milliseconds
const DEFAULT_REQUEST_TIMEOUT_MS = 10000;

const config = getConfigOrThrow();

// Needed to call notifications API
const publicApiUrl = getRequiredStringEnv("PUBLIC_API_URL");
const publicApiKey = getRequiredStringEnv("PUBLIC_API_KEY");
const publicApiUrl = config.PUBLIC_API_URL;
const publicApiKey = config.PUBLIC_API_KEY;

// HTTP-only fetch with optional keepalive agent
// @see https://github.com/pagopa/io-ts-commons/blob/master/src/agent.ts#L10
Expand Down
19 changes: 18 additions & 1 deletion StoreSpidLogs/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
/* tslint:disable:no-object-mutation */

process.env = {
UBSCRIPTIONS_FEED_TABLE: "feed",
APPINSIGHTS_INSTRUMENTATIONKEY: "foo",
QueueStorageConnection: "foobar",
SUBSCRIPTIONS_FEED_TABLE: "feed",
SPID_BLOB_CONTAINER_NAME: "spidblob",
SPID_BLOB_STORAGE_CONNECTION_STRING: "foobar",
SPID_LOGS_PUBLIC_KEY: `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhiXpvLD8UMMUy1T2JCzo/Sj5E
l09Fs0z2U4aA37BrXlSo1DwQ2O9i2XFxXGJmE83siSWEfRlMWlabMu7Yj6dkZvmj
dGIO4gotO33TgiAQcwRo+4pwjoCN7Td47yssCcj9C727zBt+Br+XK7B1bRcqjc0J
YdF4yiVtD7G4RDXmRQIDAQAB
-----END PUBLIC KEY-----`
-----END PUBLIC KEY-----`,
MESSAGE_CONTAINER_NAME: "msg",
CUSTOMCONNSTR_COSMOSDB_URI: "uri",
CUSTOMCONNSTR_COSMOSDB_KEY: "key",
COSMOSDB_NAME: "cosmoname",
COSMOSDB_URI: "uri",
COSMOSDB_KEY: "key",
FUNCTIONS_PUBLIC_URL: "url",
PUBLIC_API_URL: "url",
PUBLIC_API_KEY: "key",
MAILHOG_HOSTNAME: "mailhog",
MAIL_FROM: "mail@example.it",
NODE_ENV: "dev",
REQ_SERVICE_ID: "req_id_dev",
AZURE_NH_HUB_NAME: "azhub",
AZURE_NH_ENDPOINT: "azendpoint",
};

import { format } from "date-fns";
Expand Down
5 changes: 3 additions & 2 deletions StoreSpidLogs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Context } from "@azure/functions";
import { sequenceS } from "fp-ts/lib/Apply";
import { either } from "fp-ts/lib/Either";
import { curry } from "fp-ts/lib/function";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import * as t from "io-ts";
import { UTCISODateFromString } from "italia-ts-commons/lib/dates";
import {
Expand All @@ -12,8 +11,10 @@ import {
import { readableReport } from "italia-ts-commons/lib/reporters";
import { IPString, PatternString } from "italia-ts-commons/lib/strings";
import { initTelemetryClient } from "../utils/appinsights";
import { getConfigOrThrow } from "../utils/config";

const rsaPublicKey = getRequiredStringEnv("SPID_LOGS_PUBLIC_KEY");
const config = getConfigOrThrow();
const rsaPublicKey = config.SPID_LOGS_PUBLIC_KEY;
const encrypt = curry(toEncryptedPayload)(rsaPublicKey);

/**
Expand Down
10 changes: 6 additions & 4 deletions UpdateSubscriptionsFeedActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import { createTableService, TableUtilities } from "azure-storage";
import { readableReport } from "italia-ts-commons/lib/reporters";
import { FiscalCode } from "italia-ts-commons/lib/strings";

import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { ServiceId } from "io-functions-commons/dist/generated/definitions/ServiceId";

import { isNone } from "fp-ts/lib/Option";
import { deleteTableEntity, insertTableEntity } from "../utils/storage";

const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
import { getConfigOrThrow } from "../utils/config";

const config = getConfigOrThrow();

const storageConnectionString = config.QueueStorageConnection;
const tableService = createTableService(storageConnectionString);

const subscriptionsFeedTable = getRequiredStringEnv("SUBSCRIPTIONS_FEED_TABLE");
const subscriptionsFeedTable = config.SUBSCRIPTIONS_FEED_TABLE;

const insertEntity = insertTableEntity(tableService, subscriptionsFeedTable);
const deleteEntity = deleteTableEntity(tableService, subscriptionsFeedTable);
Expand Down
13 changes: 8 additions & 5 deletions docker/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import {

import { sequenceT } from "fp-ts/lib/Apply";
import { TaskEither, taskEitherSeq, tryCatch } from "fp-ts/lib/TaskEither";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

const cosmosDbKey = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_KEY");
const cosmosDbUri = getRequiredStringEnv("CUSTOMCONNSTR_COSMOSDB_URI");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
import { getConfigOrThrow } from "../../utils/config";

const config = getConfigOrThrow();

const cosmosDbKey = config.CUSTOMCONNSTR_COSMOSDB_KEY;
const cosmosDbUri = config.CUSTOMCONNSTR_COSMOSDB_URI;
const cosmosDbName = config.COSMOSDB_NAME;

export const cosmosdbClient = new CosmosClient({
endpoint: cosmosDbUri,
Expand Down Expand Up @@ -56,7 +59,7 @@ const aService: Service = Service.decode({
organizationFiscalCode: "01234567890",
organizationName: "Organization name",
requireSecureChannels: false,
serviceId: process.env.REQ_SERVICE_ID,
serviceId: config.REQ_SERVICE_ID,
serviceName: "MyServiceName"
}).getOrElseL(() => {
throw new Error("Cannot decode service payload.");
Expand Down
Loading

0 comments on commit 3d56063

Please sign in to comment.