github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/keys/contract.go (about)

     1  package keys
     2  
     3  import (
     4  	"github.com/Sirupsen/logrus"
     5  	"github.com/daticahealth/cli/commands/deploykeys"
     6  	"github.com/daticahealth/cli/lib/auth"
     7  	"github.com/daticahealth/cli/lib/prompts"
     8  	"github.com/daticahealth/cli/models"
     9  	"github.com/jault3/mow.cli"
    10  )
    11  
    12  // Cmd for keys
    13  var Cmd = models.Command{
    14  	Name:      "keys",
    15  	ShortHelp: "Tasks for SSH keys",
    16  	LongHelp: "The <code>keys</code> command gives access to SSH key management for your user account. " +
    17  		"SSH keys can be used for authentication and pushing code to the Datica platform. " +
    18  		"Any SSH keys added to your user account should not be shared but be treated as private SSH keys. " +
    19  		"Any SSH key uploaded to your user account will be able to be used with all code services and environments that you have access to. " +
    20  		"The keys command can not be run directly but has subcommands.",
    21  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    22  		return func(cmd *cli.Cmd) {
    23  			cmd.CommandLong(AddSubCmd.Name, AddSubCmd.ShortHelp, AddSubCmd.LongHelp, AddSubCmd.CmdFunc(settings))
    24  			cmd.CommandLong(ListSubCmd.Name, ListSubCmd.ShortHelp, ListSubCmd.LongHelp, ListSubCmd.CmdFunc(settings))
    25  			cmd.CommandLong(RemoveSubCmd.Name, RemoveSubCmd.ShortHelp, RemoveSubCmd.LongHelp, RemoveSubCmd.CmdFunc(settings))
    26  			cmd.CommandLong(SetSubCmd.Name, SetSubCmd.ShortHelp, SetSubCmd.LongHelp, SetSubCmd.CmdFunc(settings))
    27  		}
    28  	},
    29  }
    30  
    31  var AddSubCmd = models.Command{
    32  	Name:      "add",
    33  	ShortHelp: "Add a public key",
    34  	LongHelp: "<code>keys add</code> allows you to add a new SSH key to your user account. " +
    35  		"SSH keys added to your user account should be private and not shared with others. " +
    36  		"SSH keys can be used for authentication (as opposed to the traditional email and password) as well as pushing code to an environment's code services. " +
    37  		"Please note, you must specify the path to the public key file and not the private key. " +
    38  		"All SSH keys should be in either OpenSSH RSA format or PEM format. Here is a sample command\n\n" +
    39  		"<pre>\ndatica keys add my_prod_key ~/.ssh/prod_rsa.pub\n</pre>",
    40  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    41  		return func(cmd *cli.Cmd) {
    42  			name := cmd.StringArg("NAME", "", "The name for the new key, for your own purposes")
    43  			path := cmd.StringArg("PUBLIC_KEY_PATH", "", "Relative path to the public key file")
    44  			cmd.Action = func() {
    45  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    46  					logrus.Fatal(err.Error())
    47  				}
    48  				err := CmdAdd(*name, *path, New(settings), deploykeys.New(settings))
    49  				if err != nil {
    50  					logrus.Fatal(err)
    51  				}
    52  			}
    53  		}
    54  	},
    55  }
    56  
    57  var ListSubCmd = models.Command{
    58  	Name:      "list",
    59  	ShortHelp: "List your public keys",
    60  	LongHelp: "<code>keys list</code> lists all public keys by name that have been uploaded to your user account including the key's fingerprint in SHA256 format. " +
    61  		"Here is a sample command\n\n" +
    62  		"<pre>\ndatica keys list\n</pre>",
    63  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    64  		return func(cmd *cli.Cmd) {
    65  			cmd.Action = func() {
    66  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    67  					logrus.Fatal(err.Error())
    68  				}
    69  				err := CmdList(New(settings), deploykeys.New(settings))
    70  				if err != nil {
    71  					logrus.Fatal(err)
    72  				}
    73  			}
    74  		}
    75  	},
    76  }
    77  
    78  var RemoveSubCmd = models.Command{
    79  	Name:      "rm",
    80  	ShortHelp: "Remove a public key",
    81  	LongHelp: "<code>keys rm</code> allows you to remove an SSH key previously uploaded to your account. " +
    82  		"The name of the key can be found by using the keys list command. Here is a sample command\n\n" +
    83  		"<pre>\ndatica keys rm my_prod_key\n</pre>",
    84  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
    85  		return func(cmd *cli.Cmd) {
    86  			name := cmd.StringArg("NAME", "", "The name of the key to remove.")
    87  			cmd.Action = func() {
    88  				if _, err := auth.New(settings, prompts.New()).Signin(); err != nil {
    89  					logrus.Fatal(err.Error())
    90  				}
    91  				err := CmdRemove(*name, settings.PrivateKeyPath, New(settings), deploykeys.New(settings))
    92  				if err != nil {
    93  					logrus.Fatal(err)
    94  				}
    95  			}
    96  		}
    97  	},
    98  }
    99  
   100  var SetSubCmd = models.Command{
   101  	Name:      "set",
   102  	ShortHelp: "Set your auth key",
   103  	LongHelp: "<code>keys set</code> allows the CLI to use an SSH key for authentication instead of the traditional email and password combination. " +
   104  		"This can be useful for automation or where shared workstations are involved. " +
   105  		"Please note that you must pass in the path to the private key and not the public key. " +
   106  		"The given key must already be added to your account by using the keys add command. " +
   107  		"Here is a sample command\n\n" +
   108  		"<pre>\ndatica keys set ~/.ssh/my_key\n</pre>",
   109  	CmdFunc: func(settings *models.Settings) func(cmd *cli.Cmd) {
   110  		return func(cmd *cli.Cmd) {
   111  			path := cmd.StringArg("PRIVATE_KEY_PATH", "", "Relative path to the private key file")
   112  			cmd.Action = func() {
   113  				err := CmdSet(*path, settings)
   114  				if err != nil {
   115  					logrus.Fatal(err)
   116  				}
   117  			}
   118  		}
   119  	},
   120  }
   121  
   122  type IKeys interface {
   123  	List() (*[]models.UserKey, error)
   124  	Add(name, publicKey string) error
   125  	Remove(name string) error
   126  }
   127  
   128  type SKeys struct {
   129  	Settings *models.Settings
   130  }
   131  
   132  func New(settings *models.Settings) IKeys {
   133  	return &SKeys{Settings: settings}
   134  }