Skip to content

Instantly share code, notes, and snippets.

@josharrington
Last active January 26, 2022 16:59
Show Gist options
  • Save josharrington/9be84caa72431cfa900f2762d6c8fa0e to your computer and use it in GitHub Desktop.
Save josharrington/9be84caa72431cfa900f2762d6c8fa0e to your computer and use it in GitHub Desktop.
AWS SSO Surface Account Name
// ==UserScript==
// @name AWS SSO Display Account
// @namespace http://tampermonkey.net/
// @version 0.6
// @description This will make the account switcher show the account name and role when using AWS SSO
// @author josharrington
// @include /^https?://([\-\d\w]+\.)?console\.aws\.amazon\.com/.*/
// @icon https://www.google.com/s2/favicons?domain=amazon.com
// ==/UserScript==
(function() {
'use strict';
var accountMapping = {
// You can add accounts here to customize the behavior and change the name or color.
// Color can be any CSS color including hex in format #abc123
"1234567890" : {"name": "Custom-Name", "color": "#d6146ec4"},
}
const regex = /AWSReservedSSO_(\w+)_\w+\/([\w\.@\+]+)/
var sso_string = document.querySelector("#menu--account > div[class*='globalNav'] > div > div:nth-child(2) > span:nth-child(2)");
var switcher = document.querySelector("#nav-usernameMenu > span > span");
try {
const match = sso_string.innerText.match(regex);
var accountNumber = document.querySelector("#menu--account > div[class*='globalNav'] div > div:nth-child(1) > span:nth-child(2)").innerText.replaceAll('-', '');
var name = "Unknown";
if (accountMapping[accountNumber]) {
name = accountMapping[accountNumber].name;
if (accountMapping[accountNumber].color) {
switcher.style = "background-color: " + (accountMapping[accountNumber].color ?? "null") + ";";
}
}
switcher.innerText = `AWS SSO: ${match[1]}/${match[2]} @ ${name} (${accountNumber})`;
} catch (e) {
console.log("We don't appear to be using AWS SSO. TamperMonkey Script will ignore this.")
return;
}
})();
@axot
Copy link

axot commented Sep 11, 2021

Is it possible to fetch account name via a api call?

@josharrington
Copy link
Author

josharrington commented Sep 12, 2021 via email

@josharrington
Copy link
Author

Updated to work with the new aws console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment