-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement IP cleanup when job is completed
- Loading branch information
Showing
11 changed files
with
127 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const publicIp = require('public-ip'); | ||
|
||
const config = require('./config'); | ||
|
||
async function run() { | ||
try { | ||
const myPublicIp = await publicIp.v4(); | ||
|
||
for (const groupId of config.groupIds) { | ||
await config.ec2.revokeSecurityGroupIngress({ | ||
GroupId: groupId, | ||
CidrIp: `${myPublicIp}/32`, | ||
IpProtocol: 'tcp', | ||
FromPort: config.port, | ||
ToPort: config.port, | ||
}).promise(); | ||
} | ||
|
||
console.log(`The IP ${myPublicIp} is removed`); | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const core = require('@actions/core'); | ||
const AWS = require('aws-sdk/global'); | ||
const EC2 = require('aws-sdk/clients/ec2'); | ||
|
||
const region = core.getInput('aws-region', { required: true }); | ||
const accessKeyId = core.getInput('aws-access-key-id', { required: true }); | ||
const secretAccessKey = core.getInput('aws-secret-access-key', { required: true }); | ||
const groupIds = core | ||
.getInput('aws-security-group-id', { required: true }) | ||
.split(',') | ||
.map(item => item.trim()); | ||
const port = parseInt(core.getInput('port', { required: false })); | ||
const description = core.getInput('description', { required: false }); | ||
|
||
AWS.config.update({ | ||
region, | ||
accessKeyId, | ||
secretAccessKey, | ||
}); | ||
const ec2 = new EC2(); | ||
|
||
module.exports = { | ||
region, | ||
accessKeyId, | ||
secretAccessKey, | ||
groupIds, | ||
port, | ||
description, | ||
ec2, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const core = require('@actions/core'); | ||
const publicIp = require('public-ip'); | ||
|
||
const config = require('./config'); | ||
|
||
async function run() { | ||
try { | ||
const result = await config.ec2.describeSecurityGroups({ | ||
GroupIds: config.groupIds, | ||
}).promise(); | ||
|
||
for (const group of result.SecurityGroups) { | ||
const ruleByPort = group.IpPermissions | ||
.find(permission => permission.FromPort === config.port); | ||
|
||
if (ruleByPort) { | ||
const ipByDesc = ruleByPort.IpRanges | ||
.find(ip => ip.Description === config.description); | ||
|
||
if (ipByDesc) { | ||
await config.ec2.revokeSecurityGroupIngress({ | ||
GroupId: group.GroupId, | ||
CidrIp: ipByDesc.CidrIp, | ||
IpProtocol: 'tcp', | ||
FromPort: config.port, | ||
ToPort: config.port, | ||
}).promise(); | ||
} | ||
} | ||
|
||
const myPublicIp = await publicIp.v4(); | ||
await config.ec2.authorizeSecurityGroupIngress({ | ||
GroupId: group.GroupId, | ||
IpPermissions: [{ | ||
IpProtocol: 'tcp', | ||
FromPort: config.port, | ||
ToPort: config.port, | ||
IpRanges: [{ | ||
CidrIp: `${myPublicIp}/32`, | ||
Description: config.description, | ||
}], | ||
}] | ||
}).promise(); | ||
|
||
console.log(`The IP ${myPublicIp} is added`); | ||
} | ||
|
||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
run(); |