Skip to content

Commit

Permalink
Introduce dynamic alphanumeric strings support in HTTP requests (#227)
Browse files Browse the repository at this point in the history
Now `$DYNAMIC_ALPHANUMERIC_STRING` placeholders in the request URL or body will be substituted with actual random alphanumeric strings. The length for these strings can be configured using the `DYNAMIC_STRING_LENGTH_DEFAULT` environment variable, which defaults to `10`.

Discussion: upptime/upptime#857
  • Loading branch information
artemmukhin authored Oct 13, 2023
1 parent e5efa37 commit bc6bbd3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ export const DEFAULT_RUNNER = "ubuntu-latest";
export const DYNAMIC_RANDOM_NUMBER = "$DYNAMIC_RANDOM_NUMBER";
export const RANDOM_MIN_DEFAULT = "0";
export const RANDOM_MAX_DEFAULT = "1000000";

export const DYNAMIC_ALPHANUMERIC_STRING = "$DYNAMIC_ALPHANUMERIC_STRING";
export const DYNAMIC_STRING_LENGTH_DEFAULT = "10";
36 changes: 34 additions & 2 deletions src/helpers/environment.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import {RANDOM_MAX_DEFAULT, RANDOM_MIN_DEFAULT, DYNAMIC_RANDOM_NUMBER} from "./constants";
import {
RANDOM_MAX_DEFAULT,
RANDOM_MIN_DEFAULT,
DYNAMIC_RANDOM_NUMBER,
DYNAMIC_ALPHANUMERIC_STRING,
DYNAMIC_STRING_LENGTH_DEFAULT
} from "./constants";

export const replaceEnvironmentVariables = (str: string) => {
Object.keys(process.env).forEach((key) => {
str = str.replace(`$${key}`, process.env[key] || `$${key}`);
});

const SECRETS_CONTEXT = process.env.SECRETS_CONTEXT || "{}";
const allSecrets: Record<string, string> = JSON.parse(SECRETS_CONTEXT);
const secrets: Record<string, any> = { ...JSON.parse(JSON.stringify(process.env)), allSecrets };
Object.keys(secrets).forEach((key) => {
str = str.replace(`$${key}`, secrets[key] || `$${key}`);
});
return substituteRandomNumbers(str);

str = substituteRandomNumbers(str);
str = substituteDynamicAlphanumericString(str);

return str;
};

const substituteRandomNumbers = (str: string) => {
Expand All @@ -22,6 +33,27 @@ const substituteRandomNumbers = (str: string) => {
return str;
}

const substituteDynamicAlphanumericString = (str: string) => {
if (str.includes(DYNAMIC_ALPHANUMERIC_STRING)) {
const length = parseInt(process.env.DYNAMIC_STRING_LENGTH || DYNAMIC_STRING_LENGTH_DEFAULT);
str = str.replaceAll(DYNAMIC_ALPHANUMERIC_STRING, () => getRandomAlphanumericString(length));
}
return str;
};


/** Return a random integer N such that min <= N <= max */
const getRandomNumber = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;


/** Return a random alphanumeric string of given length */
const getRandomAlphanumericString = (length: number) => {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
result += charset[randomIndex];
}
return result;
};

0 comments on commit bc6bbd3

Please sign in to comment.