common-fate / granted

The easiest way to access your cloud.
https://granted.dev
MIT License
1.01k stars 93 forks source link

How do I do string manipulations in `granted sso populate` ? Error `"strings" not defined` #478

Closed nitrocode closed 2 months ago

nitrocode commented 11 months ago

Context

I'd like to generate my profiles with the following template

{{ .RoleName }}-{{ .AccountName }}

I'd like to lowercase it using strings.ToLower. Here is an example from gomplate which works

$ echo '{ "RoleName": "Admin", "AccountName": "Prod" }' | gomplate -d test=stdin:///in.json -i '{{ (ds "test").RoleName | strings.ToLower }}-{{ (ds "test").AccountName | strings.ToLower }}'

admin-prod

Here is the error

$ granted sso populate --sso-region us-east-1 --prune --prefix gen_ --profile-template '{{ .RoleName | strings.ToLower }}-{{ .AccountName | strings.ToLower }}' https://<snip>.awsapps.com/start
[i] listing available profiles from AWS IAM Identity Center...
[✘] template: :1: function "strings" not defined

Workaround

If I just want to lowercase, then it's pretty simple as I can lowercase all the profile names

granted sso generate --sso-region us-east-1 --profile-template '{{ .RoleName }}-{{ .AccountName }}' https://<snip>.awsapps.com/start | sed -e 's/\(\[profile.*\)/\L\1/'

But I'd also like to delete -,. n names and maybe do additional string manipulations. I suppose these can be done using sed as well.

References

Maybe the issue is in the common-fate/awsconfigfile package

Perhaps instead of using text/template, the masterminds/sprig library should be used instead?

jmreicha commented 2 months ago

Just tried this with v0.27.0 which should have the support but get an error. Here's the command I'm using (adapted from release notes):

granted sso populate --sso-region us-west-2 --profile-template '{{ .RoleName | strings.ToLower }}/{{ .AccountName | strings.ToLower }}' https://<endpoint>.awsapps.com/start

Here's the error.

[✘] template: :1: function "strings" not defined

Am I just missing something simple?

jmreicha commented 2 months ago

Nevermind, this works. --profile-template '{{ .RoleName | lower }}/{{ .AccountName | lower }}'

nitrocode commented 2 months ago

Oh very nice. Thank you @mikesarver for the contribution and @jmreicha for confirming that it works.

Also, it looks like sprig functions have been in granted's cli since https://github.com/common-fate/granted/releases/tag/v0.25.0 which was released shortly after the dependency was bumped https://github.com/common-fate/granted/pull/648 to v0.9.0