|
| 1 | +package login |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "golang.org/x/crypto/ssh/terminal" |
| 11 | + "oras.land/oras-go/pkg/auth" |
| 12 | + "oras.land/oras-go/pkg/auth/docker" |
| 13 | + |
| 14 | + "github.com/werf/logboek" |
| 15 | + "github.com/werf/werf/cmd/werf/common" |
| 16 | + secret_common "github.com/werf/werf/cmd/werf/helm/secret/common" |
| 17 | + "github.com/werf/werf/pkg/werf" |
| 18 | + "github.com/werf/werf/pkg/werf/global_warnings" |
| 19 | +) |
| 20 | + |
| 21 | +var commonCmdData common.CmdData |
| 22 | + |
| 23 | +var cmdData struct { |
| 24 | + Username string |
| 25 | + Password string |
| 26 | + PasswordStdin bool |
| 27 | +} |
| 28 | + |
| 29 | +func NewCmd() *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "login registry", |
| 32 | + Short: "Login into remote registry", |
| 33 | + Long: common.GetLongCommandDescription(`Login into remote registry`), |
| 34 | + Example: `# Login with username and password from command line |
| 35 | +werf cr login -u username -p password registry.example.com |
| 36 | +
|
| 37 | +# Login with token from command line |
| 38 | +werf cr login -p token registry.example.com |
| 39 | +
|
| 40 | +# Login into insecure registry (over http) |
| 41 | +werf cr login --insecure-registry registry.example.com`, |
| 42 | + DisableFlagsInUseLine: true, |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + ctx := common.BackgroundContext() |
| 45 | + |
| 46 | + defer global_warnings.PrintGlobalWarnings(ctx) |
| 47 | + |
| 48 | + if err := common.ProcessLogOptions(&commonCmdData); err != nil { |
| 49 | + common.PrintHelp(cmd) |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + if len(args) != 1 { |
| 54 | + common.PrintHelp(cmd) |
| 55 | + return fmt.Errorf("registry address argument required") |
| 56 | + } |
| 57 | + |
| 58 | + return Login(ctx, args[0], LoginOptions{ |
| 59 | + Username: cmdData.Username, |
| 60 | + Password: cmdData.Password, |
| 61 | + PasswordStdin: cmdData.PasswordStdin, |
| 62 | + DockerConfigDir: *commonCmdData.DockerConfig, |
| 63 | + InsecureRegistry: *commonCmdData.InsecureRegistry, |
| 64 | + }) |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + common.SetupDockerConfig(&commonCmdData, cmd, "") |
| 69 | + common.SetupInsecureRegistry(&commonCmdData, cmd) |
| 70 | + // common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd) |
| 71 | + common.SetupLogOptions(&commonCmdData, cmd) |
| 72 | + |
| 73 | + cmd.Flags().StringVarP(&cmdData.Username, "username", "u", os.Getenv("WERF_USERNAME"), "Use specified username for login (default $WERF_USERNAME)") |
| 74 | + cmd.Flags().StringVarP(&cmdData.Password, "password", "p", os.Getenv("WERF_PASSWORD"), "Use specified password for login (default $WERF_PASSWORD)") |
| 75 | + cmd.Flags().BoolVarP(&cmdData.PasswordStdin, "password-stdin", "", common.GetBoolEnvironmentDefaultFalse("WERF_PASSWORD_STDIN"), "Read password from stdin for login (default $WERF_PASSWORD_STDIN)") |
| 76 | + |
| 77 | + return cmd |
| 78 | +} |
| 79 | + |
| 80 | +type LoginOptions struct { |
| 81 | + Username string |
| 82 | + Password string |
| 83 | + PasswordStdin bool |
| 84 | + DockerConfigDir string |
| 85 | + InsecureRegistry bool |
| 86 | +} |
| 87 | + |
| 88 | +func Login(ctx context.Context, registry string, opts LoginOptions) error { |
| 89 | + var dockerConfigDir string |
| 90 | + if opts.DockerConfigDir != "" { |
| 91 | + dockerConfigDir = opts.DockerConfigDir |
| 92 | + } else { |
| 93 | + dockerConfigDir = filepath.Join(os.Getenv("HOME"), ".docker") |
| 94 | + } |
| 95 | + |
| 96 | + cli, err := docker.NewClient(filepath.Join(dockerConfigDir, "config.json")) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("unable to create oras auth client: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + if opts.Username == "" { |
| 102 | + return fmt.Errorf("provide --username") |
| 103 | + } |
| 104 | + |
| 105 | + var password string |
| 106 | + if opts.PasswordStdin { |
| 107 | + if opts.Password != "" { |
| 108 | + return fmt.Errorf("--password and --password-stdin could not be used at the same time") |
| 109 | + } |
| 110 | + |
| 111 | + var bytePassword []byte |
| 112 | + if terminal.IsTerminal(int(os.Stdin.Fd())) { |
| 113 | + bytePassword, err = secret_common.InputFromInteractiveStdin("Password: ") |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("error reading password from interactive stdin: %s", err) |
| 116 | + } |
| 117 | + } else { |
| 118 | + bytePassword, err = secret_common.InputFromStdin() |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("error reading password from stdin: %s", err) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + password = string(bytePassword) |
| 125 | + } else if opts.Password != "" { |
| 126 | + password = opts.Password |
| 127 | + } else { |
| 128 | + return fmt.Errorf("provide --password or --password-stdin") |
| 129 | + } |
| 130 | + |
| 131 | + if err := cli.LoginWithOpts(func(settings *auth.LoginSettings) { |
| 132 | + settings.Context = ctx |
| 133 | + settings.Hostname = registry |
| 134 | + settings.Username = opts.Username |
| 135 | + settings.Secret = password |
| 136 | + settings.Insecure = opts.InsecureRegistry |
| 137 | + settings.UserAgent = fmt.Sprintf("werf %s", werf.Version) |
| 138 | + }); err != nil { |
| 139 | + return fmt.Errorf("unable to login into %q: %s", registry, err) |
| 140 | + } |
| 141 | + |
| 142 | + logboek.Context(ctx).Default().LogFHighlight("Successful login\n") |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments