forked from llun/le-store-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificates.js
149 lines (133 loc) · 4.34 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
const Promise = require('bluebird')
const path = require('path')
class Certificates {
constructor(store, keypairs, configs) {
this.store = store
this.keypairs = keypairs
this.configs = configs
}
checkAsync({ fullchainPath, privkeyPath, certPath, chainPath }) {
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.getObjectAsync({ Bucket, Key: privkeyPath }),
s3.getObjectAsync({ Bucket, Key: certPath }),
s3.getObjectAsync({ Bucket, Key: chainPath })])
.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
}) {
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.putObjectAsync({ Bucket, Key: certArchive, Body: pems.cert }),
s3.putObjectAsync({ Bucket, Key: certPath, Body: pems.cert }),
s3.putObjectAsync({ Bucket, Key: chainArchive, Body: pems.chain }),
s3.putObjectAsync({ Bucket, Key: chainPath, Body: pems.chain }),
s3.putObjectAsync({ Bucket, Key: fullchainArchive, Body: pems.cert + pems.chain }),
s3.putObjectAsync({ Bucket, Key: fullchainPath, Body: pems.cert + pems.chain }),
s3.putObjectAsync({ Bucket, Key: privkeyArchive, Body: pems.privkey }),
s3.putObjectAsync({ Bucket, Key: privkeyPath, Body: pems.privkey })])
.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 }) {
if (!domainKeyPath) return Promise.reject(new Error('missing options.domainKeyPath'))
return this.keypairs.checkAsync(domainKeyPath, 'pem')
}
setKeypairAsync({ domainKeyPath }, keypair) {
return this.keypairs.setAsync(domainKeyPath, keypair, 'pem')
}
}
module.exports = Certificates