From a65969c655ebc46e88d72cfd036f286ab8017cd7 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Mon, 14 Oct 2024 10:38:54 +1100 Subject: [PATCH] refactor: use newer sshkey functions to expand key support --- cmd/users.go | 40 ++++++++++------------------------------ go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/cmd/users.go b/cmd/users.go index 7fc9b640..7264607a 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -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 { @@ -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{ @@ -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 } @@ -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 } diff --git a/go.mod b/go.mod index 521839cf..438ff8bf 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c1e5de2e..88d71ae4 100644 --- a/go.sum +++ b/go.sum @@ -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=