Skip to content

Commit

Permalink
Merge pull request #4 from tilfin/feature/multi-base-accounts
Browse files Browse the repository at this point in the history
Support multi base accounts
  • Loading branch information
tilfin authored Nov 16, 2016
2 parents 01fbafa + cb0adde commit e8f195f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ color=bbeeff
- Required `role_arn` or (`aws_account_id` and `role_name`)
- Optional `color` that is RGB hex value without prefix `#`

### Multi base accounts
- A profile that has only `aws_account_id` is defined as **base account**.
- A profile that has `source_profile` is defined as **target account**.
- A **base account** is associated with **target account**s.

```
[baseaccount]
aws_account_id = 000000000000
[targetaccount]
role_arn = arn:aws:iam::123456789012:role/targetaccount
source_profile = baseaccount
```

If you sign-in a base account, target accounts of the other base accounts are excluded.

## Settings

- Can hide original role history (Show only roles in the configuration)
Expand Down
73 changes: 65 additions & 8 deletions js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ function extendIAMFormList() {
chrome.storage.sync.get(['profiles', 'hidesHistory'], function(data) {
var hidesHistory = data.hidesHistory || false;
if (data.profiles) {
loadProfiles(data.profiles, list, csrf, hidesHistory);
loadProfiles(new Profile(data.profiles), list, csrf, hidesHistory);
attachColorLine(data.profiles);
}
});
}

function loadProfiles(profiles, list, csrf, hidesHistory) {
var submits = list.querySelectorAll('input[type="submit"]');
function loadProfiles(profile, list, csrf, hidesHistory) {
var recentNames = [];

if (hidesHistory) {
Expand All @@ -30,13 +29,23 @@ function loadProfiles(profiles, list, csrf, hidesHistory) {
label.textContent = label.textContent.replace('History', 'List');
}
} else {
submits.forEach(function(input){
input.style = 'white-space:pre';
recentNames.push(input.value);
});
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;
}
}
}

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

Expand Down Expand Up @@ -86,4 +95,52 @@ function attachColorLine(profiles) {
}
}

function Profile(items) {
function getAccountId(elId) {
var el = document.getElementById(elId);
if (!el) return null;
return el.textContent.replace(/\-/g, '');
}

var baseAccountId = getAccountId('awsc-login-display-name-account');
var srcProfileMap = {};
var destProfiles = [];
var destProfileMap = {};

items.forEach(function(item){
if (item.source_profile) {
if (item.source_profile in destProfileMap) {
destProfileMap[item.source_profile].push(item);
} else {
destProfileMap[item.source_profile] = [item];
}
} else if (item.aws_account_id && !item.role_name) {
srcProfileMap[item.aws_account_id] = item;
} else {
destProfiles.push(item);
}
});

this.destProfiles = (function(){
var result = [].concat(destProfiles);
var baseProfile = srcProfileMap[baseAccountId];
if (baseProfile) {
var name = baseProfile.profile;
result = result.concat(destProfileMap[name] || []);
delete destProfileMap[name];
}
return result;
})();

this.exProfileNames = (function(){
var result = [];
for (var name in destProfileMap) {
destProfileMap[name].forEach(function(item){
result.push(item.profile + ' | ' + item.aws_account_id);
});
}
return result;
})();
}

extendIAMFormList();

0 comments on commit e8f195f

Please sign in to comment.