-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from ivanilves/auth-headers
feat(auth): Set BASIC auth with no respect to remote server
- Loading branch information
Showing
6 changed files
with
175 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package store | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// Store stores BASIC authentication credentials | ||
type Store struct { | ||
logins map[string]*Login | ||
} | ||
|
||
// Login stores username and password for BASIC authentication | ||
type Login struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
// LoadAll parses and loads a list of BASIC authentication strings | ||
func (st *Store) LoadAll(aa []string) error { | ||
logins := make(map[string]*Login, 0) | ||
|
||
for _, a := range aa { | ||
registry, login, err := loadOne(strings.TrimSpace(a)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
logins[registry] = login | ||
} | ||
|
||
st.logins = logins | ||
|
||
return nil | ||
} | ||
|
||
// GetByHostname gets a BASIC auth login for a registry hostname passed | ||
func (st *Store) GetByHostname(registryHostname string) *Login { | ||
login, defined := st.logins[registryHostname] | ||
if !defined { | ||
return nil | ||
} | ||
|
||
return login | ||
} | ||
|
||
// GetByURL gets a BASIC auth login for a registry URL passed | ||
func (st *Store) GetByURL(registryURL string) *Login { | ||
u, _ := url.Parse(registryURL) | ||
|
||
return st.GetByHostname(u.Host) | ||
} | ||
|
||
func loadOne(a string) (string, *Login, error) { | ||
const format = "REGISTRY[:PORT] username:password" | ||
|
||
var formatErr = fmt.Errorf( | ||
"invalid format for BASIC auth (should be: %s)", | ||
format, | ||
) | ||
|
||
ss := strings.SplitN(a, " ", 2) | ||
if len(ss) != 2 { | ||
return "", nil, formatErr | ||
} | ||
|
||
up := strings.SplitN(ss[1], ":", 2) | ||
if len(up) != 2 { | ||
return "", nil, formatErr | ||
} | ||
|
||
registry := ss[0] | ||
username := up[0] | ||
password := up[1] | ||
|
||
if password == "" { | ||
return "", nil, formatErr | ||
} | ||
|
||
return registry, &Login{Username: username, Password: password}, 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,55 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var examples = []string{"localhost:5000 foo:bar", "quay.io quser:qpass"} | ||
|
||
func TestLoadAllValid(t *testing.T) { | ||
var store Store | ||
|
||
err := store.LoadAll(examples) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestLoadAllInvalid(t *testing.T) { | ||
var store Store | ||
|
||
assert.Error(t, store.LoadAll([]string{""})) | ||
assert.Error(t, store.LoadAll([]string{"us.gcr.io"})) | ||
assert.Error(t, store.LoadAll([]string{"us.gcr.io forgotsomething"})) | ||
assert.Error(t, store.LoadAll([]string{"quay.io quser:"})) | ||
assert.Error(t, store.LoadAll([]string{" foo:bar"})) | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
var store Store | ||
|
||
store.LoadAll(examples) | ||
|
||
assert.NotNil(t, store.GetByHostname("localhost:5000")) | ||
assert.NotNil(t, store.GetByHostname("quay.io")) | ||
assert.Nil(t, store.GetByHostname("eu.gcr.io")) | ||
|
||
assert.NotNil(t, store.GetByURL("http://localhost:5000")) | ||
assert.NotNil(t, store.GetByURL("https://quay.io")) | ||
assert.Nil(t, store.GetByURL("https://eu.gcr.io")) | ||
} | ||
|
||
func TestGetValues(t *testing.T) { | ||
var store Store | ||
|
||
store.LoadAll(examples) | ||
|
||
login1 := store.GetByHostname("localhost:5000") | ||
login2 := store.GetByHostname("quay.io") | ||
|
||
assert.Equal(t, login1.Username, "foo") | ||
assert.Equal(t, login1.Password, "bar") | ||
assert.Equal(t, login2.Username, "quser") | ||
assert.Equal(t, login2.Password, "qpass") | ||
} |
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