Skip to content

Commit 24d66e7

Browse files
authored
Merge pull request #42 from auth0-training/feat/keyword-replacement-mappings
Default Keyword Replacement Mapping Feature
2 parents fae60d2 + e092b30 commit 24d66e7

File tree

8 files changed

+110
-13
lines changed

8 files changed

+110
-13
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
139139
- deploy cli config.json placed next to tenant.yml will be merged into deploy configuration.
140140

141141
### Changed
142-
- reverted to auth0 v2.42.0
142+
- reverted to auth0 v2.42.0
143+
144+
## [1.4.2] - 2023-04-19
145+
### Added
146+
- Support for Codespaces added so that when the registered command `openEndpointByName` is used, it resolves the correct URL for apps launched in the Codespaces environment.
147+
148+
## [1.4.3] - 2023-05-01
149+
### Added
150+
- Implemented `auth0.lab.postConfigureCommand` command
151+
- Support for writing more than one app config to a single `.env` without overwriting values
152+
- Added runtime-specific replacement values for tenant configuration from yaml: `CODESPACE_NAME` (same as defined [here]( https://docs.github.com/en/codespaces/developing-in-codespaces/default-environment-variables-for-your-codespace)) and `AUTH0_DOMAIN` (your tenant domain).

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ In addition to the visual features listed below, the Labs extension also contrib
6969
- **Auth0: Configure Tenant Resources** - `auth0.lab.tenantConfigure` Configures tenant for the current lab materials. Only available when lab materials are present in workspace.
7070
- **Auth0: Export Tenant** - `auth0.exportTenant` Exports tenant configuration yml to the root directory. This exports everything and needs to be edited down for a lab.
7171
- **Auth0: Open Endpoint Url** - `auth0.lab.openEndpointByName?["Endpoint 1, Endpoint 2"]` Opens the URL associated with a specific named endpoint in the default browser. Multiple endpoints can be opened by supplying a comma seperated list. Only available when lab materials are present in workspace.
72+
- **Auth0: Run Post Configure Command** `auth0.lab.postConfigureCommand` Allows you to specify the path of a shell script in `environment.json` (i.e., `"postConfigureCommand": "<script-path>"`), that will execute upon configuration completion. Enables you to run a script to perform actions like storing values client id and client secret as secrets in the environment. This command currently has access to the following environment variables:
73+
- `AUTH0_DOMAIN`: Your Auth0 domain URI
74+
- `AUTH0_TOKEN`: The access token issued to your client from the authorization server.
7275

7376
### Authenticating
7477

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-labs",
33
"preview": true,
44
"displayName": "Auth0 Labs",
5-
"version": "1.4.2",
5+
"version": "1.4.3",
66
"description": "A Visual Studio Code extension for training lab automation and quick access to tenant information.",
77
"main": "./dist/extension.js",
88
"publisher": "auth0",
@@ -224,6 +224,13 @@
224224
"icon": "$(beaker)",
225225
"enablement": "auth0:authenticated && auth0:isLabWorkspace"
226226
},
227+
{
228+
"category": "Auth0",
229+
"command": "auth0.lab.postConfigureCommand",
230+
"title": "Run Post Configure Command",
231+
"icon": "$(beaker)",
232+
"enablement": "auth0:authenticated && auth0:isLabWorkspace"
233+
},
227234
{
228235
"category": "Auth0",
229236
"command": "auth0.lab.openLocalEndpoint",

src/features/deploy/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class DeployCommands {
7171
AUTH0_DOMAIN: getDomainFromToken(accessToken),
7272
AUTH0_CLIENT_ID: 'NEEDED FOR v7.17.0 Deploy CLI',
7373
AUTH0_ALLOW_DELETE: false,
74+
AUTH0_KEYWORD_REPLACE_MAPPINGS: {
75+
AUTH0_DOMAIN: getDomainFromToken(accessToken),
76+
CODESPACE_NAME: process.env.CODESPACE_NAME
77+
}
7478
}),
7579
};
7680
await deploy(opts);

src/features/labs/commands.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import * as path from 'path';
33
import { getLabEnvironment, getLabWorkspace } from './workspace';
44
import { LabResourceResolverBuilder } from './resolver';
55
import { LabEnvWriter } from './writer';
6-
import { getUrlForPort, getFileUri, startTour } from '../../utils';
6+
import { getClient } from '../../client';
7+
import {
8+
getDomainFromToken,
9+
getUrlForPort,
10+
getFileUri,
11+
startTour,
12+
} from '../../utils';
713

814
const registerCommand = commands.registerCommand;
915
const executeCommand = commands.executeCommand;
@@ -25,6 +31,10 @@ export class LabCommands {
2531
registerCommand('auth0.lab.configure', this.configureLab),
2632
registerCommand('auth0.lab.localConfigure', this.localConfigure),
2733
registerCommand('auth0.lab.tenantConfigure', this.tenantConfigure),
34+
registerCommand(
35+
'auth0.lab.postConfigureCommand',
36+
this.postConfigureCommand
37+
),
2838
registerCommand('auth0.lab.openLocalEndpoint', this.openLocalEndpoint),
2939
registerCommand(
3040
'auth0.lab.openEndpointByName',
@@ -144,11 +154,20 @@ export class LabCommands {
144154
await executeCommand('auth0.lab.localConfigure');
145155
}
146156

157+
if (labEnv.postConfigureCommand && !token.isCancellationRequested) {
158+
progress.report({
159+
message: 'running post configure command',
160+
increment: 4,
161+
});
162+
163+
await executeCommand('auth0.lab.postConfigureCommand');
164+
}
165+
147166
//issue post command to kick off next process
148167
if (labEnv.postConfigureTour && !token.isCancellationRequested) {
149168
progress.report({
150169
message: 'Starting Lab',
151-
increment: 4,
170+
increment: 5,
152171
});
153172

154173
const uri = getFileUri(
@@ -190,4 +209,31 @@ export class LabCommands {
190209
new LabEnvWriter(workspace.uri).writeAll(resolvers);
191210
}
192211
};
212+
213+
postConfigureCommand = async (): Promise<void> => {
214+
console.log('auth0.labs.postConfigure');
215+
const workspace = getLabWorkspace();
216+
const labEnv = await getLabEnvironment();
217+
const client = await getClient();
218+
const accessToken = await client.getAccessToken();
219+
220+
if (workspace && labEnv && labEnv.postConfigureCommand) {
221+
const uri = getFileUri(
222+
`/.auth0/lab/${labEnv.postConfigureCommand}`,
223+
workspace.uri
224+
);
225+
226+
const terminal = window.createTerminal({
227+
name: 'Post Configure Script',
228+
env: {
229+
// eslint-disable-next-line @typescript-eslint/naming-convention
230+
AUTH0_DOMAIN: getDomainFromToken(accessToken),
231+
// eslint-disable-next-line @typescript-eslint/naming-convention
232+
AUTH0_TOKEN: accessToken,
233+
},
234+
});
235+
terminal.show();
236+
terminal.sendText(uri.fsPath);
237+
}
238+
};
193239
}

src/features/labs/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface LocalEnvironment {
44
resources: string | null;
55
unauthenticatedTour: string | null;
66
postConfigureTour: string | null;
7+
postConfigureCommand: string | null;
78
clients: Array<Resource>;
89
resourceServers: Array<Resource>;
910
}

src/features/labs/writer.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
import * as vscode from 'vscode';
2+
import { readUriContents } from '../../utils';
23
import { Resolver } from './resolver';
34

45
const openTextDocument = vscode.workspace.openTextDocument;
56
const fs = vscode.workspace.fs;
67

78
export class LabEnvWriter {
8-
constructor(private worspace: vscode.Uri) {}
9+
constructor(private workspace: vscode.Uri) {}
10+
11+
getExisting = async (uri: vscode.Uri) => {
12+
try {
13+
// will error if file does not exist
14+
const stat = await fs.stat(uri);
15+
16+
// read and parse file into name/value pairs
17+
return await readUriContents(uri)
18+
.then((file) => file.split('\n'))
19+
.then((lines) =>
20+
lines.map((line) => {
21+
const pair = line.split('=');
22+
return { name: pair[0], value: pair[1] };
23+
})
24+
);
25+
} catch {
26+
// return an empty array if file does not exist
27+
return [];
28+
}
29+
};
930

1031
writeAll = async (resolvers: Resolver[]) => {
11-
resolvers.forEach(async (resolver) => {
32+
for (const resolver of resolvers) {
1233
const uri = vscode.Uri.joinPath(
13-
this.worspace,
34+
this.workspace,
1435
resolver.getDirectory(),
1536
'.env'
1637
);
17-
const env = resolver
18-
.resolveEnv(resolvers)
19-
.map((i) => `${i.name}=${i.value}`)
20-
.join('\n');
38+
39+
const existingEnv = await this.getExisting(uri);
40+
const newEnv = resolver.resolveEnv(resolvers);
41+
const unique = [
42+
...new Map(existingEnv.concat(newEnv).map((m) => [m.name, m])).values(),
43+
];
44+
45+
const env = unique.map((i) => `${i.name}=${i.value}`).join('\n');
46+
2147
await fs.writeFile(uri, Buffer.from(env, 'utf8'));
22-
});
48+
}
2349
};
2450
}

tests/suite/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ suite('Extension', () => {
1515
await started.activate();
1616
assert.strictEqual(started && started.isActive, true);
1717
}
18-
});
18+
}).timeout(1000 * 10);//10 seconds
1919
});

0 commit comments

Comments
 (0)