Skip to content

Commit

Permalink
libimage: search: add options for authentication
Browse files Browse the repository at this point in the history
When we searching any image at a container registry,
the authentication could be required as well as push, pull, etc.

Signed-off-by: Toshiki Sonoda <sonoda.toshiki@fujitsu.com>
  • Loading branch information
sstosh committed Feb 10, 2023
1 parent 6314b43 commit 98c0c27
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions libimage/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libimage

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -54,6 +55,16 @@ type SearchOptions struct {
NoTrunc bool
// Authfile is the path to the authentication file.
Authfile string
// Path to the certificates directory.
CertDirPath string
// Username to use when authenticating at a container registry.
Username string
// Password to use when authenticating at a container registry.
Password string
// Credentials is an alternative way to specify credentials in format
// "username[:password]". Cannot be used in combination with
// Username/Password.
Credentials string
// InsecureSkipTLSVerify allows to skip TLS verification.
InsecureSkipTLSVerify types.OptionalBool
// ListTags returns the search result with available tags
Expand Down Expand Up @@ -201,6 +212,31 @@ func (r *Runtime) searchImageInRegistry(ctx context.Context, term, registry stri
sys.AuthFilePath = options.Authfile
}

if options.CertDirPath != "" {
sys.DockerCertPath = options.CertDirPath
}

authConf := &types.DockerAuthConfig{}
if options.Username != "" {
if options.Credentials != "" {
return nil, errors.New("username/password cannot be used with credentials")
}
authConf.Username = options.Username
authConf.Password = options.Password
}

if options.Credentials != "" {
split := strings.SplitN(options.Credentials, ":", 2)
switch len(split) {
case 1:
authConf.Username = split[0]
default:
authConf.Username = split[0]
authConf.Password = split[1]
}
}
sys.DockerAuthConfig = authConf

if options.ListTags {
results, err := searchRepositoryTags(ctx, sys, registry, term, options)
if err != nil {
Expand Down

0 comments on commit 98c0c27

Please sign in to comment.