Skip to content

Commit

Permalink
Fix #32 add cybr logon --non-interactive (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewCopeland authored Jan 28, 2021
1 parent 6d6557d commit f5bc894
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions cmd/logon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"os"
"strings"
"syscall"

Expand All @@ -20,6 +21,8 @@ var (
InsecureTLS bool
// BaseURL to send PAS REST API logon request to
BaseURL string
// NonInteractive logon
NonInteractive bool
)

var logonCmd = &cobra.Command{
Expand All @@ -33,13 +36,22 @@ var logonCmd = &cobra.Command{
$ cybr logon -u $USERNAME -a $AUTH_TYPE -b https://pvwa.example.com -i`,
Aliases: []string{"login"},
Run: func(cmd *cobra.Command, args []string) {
// Get secret value from STDIN
fmt.Print("Enter password: ")
byteSecretVal, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err != nil {
log.Fatalln("An error occurred trying to read password from " +
"Stdin. Exiting...")
password := os.Getenv("PAS_PASSWORD")

if !NonInteractive {
// Get secret value from STDIN
fmt.Print("Enter password: ")
byteSecretVal, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println()
if err != nil {
log.Fatalln("An error occurred trying to read password from " +
"Stdin. Exiting...")
}
password = string(byteSecretVal)
}

if password == "" {
log.Fatalf("Provided password is empty")
}

client := pasapi.Client{
Expand All @@ -50,10 +62,10 @@ var logonCmd = &cobra.Command{

credentials := pasapi.LogonRequest{
Username: Username,
Password: string(byteSecretVal),
Password: password,
}

err = client.Logon(credentials)
err := client.Logon(credentials)
if err != nil && !strings.Contains(err.Error(), "ITATS542I") {
log.Fatalf("Failed to Logon to the PVWA. %s", err)
}
Expand Down Expand Up @@ -93,5 +105,7 @@ func init() {
logonCmd.Flags().BoolVarP(&InsecureTLS, "insecure-tls", "i", false, "If detected, TLS will not be verified")
logonCmd.Flags().StringVarP(&BaseURL, "base-url", "b", "", "Base URL to send Logon request to [https://pvwa.example.com]")
logonCmd.MarkFlagRequired("base-url")
logonCmd.Flags().BoolVar(&NonInteractive, "non-interactive", false, "If detected, will retrieve the password from the PAS_PASSWORD environment variable")

rootCmd.AddCommand(logonCmd)
}

0 comments on commit f5bc894

Please sign in to comment.