The Challenge
After several months of using the aws_saml_auth script (based on the samlapi_formauth_adfs3 script from AWS), there were several areas that were clearly lacking. Some of these included the fact that it didn’t have parameters, meaning you had to modify the source code each time you wanted to tweak your credential location, profile name, etc. I still haven’t parameterized everything, but this is much closer to a production ready script in my mind. Another feature request that came from some of my peers was adding a friendly name instead of the ARN to the indexed list of roles (example below).
The Solution
I have a github repository with the full code that you can star/watch here.
First, I began to parameterize using a Python package plac. By wrapping the entire script in a “login” function, I was able to use parameters by calling that function with plac: plac.call(login)
. The actual parameters themselves are included in the function defintion:
plac lets us run aws_saml_auth.py with parameters such as “–username tjsullivan1@sullivanenterprises.org” or “-u tjsullivan1@sullivanenterprises.org”, which gives us much more flexibility to quickly adjust things on the fly. Note that parameters without an = after the parentheses do not have a default option and will be prompted if not provided. All in all, plac seems to be a really useful and I will likely use it as a python script addition in the future.
As you can see, a couple of other useful parameters are profile. For instance, if you want to use a ‘saml’ profile, you can specify that here. You can have multiple profiles created from this script at once, which can allow for a lot of command line flexibility.
Second, I wanted to change arn:aws:iam::012345678901:role/S3FullAccess
to something more like SE-Test/S3FullAccess
, so that fellow admins who aren’t in our accounts every day don’t need to remember that 012345678901 is the test account. This was achieved with two major functions, seen here.
The first function replaces a specified account id with a specified account name in an arn string like the one listed above. It does this by using a regular expression substition and then removing the aws default ARN info. That is to say, the first piece (re.sub()) takes the string arn:aws:iam::012345678901:role/S3FullAccess and changes it to arn:aws:iam::SE-Test::role. Then, we replace the arn:aws:iam:: with a null string, and then we replace :role with a null value.
The second function reads a file line by line. If the file contains the account ID returned by the saml response, we flag that it was found and then we grab the name that we want assigned to the account from that line. We then return the response value of the make_friendly_name function – if the line does not contain the value, we will just return what we used to return. Also, if the file provided doesn’t exist, we return the value that we used to return.
With these two functions and the addition of plac, the script came a long way to being much more usable.