@@ -24,6 +24,7 @@ import (
2424 "text/template"
2525 "time"
2626
27+ "github.com/arduino/go-paths-helper"
2728 "github.com/gin-gonic/gin"
2829 log "github.com/sirupsen/logrus"
2930)
@@ -133,28 +134,27 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
133134 return & template , nil
134135}
135136
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 ()
142142
143143 // Create the key for the certification authority
144144 caKey , err := generateKey ("P256" )
145145 if err != nil {
146146 log .Error (err .Error ())
147147 os .Exit (1 )
148148 }
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 )
151151 if err != nil {
152152 log .Error (err .Error ())
153153 os .Exit (1 )
154154 }
155155 pem .Encode (keyOut , pemBlockForKey (caKey ))
156156 keyOut .Close ()
157- log .Println ("written ca.key.pem" )
157+ log .Printf ("written %s" , keyOutPath )
158158
159159 // Create the certification authority
160160 caTemplate , err := generateSingleCertificate (true )
@@ -166,17 +166,19 @@ func generateCertificates() {
166166
167167 derBytes , _ := x509 .CreateCertificate (rand .Reader , caTemplate , caTemplate , publicKey (caKey ), caKey )
168168
169- certOut , err := os .Create ("ca.cert.pem" )
169+ certOutPath := path .Join ("ca.cert.pem" ).String ()
170+ certOut , err := os .Create (certOutPath )
170171 if err != nil {
171172 log .Error (err .Error ())
172173 os .Exit (1 )
173174 }
174175 pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
175176 certOut .Close ()
176- log .Print ("written ca.cert.pem" )
177+ log .Printf ("written %s" , certOutPath )
177178
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 )
180182
181183 // Create the key for the final certificate
182184 key , err := generateKey ("P256" )
@@ -185,14 +187,15 @@ func generateCertificates() {
185187 os .Exit (1 )
186188 }
187189
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 )
189192 if err != nil {
190193 log .Error (err .Error ())
191194 os .Exit (1 )
192195 }
193196 pem .Encode (keyOut , pemBlockForKey (key ))
194197 keyOut .Close ()
195- log .Println ("written key.pem" )
198+ log .Printf ("written %s" , keyOutPath )
196199
197200 // Create the final certificate
198201 template , err := generateSingleCertificate (false )
@@ -204,17 +207,19 @@ func generateCertificates() {
204207
205208 derBytes , _ = x509 .CreateCertificate (rand .Reader , template , caTemplate , publicKey (key ), caKey )
206209
207- certOut , err = os .Create ("cert.pem" )
210+ certOutPath = path .Join ("cert.pem" ).String ()
211+ certOut , err = os .Create (certOutPath )
208212 if err != nil {
209213 log .Error (err .Error ())
210214 os .Exit (1 )
211215 }
212216 pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : derBytes })
213217 certOut .Close ()
214- log .Print ("written cert.pem" )
218+ log .Printf ("written %s" , certOutPath )
215219
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 )
218223
219224}
220225
@@ -230,14 +235,14 @@ func certHandler(c *gin.Context) {
230235}
231236
232237func deleteCertHandler (c * gin.Context ) {
233- DeleteCertificates ()
238+ DeleteCertificates (agentDir )
234239}
235240
236241// 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 ( )
241246}
242247
243248const noFirefoxTemplateHTML = `<!DOCTYPE html>
0 commit comments