Skip to content

Commit

Permalink
more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
aramikm committed Sep 17, 2024
1 parent d54c781 commit b3d09b6
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/content-publishing-api/src/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import { CacheModule } from '#content-publishing-lib/cache/cache.module';
useFactory: async (configService: ConfigService) => ({
limits: {
fileSize: configService.fileUploadMaxSizeInBytes,
files: configService.fileUploadCountLimit,
},
}),
inject: [ConfigService],
Expand Down
1 change: 1 addition & 0 deletions apps/content-publishing-api/src/build-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ process.env.CAPACITY_LIMIT = '{"type":"amount","value":"80"}';
process.env.IPFS_ENDPOINT = 'http://127.0.0.1';
process.env.IPFS_GATEWAY_URL = 'http://127.0.0.1';
process.env.FILE_UPLOAD_MAX_SIZE_IN_BYTES = '100';
process.env.FILE_UPLOAD_COUNT_LIMIT = '10';
process.env.ASSET_EXPIRATION_INTERVAL_SECONDS = '100';
process.env.BATCH_INTERVAL_SECONDS = '100';
process.env.BATCH_MAX_COUNT = '100';
Expand Down
1 change: 1 addition & 0 deletions developer-docs/content-publishing/ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This application recognizes the following environment variables:
| `CACHE_KEY_PREFIX` | Prefix to use for Redis cache keys | string | | content-publishing-service: |
| `CAPACITY_LIMIT` | Maximum amount of provider capacity this app is allowed to use (per epoch) type: 'percentage' 'amount' value: number (may be percentage, ie '80', or absolute amount of capacity) | JSON [(example)](https://github.com/ProjectLibertyLabs/gateway/blob/main/env-files/content-publishing.template.env) | Y | |
| `FILE_UPLOAD_MAX_SIZE_IN_BYTES` | Max file size (in bytes) allowed for asset upload | > 0 | Y | |
| `FILE_UPLOAD_COUNT_LIMIT` | Max number of files to be able to upload at the same time via one upload call | > 0 | Y | |
| `FREQUENCY_URL` | Blockchain node address | http(s): or ws(s): URL | Y | |
| `IPFS_BASIC_AUTH_SECRET` | If using Infura, put auth token here, or leave blank for Kubo RPC | string | | blank |
| `IPFS_BASIC_AUTH_USER` | If using Infura, put Project ID here, or leave blank for Kubo RPC | string | | blank |
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ x-common-environment: &common-environment
x-content-publishing-env: &content-publishing-env
START_PROCESS: content-publishing-api
FILE_UPLOAD_MAX_SIZE_IN_BYTES: 2000000000
FILE_UPLOAD_COUNT_LIMIT: 10
ASSET_EXPIRATION_INTERVAL_SECONDS: 300
BATCH_INTERVAL_SECONDS: 12
BATCH_MAX_COUNT: 1000
Expand Down
3 changes: 3 additions & 0 deletions env-files/content-publishing.template.env
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ API_PORT=3000
# Max file size allowed for asset upload
FILE_UPLOAD_MAX_SIZE_IN_BYTES=2000000000

# Max number of files to be able to upload at the same time via one upload call
FILE_UPLOAD_COUNT_LIMIT=10

# Number of seconds to keep completed asset entries in the cache
# before expiring them
ASSET_EXPIRATION_INTERVAL_SECONDS=300
Expand Down
12 changes: 12 additions & 0 deletions libs/content-publishing-lib/src/config/config.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('ContentPublishingConfigService', () => {
HEALTH_CHECK_MAX_RETRIES: undefined,
CAPACITY_LIMIT: undefined,
FILE_UPLOAD_MAX_SIZE_IN_BYTES: undefined,
FILE_UPLOAD_COUNT_LIMIT: undefined,
API_PORT: undefined,
ASSET_EXPIRATION_INTERVAL_SECONDS: undefined,
BATCH_INTERVAL_SECONDS: undefined,
Expand Down Expand Up @@ -131,6 +132,11 @@ describe('ContentPublishingConfigService', () => {
await expect(setupConfigService({ FILE_UPLOAD_MAX_SIZE_IN_BYTES: -1, ...env })).rejects.toBeDefined();
});

it('invalid max file count should fail', async () => {
const { FILE_UPLOAD_COUNT_LIMIT: dummy, ...env } = ALL_ENV;
await expect(setupConfigService({ FILE_UPLOAD_COUNT_LIMIT: -1, ...env })).rejects.toBeDefined();
});

it('invalid api port should fail', async () => {
const { API_PORT: dummy, ...env } = ALL_ENV;
await expect(setupConfigService({ API_PORT: -1, ...env })).rejects.toBeDefined();
Expand Down Expand Up @@ -207,6 +213,12 @@ describe('ContentPublishingConfigService', () => {
);
});

it('should get file upload count limit', () => {
expect(contentPublishingConfigService.fileUploadCountLimit).toStrictEqual(
parseInt(ALL_ENV.FILE_UPLOAD_COUNT_LIMIT as string, 10),
);
});

it('should get api port', () => {
expect(contentPublishingConfigService.apiPort).toStrictEqual(parseInt(ALL_ENV.API_PORT as string, 10));
});
Expand Down
5 changes: 5 additions & 0 deletions libs/content-publishing-lib/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ConfigEnvironmentVariables {
PROVIDER_ACCOUNT_SEED_PHRASE: string;
CAPACITY_LIMIT: ICapacityLimits;
FILE_UPLOAD_MAX_SIZE_IN_BYTES: number;
FILE_UPLOAD_COUNT_LIMIT: number;
API_PORT: number;
ASSET_EXPIRATION_INTERVAL_SECONDS: number;
BATCH_INTERVAL_SECONDS: number;
Expand Down Expand Up @@ -119,6 +120,10 @@ export class ConfigService implements OnModuleInit {
return this.nestConfigService.get<number>('FILE_UPLOAD_MAX_SIZE_IN_BYTES')!;
}

public get fileUploadCountLimit(): number {
return this.nestConfigService.get<number>('FILE_UPLOAD_COUNT_LIMIT')!;
}

public get apiPort(): number {
return this.nestConfigService.get<number>('API_PORT')!;
}
Expand Down
1 change: 1 addition & 0 deletions libs/content-publishing-lib/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const configModuleOptions: ConfigModuleOptions = {
}),
PROVIDER_ACCOUNT_SEED_PHRASE: Joi.string().required(),
FILE_UPLOAD_MAX_SIZE_IN_BYTES: Joi.number().min(1).required(),
FILE_UPLOAD_COUNT_LIMIT: Joi.number().min(1).required(),
API_PORT: Joi.number().min(0).default(3000),
ASSET_EXPIRATION_INTERVAL_SECONDS: Joi.number().min(1).required(),
BATCH_INTERVAL_SECONDS: Joi.number().min(1).required(),
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"start:account-worker:dev": "dotenvx run -f .env.account -- nest start account-worker --watch",
"start:account-worker:debug": "dotenvx run -f .env.account -- nest start account-worker --debug --watch",
"start:content-publishing-api": "nest start content-publishing-api",
"start:content-publishing-api:watch": "nest start content-publishing-api --watch",
"start:content-publishing-api:watch": "nest start content-publishing-api --debug --watch",
"start:content-publishing-api:prod": "node dist/apps/content-publishing-api/main.js",
"start:content-publishing-api:dev": "dotenvx run -f .env.content-publishing -- nest start content-publishing-api --watch",
"start:content-publishing-api:debug": "dotenvx run -f .env.content-publishing -- nest start content-publishing-api --debug --watch",
Expand Down Expand Up @@ -74,8 +74,13 @@
"test:verbose": "jest --coverage --verbose",
"test:e2e:account": "dotenvx run -f env-files/account.template.env -- jest --silent --runInBand --detectOpenHandles --testRegex 'account-api/.*\\.e2e-spec\\.ts'",
"test:e2e:content-publishing": "dotenvx run -f env-files/content-publishing.template.env -- jest --testRegex \".e2e-spec.ts\" --detectOpenHandles 'content-publishing*'",
<<<<<<< Updated upstream
"test:e2e:content-watcher": "dotenvx run -f env-files/content-watcher.template.env -- jest --testRegex \".e2e-spec.ts\" --detectOpenHandles 'content-watcher*'",
"test:e2e:graph": "dotenvx run -f env-files/graph.template.env -- jest --testRegex \".e2e-spec.ts\" --detectOpenHandles 'graph*'",
=======
"test:e2e:content-watcher": "dotenvx run -f .env.content-watcher -- jest --testRegex \".e2e-spec.ts\" --detectOpenHandles 'content-watcher*'",
"test:e2e:graph": "dotenvx run -f .env.graph -- jest --testRegex \".e2e-spec.ts\" --detectOpenHandles 'graph*'",
>>>>>>> Stashed changes
"format": "prettier --write .",
"lint": "eslint apps/**/*.[tj]s libs/**/*.[tj]s",
"lint:account": "eslint apps/account*/**/*.[tj]s libs/account-lib/**/*.[tj]s",
Expand Down

0 comments on commit b3d09b6

Please sign in to comment.