Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76800b3

Browse files
christoph-zededarene
authored andcommitted
pillar/collectinfo: use IsToken* from zedUpload
this fixes a bug in IsToken68 that disallowed strings ending with '==' Signed-off-by: Christoph Ostarek <[email protected]>
1 parent 356ac80 commit 76800b3

2 files changed

Lines changed: 3 additions & 123 deletions

File tree

pkg/pillar/cmd/collectinfo/collectinfo.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"net/url"
1111
"path"
1212
"reflect"
13-
"regexp"
1413
"strings"
1514
"time"
1615

1716
zconfig "github.com/lf-edge/eve-api/go/config"
17+
"github.com/lf-edge/eve-libs/zedUpload"
1818
"github.com/lf-edge/eve/pkg/pillar/agentbase"
1919
"github.com/lf-edge/eve/pkg/pillar/base"
2020
"github.com/lf-edge/eve/pkg/pillar/cipher"
@@ -80,20 +80,6 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar
8080
return 0
8181
}
8282

83-
func isToken(str string) bool {
84-
// https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6
85-
rex := regexp.MustCompile(`^[a-zA-Z0-9-._~+/!#\$%&'\*\+^` + "`" + `|']*$`)
86-
87-
return rex.MatchString(str)
88-
}
89-
90-
func isToken68(str string) bool {
91-
// https://datatracker.ietf.org/doc/html/rfc7235#appendix-C
92-
rex := regexp.MustCompile(`^[a-zA-Z0-9-._~+/]*=?$`)
93-
94-
return rex.MatchString(str)
95-
}
96-
9783
type uploadInfo struct {
9884
url string
9985
// authMethod can be f.e. Bearer, Basic, NTLM
@@ -171,10 +157,10 @@ func (ci *collectInfo) retrieveDSUploadInfo() (uploadInfo, error) {
171157
return uploadInfo{}, fmt.Errorf("no datastore with upload URL found")
172158
}
173159

174-
if !isToken68(apiPassword) {
160+
if !zedUpload.IsToken68(apiPassword) {
175161
return uploadInfo{}, fmt.Errorf("DsPassword is not token68-conform")
176162
}
177-
if !isToken(apiKey) {
163+
if !zedUpload.IsToken(apiKey) {
178164
return uploadInfo{}, fmt.Errorf("DsAPIKey is not token-conform")
179165
}
180166

pkg/pillar/cmd/collectinfo/collectinfo_test.go

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -114,112 +114,6 @@ func TestCollectInfoRetrieveDSUploadInfo(t *testing.T) {
114114
}
115115
}
116116

117-
func TestIsToken(t *testing.T) {
118-
tests := []struct {
119-
name string // description of this test case
120-
// Named input parameters for target function.
121-
str string
122-
want bool
123-
}{
124-
{
125-
name: "NTLM",
126-
str: "NTLM",
127-
want: true,
128-
},
129-
{
130-
name: "Bearer",
131-
str: "Bearer",
132-
want: true,
133-
},
134-
{
135-
name: "Basic",
136-
str: "Basic",
137-
want: true,
138-
},
139-
{
140-
name: "Spaces",
141-
str: " ",
142-
want: false,
143-
},
144-
{
145-
name: "Empty",
146-
str: "",
147-
want: true,
148-
},
149-
{
150-
name: "Newline",
151-
str: "\n",
152-
want: false,
153-
},
154-
{
155-
name: "Colon",
156-
str: ":",
157-
want: false,
158-
},
159-
{
160-
name: "Equal sign",
161-
str: "=",
162-
want: false,
163-
},
164-
}
165-
for _, tt := range tests {
166-
t.Run(tt.name, func(t *testing.T) {
167-
got := isToken(tt.str)
168-
if got != tt.want {
169-
t.Errorf("isToken() = %v, want %v", got, tt.want)
170-
}
171-
})
172-
}
173-
}
174-
175-
func TestIsToken68(t *testing.T) {
176-
tests := []struct {
177-
name string // description of this test case
178-
// Named input parameters for target function.
179-
str string
180-
want bool
181-
}{
182-
{
183-
name: "Spaces",
184-
str: " ",
185-
want: false,
186-
},
187-
{
188-
name: "Empty",
189-
str: "",
190-
want: true,
191-
},
192-
{
193-
name: "Newline",
194-
str: "\n",
195-
want: false,
196-
},
197-
{
198-
name: "Colon",
199-
str: ":",
200-
want: false,
201-
},
202-
{
203-
name: "Trailing equal sign",
204-
str: "=",
205-
want: true,
206-
},
207-
{
208-
name: "Non-Trailing equal sign",
209-
str: "=A",
210-
want: false,
211-
},
212-
}
213-
for _, tt := range tests {
214-
t.Run(tt.name, func(t *testing.T) {
215-
got := isToken68(tt.str)
216-
if got != tt.want {
217-
t.Errorf("isToken68() = %v, want %v", got, tt.want)
218-
}
219-
})
220-
}
221-
}
222-
223117
func TestCreateHttpURLFromDatastore(t *testing.T) {
224118
tests := []struct {
225119
name string // description of this test case

0 commit comments

Comments
 (0)