Skip to content

Commit

Permalink
Improving role security
Browse files Browse the repository at this point in the history
  • Loading branch information
zanon-io committed Nov 5, 2016
1 parent e1e1d6b commit 99f48f1
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions create-role/index.js
@@ -1,40 +1,48 @@
const AWS = require('aws-sdk');
const iam = new AWS.IAM();
const sts = new AWS.STS();
const roleName = 'serverless-notifications';

const createRoleParams = {
AssumeRolePolicyDocument: `{
"Version":"2012-10-17",
"Statement":[{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "sts:AssumeRole"
}
]
}`,
RoleName: roleName
};

iam.createRole(createRoleParams, (err, data) => {
// get the account id
sts.getCallerIdentity({}, (err, data) => {
if (err) return console.log(err, err.stack);

const attachPolicyParams = {
PolicyDocument: `{
"Version": "2012-10-17",
"Statement": [{
"Action": ["iot:Connect", "iot:Subscribe", "iot:Publish", "iot:Receive"],
"Resource": "*",
"Effect": "Allow"
}]
const createRoleParams = {
AssumeRolePolicyDocument: `{
"Version":"2012-10-17",
"Statement":[{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${data.Account}:root"
},
"Action": "sts:AssumeRole"
}
]
}`,
PolicyName: roleName,
RoleName: roleName
};

iam.putRolePolicy(attachPolicyParams, (err, data) => {
if (err) console.log(err, err.stack);
else console.log(`Finished creating IoT Role: ${roleName}`);
// create role
iam.createRole(createRoleParams, (err, data) => {
if (err) return console.log(err, err.stack);

const attachPolicyParams = {
PolicyDocument: `{
"Version": "2012-10-17",
"Statement": [{
"Action": ["iot:Connect", "iot:Subscribe", "iot:Publish", "iot:Receive"],
"Resource": "*",
"Effect": "Allow"
}]
}`,
PolicyName: roleName,
RoleName: roleName
};

// add iot policy
iam.putRolePolicy(attachPolicyParams, (err, data) => {
if (err) console.log(err, err.stack);
else console.log(`Finished creating IoT Role: ${roleName}`);
});
});
});

0 comments on commit 99f48f1

Please sign in to comment.