Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dterracino/ac442dbafb8e4bdc83c4f53238b3a3ed to your computer and use it in GitHub Desktop.
Save dterracino/ac442dbafb8e4bdc83c4f53238b3a3ed to your computer and use it in GitHub Desktop.
Prettifies the AWS navbar where the role, account, and region are located. Click Raw to install on script managers like GreaseMonkey and Tampermonkey.
// ==UserScript==
// @name Make AWS Profile and Region Clearer
// @version 1.2
// @include https://*console.aws.amazon.com/*
// ==/UserScript==
// *** START Configuration ***
const fontSize = '17px';
const safeUserNames = ['view'];
const dangerousUserNames = ['admin'];
const safeEnvironmentTerms = ['test-', '-test-'];
const dangerousEnvironmentTerms = ['prod-', '-prod-'];
const safeRegions = ['eu-west-1'];
const dangerousRegions = ['ap-east-1', 'ap-south-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2'];
const safeColour = "lime";
const cautiousColour = "#ffb600";
const dangerousColour = "red";
// *** END Configuration ***
// *** START Helper functions ***
function getIAMUserNameColour(userName) {
loweredUserName = userName.toLowerCase();
if (dangerousUserNames.includes(loweredUserName)) {
return dangerousColour;
} else if (safeUserNames.includes(loweredUserName)) {
return safeColour;
} else {
return cautiousColour;
}
}
function getRegionColour(region) {
loweredRegion = region.toLowerCase();
if (dangerousRegions.includes(loweredRegion)) {
return dangerousColour;
} else if (safeRegions.includes(loweredRegion)) {
return safeColour;
} else {
return cautiousColour;
}
}
function getEnvironmentColour(environment) {
loweredEnvironment = environment.toLowerCase();
if (doesElementAppearInString(loweredEnvironment, safeEnvironmentTerms)) {
return safeColour;
} else if (doesElementAppearInString(loweredEnvironment, dangerousEnvironmentTerms)) {
return dangerousColour;
}
return cautiousColour
}
function doesElementAppearInString(string, container) {
for (i = 0; i < container.length; ++container) {
if (string.includes(container[i])) {
return true;
}
}
return false;
}
// *** END Helper functions ***
// -------------- Script Itself --------------
// Gather the containers for the profile and region
var profileElement = document.querySelector('#nav-usernameMenu > .nav-elt-label');
var regionElement = document.querySelector('#nav-regionMenu > .nav-elt-label');
// Extend the container of the profile to not cut the text off
profileElement.style["max-width"] = "2000px";
// Get the profile, IAM user name, environment, and region
var profile = profileElement.textContent;
var environment = profile.split('@').pop().trim();
var iamUserName = ((profile.includes('/')) ? (profile.split('/')) : profile.split('@'))[0].trim();
var region = document.getElementById('awsc-mezz-region').content;
// Update the profile
const iamUserNameHTML = '<font color="' + getIAMUserNameColour(iamUserName) + '">' + iamUserName + '</font>';
const environmentHTML = '<font color="' + getEnvironmentColour(environment) + '">' + environment + '</font>';
profileElement.innerHTML = '<strong style="font-size: ' + fontSize + ';">' + iamUserNameHTML + ' @ ' + environmentHTML + '</strong>';
// Update the region
regionElement.innerHTML = '<strong style="font-size: ' + fontSize + ';"><font color="' + getRegionColour(region) + '">' + region + ' (' + regionElement.textContent + ')</font></strong>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment