Skip to content

Commit

Permalink
refactor: use newer sshkey functions to expand key support
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 13, 2024
1 parent 82a91e3 commit a65969c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 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
if strings.EqualFold(string(splitKey[0]), "ssh-rsa") {
keyType = schema.SSHRsa
} else if strings.EqualFold(string(splitKey[0]), "ssh-ed25519") {
keyType = schema.SSHEd25519
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp256") {
keyType = schema.SSHECDSA256
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp384") {
keyType = schema.SSHECDSA384
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp521") {
keyType = schema.SSHECDSA521
} else {
// return error stating key type not supported
keyType = schema.SSHRsa
err = fmt.Errorf(fmt.Sprintf("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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.2
github.com/uselagoon/machinery v0.0.29
github.com/uselagoon/machinery v0.0.30-0.20241010061852-aafb4bdec1e4
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.26.0
golang.org/x/term v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c=
github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc=
github.com/uselagoon/machinery v0.0.30-0.20241010061852-aafb4bdec1e4 h1:odS15hoYXdwMa9BZEuqjHbo1fQr9mZscARMSD/CS69k=
github.com/uselagoon/machinery v0.0.30-0.20241010061852-aafb4bdec1e4/go.mod h1:RsHzIMOam3hiA4CKR12yANgzdTGy6tz4D19umjMzZyw=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
Expand Down

0 comments on commit a65969c

Please sign in to comment.