Skip to content

Commit

Permalink
feat(login): Add option to login to container registries (#738)
Browse files Browse the repository at this point in the history
Reviewed-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
Approved-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
  • Loading branch information
marcrittinghaus authored Aug 23, 2023
2 parents 61635e5 + 08b7632 commit 7485481
Showing 1 changed file with 69 additions and 7 deletions.
76 changes: 69 additions & 7 deletions cmd/kraft/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ package login

import (
"bufio"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"golang.org/x/term"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/iostreams"

"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)

type Login struct {
User string `long:"user" short:"u" usage:"Username" env:"KRAFTKIT_LOGIN_USER"`
Token string `long:"token" short:"t" usage:"Authentication token" env:"KRAFTKIT_LOGIN_TOKEN"`
Registry bool `long:"registry" short:"r" usage:"Specify whether to login to a registry"`
Token string `long:"token" short:"t" usage:"Authentication token" env:"KRAFTKIT_LOGIN_TOKEN"`
User string `long:"user" short:"u" usage:"Username" env:"KRAFTKIT_LOGIN_USER"`
}

func New() *cobra.Command {
Expand Down Expand Up @@ -82,10 +88,66 @@ func (opts *Login) Run(cmd *cobra.Command, args []string) error {
authConfig.Token = opts.Token
}

if config.G[config.KraftKit](ctx).Auth == nil {
config.G[config.KraftKit](ctx).Auth = make(map[string]config.AuthConfig)
}
config.G[config.KraftKit](ctx).Auth[host] = authConfig
if opts.Registry {
home, err := homedir.Dir()
if err != nil {
return err
}

// The default path for registry credentials is:
defaultPath := filepath.Join(home, ".docker", "config.json")

// If the DOCKER_CONFIG environment variable is set, use that instead
if os.Getenv("DOCKER_CONFIG") != "" {
defaultPath = filepath.Join(os.Getenv("DOCKER_CONFIG"), "config.json")
}

var bytes []byte
var config map[string](map[string]interface{})
if _, err := os.Stat(defaultPath); !os.IsNotExist(err) {
bytes, err := os.ReadFile(defaultPath)
if err != nil {
return err
}

err = json.Unmarshal(bytes, &config)
if err != nil {
return err
}
} else {
config = make(map[string](map[string]interface{}))
config["auths"] = make(map[string]interface{})
}

// Add the new auth config
auth := make(map[string]interface{})

var token string
if opts.User != "" {
token = opts.User + ":" + opts.Token

return config.M[config.KraftKit](ctx).Write(true)
// Base64 encode the token
token = base64.StdEncoding.EncodeToString([]byte(token))
} else {
token = opts.Token
}

auth["auth"] = token
config["auths"][host] = auth

// Write it back
bytes, err = json.Marshal(config)
if err != nil {
return err
}

return os.WriteFile(defaultPath, bytes, 0644)

Check failure on line 144 in cmd/kraft/login/login.go

View workflow job for this annotation

GitHub Actions / All

File is not `gofumpt`-ed (gofumpt)
} else {
if config.G[config.KraftKit](ctx).Auth == nil {
config.G[config.KraftKit](ctx).Auth = make(map[string]config.AuthConfig)
}
config.G[config.KraftKit](ctx).Auth[host] = authConfig

return config.M[config.KraftKit](ctx).Write(true)
}
}

0 comments on commit 7485481

Please sign in to comment.