Skip to content

Commit f08f077

Browse files
umbynoscmaglie
authored andcommitted
certs are now created in .arduino-create folder
1 parent cafa3cb commit f08f077

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

certificates.go

+29-24
Original file line numberDiff line numberDiff line change
@@ -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

232237
func 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

243248
const noFirefoxTemplateHTML = `<!DOCTYPE html>

hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func checkCmd(m []byte) {
182182
} else if strings.HasPrefix(sl, "downloadtool") {
183183
// Always delete root certificates when we receive a downloadtool command
184184
// Useful if the install procedure was not followed strictly (eg. manually)
185-
DeleteCertificates()
185+
DeleteCertificates(agentDir)
186186
go func() {
187187
args := strings.Split(s, " ")
188188
var tool, toolVersion, pack, behaviour string

main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func main() {
154154

155155
// Generate certificates
156156
if *genCert {
157-
generateCertificates()
157+
generateCertificates(agentDir)
158158
os.Exit(0)
159159
}
160160

@@ -419,7 +419,7 @@ func loop() {
419419

420420
go func() {
421421
// check if certificates exist; if not, use plain http
422-
if srcDir.Join("cert.pem").NotExist() {
422+
if agentDir.Join("cert.pem").NotExist() {
423423
log.Error("Could not find HTTPS certificate. Using plain HTTP only.")
424424
return
425425
}
@@ -430,7 +430,7 @@ func loop() {
430430
for i < end {
431431
i = i + 1
432432
portSSL = ":" + strconv.Itoa(i)
433-
if err := r.RunTLS(*address+portSSL, srcDir.Join("cert.pem").String(), srcDir.Join("key.pem").String()); err != nil {
433+
if err := r.RunTLS(*address+portSSL, agentDir.Join("cert.pem").String(), agentDir.Join("key.pem").String()); err != nil {
434434
log.Printf("Error trying to bind to port: %v, so exiting...", err)
435435
continue
436436
} else {

0 commit comments

Comments
 (0)