From 74b83174fb404c085b64d703f41f935e02bf2621 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Mon, 23 Sep 2019 14:30:20 +0300 Subject: [PATCH 1/2] PMM-4743 Allow to pass password individually. --- commands/base.go | 26 +++++++++++++++++++++++++- main.go | 6 ++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/commands/base.go b/commands/base.go index 3403b94d..bb0eff33 100644 --- a/commands/base.go +++ b/commands/base.go @@ -146,9 +146,10 @@ func (e errFromNginx) GoString() string { } // SetupClients configures local and PMM Server API clients. -func SetupClients(ctx context.Context, serverURL string) { +func SetupClients(ctx context.Context, serverURL, serverUsername, serverPassword string, serverInsecureTLS bool) { agentlocal.SetTransport(ctx, GlobalFlags.Debug || GlobalFlags.Trace) + // set server URL if serverURL == "" { status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo) if err != nil { @@ -181,6 +182,29 @@ func SetupClients(ctx context.Context, serverURL string) { } } + // override username, password, insecure-tls if given + if serverUsername != "" || serverPassword != "" { + var newUsername, newPassword string + if GlobalFlags.ServerURL.User != nil { + newUsername = GlobalFlags.ServerURL.User.Username() + newPassword, _ = GlobalFlags.ServerURL.User.Password() + } + if serverUsername != "" { + newUsername = serverUsername + } + if serverPassword != "" { + newPassword = serverPassword + } + if newPassword != "" { + GlobalFlags.ServerURL.User = url.UserPassword(newUsername, newPassword) + } else { + GlobalFlags.ServerURL.User = url.User(newUsername) + } + } + if serverInsecureTLS { + GlobalFlags.ServerInsecureTLS = true + } + // use JSON APIs over HTTP/1.1 transport := httptransport.New(GlobalFlags.ServerURL.Host, GlobalFlags.ServerURL.Path, []string{GlobalFlags.ServerURL.Scheme}) if u := GlobalFlags.ServerURL.User; u != nil { diff --git a/main.go b/main.go index 56a1657a..cd923e9a 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,9 @@ func main() { kingpin.CommandLine.UsageTemplate(commands.UsageTemplate) serverURLF := kingpin.Flag("server-url", "PMM Server URL in `https://username:password@pmm-server-host/` format").String() - kingpin.Flag("server-insecure-tls", "Skip PMM Server TLS certificate validation").BoolVar(&commands.GlobalFlags.ServerInsecureTLS) + serverUsernameF := kingpin.Flag("server-username", "Username to connect to PMM Server").String() + serverPasswordF := kingpin.Flag("server-password", "Password to connect to PMM Server").String() + serverInsecureTLS := kingpin.Flag("server-insecure-tls", "Skip PMM Server TLS certificate validation").Bool() kingpin.Flag("debug", "Enable debug logging").BoolVar(&commands.GlobalFlags.Debug) kingpin.Flag("trace", "Enable trace logging (implies debug)").BoolVar(&commands.GlobalFlags.Trace) jsonF := kingpin.Flag("json", "Enable JSON output").Bool() @@ -73,7 +75,7 @@ func main() { cancel() }() - commands.SetupClients(ctx, *serverURLF) + commands.SetupClients(ctx, *serverURLF, *serverUsernameF, *serverPasswordF, *serverInsecureTLS) allCommands := map[string]commands.Command{ management.RegisterC.FullCommand(): management.Register, From 811f50d67d6bb1332e3aec4071e4d95f892d785e Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Mon, 23 Sep 2019 17:54:40 +0300 Subject: [PATCH 2/2] PMM-4743 Do not store password if requested. --- commands/config.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/commands/config.go b/commands/config.go index a9c680e0..af0d688f 100644 --- a/commands/config.go +++ b/commands/config.go @@ -51,7 +51,8 @@ type configCommand struct { Region string Az string - Force bool + Force bool + DontStorePassword bool } func (cmd *configCommand) args() (res []string, switchedToTLS bool) { @@ -98,6 +99,9 @@ func (cmd *configCommand) args() (res []string, switchedToTLS bool) { if cmd.Force { res = append(res, "--force") } + if cmd.DontStorePassword { + res = append(res, "--dont-store-password") + } res = append(res, cmd.NodeAddress, cmd.NodeType, cmd.NodeName) return //nolint:nakedret } @@ -148,4 +152,5 @@ func init() { ConfigC.Flag("az", "Node availability zone").StringVar(&Config.Az) ConfigC.Flag("force", "Remove Node with that name with all dependent Services and Agents if one exist").BoolVar(&Config.Force) + ConfigC.Flag("dont-store-password", "Do not store password in configuration file").BoolVar(&Config.DontStorePassword) }