Skip to content

Commit

Permalink
Alias mapping (#1)
Browse files Browse the repository at this point in the history
* Map aliases to one or more roles

* update changelog

* escape the square brackets
  • Loading branch information
MichaelPalmer1 authored Nov 20, 2018
1 parent ff31050 commit a28b6fc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.0
- \[BREAKING\] Allow mapping an alias to one or more roles. This also modifies the `RoleAliases` block in the config file,
requiring a configuration update by the user.

## 1.2.2
- Add executable paths to config

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ Output file that will contain all the generated aliases
This argument is optional and defaults to `goss-generated-aliases.sh`.
### `RoleAliases`
Mapping of role names to aliases.
Mapping of aliases to roles.
This argument is optional. When not specified, the gossamer command to assume all accounts in the
same role will use a normalized version (`/`'s are replaced with `-`'s) of the role name. For instance,
if you are trying to assume role `path/role1` in all accounts, and this parameter is not specified, the
alias will be generated as `goss-path-role1`. If you specified the following configuration:
This argument is optional and allows for the creation of an alias that maps to one or more roles.
```json
"RoleAliases": {
"path/role1": "r1"
"all": [
"path/role1",
"path/role2
]
}
```

Then the generated alias would be `goss-r1`.
For instance, if you are trying to assume roles `path/role1` and `path/role2` in all accounts using the above
configuration, the generated alias would be `goss-all`. In addition to these aliases, by default, an alias for each
normalized version (`/`'s are replaced with `-`'s) of a role name will be created.

### `GossamerPath`

Expand Down
12 changes: 10 additions & 2 deletions goss-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
"OutputDirectory": "$HOME/gossamer",
"OutputFile": "goss-generated-aliases.sh",
"RoleAliases": {
"path/role1": "r1",
"path/role2": "r2"
"r1": [
"path/role1"
],
"r2": [
"path/role2"
],
"r3": [
"path/role1",
"path/role2"
]
},
"GossamerPath": "/usr/local/bin/gossamer",
"AWSCredentialsPath": "$HOME/.aws/credentials",
Expand Down
34 changes: 31 additions & 3 deletions goss_config_gen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ def main():
}
)

# Write role alias files
aliased_roles = set()
for alias, role_names in role_aliases.items():
role_file = os.path.join(
output_dir,
alias + '.json'
)

role_data = []
for role in role_names:
role_data += roles.get(role)
aliased_roles.add(role)

output_data = {'Roles': role_data}

with open(role_file, 'w') as f:
f.write(json.dumps(output_data, indent=4))

# Write gossamer alias
aliases.append(
"alias goss-%(alias)s='%(gossamer_path)s -rolesfile %(role_file)s -profile %(profile)s -serialnumber $MFA "
"-o %(aws_creds_path)s -force -tokencode'\n" % {
'alias': alias,
'gossamer_path': config['GossamerPath'],
'role_file': role_file,
'profile': config['BaseProfile'],
'aws_creds_path': config['AWSCredentialsPath']
}
)

# Write role files
for role_name, role_data in roles.items():
normalized_role_name = role_name.replace('/', '-')
Expand All @@ -136,13 +166,11 @@ def main():
with open(role_file, 'w') as f:
f.write(json.dumps(output_data, indent=4))

alias = role_aliases.get(role_name, normalized_role_name)

# Write gossamer alias
aliases.append(
"alias goss-%(alias)s='%(gossamer_path)s -rolesfile %(role_file)s -profile %(profile)s -serialnumber $MFA "
"-o %(aws_creds_path)s -force -tokencode'\n" % {
'alias': alias,
'alias': normalized_role_name,
'gossamer_path': config['GossamerPath'],
'role_file': role_file,
'profile': config['BaseProfile'],
Expand Down

0 comments on commit a28b6fc

Please sign in to comment.