Skip to content

Commit 7f40d82

Browse files
patch: Improve URL detection for DT_ENVIRONMENT (#224)
1 parent 8994069 commit 7f40d82

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/getDynatraceEnv.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ describe('getDynatraceEnv', () => {
6868
expect(() => getDynatraceEnv(env)).toThrow(/Dynatrace Platform Environment URL/);
6969
});
7070

71+
it('throws if DT_ENVIRONMENT is malformed with extra domain', () => {
72+
const env = {
73+
...baseEnv,
74+
DT_ENVIRONMENT: 'https://demo.dev.apps.dynatracelabs.com.example.com',
75+
};
76+
expect(() => getDynatraceEnv(env)).toThrow(/Dynatrace Platform Environment URL/);
77+
});
78+
7179
it('accepts DT_ENVIRONMENT with apps.dynatracelabs.com', () => {
7280
const env = {
7381
...baseEnv,
@@ -84,6 +92,22 @@ describe('getDynatraceEnv', () => {
8492
expect(() => getDynatraceEnv(env)).not.toThrow();
8593
});
8694

95+
it('accepts DT_ENVIRONMENT with apps.dynatrace.com with trailing slash', () => {
96+
const env = {
97+
...baseEnv,
98+
DT_ENVIRONMENT: 'https://env123.apps.dynatrace.com/',
99+
};
100+
expect(() => getDynatraceEnv(env)).not.toThrow();
101+
});
102+
103+
it('accepts DT_ENVIRONMENT with apps.dynatracelabs.com with trailing slash', () => {
104+
const env = {
105+
...baseEnv,
106+
DT_ENVIRONMENT: 'https://xyz789.apps.dynatracelabs.com/',
107+
};
108+
expect(() => getDynatraceEnv(env)).not.toThrow();
109+
});
110+
87111
it('Defaults the Grail Budget to 1000', () => {
88112
const env = {
89113
...baseEnv,

src/getDynatraceEnv.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export function getDynatraceEnv(env: NodeJS.ProcessEnv = process.env): Dynatrace
4545
);
4646
}
4747

48-
if (!dtEnvironment.includes('apps.dynatrace.com') && !dtEnvironment.includes('apps.dynatracelabs.com')) {
48+
// Only allow certain Dynatrace specific Platform URLs (this is to avoid that users enter URLs like <enviornment-i>.live.dynatrace.com)
49+
if (!/\.apps\.(dynatrace|dynatracelabs)\.com\/?$/.test(dtEnvironment)) {
4950
throw new Error(
50-
'Please set DT_ENVIRONMENT to a valid Dynatrace Platform Environment URL (e.g., https://<environment-id>.apps.dynatrace.com)',
51+
'Please set DT_ENVIRONMENT to a valid Dynatrace Platform Environment URL (e.g., https://<environment-id>.apps.dynatrace.com or https://<environment-id>.sprint.apps.dynatracelabs.com)',
5152
);
5253
}
5354

0 commit comments

Comments
 (0)