mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-02-14 18:09:51 +00:00
@@ -32,6 +32,7 @@ func New() *cobra.Command {
|
|||||||
|
|
||||||
// Add subcommands
|
// Add subcommands
|
||||||
addDownload(cmd)
|
addDownload(cmd)
|
||||||
|
addLogin(cmd)
|
||||||
addStore(cmd)
|
addStore(cmd)
|
||||||
addServe(cmd)
|
addServe(cmd)
|
||||||
addVersion(cmd)
|
addVersion(cmd)
|
||||||
|
|||||||
75
cmd/hauler/cli/login.go
Normal file
75
cmd/hauler/cli/login.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"os"
|
||||||
|
"io"
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"oras.land/oras-go/pkg/content"
|
||||||
|
|
||||||
|
"github.com/rancherfederal/hauler/pkg/cosign"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Opts struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
PasswordStdin bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Opts) AddArgs(cmd *cobra.Command) {
|
||||||
|
f := cmd.Flags()
|
||||||
|
f.StringVarP(&o.Username, "username", "u", "", "Username")
|
||||||
|
f.StringVarP(&o.Password, "password", "p", "", "Password")
|
||||||
|
f.BoolVarP(&o.PasswordStdin, "password-stdin", "", false, "Take the password from stdin")
|
||||||
|
}
|
||||||
|
|
||||||
|
func addLogin(parent *cobra.Command) {
|
||||||
|
o := &Opts{}
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "login",
|
||||||
|
Short: "Log in to a registry",
|
||||||
|
Example: `
|
||||||
|
# Log in to reg.example.com
|
||||||
|
hauler login reg.example.com -u bob -p haulin`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, arg []string) error {
|
||||||
|
ctx := cmd.Context()
|
||||||
|
|
||||||
|
if o.PasswordStdin {
|
||||||
|
contents, err := io.ReadAll(os.Stdin)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
o.Password = strings.TrimSuffix(string(contents), "\n")
|
||||||
|
o.Password = strings.TrimSuffix(o.Password, "\r")
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Username == "" && o.Password == "" {
|
||||||
|
return fmt.Errorf("username and password required")
|
||||||
|
}
|
||||||
|
|
||||||
|
return login(ctx, o, arg[0])
|
||||||
|
},
|
||||||
|
}
|
||||||
|
o.AddArgs(cmd)
|
||||||
|
|
||||||
|
parent.AddCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func login(ctx context.Context, o *Opts, registry string) error {
|
||||||
|
ropts := content.RegistryOptions{
|
||||||
|
Username: o.Username,
|
||||||
|
Password: o.Password,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := cosign.RegistryLogin(ctx, nil, registry, ropts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -140,17 +140,18 @@ func LoadImages(ctx context.Context, s *store.Layout, registry string, ropts con
|
|||||||
|
|
||||||
// RegistryLogin - performs cosign login
|
// RegistryLogin - performs cosign login
|
||||||
func RegistryLogin(ctx context.Context, s *store.Layout, registry string, ropts content.RegistryOptions) error {
|
func RegistryLogin(ctx context.Context, s *store.Layout, registry string, ropts content.RegistryOptions) error {
|
||||||
|
log := log.FromContext(ctx)
|
||||||
cosignBinaryPath, err := getCosignPath(ctx)
|
cosignBinaryPath, err := getCosignPath(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(cosignBinaryPath, "login", registry, "-u", ropts.Username, "-p", ropts.Password)
|
cmd := exec.Command(cosignBinaryPath, "login", registry, "-u", ropts.Username, "-p", ropts.Password)
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error logging into registry: %v, output: %s", err, output)
|
return fmt.Errorf("error logging into registry: %v, output: %s", err, output)
|
||||||
}
|
}
|
||||||
|
log.Infof(strings.Trim(string(output), "\n"))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user