github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/login.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "errors" 21 "io" 22 "strings" 23 24 "github.com/containerd/log" 25 "github.com/containerd/nerdctl/pkg/api/types" 26 "github.com/containerd/nerdctl/pkg/cmd/login" 27 28 "github.com/spf13/cobra" 29 ) 30 31 type loginOptions struct { 32 serverAddress string 33 username string 34 password string 35 passwordStdin bool 36 } 37 38 var options = new(loginOptions) 39 40 func newLoginCommand() *cobra.Command { 41 var loginCommand = &cobra.Command{ 42 Use: "login [flags] [SERVER]", 43 Args: cobra.MaximumNArgs(1), 44 Short: "Log in to a container registry", 45 RunE: loginAction, 46 SilenceUsage: true, 47 SilenceErrors: true, 48 } 49 loginCommand.Flags().StringVarP(&options.username, "username", "u", "", "Username") 50 loginCommand.Flags().StringVarP(&options.password, "password", "p", "", "Password") 51 loginCommand.Flags().BoolVar(&options.passwordStdin, "password-stdin", false, "Take the password from stdin") 52 return loginCommand 53 } 54 55 func loginAction(cmd *cobra.Command, args []string) error { 56 if len(args) == 1 { 57 options.serverAddress = args[0] 58 } 59 if err := verifyLoginOptions(cmd, options); err != nil { 60 return err 61 } 62 63 globalOptions, err := processRootCmdFlags(cmd) 64 if err != nil { 65 return err 66 } 67 68 return login.Login(cmd.Context(), types.LoginCommandOptions{ 69 GOptions: globalOptions, 70 ServerAddress: options.serverAddress, 71 Username: options.username, 72 Password: options.password, 73 }, cmd.OutOrStdout()) 74 } 75 76 // copied from github.com/docker/cli/cli/command/registry/login.go (v20.10.3) 77 func verifyLoginOptions(cmd *cobra.Command, options *loginOptions) error { 78 if options.password != "" { 79 log.L.Warn("WARNING! Using --password via the CLI is insecure. Use --password-stdin.") 80 if options.passwordStdin { 81 return errors.New("--password and --password-stdin are mutually exclusive") 82 } 83 } 84 85 if options.passwordStdin { 86 if options.username == "" { 87 return errors.New("must provide --username with --password-stdin") 88 } 89 90 contents, err := io.ReadAll(cmd.InOrStdin()) 91 if err != nil { 92 return err 93 } 94 95 options.password = strings.TrimSuffix(string(contents), "\n") 96 options.password = strings.TrimSuffix(options.password, "\r") 97 } 98 return nil 99 }