-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcertificates.js
183 lines (162 loc) · 5.13 KB
/
certificates.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// @flow
const Promise = require('bluebird')
const path = require('path')
/*::
import type Store from './store'
import type KeyPairs, { KeyPair } from './keypairs'
import type Configs, { ConfigsArgs } from './configs'
type Pems = {
cert: string,
chain: string,
privkey: string
}
type DomainArgs = {
domainKeyPath: string
}
type CertificatePathsArgs = {
fullchainPath: string,
privkeyPath: string,
certPath: string,
chainPath: string
}
type CertificateArgs = ConfigsArgs & DomainArgs & {
pems: Pems,
archiveDir: string
}
*/
class Certificates {
/*::
store: Store
keypairs: KeyPairs
configs: Configs
*/
constructor(store/*:Store*/, keypairs/*:KeyPairs*/, configs/*:Configs*/) {
this.store = store
this.keypairs = keypairs
this.configs = configs
}
checkAsync({ fullchainPath, privkeyPath, certPath, chainPath }/*:CertificatePathsArgs*/)/*:Promise<?Pems>*/ {
if (!fullchainPath || !privkeyPath || !certPath || !chainPath) {
return Promise.reject(new Error('missing one or more of privkeyPath, fullchainPath, certPath, chainPath from options'))
}
const { s3, options } = this.store
const Bucket = options.S3.bucketName
return Promise.all([
s3.getObject({ Bucket, Key: privkeyPath }).promise(),
s3.getObject({ Bucket, Key: certPath }).promise(),
s3.getObject({ Bucket, Key: chainPath }).promise()])
.then(result => ({
privkey: result[0].Body.toString('ascii'),
cert: result[1].Body.toString('ascii'),
chain: result[2].Body.toString('ascii')
}))
.catch(err => {
if (options.debug) {
console.error('[le-store-s3] certificates.check')
console.error(err.stack)
}
return null
})
}
setAsync({
pems,
liveDir,
configDir,
archiveDir,
domains,
certPath,
fullchainPath,
chainPath,
privkeyPath,
domainPrivateKeyPath,
domainKeyPath,
renewalPath,
account,
email,
agreeTos,
server,
acmeDiscoveryUrl,
http01Port,
rsaKeySize
}/*:CertificateArgs*/)/*:Promise<Pems>*/ {
const { s3, options } = this.store
const configs = this.configs
const Bucket = options.S3.bucketName
return this.configs.getAsync({
liveDir,
configDir,
domains,
certPath,
fullchainPath,
chainPath,
privkeyPath,
domainPrivateKeyPath,
renewalPath,
account,
email,
agreeTos,
server,
acmeDiscoveryUrl,
http01Port,
rsaKeySize
})
.then(pyobj => {
pyobj.checkpoints = parseInt(pyobj.checkpoints, 10) || 0
liveDir = liveDir || path.join(configDir, 'live', domains[0])
certPath = certPath || pyobj.cert || path.join(liveDir, 'cert.pem')
fullchainPath = fullchainPath || pyobj.fullchain || path.join(liveDir, 'fullchain.pem')
chainPath = chainPath || pyobj.chain || path.join(liveDir, 'chain.pem')
privkeyPath = privkeyPath || pyobj.privkey || domainKeyPath || path.join(liveDir, 'privkey.pem')
archiveDir = archiveDir || path.join(configDir, 'archive', domains[0])
const checkpoints = pyobj.checkpoints.toString()
const certArchive = path.join(archiveDir, `cert${checkpoints}.pem`)
const fullchainArchive = path.join(archiveDir, `fullchain${checkpoints}.pem`)
const chainArchive = path.join(archiveDir, `chain${checkpoints}.pem`)
const privkeyArchive = path.join(archiveDir, `privkey${checkpoints}.pem`)
return Promise.all([
s3.putObject({ Bucket, Key: certArchive, Body: pems.cert }).promise(),
s3.putObject({ Bucket, Key: certPath, Body: pems.cert }).promise(),
s3.putObject({ Bucket, Key: chainArchive, Body: pems.chain }).promise(),
s3.putObject({ Bucket, Key: chainPath, Body: pems.chain }).promise(),
s3.putObject({ Bucket, Key: fullchainArchive, Body: pems.cert + pems.chain }).promise(),
s3.putObject({ Bucket, Key: fullchainPath, Body: pems.cert + pems.chain }).promise(),
s3.putObject({ Bucket, Key: privkeyArchive, Body: pems.privkey }).promise(),
s3.putObject({ Bucket, Key: privkeyPath, Body: pems.privkey }).promise()])
.then(() => {
pyobj.checkpoints += 1
return configs.writeRenewalConfig({
pyobj,
liveDir,
configDir,
domains,
certPath,
fullchainPath,
chainPath,
privkeyPath,
domainPrivateKeyPath,
account,
email,
agreeTos,
server,
acmeDiscoveryUrl,
http01Port,
rsaKeySize,
renewalPath
})
})
.then(() => ({
privkey: pems.privkey,
cert: pems.cert,
chain: pems.chain
}))
})
}
checkKeypairAsync({ domainKeyPath }/*:DomainArgs*/)/*:Promise<?KeyPair>*/ {
if (!domainKeyPath) return Promise.reject(new Error('missing options.domainKeyPath'))
return this.keypairs.checkAsync(domainKeyPath, 'pem')
}
setKeypairAsync({ domainKeyPath }/*:DomainArgs*/, keypair/*:KeyPair*/)/*:Promise<KeyPair>*/ {
return this.keypairs.setAsync(domainKeyPath, keypair, 'pem')
}
}
module.exports = Certificates