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

Add validation for namespace, metric and timestamp #135

Merged
merged 9 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 10 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier",
"prettier/@typescript-eslint"
"prettier"
],
"rules": {
"@typescript-eslint/interface-name-prefix": "off"
Expand All @@ -24,7 +23,15 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/unbound-method": "off"
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}
]
Expand Down
12,084 changes: 4,809 additions & 7,275 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@
"@datastructures-js/heap": "^4.0.2"
},
"devDependencies": {
"@faker-js/faker": "^7.5.0",
"@types/faker": "^4.1.5",
"@types/jest": "^26.0.22",
"@types/node": "^12.0.8",
"@types/validator": "^13.7.5",
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"aws-sdk": "^2.551.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"faker": "^4.1.0",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^26.6.3",
"node-notifier": ">=8.0.1",
"npm-pack-zip": "^1.2.7",
"prettier": "^1.19.1",
"npm-pack-zip": "^1.3.0",
"prettier": "^2.7.1",
"ts-jest": "^26.5.4",
"typescript": "^3.8.0",
"typescript": "^4.8.2",
"validator": "^13.7.0",
"y18n": ">=4.0.1"
},
Expand Down
7 changes: 6 additions & 1 deletion src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
*/

export enum Constants {
MAX_DIMENSION_SET_SIZE = 30,
MAX_DIMENSION_NAME_LENGTH = 250,
MAX_DIMENSION_VALUE_LENGTH = 1024,
MAX_METRIC_NAME_LENGTH = 1024,
MAX_NAMESPACE_LENGTH = 256,
VALID_NAMESPACE_REGEX = '^[a-zA-Z0-9._#:/-]+$',
MAX_TIMESTAMP_PAST_AGE = 1209600000, // 2 weeks
MAX_TIMESTAMP_FUTURE_AGE = 7200000, // 2 hours

MAX_DIMENSION_SET_SIZE = 30,
DEFAULT_NAMESPACE = 'aws-embedded-metrics',
MAX_METRICS_PER_EVENT = 100,
MAX_VALUES_PER_METRIC = 100,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import Environments from '../../environment/Environments';

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/environment/__tests__/DefaultEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import config from '../../config/Configuration';
import { DefaultEnvironment } from '../DefaultEnvironment';

Expand Down
4 changes: 2 additions & 2 deletions src/environment/__tests__/ECSEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import config from '../../config/Configuration';
import { ECSEnvironment } from '../ECSEnvironment';
import { MetricsContext } from '../../logger/MetricsContext';
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('getName()', () => {
test('returns formatted image name if available', async () => {
// arrange
const formattedImageName = `${faker.lorem.word()}:latest`;
const fullImageName = `${faker.random.number({
const fullImageName = `${faker.datatype.number({
min: 0,
max: 999999999999,
})}.dkr.ecr.<region>.amazonaws.com/${formattedImageName}`;
Expand Down
2 changes: 1 addition & 1 deletion src/environment/__tests__/LambdaEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { MetricsContext } from '../../logger/MetricsContext';
import { LambdaEnvironment } from '../LambdaEnvironment';

Expand Down
2 changes: 1 addition & 1 deletion src/environment/__tests__/LocalEnvironment.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import config from '../../config/Configuration';
import { LocalEnvironment } from '../LocalEnvironment';

Expand Down
15 changes: 15 additions & 0 deletions src/exceptions/DimensionSetExceededError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class DimensionSetExceededError extends Error {
constructor(msg: string) {
super(msg);
Expand Down
15 changes: 15 additions & 0 deletions src/exceptions/InvalidDimensionError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class InvalidDimensionError extends Error {
constructor(msg: string) {
super(msg);
Expand Down
24 changes: 24 additions & 0 deletions src/exceptions/InvalidMetricError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class InvalidMetricError extends Error {
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, InvalidMetricError.prototype);
}
}

24 changes: 24 additions & 0 deletions src/exceptions/InvalidNamespaceError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class InvalidNamespaceError extends Error {
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, InvalidNamespaceError.prototype);
}
}

24 changes: 24 additions & 0 deletions src/exceptions/InvalidTimestampError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class InvalidTimestampError extends Error {
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, InvalidTimestampError.prototype);
}
}

4 changes: 2 additions & 2 deletions src/logger/MetricValues.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Unit } from '..';

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
Expand All @@ -15,6 +13,8 @@ import { Unit } from '..';
* limitations under the License.
*/

import { Unit } from '..';

export class MetricValues {
public values: number[];
public unit: string;
Expand Down
4 changes: 4 additions & 0 deletions src/logger/MetricsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class MetricsContext {
}

public setNamespace(value: string): void {
Validator.validateNamespace(value);
this.namespace = value;
}

Expand All @@ -91,6 +92,7 @@ export class MetricsContext {
}

public setTimestamp(timestamp: Date | number): void {
Validator.validateTimestamp(timestamp);
this.timestamp = timestamp;
this.meta.Timestamp = MetricsContext.resolveMetaTimestamp(timestamp);
}
Expand Down Expand Up @@ -172,6 +174,8 @@ export class MetricsContext {
}

public putMetric(key: string, value: number, unit?: Unit | string): void {
Validator.validateMetric(key, value, unit);

const currentMetric = this.metrics.get(key);
if (currentMetric) {
currentMetric.addValue(value);
Expand Down
Loading