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

feat: support reading auth from docker config #194

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
5 changes: 3 additions & 2 deletions misc/config/config.estargz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ provider:
hub.harbor.com:
# base64 encoded `<robot-name>:<robot-secret>` for robot
# account created in harbor
auth: YTpiCg==
# auth: YTpiCg==
# skip verifying server certs for HTTPS source registry
insecure: false
webhook:
# webhook request auth header configured in harbor
auth_header: header
localhost:
Desiki-high marked this conversation as resolved.
Show resolved Hide resolved
auth: YWRtaW46SGFyYm9yMTIzNDU=
# If auth is not provided, it will attempt to read from docker config
# auth: YWRtaW46SGFyYm9yMTIzNDU=
# work directory of acceld
work_dir: /tmp
gcpolicy:
Expand Down
5 changes: 3 additions & 2 deletions misc/config/config.nydus.ref.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ provider:
hub.harbor.com:
# base64 encoded `<robot-name>:<robot-secret>` for robot
# account created in harbor
auth: YTpiCg==
# auth: YTpiCg==
# skip verifying server certs for HTTPS source registry
insecure: false
webhook:
# webhook request auth header configured in harbor
auth_header: header
localhost:
auth: YWRtaW46SGFyYm9yMTIzNDU=
# If auth is not provided, it will attempt to read from docker config
# auth: YWRtaW46SGFyYm9yMTIzNDU=
# work directory of acceld
work_dir: /tmp
gcpolicy:
Expand Down
5 changes: 3 additions & 2 deletions misc/config/config.nydus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ provider:
hub.harbor.com:
# base64 encoded `<robot-name>:<robot-secret>` for robot
# account created in harbor
auth: YTpiCg==
# auth: YTpiCg==
# skip verifying server certs for HTTPS source registry
insecure: false
webhook:
# webhook request auth header configured in harbor
auth_header: header
localhost:
auth: YWRtaW46SGFyYm9yMTIzNDU=
# If auth is not provided, it will attempt to read from docker config
# auth: YWRtaW46SGFyYm9yMTIzNDU=
# work directory of acceld
work_dir: /tmp
gcpolicy:
Expand Down
17 changes: 13 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"

"github.com/docker/cli/cli/config"
"github.com/goharbor/acceleration-service/pkg/remote"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -105,11 +107,18 @@ func (cfg *Config) Host(ref string) (remote.CredentialFunc, bool, error) {
return nil, errors.Wrap(err, "parse reference of source image")
}

auth, ok := cfg.Provider.Source[refURL.Host]
if !ok {
return nil, fmt.Errorf("not found matched hostname %s in config", refURL.Host)
auth := cfg.Provider.Source[refURL.Host]
// try to finds auth for a given host in docker's config.json settings.
if len(auth.Auth) == 0 {
config := config.LoadDefaultConfigFile(os.Stderr)
authConfig, err := config.GetAuthConfig(refURL.Host)
if err != nil {
return nil, err
}
if len(authConfig.Username) != 0 || len(authConfig.Password) != 0 {
auth.Auth = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", authConfig.Username, authConfig.Password)))
}
}

return &auth, nil
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ func NewLocalHandler(cfg *config.Config) (*LocalHandler, error) {

func (handler *LocalHandler) Auth(ctx context.Context, host string, authHeader string) error {
if authHeader != "" {
source, ok := handler.cfg.Provider.Source[host]
if !ok {
return fmt.Errorf("not found config for host %s", host)
}
source := handler.cfg.Provider.Source[host]
if authHeader != source.Webhook.AuthHeader {
return fmt.Errorf("unmatched auth header for host %s", host)
}
Expand Down
3 changes: 2 additions & 1 deletion script/integration/concurrent/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ metric:
provider:
source:
localhost:
auth: YWRtaW46SGFyYm9yMTIzNDU=
# If auth is not provided, it will attempt to read from docker config
# auth: YWRtaW46SGFyYm9yMTIzNDU=
work_dir: /tmp
gcpolicy:
threshold: 10MB
Expand Down