Skip to content

Commit

Permalink
feat: Add CLI credentials from CLI or JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
remingtonc committed Aug 14, 2019
1 parent 52dcbf2 commit d7545e5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/gnmi_cli/gnmi_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sync"
"time"
"unicode/utf8"
"encoding/json"

"flag"

Expand Down Expand Up @@ -71,6 +72,9 @@ var (
setFlag = flag.Bool("set", false, `When set, CLI will perform a Set request. Usage: gnmi_cli -set -proto <gnmi.SetRequest> -address <address> [other flags ...]`)

withUserPass = flag.Bool("with_user_pass", false, "When set, CLI will prompt for username/password to use when connecting to a target.")
insecureUsername = flag.String("insecure_username", "", "Username passed via argument.")
insecurePassword = flag.String("insecure_password", "", "Password passed via argument for Username.")
credentialsFile = flag.String("credentials_file", "", `File of format { "Username": "demo", "Password": "demo" }`)

// Certificate files.
caCert = flag.String("ca_crt", "", "CA certificate file. Used to verify server TLS certificate.")
Expand Down Expand Up @@ -133,11 +137,36 @@ func main() {
log.Exit("--address must be set")
}
if *withUserPass {
if *insecureUsername != "" || *insecurePassword != "" {
log.Exit("Do not pass --insecure_username or --insecure_password if prompting --with_user_pass")
} else if *credentialsFile != "" {
log.Exit("Do not pass --credentials_file file if prompting --with_user_pass")
}
var err error
q.Credentials, err = readCredentials()
if err != nil {
log.Exit(err)
}
} else if *insecureUsername != "" || *insecurePassword != "" {
if *credentialsFile != "" {
log.Exit("Do not pass --credentials_file with --insecure_username or --insecure_password")
}
if *insecureUsername == "" {
log.Exit("Must supply --insecure_username with --insecure_password")
}
if *insecurePassword == "" {
log.Exit("Must supply --insecure_password with --insecure_username")
}
q.Credentials = &client.Credentials{
Username: *insecureUsername,
Password: *insecurePassword,
}
} else if *credentialsFile != "" {
var err error
q.Credentials, err = readCredentialsFile(*credentialsFile)
if err != nil {
log.Exit(err)
}
}

if *caCert != "" {
Expand Down Expand Up @@ -307,6 +336,21 @@ func readCredentials() (*client.Credentials, error) {
return c, nil
}

func readCredentialsFile(filename string) (*client.Credentials, error) {
creds := &client.Credentials{}

credFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
err = json.Unmarshal(credFile, &creds)
if err != nil {
return nil, err
}

return creds, nil
}

func parseQuery(query, delim string) ([]string, error) {
d, w := utf8.DecodeRuneInString(delim)
if w == 0 || w != len(delim) {
Expand Down

0 comments on commit d7545e5

Please sign in to comment.