Skip to content

Commit 02eacd1

Browse files
committed
move certificate.go in it's own package and make functions public
1 parent ac361f7 commit 02eacd1

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

certificates.go renamed to certificates/certificates.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Generate a self-signed X.509 certificate for a TLS server. Outputs to
66
// 'cert.pem' and 'key.pem' and will overwrite existing files.
77

8-
package main
8+
package certificates
99

1010
import (
1111
"crypto/ecdsa"
@@ -134,10 +134,10 @@ func generateSingleCertificate(isCa bool) (*x509.Certificate, error) {
134134
return &template, nil
135135
}
136136

137-
// migrateCertificatesGeneratedWithOldAgentVersions checks if certificates generated
137+
// MigrateCertificatesGeneratedWithOldAgentVersions checks if certificates generated
138138
// with an old version of the Agent needs to be migrated to the current certificates
139139
// directory, and performs the migration if needed.
140-
func migrateCertificatesGeneratedWithOldAgentVersions(certsDir *paths.Path) {
140+
func MigrateCertificatesGeneratedWithOldAgentVersions(certsDir *paths.Path) {
141141
if certsDir.Join("ca.cert.pem").Exist() {
142142
// The new certificates are already set-up, nothing to do
143143
return
@@ -161,7 +161,8 @@ func migrateCertificatesGeneratedWithOldAgentVersions(certsDir *paths.Path) {
161161
}
162162
}
163163

164-
func generateCertificates(certsDir *paths.Path) {
164+
// GenerateCertificates will generate the required certificates useful for a HTTPS connection on localhost
165+
func GenerateCertificates(certsDir *paths.Path) {
165166
certsDir.Join("ca.cert.pem").Remove()
166167
certsDir.Join("ca.key.pem").Remove()
167168
certsDir.Join("cert.pem").Remove()
@@ -260,7 +261,8 @@ func generateCertificates(certsDir *paths.Path) {
260261
}
261262
}
262263

263-
func certHandler(c *gin.Context) {
264+
// CertHandler will expone the certificate (we do not know why this was required)
265+
func CertHandler(c *gin.Context) {
264266
if strings.Contains(c.Request.UserAgent(), "Firefox") {
265267
c.Header("content-type", "application/x-x509-ca-cert")
266268
c.File("ca.cert.cer")
@@ -271,7 +273,8 @@ func certHandler(c *gin.Context) {
271273
})
272274
}
273275

274-
func deleteCertHandler(c *gin.Context) {
276+
// DeleteCertHandler will delete the certificates
277+
func DeleteCertHandler(c *gin.Context) {
275278
DeleteCertificates(config.GetCertificatesDir())
276279
}
277280

hub.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
"strconv"
2727
"strings"
2828

29+
cert "github.com/arduino/arduino-create-agent/certificates"
2930
"github.com/arduino/arduino-create-agent/config"
31+
3032
"github.com/arduino/arduino-create-agent/upload"
3133
log "github.com/sirupsen/logrus"
3234
)
@@ -183,7 +185,7 @@ func checkCmd(m []byte) {
183185
} else if strings.HasPrefix(sl, "downloadtool") {
184186
// Always delete root certificates when we receive a downloadtool command
185187
// Useful if the install procedure was not followed strictly (eg. manually)
186-
DeleteCertificates(config.GetCertificatesDir())
188+
cert.DeleteCertificates(config.GetCertificatesDir())
187189
go func() {
188190
args := strings.Split(s, " ")
189191
var tool, toolVersion, pack, behaviour string

main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
cors "github.com/andela/gin-cors"
34+
cert "github.com/arduino/arduino-create-agent/certificates"
3435
"github.com/arduino/arduino-create-agent/config"
3536
"github.com/arduino/arduino-create-agent/systray"
3637
"github.com/arduino/arduino-create-agent/tools"
@@ -128,11 +129,11 @@ func main() {
128129

129130
// Generate certificates
130131
if *genCert {
131-
generateCertificates(config.GetCertificatesDir())
132+
cert.GenerateCertificates(config.GetCertificatesDir())
132133
os.Exit(0)
133134
}
134135
// Check if certificates made with Agent <=1.2.7 needs to be moved over the new location
135-
migrateCertificatesGeneratedWithOldAgentVersions(config.GetCertificatesDir())
136+
cert.MigrateCertificatesGeneratedWithOldAgentVersions(config.GetCertificatesDir())
136137

137138
// Launch main loop in a goroutine
138139
go loop()
@@ -364,8 +365,8 @@ func loop() {
364365
r.LoadHTMLFiles("templates/nofirefox.html")
365366

366367
r.GET("/", homeHandler)
367-
r.GET("/certificate.crt", certHandler)
368-
r.DELETE("/certificate.crt", deleteCertHandler)
368+
r.GET("/certificate.crt", cert.CertHandler)
369+
r.DELETE("/certificate.crt", cert.DeleteCertHandler)
369370
r.POST("/upload", uploadHandler)
370371
r.GET("/socket.io/", socketHandler)
371372
r.POST("/socket.io/", socketHandler)

0 commit comments

Comments
 (0)