Skip to content

Commit

Permalink
Merge pull request #177 from amartin120/add-login
Browse files Browse the repository at this point in the history
add login command
  • Loading branch information
amartin120 authored Feb 16, 2024
2 parents 8d2a84d + cd8d4f6 commit e825437
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/hauler/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func New() *cobra.Command {

// Add subcommands
addDownload(cmd)
addLogin(cmd)
addStore(cmd)
addServe(cmd)
addVersion(cmd)
Expand Down
75 changes: 75 additions & 0 deletions cmd/hauler/cli/login.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/cosign/cosign.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ func LoadImages(ctx context.Context, s *store.Layout, registry string, ropts con

// RegistryLogin - performs cosign login
func RegistryLogin(ctx context.Context, s *store.Layout, registry string, ropts content.RegistryOptions) error {
log := log.FromContext(ctx)
cosignBinaryPath, err := getCosignPath(ctx)
if err != nil {
return err
}

cmd := exec.Command(cosignBinaryPath, "login", registry, "-u", ropts.Username, "-p", ropts.Password)

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error logging into registry: %v, output: %s", err, output)
}
log.Infof(strings.Trim(string(output), "\n"))

return nil
}
Expand Down

0 comments on commit e825437

Please sign in to comment.