Skip to content

Commit

Permalink
Fixes & updates user-sshkey
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Nov 16, 2023
1 parent 5ce8a2a commit 5e8c5a5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 25 deletions.
95 changes: 72 additions & 23 deletions cmd/users.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -147,22 +151,36 @@ var deleteSSHKeyCmd = &cobra.Command{
Use: "user-sshkey",
Aliases: []string{"u"},
Short: "Delete an SSH key from Lagoon",
Run: func(cmd *cobra.Command, args []string) {
if sshKeyName == "" {
fmt.Println("Missing arguments: SSH key name is not defined")
cmd.Help()
os.Exit(1)
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
sshKeyID, err := cmd.Flags().GetUint("id")
if err != nil {
return err
}
var customReqResult []byte
var err error
if yesNo(fmt.Sprintf("You are attempting to delete SSH key named '%s', are you sure?", sshKeyName)) {
customReqResult, err = uClient.DeleteSSHKey(sshKeyName)
if sshKeyID == 0 {
fmt.Println("Missing arguments: SSH key ID is not defined")
return nil
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)

if yesNo(fmt.Sprintf("You are attempting to delete SSH key ID:'%d', are you sure?", sshKeyID)) {
_, err := l.RemoveSSHKey(context.TODO(), sshKeyID, lc)
handleError(err)
resultData := output.Result{
Result: string(customReqResult),
Result: "success",
}
output.RenderResult(resultData, outputOptions)
}
return nil
},
}

Expand Down Expand Up @@ -226,23 +244,55 @@ var getUserKeysCmd = &cobra.Command{
Aliases: []string{"us"},
Short: "Get a user's SSH keys",
Long: `Get a user's SSH keys. This will only work for users that are part of a group`,
Run: func(cmd *cobra.Command, args []string) {
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
userEmail, err := cmd.Flags().GetString("email")
if err != nil {
return err
}
if userEmail == "" {
fmt.Println("Missing arguments: Email address is not defined")
cmd.Help()
os.Exit(1)
return nil
}
returnedJSON, err := uClient.ListUserSSHKeys(groupName, strings.ToLower(userEmail), false)
handleError(err)
var dataMain output.Table
err = json.Unmarshal([]byte(returnedJSON), &dataMain)

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
userKeys, err := l.GetUserSSHKeysByEmail(context.TODO(), userEmail, lc)
handleError(err)
if len(dataMain.Data) == 0 {
if len(userKeys.SSHKeys) == 0 {
output.RenderInfo(fmt.Sprintf("No SSH keys for user '%s'", strings.ToLower(userEmail)), outputOptions)
os.Exit(0)
return nil
}

data := []output.Data{}
for _, userkey := range userKeys.SSHKeys {
data = append(data, []string{
strconv.Itoa(int(userkey.ID)),
userKeys.Email,
userkey.Name,
string(userkey.KeyType),
userkey.KeyValue,
})
}
output.RenderOutput(dataMain, outputOptions)

dataMain := output.Table{
Header: []string{"ID", "Email", "Name", "Type", "Value"},
Data: data,
}

output.RenderOutput(dataMain, outputOptions)
return nil
},
}

Expand Down Expand Up @@ -281,12 +331,11 @@ func init() {
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 SSH key")
deleteSSHKeyCmd.Flags().Uint("id", 0, "ID of the SSH key")
updateUserCmd.Flags().StringVarP(&userFirstName, "firstName", "F", "", "New first name of the user")
updateUserCmd.Flags().StringVarP(&userLastName, "lastName", "L", "", "New last name of the user")
updateUserCmd.Flags().StringVarP(&userEmail, "email", "E", "", "New email address of the user")
updateUserCmd.Flags().StringVarP(&currentUserEmail, "current-email", "C", "", "Current email address of the user")
getUserKeysCmd.Flags().StringVarP(&userEmail, "email", "E", "", "New email address of the user")
getUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to check users in (if not specified, will default to all groups)")
getUserKeysCmd.Flags().StringP("email", "E", "", "New email address of the user")
getAllUserKeysCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)")
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/guregu/null v4.0.0+incompatible
// workaround for https://github.com/manifoldco/promptui/issues/98
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/uselagoon/machinery v0.0.11
github.com/uselagoon/machinery v0.0.13-0.20231116024123-c712ade42522
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
Expand All @@ -41,4 +41,4 @@ replace github.com/olekukonko/tablewriter => github.com/shreddedbacon/tablewrite

// replace github.com/machinebox/graphql => ../../shreddedbacon/graphql

// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter
// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
Expand Down Expand Up @@ -107,6 +108,8 @@ github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYv
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/uselagoon/machinery v0.0.11 h1:s6EhyU/pj1+C4FdS0EqmR6C0dLsoeCd9n+5xHL1YDag=
github.com/uselagoon/machinery v0.0.11/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE=
github.com/uselagoon/machinery v0.0.13-0.20231116024123-c712ade42522 h1:6YU7GMpDL+3xwIbdtTwIYEOWJFbrKd20BeOXtAFEkyg=
github.com/uselagoon/machinery v0.0.13-0.20231116024123-c712ade42522/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down

0 comments on commit 5e8c5a5

Please sign in to comment.