Skip to content

Commit

Permalink
Finalising users api (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizedkyle authored Dec 4, 2020
1 parent 9e1c7bd commit 42cc228
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 58 deletions.
12 changes: 12 additions & 0 deletions api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ type CreateUserRequest struct {
Roleids []string `json:"roleIds"`
}

type DisableUserMfa struct {
Email string `json:"email"`
Password string `json:"password"`
}

type UpdateUserEmail struct {
Email string `json:"email"`
}

type UpdateUserRequest struct {
Firstname string `json:"firstName"`
Lastname string `json:"lastName"`
IsActive bool `json:"isActive"`
Roleids []string `json:"roleIds"`
}

type UserResponse struct {
Firstname string `json:"firstName"`
Lastname string `json:"lastName"`
Expand Down
29 changes: 15 additions & 14 deletions pkg/cmd/roles/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewCmdRoleUpdate() *cobra.Command {

cmd := &cobra.Command{
Use: "update",
Short: "Updates a Sumo Logic role",
Short: "Updates a Sumo Logic role.",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("Role update request started.")
Expand All @@ -39,25 +39,26 @@ func NewCmdRoleUpdate() *cobra.Command {
},
}

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the role to update")
cmd.Flags().StringVar(&name, "name", "", "Specify the name for the role")
cmd.Flags().StringVar(&description, "description", "", "Specify the role description")
cmd.Flags().StringVar(&filter, "filter", "", "Search filter for the role")
cmd.Flags().StringSliceVar(&users, "users", []string{}, "Comma deliminated list of user ids to add to the role")
cmd.Flags().StringSliceVar(&capabilities, "capabilities", []string{}, "Comma deliminated list of capabilities")
cmd.Flags().StringVar(&id, "id", "", "Specify the id of the role to update.")
cmd.Flags().StringVar(&name, "name", "", "Specify the name for the role.")
cmd.Flags().StringVar(&description, "description", "", "Specify the role description.")
cmd.Flags().StringVar(&filter, "filter", "", "Search filter for the role.")
cmd.Flags().StringSliceVar(&users, "users", []string{}, "Comma deliminated list of user ids to add to the role.")
cmd.Flags().StringSliceVar(&capabilities, "capabilities", []string{}, "Comma deliminated list of capabilities.")
cmd.Flags().BoolVar(&autofill, "autofill", true, "Is set to true by default.")
cmd.Flags().BoolVar(&merge, "merge", true, "Is set to true by default, if set to false it will overwrite the role")
cmd.Flags().StringVar(&output, "output", "", "Specify the field to export the value from")

cmd.Flags().BoolVar(&merge, "merge", true, "Is set to true by default, if set to false it will overwrite the role.")
cmd.Flags().StringVar(&output, "output", "", "Specify the field to export the value from.")
cmd.MarkFlagRequired("id")
cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("description")
cmd.MarkFlagRequired("filter")
cmd.MarkFlagRequired("users")
cmd.MarkFlagRequired("capabilities")
return cmd
}

func updateRole(id string, name string, description string, filter string, users []string, capabilities []string, autofill bool, merge bool, output string, logger zerolog.Logger) {
var roleInfo api.RoleData
if id == "" {
fmt.Println("--id field needs to be set.")
os.Exit(0)
}

if merge == true {
requestUrl := "v1/roles/" + id
Expand Down
12 changes: 2 additions & 10 deletions pkg/cmd/users/change/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUserChangeEmail() *cobra.Command {
Expand All @@ -31,20 +30,13 @@ func NewCmdUserChangeEmail() *cobra.Command {

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user that needs to have the email changed.")
cmd.Flags().StringVar(&email, "email", "", "Specify the users new email address.")
cmd.MarkFlagRequired("id")
cmd.MarkFlagRequired("email")

return cmd
}

func userChangeEmail(id string, email string, logger zerolog.Logger) {
if id == "" {
fmt.Println("--id field needs to be set.")
os.Exit(0)
}
if email == "" {
fmt.Println("--email field needs to be set.")
os.Exit(0)
}

requestBodySchema := &api.UpdateUserEmail{
Email: email,
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/users/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func NewCmdUserCreate() *cobra.Command {
cmd.Flags().StringVar(&emailAddress, "email", "", "Email address of the user")
cmd.Flags().StringSliceVar(&roleIds, "roleids", []string{}, "Comma deliminated list of Role Ids")
cmd.Flags().StringVar(&output, "output", "", "Specify the field to export the value from")
cmd.MarkFlagRequired("firstname")
cmd.MarkFlagRequired("lastname")
cmd.MarkFlagRequired("email")
cmd.MarkFlagRequired("roleids")

return cmd
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/cmd/users/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUserDelete() *cobra.Command {
Expand All @@ -31,16 +30,11 @@ func NewCmdUserDelete() *cobra.Command {

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user to delete.")
cmd.Flags().StringVar(&transferTo, "transferto", "", "Specify the id of the user to transfer data to.")

cmd.MarkFlagRequired("id")
return cmd
}

func deleteUser(id string, transferTo string, logger zerolog.Logger) {
if id == "" {
fmt.Println("--id field needs to be set.")
os.Exit(0)
}

requestUrl := "v1/users/" + id
client, request := factory.NewHttpRequest("DELETE", requestUrl)
response, err := client.Do(request)
Expand Down
82 changes: 74 additions & 8 deletions pkg/cmd/users/disable/disable.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,98 @@
package disable

import (
"encoding/json"
"errors"
"fmt"
"github.com/manifoldco/promptui"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/wizedkyle/sumocli/api"
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUserDisableMFA() *cobra.Command {
var (
id string
email string
password string
)

cmd := &cobra.Command{
Use: "disable mfa",
Short: "Disables MFA for a Sumo Logic user.",
Short: "Disables MFA for a Sumo Logic user (this command only works interactively).",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("User disable mfa request started.")
userDisableMFA(id, email, password, logger)
userDisableMFA(logger)
logger.Debug().Msg("User disable mfa request finished.")
},
}

return cmd
}

func userDisableMFA(id string, email string, password string, logger zerolog.Logger) {
func userDisableMFA(logger zerolog.Logger) {
validate := func(input string) error {
if input == "" {
return errors.New("Value is empty")
}
return nil
}

promptId := promptui.Prompt{
Label: "Please enter the Sumo Logic id for the user",
Validate: validate,
}

promptEmail := promptui.Prompt{
Label: "Please enter the Sumo Logic users email address",
Validate: validate,
}

promptPassword := promptui.Prompt{
Label: "Please enter the Sumo Logic users password",
Mask: '*',
Validate: validate,
}

promptConfirm := promptui.Prompt{
Label: "Confirm that you want to disable MFA? Removing MFA can be a security risk!",
IsConfirm: true,
}

idResult, err := promptId.Run()
emailResult, err := promptEmail.Run()
passwordResult, err := promptPassword.Run()
_, err = promptConfirm.Run()

if err != nil {
logging.LogError(err, logger)
os.Exit(0)
}

requestBodySchema := &api.DisableUserMfa{
Email: emailResult,
Password: passwordResult,
}
requestBody, _ := json.Marshal(requestBodySchema)
requestUrl := "v1/users/" + idResult + "/mfa/disable"
client, request := factory.NewHttpRequestWithBody("PUT", requestUrl, requestBody)
response, err := client.Do(request)
logging.LogError(err, logger)

defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
logging.LogError(err, logger)

if response.StatusCode != 204 {
var responseError api.ResponseError
jsonErr := json.Unmarshal(responseBody, &responseError)
logging.LogError(jsonErr, logger)
if responseError.Errors[0].Message != "" {
fmt.Println(responseError.Errors[0].Message)
} else if responseError.Errors[0].Code == "auth1:mfa_not_allowed" {
fmt.Println("MFA is not enabled on user " + emailResult)
}
} else {
fmt.Println("MFA removed from user " + emailResult)
}
}
8 changes: 1 addition & 7 deletions pkg/cmd/users/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
"strings"
)

Expand Down Expand Up @@ -44,18 +43,13 @@ lastLoginTimestamp

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user to get")
cmd.Flags().StringVar(&output, "output", "", "Specify the field to export the value from")

cmd.MarkFlagRequired("id")
return cmd
}

func getUser(id string, output string, logger zerolog.Logger) {
var userInfo api.UserResponse

if id == "" {
fmt.Println("--id field needs to be specified.")
os.Exit(0)
}

requestUrl := "v1/users/" + id
client, request := factory.NewHttpRequest("GET", requestUrl)
response, err := client.Do(request)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/users/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func listUsers(email string, numberOfResults string, sortBy string, output strin
} else {
if factory.ValidateUserOutput(output) == true {
value := gjson.Get(string(userInfoJson), "#."+output)
formattedValue := strings.Trim(value.String(), `"[]"`)
formattedValue := strings.Trim(value.String(), `[]`)
formattedValue = strings.ReplaceAll(formattedValue, "\"", "")
fmt.Println(formattedValue)
} else {
fmt.Println(string(userInfoJson))
Expand Down
55 changes: 55 additions & 0 deletions pkg/cmd/users/reset/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package reset

import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/wizedkyle/sumocli/api"
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
)

func NewCmdUserResetPassword() *cobra.Command {
var id string

cmd := &cobra.Command{
Use: "reset password",
Short: "Initiates a password reset for a Sumo Logic user.",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.GetLoggerForCommand(cmd)
logger.Debug().Msg("User reset password request started.")
userResetPassword(id, logger)
logger.Debug().Msg("User reset password request finished.")
},
}

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user which requires a password reset.")
cmd.MarkFlagRequired("id")

return cmd
}

func userResetPassword(id string, logger zerolog.Logger) {
requestUrl := "v1/users/" + id + "/password/reset"
client, request := factory.NewHttpRequest("POST", requestUrl)
response, err := client.Do(request)
logging.LogError(err, logger)

defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
logging.LogError(err, logger)

if response.StatusCode != 204 {
var responseError api.ResponseError
jsonErr := json.Unmarshal(responseBody, &responseError)
fmt.Println(responseBody)
logging.LogError(jsonErr, logger)
if responseError.Errors[0].Message != "" {
fmt.Println(responseError.Errors[0].Message)
}
} else {
fmt.Println("Password reset request completed.")
}
}
8 changes: 1 addition & 7 deletions pkg/cmd/users/unlock/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/wizedkyle/sumocli/pkg/cmd/factory"
"github.com/wizedkyle/sumocli/pkg/logging"
"io/ioutil"
"os"
)

func NewCmdUnlockUser() *cobra.Command {
Expand All @@ -27,16 +26,11 @@ func NewCmdUnlockUser() *cobra.Command {
}

cmd.Flags().StringVar(&id, "id", "", "Specify the id of the user account to unlock")

cmd.MarkFlagRequired("id")
return cmd
}

func unlockUser(id string, logger zerolog.Logger) {
if id == "" {
fmt.Println("--id field needs to be specified.")
os.Exit(0)
}

requestUrl := "v1/users/" + id + "/unlock"
client, request := factory.NewHttpRequest("POST", requestUrl)
response, err := client.Do(request)
Expand Down
Loading

0 comments on commit 42cc228

Please sign in to comment.