-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
335 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.