mirror of
https://github.com/containers/skopeo.git
synced 2025-07-31 22:45:20 +00:00
get authConfig from either docker or cli
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
e240045aa9
commit
be0a455d5b
46
inspect.go
46
inspect.go
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
@ -10,6 +12,7 @@ import (
|
|||||||
"github.com/docker/distribution/digest"
|
"github.com/docker/distribution/digest"
|
||||||
distreference "github.com/docker/distribution/reference"
|
distreference "github.com/docker/distribution/reference"
|
||||||
"github.com/docker/docker/api"
|
"github.com/docker/docker/api"
|
||||||
|
"github.com/docker/docker/cliconfig"
|
||||||
"github.com/docker/docker/image"
|
"github.com/docker/docker/image"
|
||||||
"github.com/docker/docker/opts"
|
"github.com/docker/docker/opts"
|
||||||
versionPkg "github.com/docker/docker/pkg/version"
|
versionPkg "github.com/docker/docker/pkg/version"
|
||||||
@ -17,6 +20,7 @@ import (
|
|||||||
"github.com/docker/docker/registry"
|
"github.com/docker/docker/registry"
|
||||||
types "github.com/docker/engine-api/types"
|
types "github.com/docker/engine-api/types"
|
||||||
containerTypes "github.com/docker/engine-api/types/container"
|
containerTypes "github.com/docker/engine-api/types/container"
|
||||||
|
registryTypes "github.com/docker/engine-api/types/registry"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,22 +79,22 @@ func inspect(c *cli.Context) (*imageInspect, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
authConfig, err := getAuthConfig(c, ref)
|
imgInspect, err := getData(c, ref)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
imgInspect, err := getData(ref, authConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return imgInspect, nil
|
return imgInspect, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getData(ref reference.Named, authConfig types.AuthConfig) (*imageInspect, error) {
|
func getData(c *cli.Context, ref reference.Named) (*imageInspect, error) {
|
||||||
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
authConfig, err := getAuthConfig(c, repoInfo.Index)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := validateRepoName(repoInfo.Name()); err != nil {
|
if err := validateRepoName(repoInfo.Name()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -207,25 +211,31 @@ func newManifestFetcher(endpoint registry.APIEndpoint, repoInfo *registry.Reposi
|
|||||||
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAuthConfig(c *cli.Context, ref reference.Named) (types.AuthConfig, error) {
|
func isDockerAvailable() bool {
|
||||||
|
_, err := exec.LookPath("docker")
|
||||||
// TODO(runcom):
|
return err == nil
|
||||||
// use docker/cliconfig
|
}
|
||||||
// if no /.docker -> docker not installed fallback to require username|password
|
|
||||||
// maybe prompt user:passwd?
|
|
||||||
|
|
||||||
|
func getAuthConfig(c *cli.Context, index *registryTypes.IndexInfo) (types.AuthConfig, error) {
|
||||||
var (
|
var (
|
||||||
authConfig types.AuthConfig
|
username = c.GlobalString("username")
|
||||||
username = c.GlobalString("username")
|
password = c.GlobalString("password")
|
||||||
password = c.GlobalString("password")
|
cfg = c.GlobalString("docker-cfg")
|
||||||
)
|
)
|
||||||
if username != "" && password != "" {
|
if _, err := os.Stat(cfg); err != nil {
|
||||||
authConfig = types.AuthConfig{
|
logrus.Infof("Docker cli config file not found: %v, falling back to --username and --password", err)
|
||||||
|
return types.AuthConfig{
|
||||||
Username: username,
|
Username: username,
|
||||||
Password: password,
|
Password: password,
|
||||||
Email: "stub@example.com",
|
Email: "stub@example.com",
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
confFile, err := cliconfig.Load(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return types.AuthConfig{}, err
|
||||||
|
}
|
||||||
|
authConfig := registry.ResolveAuthConfig(confFile.AuthConfigs, index)
|
||||||
|
logrus.Debugf("authConfig for %s: %v", index.Name, authConfig)
|
||||||
|
|
||||||
return authConfig, nil
|
return authConfig, nil
|
||||||
}
|
}
|
||||||
|
8
main.go
8
main.go
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
|
"github.com/docker/docker/cliconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -34,7 +35,7 @@ func main() {
|
|||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "debug",
|
Name: "debug",
|
||||||
Usage: "enable debug output for logging",
|
Usage: "enable debug output",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "username",
|
Name: "username",
|
||||||
@ -46,6 +47,11 @@ func main() {
|
|||||||
Value: "",
|
Value: "",
|
||||||
Usage: "registry password",
|
Usage: "registry password",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "docker-cfg",
|
||||||
|
Value: cliconfig.ConfigDir(),
|
||||||
|
Usage: "Docker's cli config for auth",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
app.Before = func(c *cli.Context) error {
|
app.Before = func(c *cli.Context) error {
|
||||||
if c.GlobalBool("debug") {
|
if c.GlobalBool("debug") {
|
||||||
|
Loading…
Reference in New Issue
Block a user