Skip to content

Commit 6d13687

Browse files
committed
Merge PR drone#9: Case insensitive match
2 parents 17add00 + dc339cb commit 6d13687

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

plugin/match.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ func match(name string, patterns []string) bool {
2323
}
2424
return false
2525
}
26+
27+
func matchCaseInsensitive(name string, params map[string]string) (string, bool) {
28+
for key, value := range params {
29+
if strings.ToLower(key) == strings.ToLower(name) {
30+
return value, true
31+
}
32+
}
33+
return "", false
34+
}

plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (p *plugin) Find(ctx context.Context, req *secret.Request) (*drone.Secret,
4646
if err != nil {
4747
return nil, errors.New("secret not found")
4848
}
49-
value, ok := params[name]
49+
value, ok := matchCaseInsensitive(name, params)
5050
if !ok {
5151
return nil, errors.New("secret key not found")
5252
}

plugin/plugin_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,45 @@ func Test_rewritePath(t *testing.T) {
308308
})
309309
}
310310
}
311+
312+
func TestPlugin_FindMismatchCase(t *testing.T) {
313+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
314+
out, _ := ioutil.ReadFile("testdata/uppercase_secret.json")
315+
w.Write(out)
316+
}))
317+
defer ts.Close()
318+
319+
client, _ := api.NewClient(&api.Config{
320+
Address: ts.URL,
321+
MaxRetries: 1,
322+
})
323+
324+
req := &secret.Request{
325+
Path: "secret/docker",
326+
Name: "USERNAME",
327+
Build: drone.Build{
328+
Event: "push",
329+
Target: "master",
330+
},
331+
Repo: drone.Repo{
332+
Slug: "octocat/hello-world",
333+
},
334+
}
335+
plugin := New(client)
336+
got, err := plugin.Find(noContext, req)
337+
if err != nil {
338+
t.Error(err)
339+
return
340+
}
341+
342+
want := &drone.Secret{
343+
Name: "USERNAME",
344+
Data: "david",
345+
Pull: true,
346+
Fork: true,
347+
}
348+
if diff := cmp.Diff(got, want); diff != "" {
349+
t.Errorf(diff)
350+
return
351+
}
352+
}

plugin/testdata/uppercase_secret.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": {
3+
"Username": "david",
4+
"password": "BnQw&XDWgaEeT9XGTT29",
5+
"X-Drone-Repos":"octocat/*",
6+
"X-Drone-Events":"tag,push",
7+
"X-Drone-Branches":"master",
8+
"timestmap": 2764800
9+
},
10+
"lease_duration": 2764800,
11+
"renewable": false,
12+
"request_id": "5e246671-ec05-6fc8-9f93-4fe4512f34ab"
13+
}

0 commit comments

Comments
 (0)