-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional service authentication (#54)
* update docs * set the "x-api-key" header in all outgoing http requests to services * parse optional api secret from environment variable * fix broken tests & improve service config string validation
- Loading branch information
Showing
11 changed files
with
242 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var alphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]*$`) | ||
|
||
func parseServices(input string) ([]Service, error) { | ||
if input == "" { | ||
return []Service{}, nil | ||
} | ||
|
||
serializedServices := strings.Split(input, ",") | ||
services := make([]Service, 0, len(serializedServices)) | ||
|
||
for _, service := range serializedServices { | ||
majorParts := strings.Split(service, "@") | ||
if len(majorParts) != 2 { | ||
return nil, fmt.Errorf( | ||
"expected only one occurrence of '@', got %d in %s", | ||
len(majorParts)-1, | ||
service, | ||
) | ||
} | ||
|
||
minorParts := strings.Split(majorParts[0], ":") | ||
if len(minorParts) > 2 { | ||
return nil, fmt.Errorf( | ||
"expected at most one occurrence of ':', got %d in %s", | ||
len(minorParts)-1, | ||
service, | ||
) | ||
} | ||
|
||
if !alphaNumeric.MatchString(minorParts[0]) { | ||
return nil, fmt.Errorf("unexpected non-alphanumeric characters in %s", service) | ||
} | ||
|
||
secret := "" | ||
if len(minorParts) > 1 { | ||
if !alphaNumeric.MatchString(minorParts[1]) { | ||
return nil, fmt.Errorf("unexpected non-alphanumeric characters in %s", service) | ||
} | ||
secret = minorParts[1] | ||
} | ||
|
||
if !strings.HasPrefix(majorParts[1], "http") { | ||
return nil, fmt.Errorf("service endpoint URL does not start with 'http' in %s", service) | ||
} | ||
|
||
services = append(services, Service{ | ||
Label: minorParts[0], | ||
Secret: secret, | ||
Endpoint: majorParts[1], | ||
}) | ||
} | ||
|
||
return services, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestParseServices(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
input string | ||
isValid bool | ||
services []Service | ||
}{ | ||
{ | ||
name: "simple service without secret", | ||
input: "label@http://example.com", | ||
isValid: true, | ||
services: []Service{{Label: "label", Secret: "", Endpoint: "http://example.com"}}, | ||
}, | ||
{ | ||
name: "simple service with secret", | ||
input: "label:secret@http://example.com", | ||
isValid: true, | ||
services: []Service{{Label: "label", Secret: "secret", Endpoint: "http://example.com"}}, | ||
}, | ||
{ | ||
name: "service with secret containing numbers and uppercase letters", | ||
input: "label:aBcD1230XyZ@http://example.com", | ||
isValid: true, | ||
services: []Service{{Label: "label", Secret: "aBcD1230XyZ", Endpoint: "http://example.com"}}, | ||
}, | ||
{ | ||
name: "endpoint URL does not start with 'http'", | ||
input: "[email protected]", | ||
isValid: false, | ||
}, | ||
{ | ||
name: "no '@' delimiter", | ||
input: "label-http://example.com", | ||
isValid: false, | ||
}, | ||
{ | ||
name: "multiple '@' delimiters", | ||
input: "label@[email protected]", | ||
isValid: false, | ||
}, | ||
{ | ||
name: "multiple ':' delimiters", | ||
input: "label:super:secret:password@http://example.com", | ||
isValid: false, | ||
}, | ||
{ | ||
name: "non-alphanumeric characters in label", | ||
input: "definitely$$not_*?a+Trello.Label@http://example.com", | ||
isValid: false, | ||
}, | ||
{ | ||
name: "non-alphanumeric characters in secret", | ||
input: "label:-?_*@http://example.com", | ||
isValid: false, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
services, err := parseServices(tc.input) | ||
if tc.isValid != (err == nil) { | ||
t.Errorf("expected valid output? %v. Got error: %s", tc.isValid, err) | ||
return | ||
} | ||
if diff := cmp.Diff(services, tc.services); diff != "" { | ||
t.Errorf("services diff: %s", diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.