Skip to content

Commit

Permalink
Adds HTTP Basic Auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznocha committed Aug 23, 2016
1 parent f325cb7 commit 071997d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions cmd/marathon-resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ type (
AppJSON string `json:"app_json"`
Replacements map[string]string `json:"replacements"`
}
authCreds struct {
UserName string `json:"user_name"`
Password string `json:"password"`
}
source struct {
AppID string `json:"app_id"`
URI string `json:"uri"`
AppID string `json:"app_id"`
URI string `json:"uri"`
BasicAuth *authCreds `json:"basic_auth"`
}
version struct {
Ref string `json:"ref"`
Expand Down Expand Up @@ -54,7 +59,7 @@ func main() {
logger.WithError(err).Fatalf("Malformed URI %s", input.Source.URI)
}

// m := newMarathoner(http.DefaultClient, uri)
// m := newMarathoner(&http.Client{}, uri, source.BasicAuth)

switch os.Args[1] {
case check:
Expand Down
6 changes: 5 additions & 1 deletion cmd/marathon-resource/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ type (
marathon struct {
client doer
url *url.URL
auth *authCreds
}
)

func newMarathoner(client doer, uri *url.URL) marathoner {
func newMarathoner(client doer, uri *url.URL, auth *authCreds) marathoner {
return &marathon{client: client, url: uri}
}

Expand All @@ -57,6 +58,9 @@ func (m *marathon) handleReq(
return err
}
req.Header.Set("Content-type", jsonContentType)
if m.auth != nil {
req.SetBasicAuth(m.auth.UserName, m.auth.Password)
}
res, err := m.client.Do(req)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/marathon-resource/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ func Test_newMarathoner(t *testing.T) {
args args
want marathoner
}{
{"Works", args{http.DefaultClient, &url.URL{}}, &marathon{http.DefaultClient, &url.URL{}}},
{"Works", args{http.DefaultClient, &url.URL{}}, &marathon{http.DefaultClient, &url.URL{}, nil}},
}
for _, tt := range tests {
if got := newMarathoner(tt.args.client, tt.args.uri); !reflect.DeepEqual(got, tt.want) {
if got := newMarathoner(tt.args.client, tt.args.uri, nil); !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q. newMarathoner() = %v, want %v", tt.name, got, tt.want)
}
}
Expand Down

0 comments on commit 071997d

Please sign in to comment.