Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
enable per-host auth (#281)
Browse files Browse the repository at this point in the history
* enable per-host auth

Signed-off-by: Jason Hall <[email protected]>

* no log

Signed-off-by: Jason Hall <[email protected]>

---------

Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Jun 2, 2024
1 parent 2ff9aee commit 5edcf7f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
9 changes: 4 additions & 5 deletions pkg/apk/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type APK struct {
cache *cache
ignoreSignatures bool
noSignatureIndexes []string
user, pass string
auth map[string]auth

// filename to owning package, last write wins
installedFiles map[string]*Package
Expand Down Expand Up @@ -94,8 +94,7 @@ func New(options ...Option) (*APK, error) {
cache: opt.cache,
noSignatureIndexes: opt.noSignatureIndexes,
installedFiles: map[string]*Package{},
user: opt.user,
pass: opt.pass,
auth: opt.auth,
}, nil
}

Expand Down Expand Up @@ -414,7 +413,7 @@ func (a *APK) InitKeyring(ctx context.Context, keyFiles, extraKeyFiles []string)
pass, _ := asURL.User.Password()
req.SetBasicAuth(user, pass)
req.URL.User = nil
} else if a.user != "" && a.pass != "" {
} else if a, ok := a.auth[asURL.Host]; ok && a.user != "" && a.pass != "" {
req.SetBasicAuth(a.user, a.pass)
}

Expand Down Expand Up @@ -1053,7 +1052,7 @@ func (a *APK) FetchPackage(ctx context.Context, pkg InstallablePackage) (io.Read
if err != nil {
return nil, err
}
if a.user != "" && a.pass != "" {
if a, ok := a.auth[asURL.Host]; ok && a.user != "" && a.pass != "" {
req.SetBasicAuth(a.user, a.pass)
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/apk/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestInitKeyring(t *testing.T) {
http.FileServer(http.Dir(testPrimaryPkgDir)).ServeHTTP(w, r)
}))
defer s.Close()
host := strings.TrimPrefix(s.URL, "http://")

ctx := context.Background()

Expand All @@ -246,7 +247,7 @@ func TestInitKeyring(t *testing.T) {
err := src.MkdirAll("lib/apk/db", 0o755)
require.NoError(t, err, "unable to mkdir /lib/apk/db")

a, err := New(WithFS(src), WithAuth(testUser, testPass))
a, err := New(WithFS(src), WithAuth(host, testUser, testPass))
require.NoError(t, err, "unable to create APK")
err = a.InitDB(ctx)
require.NoError(t, err)
Expand All @@ -261,7 +262,7 @@ func TestInitKeyring(t *testing.T) {
err := src.MkdirAll("lib/apk/db", 0o755)
require.NoError(t, err, "unable to mkdir /lib/apk/db")

a, err := New(WithFS(src), WithAuth("baduser", "badpass"))
a, err := New(WithFS(src), WithAuth(host, "baduser", "badpass"))
require.NoError(t, err, "unable to create APK")
err = a.InitDB(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -544,6 +545,7 @@ func TestAuth_good(t *testing.T) {
http.FileServer(http.Dir(testPrimaryPkgDir)).ServeHTTP(w, r)
}))
defer s.Close()
host := strings.TrimPrefix(s.URL, "http://")

repo := Repository{URI: s.URL}
repoWithIndex := repo.WithIndex(&APKIndex{Packages: []*Package{&testPkg}})
Expand All @@ -554,7 +556,7 @@ func TestAuth_good(t *testing.T) {
err := src.MkdirAll("lib/apk/db", 0o755)
require.NoError(t, err, "unable to mkdir /lib/apk/db")

a, err := New(WithFS(src), WithAuth(testUser, testPass))
a, err := New(WithFS(src), WithAuth(host, testUser, testPass))
require.NoError(t, err, "unable to create APK")
err = a.InitDB(ctx)
require.NoError(t, err)
Expand All @@ -575,6 +577,7 @@ func TestAuth_bad(t *testing.T) {
http.FileServer(http.Dir(testPrimaryPkgDir)).ServeHTTP(w, r)
}))
defer s.Close()
host := strings.TrimPrefix(s.URL, "http://")

repo := Repository{URI: s.URL}
repoWithIndex := repo.WithIndex(&APKIndex{Packages: []*Package{&testPkg}})
Expand All @@ -585,7 +588,7 @@ func TestAuth_bad(t *testing.T) {
err := src.MkdirAll("lib/apk/db", 0o755)
require.NoError(t, err, "unable to mkdir /lib/apk/db")

a, err := New(WithFS(src), WithAuth("baduser", "badpass"))
a, err := New(WithFS(src), WithAuth(host, "baduser", "badpass"))
require.NoError(t, err, "unable to create APK")
err = a.InitDB(ctx)
require.NoError(t, err)
Expand Down
14 changes: 8 additions & 6 deletions pkg/apk/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func getRepositoryIndex(ctx context.Context, u string, keys map[string][]byte, a
user := asURL.User.Username()
pass, _ := asURL.User.Password()
req.SetBasicAuth(user, pass)
} else if opts.user != "" || opts.pass != "" {
req.SetBasicAuth(opts.user, opts.pass)
} else if a, ok := opts.auth[asURL.Host]; ok && a.user != "" || a.pass != "" {
req.SetBasicAuth(a.user, a.pass)
}

// This will return a body that retries requests using Range requests if Read() hits an error.
Expand Down Expand Up @@ -320,7 +320,7 @@ type indexOpts struct {
ignoreSignatures bool
noSignatureIndexes []string
httpClient *http.Client
user, pass string
auth map[string]auth
}
type IndexOption func(*indexOpts)

Expand All @@ -342,9 +342,11 @@ func WithHTTPClient(c *http.Client) IndexOption {
}
}

func WithIndexAuth(user, pass string) IndexOption {
func WithIndexAuth(domain, user, pass string) IndexOption {
return func(o *indexOpts) {
o.user = user
o.pass = pass
if o.auth == nil {
o.auth = make(map[string]auth)
}
o.auth[domain] = auth{user, pass}
}
}
12 changes: 8 additions & 4 deletions pkg/apk/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type opts struct {
version string
cache *cache
noSignatureIndexes []string
user, pass string
auth map[string]auth
}

type Option func(*opts) error
Expand Down Expand Up @@ -106,10 +106,14 @@ func WithNoSignatureIndexes(noSignatureIndex ...string) Option {
}
}

func WithAuth(user, pass string) Option {
type auth struct{ user, pass string }

func WithAuth(domain, user, pass string) Option {
return func(o *opts) error {
o.user = user
o.pass = pass
if o.auth == nil {
o.auth = make(map[string]auth)
}
o.auth[domain] = auth{user, pass}
return nil
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/apk/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ func (a *APK) GetRepositoryIndexes(ctx context.Context, ignoreSignatures bool) (
if a.cache != nil {
httpClient = a.cache.client(httpClient, true)
}
return GetRepositoryIndexes(ctx, repos, keys, arch,
WithIgnoreSignatures(ignoreSignatures),
opts := []IndexOption{WithIgnoreSignatures(ignoreSignatures),
WithIgnoreSignatureForIndexes(a.noSignatureIndexes...),
WithHTTPClient(httpClient),
WithIndexAuth(a.user, a.pass))
WithHTTPClient(httpClient)}
for domain, auth := range a.auth {
opts = append(opts, WithIndexAuth(domain, auth.user, auth.pass))
}
return GetRepositoryIndexes(ctx, repos, keys, arch, opts...)
}

// PkgResolver resolves packages from a list of indexes.
Expand Down
6 changes: 4 additions & 2 deletions pkg/apk/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ func TestIndexAuth_good(t *testing.T) {
http.FileServer(http.Dir(testPrimaryPkgDir)).ServeHTTP(w, r)
}))
defer s.Close()
host := strings.TrimPrefix(s.URL, "http://")

ctx := context.Background()

a, err := New(WithFS(apkfs.NewMemFS()),
WithAuth(testUser, testPass),
WithAuth(host, testUser, testPass),
WithArch("x86_64"))
require.NoErrorf(t, err, "unable to create APK")
err = a.InitDB(ctx)
Expand All @@ -343,11 +344,12 @@ func TestIndexAuth_bad(t *testing.T) {
http.FileServer(http.Dir(testPrimaryPkgDir)).ServeHTTP(w, r)
}))
defer s.Close()
host := strings.TrimPrefix(s.URL, "http://")

ctx := context.Background()

a, err := New(WithFS(apkfs.NewMemFS()),
WithAuth("baduser", "badpass"),
WithAuth(host, "baduser", "badpass"),
WithArch("x86_64"))
require.NoErrorf(t, err, "unable to create APK")
err = a.InitDB(ctx)
Expand Down

0 comments on commit 5edcf7f

Please sign in to comment.