forked from bendrucker/aws-sdk-js-v3-rds-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.31 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
import { defaultProvider } from '@aws-sdk/credential-provider-node'
import { HttpRequest } from '@aws-sdk/protocol-http'
import { SignatureV4 } from '@aws-sdk/signature-v4'
import { Hash } from '@aws-sdk/hash-node'
import { formatUrl } from '@aws-sdk/util-format-url'
const signing = {
service: 'rds-db',
protocol: 'https'
}
export class Signer {
constructor ({ credentials, hostname, port, region, username } = {}) {
this.credentials = credentials || defaultProvider()
this.hostname = hostname
this.port = port
this.region = region
this.username = username
}
async getAuthToken ({
hostname = this.hostname,
port = this.port,
username = this.username,
region = this.region,
credentials = this.credentials
} = {}) {
const signer = new SignatureV4({
service: 'rds-db',
region,
credentials,
sha256: Hash.bind(null, 'sha256')
})
const request = new HttpRequest({
method: 'GET',
protocol: signing.protocol,
hostname,
port,
query: {
Action: 'connect',
DBUser: username
},
headers: {
host: `${hostname}:${port}`
}
})
const presigned = await signer.presign(request, {
expiresIn: 900
})
return formatUrl(presigned).replace(`${presigned.protocol}//`, '')
}
}