This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
index.js
123 lines (114 loc) · 3.12 KB
/
index.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
/*
* AWS Lambda function to return authorized keys for EC2 SSH access.
* https://github.com/blueimp/aws-lambda
*
* Optional environment variables:
* - group: The IAM group of users authorized for SSH access, defaults to "ssh".
*
* Meant to be used with Amazon API Gateway
*
* Copyright 2017, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*/
'use strict'
// eslint-disable-next-line node/no-unpublished-require
const AWS = require('aws-sdk')
const IAM = new AWS.IAM()
const GROUP = process.env.group || 'ssh'
/**
* Retrievs the group users
*
* @param {string} groupName Group name
* @returns {Promise<Array>} Resolves with the group users
*/
function getGroupUsers(groupName) {
return IAM.getGroup({
GroupName: groupName
})
.promise()
.then(data => data.Users.map(user => user.UserName))
}
/**
* Retrievs the public SSH key IDs of the user
*
* @param {string} userName User name
* @returns {Promise<Array>} Resolves with the public SSH key IDs
*/
function getSSHPublicKeyIDs(userName) {
return IAM.listSSHPublicKeys({
UserName: userName
})
.promise()
.then(data =>
data.SSHPublicKeys.reduce((ids, key) => {
if (key.Status === 'Active') return [...ids, key.SSHPublicKeyId]
return ids
}, [])
)
}
/**
* Retrievs the public SSH key of the user and key
*
* @param {string} userName User name
* @param {string} keyID Key ID
* @returns {Promise<string>} Resolves with the public SSH key
*/
function getSSHPublicKey(userName, keyID) {
return IAM.getSSHPublicKey({
Encoding: 'SSH',
UserName: userName,
SSHPublicKeyId: keyID
})
.promise()
.then(data => data.SSHPublicKey.SSHPublicKeyBody)
}
/**
* Retrievs the public SSH keys of the user
*
* @param {string} userName User name
* @returns {Promise<Array>} Resolves with the public SSH keys
*/
function getSSHPublicKeys(userName) {
return getSSHPublicKeyIDs(userName).then(keyIDs => {
return Promise.all(keyIDs.map(keyID => getSSHPublicKey(userName, keyID)))
})
}
/**
* Retrievs the public SSH keys of the group users
*
* @param {string} group User group
* @returns {Promise<Array>} Resolves with the public SSH keys
*/
function getGroupSSHPublicKeys(group) {
return getGroupUsers(group)
.then(users => {
// eslint-disable-next-line no-console
console.log('Users:', JSON.stringify(users))
return Promise.all(users.map(userName => getSSHPublicKeys(userName)))
})
.then(keySets =>
keySets.reduce((keys, keySet) => {
return keys.concat(keySet)
}, [])
)
}
exports.handler = (event, context, callback) => {
// eslint-disable-next-line no-console
console.log('Event:', JSON.stringify(event))
// eslint-disable-next-line no-console
console.log('Group:', GROUP)
getGroupSSHPublicKeys(GROUP)
.then(keys => {
// eslint-disable-next-line no-console
console.log('Keys count:', keys.length)
callback(null, {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: keys.join('\n') + '\n'
})
})
.catch(err => callback(err))
}