Skip to content

Commit 9601bc0

Browse files
committed
implement suggestions
1 parent 8a60882 commit 9601bc0

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

globals/globals.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515

1616
package globals
1717

18-
// DefaultIndexURL is the default index url
1918
var (
2019
// ArduinoSignaturePubKey is the public key used to verify commands and url sent by the builder
21-
ArduinoSignaturePubKey = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----"
20+
ArduinoSignaturePubKey = `-----BEGIN PUBLIC KEY-----
21+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF
22+
IE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1
23+
ZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1
24+
pFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z
25+
CeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn
26+
2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9
27+
twIDAQAB
28+
-----END PUBLIC KEY-----`
2229
)

main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ func loop() {
284284
if len(*signatureKey) == 0 {
285285
log.Panicf("signature public key cannot be empty")
286286
}
287-
// when a public key is read from the .ini file, the '\n' are escape with an additional '\', we need to replace them with '\n'
288-
signaturePubKey, err := utilities.ParseRsaPublicKey(strings.ReplaceAll(*signatureKey, "\\n", "\n"))
287+
signaturePubKey, err := utilities.ParseRsaPublicKey([]byte(*signatureKey))
289288
if err != nil {
290289
log.Panicf("cannot parse signature key '%s'. %s", *signatureKey, err)
291290
}

main_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestValidSignatureKey(t *testing.T) {
5656

5757
func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
5858
r := gin.New()
59-
r.POST("/", uploadHandler(utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey)))
59+
r.POST("/", uploadHandler(utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
6060
ts := httptest.NewServer(r)
6161

6262
uploadEvilFileName := Upload{
@@ -92,7 +92,7 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
9292

9393
func TestUploadHandlerAgainstBase64WithoutPaddingMustFail(t *testing.T) {
9494
r := gin.New()
95-
r.POST("/", uploadHandler(utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey)))
95+
r.POST("/", uploadHandler(utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
9696
ts := httptest.NewServer(r)
9797
defer ts.Close()
9898

@@ -121,7 +121,7 @@ func TestInstallToolV2(t *testing.T) {
121121
Index := index.Init(indexURL, config.GetDataDir())
122122

123123
r := gin.New()
124-
goa := v2.Server(config.GetDataDir().String(), Index, utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
124+
goa := v2.Server(config.GetDataDir().String(), Index, utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
125125
r.Any("/v2/*path", gin.WrapH(goa))
126126
ts := httptest.NewServer(r)
127127

@@ -215,7 +215,7 @@ func TestInstalledHead(t *testing.T) {
215215
Index := index.Init(indexURL, config.GetDataDir())
216216

217217
r := gin.New()
218-
goa := v2.Server(config.GetDataDir().String(), Index, utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
218+
goa := v2.Server(config.GetDataDir().String(), Index, utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
219219
r.Any("/v2/*path", gin.WrapH(goa))
220220
ts := httptest.NewServer(r)
221221

tools/download_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestDownload(t *testing.T) {
130130
IndexFile: *paths.New("testdata", "test_tool_index.json"),
131131
LastRefresh: time.Now(),
132132
}
133-
testTools := New(tempDirPath, &testIndex, func(msg string) { t.Log(msg) }, utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
133+
testTools := New(tempDirPath, &testIndex, func(msg string) { t.Log(msg) }, utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
134134

135135
for _, tc := range testCases {
136136
t.Run(tc.name+"-"+tc.version, func(t *testing.T) {
@@ -177,7 +177,7 @@ func TestCorruptedInstalled(t *testing.T) {
177177
defer fileJSON.Close()
178178
_, err = fileJSON.Write([]byte("Hello"))
179179
require.NoError(t, err)
180-
testTools := New(tempDirPath, &testIndex, func(msg string) { t.Log(msg) }, utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
180+
testTools := New(tempDirPath, &testIndex, func(msg string) { t.Log(msg) }, utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
181181
// Download the tool
182182
err = testTools.Download("arduino-test", "avrdude", "6.3.0-arduino17", "keep")
183183
require.NoError(t, err)

utilities/utilities.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ func VerifyInput(input string, signature string, pubKey *rsa.PublicKey) error {
141141

142142
// ParseRsaPublicKey parses a public key in PEM format and returns the rsa.PublicKey object.
143143
// Returns an error if the key is invalid.
144-
func ParseRsaPublicKey(key string) (*rsa.PublicKey, error) {
145-
block, _ := pem.Decode([]byte(key))
144+
func ParseRsaPublicKey(key []byte) (*rsa.PublicKey, error) {
145+
block, _ := pem.Decode(key)
146146
if block == nil {
147147
return nil, errors.New("invalid key")
148148
}
@@ -157,7 +157,7 @@ func ParseRsaPublicKey(key string) (*rsa.PublicKey, error) {
157157

158158
// MustParseRsaPublicKey parses a public key in PEM format and returns the rsa.PublicKey object.
159159
// Panics if the key is invalid.
160-
func MustParseRsaPublicKey(key string) *rsa.PublicKey {
160+
func MustParseRsaPublicKey(key []byte) *rsa.PublicKey {
161161
parsedKey, err := ParseRsaPublicKey(key)
162162
if err != nil {
163163
panic(err)

v2/pkgs/tools_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestTools(t *testing.T) {
4747
// Instantiate Index
4848
Index := index.Init(indexURL, config.GetDataDir())
4949

50-
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
50+
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
5151

5252
ctx := context.Background()
5353

@@ -128,7 +128,7 @@ func TestEvilFilename(t *testing.T) {
128128
// Instantiate Index
129129
Index := index.Init(indexURL, config.GetDataDir())
130130

131-
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
131+
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
132132

133133
ctx := context.Background()
134134

@@ -197,7 +197,7 @@ func TestInstalledHead(t *testing.T) {
197197
// Instantiate Index
198198
Index := index.Init(indexURL, config.GetDataDir())
199199

200-
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
200+
service := pkgs.New(Index, tmp, "replace", utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
201201

202202
ctx := context.Background()
203203

@@ -218,7 +218,7 @@ func TestInstall(t *testing.T) {
218218
LastRefresh: time.Now(),
219219
}
220220

221-
tool := pkgs.New(testIndex, tmp, "replace", utilities.MustParseRsaPublicKey(globals.ArduinoSignaturePubKey))
221+
tool := pkgs.New(testIndex, tmp, "replace", utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey)))
222222

223223
ctx := context.Background()
224224

0 commit comments

Comments
 (0)