Skip to content

Commit

Permalink
Merge pull request #112 from amazeeio/sshkey-fix
Browse files Browse the repository at this point in the history
Improvements to adding SSH keys
  • Loading branch information
shreddedbacon authored Apr 3, 2020
2 parents d640a00 + 62a426c commit 7b7d31c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
43 changes: 33 additions & 10 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/amazeeio/lagoon-cli/internal/helpers"
"github.com/amazeeio/lagoon-cli/pkg/api"
"github.com/amazeeio/lagoon-cli/pkg/output"
"github.com/spf13/cobra"
Expand All @@ -26,10 +27,14 @@ func parseUser(flags pflag.FlagSet) api.User {
return parsedFlags
}

func parseSSHKeyFile(sshPubKey string, keyName string) api.SSHKey {
b, err := ioutil.ReadFile(sshPubKey) // just pass the file name
handleError(err)
splitKey := strings.Split(string(b), " ")
func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmail string) api.SSHKey {
// if we haven't got a keyvalue
if keyValue == "" {
b, err := ioutil.ReadFile(sshPubKey) // just pass the file name
handleError(err)
keyValue = string(b)
}
splitKey := strings.Split(keyValue, " ")
var keyType api.SSHKeyType
// default to ssh-rsa, otherwise check if ssh-ed25519
// will fail if neither are right
Expand All @@ -40,14 +45,14 @@ func parseSSHKeyFile(sshPubKey string, keyName string) api.SSHKey {
// if the sshkey has a comment/name in it, we can use that
if keyName == "" && len(splitKey) == 3 {
//strip new line
keyName = strings.TrimSuffix(splitKey[2], "\n")
keyName = helpers.StripNewLines(splitKey[2])
} else if keyName == "" && len(splitKey) == 2 {
output.RenderError("no name provided", outputOptions)
os.Exit(1)
keyName = userEmail
output.RenderInfo("no name provided, using email address as key name", outputOptions)
}
parsedFlags := api.SSHKey{
KeyType: keyType,
KeyValue: splitKey[1],
KeyValue: helpers.StripNewLines(splitKey[1]),
Name: keyName,
}
return parsedFlags
Expand Down Expand Up @@ -83,14 +88,30 @@ var addUserSSHKeyCmd = &cobra.Command{
Use: "user-sshkey",
Aliases: []string{"uk"},
Short: "Add an sshkey to a user",
Long: `Add an sshkey to a user
Examples:
Add key from public key file:
lagoon add user-sshkey --email [email protected] --pubkey /path/to/id_rsa.pub
Add key by defining full key value:
lagoon add user-sshkey --email [email protected] --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/ my-computer@example"
Add key by defining full key value, but a specific key name:
lagoon add user-sshkey --email [email protected] --keyname my-computer@example --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/"
Add key by defining key value, but not specifying a key name (will default to try using the email address as key name):
lagoon add user-sshkey --email [email protected] --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/"
`,
Run: func(cmd *cobra.Command, args []string) {
userFlags := parseUser(*cmd.Flags())
if userFlags.Email == "" {
fmt.Println("Missing arguments: Email address is not defined")
cmd.Help()
os.Exit(1)
}
userSSHKey := parseSSHKeyFile(pubKeyFile, sshKeyName)
userSSHKey := parseSSHKeyFile(pubKeyFile, sshKeyName, pubKeyValue, userFlags.Email)
var customReqResult []byte
var err error
customReqResult, err = uClient.AddSSHKeyToUser(userFlags, userSSHKey)
Expand Down Expand Up @@ -232,6 +253,7 @@ var getAllUserKeysCmd = &cobra.Command{

var (
currentUserEmail string
pubKeyValue string
)

func init() {
Expand All @@ -240,7 +262,8 @@ func init() {
addUserCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
addUserSSHKeyCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
addUserSSHKeyCmd.Flags().StringVarP(&sshKeyName, "keyname", "N", "", "Name of the sshkey (optional, if not provided will try use what is in the pubkey file)")
addUserSSHKeyCmd.Flags().StringVarP(&pubKeyFile, "pubkey", "K", "", "file location to the public key to add")
addUserSSHKeyCmd.Flags().StringVarP(&pubKeyFile, "pubkey", "K", "", "Specify path to the public key to add")
addUserSSHKeyCmd.Flags().StringVarP(&pubKeyValue, "keyvalue", "V", "", "Value of the public key to add (ssh-ed25519 AAA..)")
deleteUserCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
deleteSSHKeyCmd.Flags().StringVarP(&sshKeyName, "keyname", "N", "", "Name of the sshkey")
updateUserCmd.Flags().StringVarP(&userFirstName, "firstName", "F", "", "New firstname of the user")
Expand Down
24 changes: 20 additions & 4 deletions docs/commands/lagoon_add_user-sshkey.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@ Add an sshkey to a user

Add an sshkey to a user

Examples:
Add key from public key file:
lagoon add user-sshkey --email [email protected] --pubkey /path/to/id_rsa.pub

Add key by defining full key value:
lagoon add user-sshkey --email [email protected] --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/ my-computer@example"

Add key by defining full key value, but a specific key name:
lagoon add user-sshkey --email [email protected] --keyname my-computer@example --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/"

Add key by defining key value, but not specifying a key name (will default to try using the email address as key name):
lagoon add user-sshkey --email [email protected] --keyvalue "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINA0ITV2gbDc6noYeWaqfxTYpaEKq7HzU3+F71XGhSL/"



```
lagoon add user-sshkey [flags]
```

### Options

```
-E, --email string Email address of the user
-h, --help help for user-sshkey
-N, --keyname string Name of the sshkey (optional, if not provided will try use what is in the pubkey file)
-K, --pubkey string file location to the public key to add
-E, --email string Email address of the user
-h, --help help for user-sshkey
-N, --keyname string Name of the sshkey (optional, if not provided will try use what is in the pubkey file)
-V, --keyvalue string Value of the public key to add (ssh-ed25519 AAA..)
-K, --pubkey string Specify path to the public key to add
```

### Options inherited from parent commands
Expand Down
5 changes: 5 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ func GetLagoonConfigFile(configPath *string, configName *string, configExtension
// no config file found
return nil
}

// StripNewLines will strip new lines from strings helper
func StripNewLines(stripString string) string {
return strings.TrimSuffix(stripString, "\n")
}

0 comments on commit 7b7d31c

Please sign in to comment.