@@ -36,6 +36,7 @@ import (
3636 "sync"
3737 "time"
3838 "unicode/utf8"
39+ "encoding/json"
3940
4041 "flag"
4142
7172 setFlag = flag .Bool ("set" , false , `When set, CLI will perform a Set request. Usage: gnmi_cli -set -proto <gnmi.SetRequest> -address <address> [other flags ...]` )
7273
7374 withUserPass = flag .Bool ("with_user_pass" , false , "When set, CLI will prompt for username/password to use when connecting to a target." )
75+ insecureUsername = flag .String ("insecure_username" , "" , "Username passed via argument." )
76+ insecurePassword = flag .String ("insecure_password" , "" , "Password passed via argument for Username." )
77+ credentialsFile = flag .String ("credentials_file" , "" , `File of format { "Username": "demo", "Password": "demo" }` )
7478
7579 // Certificate files.
7680 caCert = flag .String ("ca_crt" , "" , "CA certificate file. Used to verify server TLS certificate." )
@@ -133,11 +137,36 @@ func main() {
133137 log .Exit ("--address must be set" )
134138 }
135139 if * withUserPass {
140+ if * insecureUsername != "" || * insecurePassword != "" {
141+ log .Exit ("Do not pass --insecure_username or --insecure_password if prompting --with_user_pass" )
142+ } else if * credentialsFile != "" {
143+ log .Exit ("Do not pass --credentials_file file if prompting --with_user_pass" )
144+ }
136145 var err error
137146 q .Credentials , err = readCredentials ()
138147 if err != nil {
139148 log .Exit (err )
140149 }
150+ } else if * insecureUsername != "" || * insecurePassword != "" {
151+ if * credentialsFile != "" {
152+ log .Exit ("Do not pass --credentials_file with --insecure_username or --insecure_password" )
153+ }
154+ if * insecureUsername == "" {
155+ log .Exit ("Must supply --insecure_username with --insecure_password" )
156+ }
157+ if * insecurePassword == "" {
158+ log .Exit ("Must supply --insecure_password with --insecure_username" )
159+ }
160+ q .Credentials = & client.Credentials {
161+ Username : * insecureUsername ,
162+ Password : * insecurePassword ,
163+ }
164+ } else if * credentialsFile != "" {
165+ var err error
166+ q .Credentials , err = readCredentialsFile (* credentialsFile )
167+ if err != nil {
168+ log .Exit (err )
169+ }
141170 }
142171
143172 if * caCert != "" {
@@ -307,6 +336,21 @@ func readCredentials() (*client.Credentials, error) {
307336 return c , nil
308337}
309338
339+ func readCredentialsFile (filename string ) (* client.Credentials , error ) {
340+ creds := & client.Credentials {}
341+
342+ credFile , err := ioutil .ReadFile (filename )
343+ if err != nil {
344+ return nil , err
345+ }
346+ err = json .Unmarshal (credFile , & creds )
347+ if err != nil {
348+ return nil , err
349+ }
350+
351+ return creds , nil
352+ }
353+
310354func parseQuery (query , delim string ) ([]string , error ) {
311355 d , w := utf8 .DecodeRuneInString (delim )
312356 if w == 0 || w != len (delim ) {
0 commit comments