Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case Insensitive Match #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions plugin/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ func match(name string, patterns []string) bool {
}
return false
}

func matchCaseInsensitive(name string, params map[string]string) (string, bool) {
for key, value := range params {
if strings.ToLower(key) == strings.ToLower(name) {
return value, true
}
}
return "", false
}
2 changes: 1 addition & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *plugin) Find(ctx context.Context, req *secret.Request) (*drone.Secret,
if err != nil {
return nil, errors.New("secret not found")
}
value, ok := params[name]
value, ok := matchCaseInsensitive(name, params)
if !ok {
return nil, errors.New("secret key not found")
}
Expand Down
42 changes: 42 additions & 0 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,45 @@ func TestPlugin_KeyNotFound(t *testing.T) {
return
}
}

func TestPlugin_FindMismatchCase(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
out, _ := ioutil.ReadFile("testdata/uppercase_secret.json")
w.Write(out)
}))
defer ts.Close()

client, _ := api.NewClient(&api.Config{
Address: ts.URL,
MaxRetries: 1,
})

req := &secret.Request{
Path: "secret/docker",
Name: "USERNAME",
Build: drone.Build{
Event: "push",
Target: "master",
},
Repo: drone.Repo{
Slug: "octocat/hello-world",
},
}
plugin := New(client)
got, err := plugin.Find(noContext, req)
if err != nil {
t.Error(err)
return
}

want := &drone.Secret{
Name: "USERNAME",
Data: "david",
Pull: true,
Fork: true,
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
return
}
}
13 changes: 13 additions & 0 deletions plugin/testdata/uppercase_secret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": {
"Username": "david",
"password": "BnQw&XDWgaEeT9XGTT29",
"X-Drone-Repos":"octocat/*",
"X-Drone-Events":"tag,push",
"X-Drone-Branches":"master",
"timestmap": 2764800
},
"lease_duration": 2764800,
"renewable": false,
"request_id": "5e246671-ec05-6fc8-9f93-4fe4512f34ab"
}