Skip to content

Commit

Permalink
env var support
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed Dec 6, 2023
1 parent 831bbe0 commit 4d6b7e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ Add the `--help` flag to any command for available options.

## Extension Settings

- `github.token`: Provide a GitHub Token instead of using VSCode Authentication. This token requires the `user:email` scope. (Ignores the `GITHUB_TOKEN` environment variable and/or VSCode Authentication if set.)
- `github.token`: Provide a GitHub Token instead of using VSCode Authentication. This token requires the `user:email` scope. (Or by the `GITHUB_TOKEN` environment variable.)
- `assumeAws.assumeRoleAtStartup`: Automatically attempt to assume a role at startup. If `assumeAws.role` or `assumeAws.rememberRole` is set, the role prompt will be skipped.
- `assumeAws.autoRefresh`: Automatically refresh the credentials before they expire.
- `assumeAws.region`: The AWS Default Region to set after assuming a role. (Overridden by the `AWS_DEFAULT_REGION` environment variable.)
- `assumeAws.role`: Skip role selection input and assume this role. (Overridden by the `AWS_ROLE_ARN` environment variable.)
- `assumeAws.region`: The AWS Default Region to set after assuming a role. (Or by the `AWS_DEFAULT_REGION` environment variable.)
- `assumeAws.role`: Skip role selection input and assume this role. (Or by the `AWS_ROLE_ARN` environment variable.)
- `assumeAws.rememberRole`: Skip role selection input and assume the last role used. This is ignored when the `assumeAws.role` setting is set.
- `assumeAws.profile.name`: Save AWS Credentials to a named profile in `~/.aws/config`. (Overridden by the `AWS_PROFILE` environment variable.)
- `assumeAws.profile.name`: Save AWS Credentials to a named profile in `~/.aws/config`. (Or by the `AWS_PROFILE` environment variable.)

## Initial Setup

Expand Down
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
- Try to invoke AWS Select Connection afterwards
- Onboarding experience for brand new user / org
- Loading progress indicator between inputs
- Prompt for Profile Name after Role Selection
- Devcontainer
- Set Role ARN
- Set Default Profile
- Get Help Action
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"saml-to.github.token": {
"type": "string",
"default": null,
"markdownDescription": "Provide a GitHub Token instead of using VSCode Authentication. This token requires the `user:email` scope. (Ignores the `GITHUB_TOKEN` environment variable and/or VSCode Authentication if set.)"
"markdownDescription": "Provide a GitHub Token instead of using VSCode Authentication. This token requires the `user:email` scope. (Or by the `GITHUB_TOKEN` environment variable.)"
},
"saml-to.assumeAws.assumeRoleAtStartup": {
"type": "boolean",
Expand All @@ -58,12 +58,12 @@
"saml-to.assumeAws.region": {
"type": "string",
"default": "us-east-1",
"markdownDescription": "The AWS Default Region to set after assuming a role. (Overridden by the `AWS_DEFAULT_REGION` environment variable.)"
"markdownDescription": "The AWS Default Region to set after assuming a role. (Or by the `AWS_DEFAULT_REGION` environment variable.)"
},
"saml-to.assumeAws.role": {
"type": "string",
"default": null,
"markdownDescription": "Skip role selection input and assume this role. (Overridden by the `AWS_ROLE_ARN` environment variable.)"
"markdownDescription": "Skip role selection input and assume this role. (Or by the `AWS_ROLE_ARN` environment variable.)"
},
"saml-to.assumeAws.rememberRole": {
"type": "string",
Expand Down Expand Up @@ -92,7 +92,7 @@
"Use the Account ID as the profile name.",
"Do not save the credentials to a profile."
],
"markdownDescription": "Save AWS Credentials to a named profile in `~/.aws/config`. (Overridden by the `AWS_PROFILE` environment variable.)"
"markdownDescription": "Save AWS Credentials to a named profile in `~/.aws/config`. (Or by the `AWS_PROFILE` environment variable.)"
}
}
}
Expand Down
47 changes: 25 additions & 22 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export type Options = {
};

export type GithubOptions = {
token: string | null;
token?: string;
};

export type AssumeAwsOptions = {
assumeRoleAtStartup: boolean;
autoRefresh: boolean;
rememberRole: RememberRole;
region: string;
role: string | null;
role?: string;
profile: AssumeAwsProfileOptions;
};

Expand All @@ -38,19 +38,20 @@ export type AwsRoleSelection = {
class GitHubConfiguration {
constructor(private configuration: vscode.WorkspaceConfiguration) {}

get token(): string | null {
return this.configuration.get<string | null>(
"token",
process.env.GITHUB_TOKEN || null
);
get token(): string | undefined {
const fromConfig = this.configuration.get<string>("token");
const fromEnv = process.env.GITHUB_TOKEN;
return fromConfig || fromEnv;
}
}

class AssumeAwsProfileConfiguration {
constructor(private configuration: vscode.WorkspaceConfiguration) {}

get name(): ProfileName {
return this.configuration.get<ProfileName>("name", "Role ARN");
const fromConfig = this.configuration.get<ProfileName>("name");
const fromEnv: ProfileName | undefined = process.env.AWS_PROFILE;
return fromConfig || fromEnv || "Role ARN";
}
}

Expand Down Expand Up @@ -89,39 +90,41 @@ class AssumeAwsConfiguration {
}

get region(): string {
return this.configuration.get<string>("region", "us-east-1");
const fromConfig = this.configuration.get<string>("region");
const fromEnv = process.env.AWS_DEFAULT_REGION;
return fromConfig || fromEnv || "us-east-1";
}

get role(): string | null {
return this.configuration.get<string | null>("role", null);
get role(): string | undefined {
const fromConfig = this.configuration.get<string | null>("role");
const fromEnv = process.env.AWS_ROLE_ARN;
return fromConfig || fromEnv;
}

get profile(): AssumeAwsProfileConfiguration {
return this.#profile;
}

get lastRoleSelection(): AwsRoleSelection | null {
get lastRoleSelection(): AwsRoleSelection | undefined {
if (this.rememberRole === "Global") {
let roleSelection = this.context.globalState.get<string | null>(
"assumeAws.lastRoleSelection",
null
let roleSelection = this.context.globalState.get<string>(
"assumeAws.lastRoleSelection"
);
return roleSelection ? JSON.parse(roleSelection) : null;
return roleSelection ? JSON.parse(roleSelection) : undefined;
}

if (this.rememberRole === "Workspace") {
let roleSelection = this.context.workspaceState.get<string | null>(
"assumeAws.lastRoleSelection",
null
let roleSelection = this.context.workspaceState.get<string>(
"assumeAws.lastRoleSelection"
);
return roleSelection ? JSON.parse(roleSelection) : null;
return roleSelection ? JSON.parse(roleSelection) : undefined;
}

return null;
return undefined;
}

set lastRoleSelection(roleSelection: AwsRoleSelection | null) {
if (roleSelection === null) {
if (!roleSelection) {
this.context.workspaceState
.update("assumeAws.lastRoleSelection", null)
.then(() => {});
Expand Down

0 comments on commit 4d6b7e1

Please sign in to comment.