@@ -24,6 +24,7 @@ import (
24
24
"text/template"
25
25
"time"
26
26
27
+ "github.com/arduino/go-paths-helper"
27
28
"github.com/gin-gonic/gin"
28
29
log "github.com/sirupsen/logrus"
29
30
)
@@ -133,28 +134,27 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
133
134
return & template , nil
134
135
}
135
136
136
- func generateCertificates () {
137
-
138
- os .Remove ("ca.cert.pem" )
139
- os .Remove ("ca.key.pem" )
140
- os .Remove ("cert.pem" )
141
- os .Remove ("key.pem" )
137
+ func generateCertificates (path * paths.Path ) {
138
+ path .Join ("ca.cert.pem" ).Remove ()
139
+ path .Join ("ca.key.pem" ).Remove ()
140
+ path .Join ("cert.pem" ).Remove ()
141
+ path .Join ("key.pem" ).Remove ()
142
142
143
143
// Create the key for the certification authority
144
144
caKey , err := generateKey ("P256" )
145
145
if err != nil {
146
146
log .Error (err .Error ())
147
147
os .Exit (1 )
148
148
}
149
-
150
- keyOut , err := os .OpenFile ("ca.key.pem" , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
149
+ keyOutPath := path . Join ( "ca.key.pem" ). String ()
150
+ keyOut , err := os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
151
151
if err != nil {
152
152
log .Error (err .Error ())
153
153
os .Exit (1 )
154
154
}
155
155
pem .Encode (keyOut , pemBlockForKey (caKey ))
156
156
keyOut .Close ()
157
- log .Println ("written ca.key.pem" )
157
+ log .Printf ("written %s" , keyOutPath )
158
158
159
159
// Create the certification authority
160
160
caTemplate , err := generateSingleCertificate (true )
@@ -166,17 +166,19 @@ func generateCertificates() {
166
166
167
167
derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168
168
169
- certOut , err := os .Create ("ca.cert.pem" )
169
+ certOutPath := path .Join ("ca.cert.pem" ).String ()
170
+ certOut , err := os .Create (certOutPath )
170
171
if err != nil {
171
172
log .Error (err .Error ())
172
173
os .Exit (1 )
173
174
}
174
175
pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
175
176
certOut .Close ()
176
- log .Print ("written ca.cert.pem" )
177
+ log .Printf ("written %s" , certOutPath )
177
178
178
- ioutil .WriteFile ("ca.cert.cer" , derBytes , 0644 )
179
- log .Print ("written ca.cert.cer" )
179
+ filePath := path .Join ("ca.cert.cer" ).String ()
180
+ ioutil .WriteFile (filePath , derBytes , 0644 )
181
+ log .Printf ("written %s" , filePath )
180
182
181
183
// Create the key for the final certificate
182
184
key , err := generateKey ("P256" )
@@ -185,14 +187,15 @@ func generateCertificates() {
185
187
os .Exit (1 )
186
188
}
187
189
188
- keyOut , err = os .OpenFile ("key.pem" , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
190
+ keyOutPath = path .Join ("key.pem" ).String ()
191
+ keyOut , err = os .OpenFile (keyOutPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
189
192
if err != nil {
190
193
log .Error (err .Error ())
191
194
os .Exit (1 )
192
195
}
193
196
pem .Encode (keyOut , pemBlockForKey (key ))
194
197
keyOut .Close ()
195
- log .Println ("written key.pem" )
198
+ log .Printf ("written %s" , keyOutPath )
196
199
197
200
// Create the final certificate
198
201
template , err := generateSingleCertificate (false )
@@ -204,17 +207,19 @@ func generateCertificates() {
204
207
205
208
derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
206
209
207
- certOut , err = os .Create ("cert.pem" )
210
+ certOutPath = path .Join ("cert.pem" ).String ()
211
+ certOut , err = os .Create (certOutPath )
208
212
if err != nil {
209
213
log .Error (err .Error ())
210
214
os .Exit (1 )
211
215
}
212
216
pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
213
217
certOut .Close ()
214
- log .Print ("written cert.pem" )
218
+ log .Printf ("written %s" , certOutPath )
215
219
216
- ioutil .WriteFile ("cert.cer" , derBytes , 0644 )
217
- log .Print ("written cert.cer" )
220
+ certPath := path .Join ("cert.cer" ).String ()
221
+ ioutil .WriteFile (certPath , derBytes , 0644 )
222
+ log .Printf ("written %s" , certPath )
218
223
219
224
}
220
225
@@ -230,14 +235,14 @@ func certHandler(c *gin.Context) {
230
235
}
231
236
232
237
func deleteCertHandler (c * gin.Context ) {
233
- DeleteCertificates ()
238
+ DeleteCertificates (agentDir )
234
239
}
235
240
236
241
// DeleteCertificates will delete the certificates
237
- func DeleteCertificates () {
238
- os . Remove ("ca.cert.pem" )
239
- os . Remove ("ca.cert.cer" )
240
- os . Remove ("ca.key.pem" )
242
+ func DeleteCertificates (path * paths. Path ) {
243
+ path . Join ("ca.cert.pem" ). Remove ( )
244
+ path . Join ("ca.cert.cer" ). Remove ( )
245
+ path . Join ("ca.key.pem" ). Remove ( )
241
246
}
242
247
243
248
const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments