Skip to content

Commit 89f64d8

Browse files
authored
Merge pull request #9327 from shirady/iam-get-user-with-tags
IAM | Add `Tags` to `GetUser` Reply
2 parents 321de03 + 7940bf8 commit 89f64d8

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

src/api/account_api.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,17 @@ module.exports = {
13051305
$ref: 'common_api#/definitions/access_keys'
13061306
}
13071307
},
1308+
tags: {
1309+
type: 'array',
1310+
items: {
1311+
type: 'object',
1312+
properties: {
1313+
member: {
1314+
$ref: '#/definitions/tag'
1315+
},
1316+
},
1317+
},
1318+
},
13081319
}
13091320
},
13101321
user_accesskey_info: {

src/endpoint/iam/ops/iam_get_user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async function get_user(req, res) {
2929
Arn: reply.arn,
3030
CreateDate: iam_utils.format_iam_xml_date(reply.create_date),
3131
PasswordLastUsed: iam_utils.format_iam_xml_date(reply.password_last_used),
32+
Tags: reply.tags && reply.tags.length === 0 ? undefined : reply.tags
3233
}
3334
},
3435
ResponseMetadata: {

src/server/system_services/account_server.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ async function get_user(req) {
12101210
const username = account_util.get_iam_username(req.rpc_params.username || requested_account.name.unwrap());
12111211
const iam_arn = iam_utils.create_arn_for_user(requesting_account._id.toString(), username,
12121212
requested_account.iam_path || IAM_DEFAULT_PATH);
1213+
const tags = account_util.get_sorted_list_tags_for_user(requested_account.tagging);
12131214
return {
12141215
user_id: requested_account._id.toString(),
12151216
iam_path: requested_account.iam_path || IAM_DEFAULT_PATH,
@@ -1219,6 +1220,7 @@ async function get_user(req) {
12191220
create_date: Date.now(),
12201221
// TODO: Dates missing : GAP
12211222
password_last_used: Date.now(),
1223+
tags: tags
12221224
};
12231225
}
12241226

@@ -1504,16 +1506,8 @@ async function list_user_tags(req) {
15041506
account_util._check_if_requested_is_owned_by_root_account(action, requesting_account, requested_account);
15051507

15061508
// TODO: Pagination not supported - currently returns all tags, ignoring marker and max_items params
1507-
const all_tags = requested_account.tagging || [];
1508-
const sorted_tags = all_tags.sort((a, b) => a.key.localeCompare(b.key));
1509-
1510-
const tags = sorted_tags.length > 0 ? sorted_tags.map(tag => ({
1511-
member: {
1512-
Key: tag.key,
1513-
Value: tag.value
1514-
}
1515-
})) : [];
1516-
dbg.log0('AccountSpaceNB.list_user_tags: returning', tags, 'tags for user', req.rpc_params.username);
1509+
const tags = account_util.get_sorted_list_tags_for_user(requested_account.tagging);
1510+
dbg.log1('AccountSpaceNB.list_user_tags: returning', tags, 'tags for user', req.rpc_params.username);
15171511

15181512
return {
15191513
tags: tags,

src/test/integration_tests/api/iam/test_iam_basic_integration.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ mocha.describe('IAM basic integration tests - happy path', async function() {
423423
assert.equal(response2.Tags.length, 2);
424424
const sorted = arr => _.sortBy(arr, 'Key');
425425
assert.deepEqual(sorted(response2.Tags), sorted(user_tags));
426+
427+
// verify it with get user (Tags are included in the User object)
428+
const input3 = {
429+
UserName: username4
430+
};
431+
const command3 = new GetUserCommand(input3);
432+
const response3 = await iam_account.send(command3);
433+
_check_status_code_ok(response3);
434+
assert.equal(response3.User.Tags.length, 2);
435+
assert.deepEqual(sorted(response3.User.Tags), sorted(user_tags));
426436
});
427437

428438
mocha.it('untag user', async function() {

src/util/account_util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,19 @@ function return_list_member(iam_user, iam_path, iam_username) {
730730
};
731731
}
732732

733+
function get_sorted_list_tags_for_user(user_tagging) {
734+
if (!user_tagging || user_tagging.length === 0) {
735+
return [];
736+
}
737+
const sorted_tags = user_tagging.sort((a, b) => a.key.localeCompare(b.key));
738+
return sorted_tags.map(tag => ({
739+
member: {
740+
Key: tag.key,
741+
Value: tag.value
742+
}
743+
}));
744+
}
745+
733746

734747
exports.delete_account = delete_account;
735748
exports.create_account = create_account;
@@ -757,3 +770,4 @@ exports.validate_and_return_requested_account = validate_and_return_requested_ac
757770
exports.get_iam_username = get_iam_username;
758771
exports.return_list_member = return_list_member;
759772
exports.get_owner_account_id = get_owner_account_id;
773+
exports.get_sorted_list_tags_for_user = get_sorted_list_tags_for_user;

0 commit comments

Comments
 (0)