Skip to content

Commit 47efc91

Browse files
authored
Merge pull request #1 from djgoku/feat/allow-reconfigure-how-profile-names-are-created
Feat/allow reconfigure how profile names are created
2 parents 4c7a82d + 23f7893 commit 47efc91

File tree

13 files changed

+627
-75
lines changed

13 files changed

+627
-75
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ on:
33
push:
44
branches:
55
- main
6+
tags:
7+
- '**'
68
env:
79
DEVBOX_API_TOKEN: ${{ secrets.DEVBOX_API_TOKEN }}
810
GH_TOKEN: ${{ secrets.GH_TOKEN }}

README.org

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,37 @@ xattr -dr com.apple.quarantine ~/aws-sso-config-generator
2626
chmod +x ~/aws-sso-config-generator
2727
#+end_example
2828

29+
More information here https://apple.stackexchange.com/a/436677
30+
2931
** Usage
3032
*Note: if no parameters are passed user will be prompted for ~region~ and ~start_url~.*
3133

3234
Example call of ~aws-sso-config-generator~ where AWS access portal is in ~us-west-2~ with a url of ~https://​<example>.awsapps.com/start/#/~.
3335

3436
#+begin_example
35-
aws-sso-config-generator --region us-west-2 --start-url https://<example>.awsapps.com/start/#/
37+
aws-sso-config-generator --region us-west-2 --sso-region us-east-1 --start-url https://<example>.awsapps.com/start/#/
3638
#+end_example
3739

3840
#+begin_example
39-
aws-sso-config-generator -r us-west-2 -u https://<example>.awsapps.com/start/#/
41+
aws-sso-config-generator -r us-west-2 --sso-region us-east-1 -u https://<example>.awsapps.com/start/#/
4042
#+end_example
4143

4244
** Parameters
4345

44-
| Option | Type | Example | Description |
45-
|---------------------+--------+-------------------------------------------+-------------------------------------------|
46-
| --region or -r | string | -r us-west-2 | Region where AWS access portal is hosted. |
47-
| --start-url or -u | string | -u https://<example>.awsapps.com/start/#/ | The URL for the AWS access portal |
48-
| --help or -h | N/A | -h | Help menu |
46+
| Option | Type | Example | Description | Default |
47+
|---------------------+--------+-------------------------------------------+-------------------------------------------------+-----------------------------|
48+
| --sso-region | string | --sso-region us-west-2 | Region where AWS resources are hosted. | |
49+
| --region or -r | string | -r us-west-2 | Region where AWS access portal is hosted. | |
50+
| --start-url or -u | string | -u https://<example>.awsapps.com/start/#/ | The URL for the AWS access portal | |
51+
| --help or -h | N/A | -h | Help menu | |
52+
| --template or -t | string | -t ~/.aws/config.template.json | JSON template file to re-kap accounts and roles | ~/.aws/config.template.json |
53+
| --out or -o | string | -o ~/.aws/config.generated | Output file for generated AWS config file | ~/.aws/config.generated |
54+
4955

50-
** Example Config (~/.aws/sso.generated)
56+
** Example Config (~/.aws/config.generated)
5157

58+
~/.aws/config.generated:
59+
#+name: ~/.aws/config.generated
5260
#+begin_example
5361
# config generated via https://github.com/djgoku/aws-sso-config-generator
5462
#
@@ -57,14 +65,45 @@ aws-sso-config-generator -r us-west-2 -u https://<example>.awsapps.com/start/#/
5765
# 1. log into aws sso via `aws sso login --profile use-any-profile-name`
5866
# 2. validate `AWS_PROFILE=use-any-profile-name aws sts get-caller-identity`
5967

60-
# AWS_CONFIG_FILE=~/.aws/config.generated AWS_PROFILE=1111111-RoleName aws sts get-caller-identity
61-
[profile 1111111-RoleName]
68+
# AWS_CONFIG_FILE=~/.aws/config.generated AWS_PROFILE=1111111-ReadOnly aws sts get-caller-identity
69+
[profile 1111111-ReadOnly]
6270
sso_start_url = https://<example>.awsapps.com/start/#/
6371
sso_region = us-east-2
6472
sso_account_id = 1111111
65-
sso_role_name = RoleName
73+
sso_role_name = ReadOnly
6674
region = us-west-2
6775
output = json
76+
77+
# AWS_CONFIG_FILE=~/.aws/config.generated AWS_PROFILE=1111111-ReadOnly aws sts get-caller-identity
78+
[profile 1111111-ReadOnly]
79+
sso_start_url = https://<example>.awsapps.com/start/#/
80+
sso_region = us-east-2
81+
sso_account_id = 1111111
82+
sso_role_name = ReadOnly
83+
region = us-west-2
84+
output = json
85+
#+end_example
86+
87+
** Example JSON Template (~/.aws/config.template.json)
88+
89+
In the =template.json= structure, the values under the =accounts= and =roles= keys are used to replace their respective keys during config generation. For example:
90+
- ="111111"= in =accounts= will be replaced with ="dev"=
91+
- ="Admin"= in =roles= will be replaced with =""= (empty string)
92+
93+
~/.aws/config.template:
94+
#+name: ~/.aws/config.template
95+
#+begin_example
96+
{
97+
"accounts": {
98+
"111111": "dev",
99+
"222222": "uat",
100+
"333333": "prod"
101+
},
102+
"roles": {
103+
"Admin": "",
104+
"ReadOnly": "read"
105+
}
106+
}
68107
#+end_example
69108

70109
** Development

devbox.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"packages": {
44
"beam.packages.erlang_27.elixir_1_18": "",
55
"erlang": "latest",
6-
"beam27Packages.elixir-ls": "latest",
6+
"beam27Packages.elixir-ls": "0.26.4",
77
"git": "latest",
88
"zig": "latest",
99
"xz": "latest",
@@ -32,14 +32,22 @@
3232
"mix local.rebar --force",
3333
],
3434
"scripts": {
35+
"escript": [
36+
"MIX_ENV=escript mix escript.build"
37+
],
3538
"do-all": [
3639
"mix deps.get --only prod",
40+
"mix test",
3741
"mix format --check-formatted",
3842
"MIX_ENV=prod mix release --overwrite"
3943
],
44+
"version-info-only": [
45+
"MIX_APPLICATION_VERSION=$(elixir -e 'Regex.run(~r/version: \"(.*)\"/, File.read!(\"mix.exs\"), capture: :all_but_first) |> List.first() |> IO.puts()')",
46+
"echo \"version '$MIX_APPLICATION_VERSION'\""
47+
],
4048
"version-info": [
41-
"MIX_APPLICATION_VERSION=$(elixir -e 'Regex.run(~r/version: \"(\\d+.\\d+.\\d+)\"/, File.read!(\"mix.exs\"), capture: :all_but_first) |> List.first() |> IO.puts()')",
42-
"echo \"version $MIX_APPLICATION_VERSION\"",
49+
"MIX_APPLICATION_VERSION=$(elixir -e 'Regex.run(~r/version: \"(.*)\"/, File.read!(\"mix.exs\"), capture: :all_but_first) |> List.first() |> IO.puts()')",
50+
"echo \"version '$MIX_APPLICATION_VERSION'\"",
4351
"cd burrito_out",
4452
"bash -c 'for file in *_*; do mv \"$file\" \"${file//_/-}\"; done'",
4553
"shasum -a 256 ./* > aws-sso-config-generator-checksums.txt",

devbox.lock

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,51 +111,51 @@
111111
}
112112
}
113113
},
114-
"beam27Packages.elixir-ls@latest": {
115-
"last_modified": "2025-04-05T00:48:53Z",
116-
"resolved": "github:NixOS/nixpkgs/250b695f41e0e2f5afbf15c6b12480de1fe0001b#beam27Packages.elixir-ls",
114+
"beam27Packages.elixir-ls@0.26.4": {
115+
"last_modified": "2025-03-02T11:46:57Z",
116+
"resolved": "github:NixOS/nixpkgs/b58e19b11fe72175fd7a9e014a4786a91e99da5f#beam27Packages.elixir-ls",
117117
"source": "devbox-search",
118-
"version": "0.27.2",
118+
"version": "0.26.4",
119119
"systems": {
120120
"aarch64-darwin": {
121121
"outputs": [
122122
{
123123
"name": "out",
124-
"path": "/nix/store/nxj7s2z8kfr6hisk94j6407ljh5aw1ck-elixir-ls-0.27.2",
124+
"path": "/nix/store/xfgs8bich8hf9j0q3bx1nk0dqahd6j6c-elixir-ls-0.26.4",
125125
"default": true
126126
}
127127
],
128-
"store_path": "/nix/store/nxj7s2z8kfr6hisk94j6407ljh5aw1ck-elixir-ls-0.27.2"
128+
"store_path": "/nix/store/xfgs8bich8hf9j0q3bx1nk0dqahd6j6c-elixir-ls-0.26.4"
129129
},
130130
"aarch64-linux": {
131131
"outputs": [
132132
{
133133
"name": "out",
134-
"path": "/nix/store/hcn1y1jbalmy9mknx4vb85mg7gcmv8ix-elixir-ls-0.27.2",
134+
"path": "/nix/store/xmk26kzrxwlr8w9mvbdghfh1hqyninnw-elixir-ls-0.26.4",
135135
"default": true
136136
}
137137
],
138-
"store_path": "/nix/store/hcn1y1jbalmy9mknx4vb85mg7gcmv8ix-elixir-ls-0.27.2"
138+
"store_path": "/nix/store/xmk26kzrxwlr8w9mvbdghfh1hqyninnw-elixir-ls-0.26.4"
139139
},
140140
"x86_64-darwin": {
141141
"outputs": [
142142
{
143143
"name": "out",
144-
"path": "/nix/store/0gisn7j9pr8d6fqnhcy9ibxxpz93w6ai-elixir-ls-0.27.2",
144+
"path": "/nix/store/f05vh1vkd164kmhcg46k99hzhdk6my3i-elixir-ls-0.26.4",
145145
"default": true
146146
}
147147
],
148-
"store_path": "/nix/store/0gisn7j9pr8d6fqnhcy9ibxxpz93w6ai-elixir-ls-0.27.2"
148+
"store_path": "/nix/store/f05vh1vkd164kmhcg46k99hzhdk6my3i-elixir-ls-0.26.4"
149149
},
150150
"x86_64-linux": {
151151
"outputs": [
152152
{
153153
"name": "out",
154-
"path": "/nix/store/279x4jxl422ly3q78ycx9m3rkds0kb3i-elixir-ls-0.27.2",
154+
"path": "/nix/store/ph1xl3gmy92rlgipyjl7j361cjvcimw4-elixir-ls-0.26.4",
155155
"default": true
156156
}
157157
],
158-
"store_path": "/nix/store/279x4jxl422ly3q78ycx9m3rkds0kb3i-elixir-ls-0.27.2"
158+
"store_path": "/nix/store/ph1xl3gmy92rlgipyjl7j361cjvcimw4-elixir-ls-0.26.4"
159159
}
160160
}
161161
},

lib/aws_sso_config_generator.ex

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ defmodule AwsSsoConfigGenerator do
55
defstruct access_token: nil,
66
account_list: [],
77
account_roles: [],
8+
args: [],
89
client: nil,
910
client_id: nil,
1011
client_secret: nil,
1112
client_name: "aws-sso-config-generator",
1213
device_code: nil,
1314
expires_in: nil,
1415
interval: nil,
16+
output_file: nil,
1517
register_client: nil,
1618
region: nil,
19+
sso_region: nil,
1720
start_url: nil,
21+
template: %{},
22+
template_file: nil,
1823
verification_uri_complete: nil
1924

25+
def main(_) do
26+
# for escript use
27+
start("not", "used")
28+
end
29+
2030
def start(_, _) do
2131
args = Util.parse_args(Burrito.Util.Args.argv())
2232

@@ -25,23 +35,29 @@ defmodule AwsSsoConfigGenerator do
2535
System.halt(0)
2636
end
2737

28-
aws_region = Util.get_region(args)
29-
start_url = Util.get_start_url(args)
30-
3138
config =
32-
%AwsSsoConfigGenerator{
33-
region: aws_region,
34-
start_url: start_url,
35-
client: %AWS.Client{region: aws_region}
36-
}
39+
%AwsSsoConfigGenerator{args: args}
40+
|> Util.map_args()
41+
|> Util.get_region()
42+
|> Util.get_start_url()
3743
|> Util.sso_oidc_register_client()
3844
|> Util.sso_oidc_start_device_authorization()
3945

4046
Util.browser_open(config.verification_uri_complete)
4147

42-
IO.puts(
43-
"\nVerification URI (copy and paste into browser if it doesn't open.)\n\n #{config.verification_uri_complete}\n\n"
44-
)
48+
output = """
49+
aws-sso-config-generator #{Application.spec(:aws_sso_config_generator, :vsn)}
50+
51+
Tool to generate an AWS config file (~/.aws/config) after authenticating and authorizing AWS SSO IAM Identity Center.
52+
53+
Source code: https://github.com/djgoku/aws-sso-config-generator
54+
55+
Verification URI (copy and paste into browser if it doesn't open.)
56+
57+
#{config.verification_uri_complete}
58+
"""
59+
60+
IO.puts(output)
4561

4662
maybe_access_token = Util.request_until(config, config.expires_in)
4763

@@ -54,13 +70,15 @@ defmodule AwsSsoConfigGenerator do
5470
%{config | access_token: maybe_access_token}
5571
|> Util.sso_list_accounts(nil)
5672
|> Util.sso_list_account_roles()
57-
|> Util.config_sort_account_roles()
73+
|> Util.duplicate_keys_with_new_keys()
74+
|> Util.maybe_load_template()
75+
|> Util.maybe_save_debug_data()
76+
|> Util.maybe_rename_accounts_and_roles()
5877
|> Util.generate_config()
5978
|> Enum.join("\n")
6079

61-
file_path = Path.join(System.user_home!(), ".aws/config.generated")
62-
File.write(file_path, config_data)
63-
IO.puts("wrote generated to #{file_path}")
80+
File.write(config.output_file, config_data)
81+
IO.puts("wrote generated to #{config.output_file}")
6482

6583
System.halt(0)
6684
end

0 commit comments

Comments
 (0)