-
Notifications
You must be signed in to change notification settings - Fork 0
/
iamRequests.js
154 lines (143 loc) · 4.16 KB
/
iamRequests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'; // eslint-disable-line strict
/* eslint no-console: 0 */
const iamClient = require('./iamSDK');
const configFile = require('./config.json');
const accountId = configFile.account.accountId;
const userName = 'Pinky';
const otherUserName = 'Brain';
const groupName = 'worldDominators';
const liberalPolicyName = 'liberalPolicy';
const liberalPolicyDoc = JSON.stringify({
Version: '2012-10-17',
Statement: [{
Effect: 'Allow',
Action: 's3:*',
Resource: 'arn:aws:s3:::*',
}],
});
const strictPolicyName = 'strictPolicy';
const strictPolicyDoc = JSON.stringify({
Version: '2012-10-17',
Statement: [{
Effect: 'Allow',
Action: ['s3:PutObject',
's3:GetObject'],
Resource: ['arn:aws:s3:::personalbucket/${aws:username}'],
}],
});
// Create user
function createUser(name) {
const createUserParams = {
UserName: name,
};
iamClient.createUser(createUserParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// createUser(userName);
// createUser(otherUserName);
// Create keys for user
function createKeysForUser(name) {
const createKeysParams = {
UserName: name,
};
iamClient.createAccessKey(createKeysParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// createKeysForUser(userName);
// Create group
function createGroup(name) {
const createGroupParams = {
GroupName: name,
};
iamClient.createGroup(createGroupParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// createGroup(groupName);
// Add user to group
function addUserToGroup(groupName, userName) {
const addUserParams = {
GroupName: groupName,
UserName: userName,
};
iamClient.addUserToGroup(addUserParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// addUserToGroup(groupName, userName);
// Create policy
function createPolicy(policyDoc, policyName) {
const createPolicyParams = {
PolicyDocument: policyDoc,
PolicyName: policyName,
};
iamClient.createPolicy(createPolicyParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// createPolicy(liberalPolicyDoc, liberalPolicyName);
// createPolicy(strictPolicyDoc, strictPolicyName);
// Attach policy to group
function attachPolicy(policyName) {
const attachPolicyParams = {
GroupName: groupName,
PolicyArn: `arn:aws:iam::${accountId}:policy/${policyName}`,
};
iamClient.attachGroupPolicy(attachPolicyParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// attachPolicy(liberalPolicyName);
// attachPolicy(strictPolicyName);
// List attached policies
function listAttachedGroupPolicies() {
const listPolicyParams = {
GroupName: groupName,
};
iamClient.listAttachedGroupPolicies(listPolicyParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// listAttachedGroupPolicies();
// Detach policy from group
function detachGroupPolicy(policyName) {
const detachPolicyParams = {
GroupName: groupName,
PolicyArn: `arn:aws:iam::${accountId}:policy/${policyName}`,
};
iamClient.detachGroupPolicy(detachPolicyParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// detachGroupPolicy(liberalPolicyName);
// detachGroupPolicy(strictPolicyName);
// List users
function listUsers() {
iamClient.listUsers((err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// listUsers();
// Delete user
function deleteUser(name) {
const deleteUserParams = {
UserName: name,
};
iamClient.deleteUser(deleteUserParams, (err, data) => {
if (err) console.log('err:', err);
else console.log('data:', data);
});
}
// deleteUser(userName);
// deleteUser(otherUserName);