Skip to content

Commit

Permalink
refactor: use newer sshkey functions to expand key support (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Oct 18, 2024
1 parent a6903c6 commit 67e51b9
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,16 @@ import (
"github.com/uselagoon/lagoon-cli/pkg/output"
)

func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmail string) (schema.AddSSHKeyInput, error) {
func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmail string) (*schema.AddUserSSHPublicKeyInput, error) {
// if we haven't got a keyvalue
if keyValue == "" {
b, err := os.ReadFile(sshPubKey) // just pass the file name
handleError(err)
if err != nil {
return nil, err
}
keyValue = string(b)
}
splitKey := strings.Split(keyValue, " ")
var keyType schema.SSHKeyType
var err error

// will fail if value is not right
switch splitKey[0] {
case "ssh-rsa":
keyType = schema.SSHRsa
case "ssh-ed25519":
keyType = schema.SSHEd25519
case "ecdsa-sha2-nistp256":
keyType = schema.SSHECDSA256
case "ecdsa-sha2-nistp384":
keyType = schema.SSHECDSA384
case "ecdsa-sha2-nistp521":
keyType = schema.SSHECDSA521
default:
keyType = schema.SSHRsa
err = fmt.Errorf("SSH key type %s not supported", splitKey[0])
}

// if the sshkey has a comment/name in it, we can use that
if keyName == "" && len(splitKey) == 3 {
Expand All @@ -51,16 +34,13 @@ func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmai
keyName = userEmail
output.RenderInfo("no name provided, using email address as key name\n", outputOptions)
}
SSHKeyInput := schema.AddSSHKeyInput{
SSHKey: schema.SSHKey{
KeyType: keyType,
KeyValue: stripNewLines(splitKey[1]),
Name: keyName,
},
SSHKeyInput := schema.AddUserSSHPublicKeyInput{
PublicKey: keyValue,
Name: keyName,
UserEmail: userEmail,
}

return SSHKeyInput, err
return &SSHKeyInput, nil
}

var addUserCmd = &cobra.Command{
Expand Down Expand Up @@ -189,7 +169,7 @@ Add key by defining key value, but not specifying a key name (will default to tr
if err != nil {
return err
}
result, err := lagoon.AddSSHKey(context.TODO(), &userSSHKey, lc)
result, err := lagoon.AddUserSSHPublicKey(context.TODO(), userSSHKey, lc)
if err != nil {
return err
}
Expand Down Expand Up @@ -236,7 +216,7 @@ var deleteSSHKeyCmd = &cobra.Command{
debug)

if yesNo(fmt.Sprintf("You are attempting to delete SSH key ID:'%d', are you sure?", sshKeyID)) {
_, err := lagoon.RemoveSSHKey(context.TODO(), sshKeyID, lc)
_, err := lagoon.DeleteUserSSHPublicKey(context.TODO(), sshKeyID, lc)
if err != nil {
return err
}
Expand Down

0 comments on commit 67e51b9

Please sign in to comment.