Skip to content

Commit

Permalink
Add tests, coverage, codecov and fix filtering roles matching name
Browse files Browse the repository at this point in the history
  • Loading branch information
tilfin committed Feb 27, 2018
1 parent 12318f7 commit 3dd67ba
Show file tree
Hide file tree
Showing 19 changed files with 1,550 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
coverage
dist
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -13,5 +13,7 @@ cache:
- node_modules
install:
- yarn
- yarn add codecov -D
script:
- yarn test_ci
- yarn coverage
1 change: 1 addition & 0 deletions bin/build.sh
Expand Up @@ -10,6 +10,7 @@ content=dist/chrome/js/content.js
popup=dist/chrome/js/popup.js

cat src/lib/profile.js > $content
cat src/lib/content.js >> $content
cat src/content.js >> $content

cat src/lib/load_aws_config.js > $popup
Expand Down
15 changes: 13 additions & 2 deletions karma.conf.js
Expand Up @@ -6,14 +6,24 @@ module.exports = function(config) {
{
pattern: 'test/fixtures/**/*',
},
'src/sanitizer.js',
'src/lib/*.js',
'test/**/*.test.js'
],
preprocessors: {
'test/**/*.html': ['html2js'],
'test/**/*.json': ['json_fixtures']
'test/**/*.json': ['json_fixtures'],
'src/**/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
coverageReporter: {
reporters: [
// generates ./coverage/lcov.info
{ type:'lcovonly', subdir: '.' },
// generates ./coverage/coverage-final.json
{ type:'json', subdir: '.' },
]
},
reporters: ['progress'],
colors: true,
logLevel: config.LOG_INFO,
browsers: ['Chrome', 'ChromeHeadless', 'MyHeadlessChrome'],
Expand All @@ -30,6 +40,7 @@ module.exports = function(config) {
'karma-chrome-launcher',
'karma-mocha',
'karma-chai',
'karma-coverage',
'karma-fixture',
'karma-html2js-preprocessor',
'karma-json-fixtures-preprocessor'
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,8 @@
"archive": "./bin/build.sh; ./bin/archive.sh",
"build": "./bin/build.sh",
"test": "karma start",
"test_ci": "karma start --single-run --browsers ChromeHeadless karma.conf.js"
"test_ci": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
"coverage": "codecov"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +28,7 @@
"karma": "^2.0.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-fixture": "^0.2.6",
"karma-html2js-preprocessor": "^1.1.0",
"karma-json-fixtures-preprocessor": "0.0.6",
Expand Down
135 changes: 0 additions & 135 deletions src/content.js
@@ -1,136 +1 @@
function extendIAMFormList() {
var csrf, list = document.getElementById('awsc-username-menu-recent-roles');
if (list) {
var firstForm = list.querySelector('#awsc-recent-role-0 form');
csrf = firstForm['csrf'].value;
} else {
list = generateEmptyRoleList();
csrf = '';
}

chrome.storage.sync.get(['profiles', 'hidesHistory', 'hidesAccountId','showOnlyMatchingRoles'], function(data) {
var hidesHistory = data.hidesHistory || false;
var hidesAccountId = data.hidesAccountId || false;
var showOnlyMatchingRoles = data.showOnlyMatchingRoles || false;
if (data.profiles) {
loadProfiles(new Profile(data.profiles, showOnlyMatchingRoles), list, csrf, hidesHistory, hidesAccountId);
attachColorLine(data.profiles);
}
});
}

function generateEmptyRoleList() {
var divLbl = document.createElement('div');
divLbl.id = 'awsc-recent-roles-label';
divLbl.textContent = 'Role List:';
var ul = document.createElement('ul');
ul.id = 'awsc-username-menu-recent-roles';
var parentEl = document.getElementById('awsc-login-account-section');
parentEl.appendChild(divLbl);
parentEl.appendChild(ul);

var script = document.createElement('script');
script.src = chrome.extension.getURL('/js/csrf-setter.js');
parentEl.appendChild(script);
return ul;
}

function loadProfiles(profile, list, csrf, hidesHistory, hidesAccountId) {
var recentNames = [];

if (hidesHistory) {
var fc = list.firstChild;
while (fc) {
list.removeChild(fc);
fc = list.firstChild;
}

var label = document.getElementById('awsc-recent-roles-label');
if (label) {
label.textContent = label.textContent.replace('History', 'List');
}
} else {
var li = list.firstElementChild;
while (li) {
input = li.querySelector('input[type="submit"]');
var name = input.value;
if (profile.exProfileNames.indexOf(name) > -1) {
var nextLi = li.nextElementSibling;
list.removeChild(li);
li = nextLi;
} else {
input.style = 'white-space:pre';
recentNames.push(name);
li = li.nextElementSibling;
}
}
}

profile.destProfiles.forEach(function(item) {
var name = item.profile;
if (!hidesAccountId) name += ' | ' + item.aws_account_id;
if (recentNames.indexOf(name) !== -1) return true;

var color = item.color || 'aaaaaa';
list.insertAdjacentHTML('beforeend', Sanitizer.escapeHTML`<li>
<form action="https://signin.aws.amazon.com/switchrole" method="POST" target="_top">
<input type="hidden" name="action" value="switchFromBasis">
<input type="hidden" name="src" value="nav">
<input type="hidden" name="roleName" value="${item.role_name}">
<input type="hidden" name="account" value="${item.aws_account_id}">
<input type="hidden" name="mfaNeeded" value="0">
<input type="hidden" name="color" value="${color}">
<input type="hidden" name="csrf" value="${csrf}">
<input type="hidden" name="redirect_uri" value="https%3A%2F%2Fconsole.aws.amazon.com%2Fs3%2Fhome">
<label for="awsc-recent-role-switch-0" class="awsc-role-color" style="background-color: #${color};">&nbsp;</label>
<input type="submit" class="awsc-role-submit awsc-role-display-name" name="displayName" value="${name}"
title="${item.role_name}@${item.aws_account_id}" style="white-space:pre"></form>
</li>`);
});
}

function attachColorLine(profiles) {
var usernameMenu = document.querySelector('#nav-usernameMenu');
if (usernameMenu.classList.contains('awsc-has-switched-role')) {
var r = usernameMenu.textContent.match(/^([^\s]+)/);
if (r.length < 2) return;

usernameMenu.style = 'white-space:pre';

var profileName = r[1];
var color = null;
profiles.some(function(item) {
if (item.profile === profileName) {
color = item.color;
return true;
}
});

if (color) {
if (needsInvertForeColorByBack(color)) {
var label = usernameMenu.querySelector('.nav-elt-label');
label.style = 'color: #eee';
}

var menubar = document.querySelector('#nav-menubar');
var barDiv = document.createElement('div');
barDiv.style = 'position:absolute;top:39px;width:100%;height:3px;z-index:0;background-color:#' + color;
menubar.appendChild(barDiv);
}
}
}

function needsInvertForeColorByBack(color) {
var r = color.substr(0, 2),
g = color.substr(2, 2),
b = color.substr(4, 2);

r = parseInt(r, 16);
g = parseInt(g, 16);
b = parseInt(b, 16);

return (0.299 * r + 0.587 * g + 0.114 * b) / 255 < 0.5;
}


extendIAMFormList();
133 changes: 133 additions & 0 deletions src/lib/content.js
@@ -0,0 +1,133 @@
function extendIAMFormList() {
var csrf, list = document.getElementById('awsc-username-menu-recent-roles');
if (list) {
var firstForm = list.querySelector('#awsc-recent-role-0 form');
csrf = firstForm['csrf'].value;
} else {
list = generateEmptyRoleList();
csrf = '';
}

chrome.storage.sync.get(['profiles', 'hidesHistory', 'hidesAccountId','showOnlyMatchingRoles'], function(data) {
var hidesHistory = data.hidesHistory || false;
var hidesAccountId = data.hidesAccountId || false;
var showOnlyMatchingRoles = data.showOnlyMatchingRoles || false;
if (data.profiles) {
loadProfiles(new Profile(data.profiles, showOnlyMatchingRoles), list, csrf, hidesHistory, hidesAccountId);
attachColorLine(data.profiles);
}
});
}

function generateEmptyRoleList() {
var divLbl = document.createElement('div');
divLbl.id = 'awsc-recent-roles-label';
divLbl.textContent = 'Role List:';
var ul = document.createElement('ul');
ul.id = 'awsc-username-menu-recent-roles';

var parentEl = document.getElementById('awsc-login-account-section');
parentEl.appendChild(divLbl);
parentEl.appendChild(ul);

var script = document.createElement('script');
script.src = chrome.extension.getURL('/js/csrf-setter.js');
parentEl.appendChild(script);
return ul;
}

function loadProfiles(profile, list, csrf, hidesHistory, hidesAccountId) {
var recentNames = [];

if (hidesHistory) {
var fc = list.firstChild;
while (fc) {
list.removeChild(fc);
fc = list.firstChild;
}

var label = document.getElementById('awsc-recent-roles-label');
if (label) {
label.textContent = label.textContent.replace('History', 'List');
}
} else {
var li = list.firstElementChild;
while (li) {
input = li.querySelector('input[type="submit"]');
var name = input.value;
if (profile.exProfileNames.indexOf(name) > -1) {
var nextLi = li.nextElementSibling;
list.removeChild(li);
li = nextLi;
} else {
input.style = 'white-space:pre';
recentNames.push(name);
li = li.nextElementSibling;
}
}
}

profile.destProfiles.forEach(function(item) {
var name = item.profile;
if (!hidesAccountId) name += ' | ' + item.aws_account_id;
if (recentNames.indexOf(name) !== -1) return true;

var color = item.color || 'aaaaaa';
list.insertAdjacentHTML('beforeend', Sanitizer.escapeHTML`<li>
<form action="https://signin.aws.amazon.com/switchrole" method="POST" target="_top">
<input type="hidden" name="action" value="switchFromBasis">
<input type="hidden" name="src" value="nav">
<input type="hidden" name="roleName" value="${item.role_name}">
<input type="hidden" name="account" value="${item.aws_account_id}">
<input type="hidden" name="mfaNeeded" value="0">
<input type="hidden" name="color" value="${color}">
<input type="hidden" name="csrf" value="${csrf}">
<input type="hidden" name="redirect_uri" value="https%3A%2F%2Fconsole.aws.amazon.com%2Fs3%2Fhome">
<label for="awsc-recent-role-switch-0" class="awsc-role-color" style="background-color: #${color};">&nbsp;</label>
<input type="submit" class="awsc-role-submit awsc-role-display-name" name="displayName" value="${name}"
title="${item.role_name}@${item.aws_account_id}" style="white-space:pre"></form>
</li>`);
});
}

function attachColorLine(profiles) {
var usernameMenu = document.querySelector('#nav-usernameMenu');
if (usernameMenu.classList.contains('awsc-has-switched-role')) {
var profileName = usernameMenu.textContent.trim();

usernameMenu.style = 'white-space:pre';

var color = null;
profiles.some(function(item) {
if (item.profile === profileName) {
color = item.color;
return true;
}
});

if (color) {
if (needsInvertForeColorByBack(color)) {
var label = usernameMenu.querySelector('.nav-elt-label');
label.style = 'color: #eee';
}

var menubar = document.querySelector('#nav-menubar');
var barDiv = document.createElement('div');
barDiv.style = 'position:absolute;top:39px;width:100%;height:3px;z-index:0;background-color:#' + color;
menubar.appendChild(barDiv);
}
}
}

function needsInvertForeColorByBack(color) {
var r = color.substr(0, 2),
g = color.substr(2, 2),
b = color.substr(4, 2);

r = parseInt(r, 16);
g = parseInt(g, 16);
b = parseInt(b, 16);

return (0.299 * r + 0.587 * g + 0.114 * b) / 255 < 0.5;
}

19 changes: 14 additions & 5 deletions src/lib/profile.js
Expand Up @@ -12,13 +12,22 @@ function Profile(items, showOnlyMatchingRoles) {
}
}

function getAssumedRole(elId) {
var el = document.getElementById(elId);
return ( !el ? null : el.textContent.split("/")[0] );
function getAssumedRole() {
var el = document.getElementById('awsc-role-display-name-user');
if (el) {
return el.textContent.trim();
}

el = document.getElementById('awsc-login-display-name-user');
if (el) {
return el.textContent.trim().split("/")[0]
}

return null;
}

var baseAccountId = getAccountId('awsc-login-display-name-account');
var baseRole = getAssumedRole('awsc-login-display-name-user');
var baseRole = getAssumedRole();
var srcProfileMap = {};
var destProfiles = [];
var destProfileMap = {};
Expand All @@ -43,7 +52,7 @@ function Profile(items, showOnlyMatchingRoles) {
if (baseProfile) {
var name = baseProfile.profile;
var profiles = destProfileMap[name] || [];
if (showOnlyMatchingRoles) {
if (showOnlyMatchingRoles && document.body.className.includes('user-type-federated')) {
profiles = profiles.filter(function(el) { return (el.role_name == baseRole); })
}
result = result.concat(profiles);
Expand Down

0 comments on commit 3dd67ba

Please sign in to comment.