Skip to content

Commit

Permalink
cluster: add missed credentials pass methods
Browse files Browse the repository at this point in the history
The patch adds etcd credentials pass via environment variables
and command flags.

Part of #631
  • Loading branch information
oleg-jukovec committed Oct 24, 2023
1 parent d7e1b4e commit 6d50cd4
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 38 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ stdout/stderr and `tt` logs go to `tt.log` file.
### Added

- tt completion: added luarocks completions.
- tt cluster: credentials could be passed via environment variables and command
flags.

### Fixed
- ``tt rocks``: broken ``--verbose`` option.
Expand Down
8 changes: 8 additions & 0 deletions cli/cluster/cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
// PublishCtx contains information abould cluster publish command execution
// context.
type PublishCtx struct {
// Username defines a etcd username.
Username string
// Password defines a etcd password.
Password string
// Force defines whether the publish should be forced and a validation step
// is omitted.
Force bool
Expand All @@ -25,6 +29,10 @@ func PublishEtcd(publishCtx PublishCtx, uri *url.URL) error {
if err != nil {
return fmt.Errorf("invalid URL %q: %w", uri, err)
}
if etcdOpts.Username == "" && etcdOpts.Password == "" {
etcdOpts.Username = publishCtx.Username
etcdOpts.Password = publishCtx.Password
}

instance := uri.Query().Get("name")
if err := publishCtxValidateConfig(publishCtx, instance); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions cli/cluster/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (

// ShowCtx contains information about cluster show command execution context.
type ShowCtx struct {
// Username defines a etcd username.
Username string
// Password defines a etcd password.
Password string
// Validate defines whether the command will check the showed
// configuration.
Validate bool
Expand All @@ -20,6 +24,10 @@ func ShowEtcd(showCtx ShowCtx, uri *url.URL) error {
if err != nil {
return fmt.Errorf("invalid URL %q: %w", uri, err)
}
if etcdOpts.Username == "" && etcdOpts.Password == "" {
etcdOpts.Username = showCtx.Username
etcdOpts.Password = showCtx.Password
}

etcdcli, err := cluster.ConnectEtcd(etcdOpts)
if err != nil {
Expand Down
37 changes: 35 additions & 2 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
clustercmd "github.com/tarantool/tt/cli/cluster/cmd"
"github.com/tarantool/tt/cli/cmd/internal"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/connect"
"github.com/tarantool/tt/cli/modules"
"github.com/tarantool/tt/cli/running"
)
Expand All @@ -23,11 +24,15 @@ const (
)

var showCtx = clustercmd.ShowCtx{
Username: "",
Password: "",
Validate: false,
}

var publishCtx = clustercmd.PublishCtx{
Force: false,
Username: "",
Password: "",
Force: false,
}

func NewClusterCmd() *cobra.Command {
Expand All @@ -51,7 +56,15 @@ Possible arguments:
* ssl_ca_path - a path to a trusted certificate authorities (CA) directory.
* verify_host - set off (default true) verification of the certificate’s name against the host.
* verify_peer - set off (default true) verification of the peer’s SSL certificate.
`, float64(cluster.DefaultEtcdTimeout)/float64(time.Second))
You could also specify etcd username and password with environment variables:
* %s - specifies a etcd username
* %s - specidies a etcd password
The priority of credentials:
environment variables < command flags < URL credentials.
`, float64(cluster.DefaultEtcdTimeout)/float64(time.Second),
connect.EtcdUsernameEnv, connect.EtcdPasswordEnv)

show := &cobra.Command{
Use: "show (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>)",
Expand Down Expand Up @@ -82,6 +95,10 @@ Possible arguments:
running.ExtractActiveInstanceNames)
},
}
show.Flags().StringVarP(&showCtx.Username, "username", "u", "",
"username (used as etcd credentials only)")
show.Flags().StringVarP(&showCtx.Password, "password", "p", "",
"password (used as etcd credentials only)")
show.Flags().BoolVar(&showCtx.Validate, "validate", showCtx.Validate,
"validate the configuration")
clusterCmd.AddCommand(show)
Expand Down Expand Up @@ -116,6 +133,10 @@ Possible arguments:
running.ExtractActiveInstanceNames)
},
}
publish.Flags().StringVarP(&publishCtx.Username, "username", "u", "",
"username (used as etcd credentials only)")
publish.Flags().StringVarP(&publishCtx.Password, "password", "p", "",
"password (used as etcd credentials only)")
publish.Flags().BoolVar(&publishCtx.Force, "force", publishCtx.Force,
"force publish and skip validation")
clusterCmd.AddCommand(publish)
Expand All @@ -126,6 +147,12 @@ Possible arguments:
// internalClusterShowModule is an entrypoint for `cluster show` command.
func internalClusterShowModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
if uri, err := url.Parse(args[0]); err == nil && uri.Scheme != "" {
if showCtx.Username == "" {
showCtx.Username = os.Getenv(connect.EtcdUsernameEnv)
}
if showCtx.Password == "" {
showCtx.Password = os.Getenv(connect.EtcdPasswordEnv)
}
return clustercmd.ShowEtcd(showCtx, uri)
}

Expand All @@ -152,6 +179,12 @@ func internalClusterPublishModule(cmdCtx *cmdcontext.CmdCtx, args []string) erro
publishCtx.Config = config

if uri, err := url.Parse(args[0]); err == nil && uri.Scheme != "" {
if publishCtx.Username == "" {
publishCtx.Username = os.Getenv(connect.EtcdUsernameEnv)
}
if publishCtx.Password == "" {
publishCtx.Password = os.Getenv(connect.EtcdPasswordEnv)
}
return clustercmd.PublishEtcd(publishCtx, uri)
}

Expand Down
6 changes: 6 additions & 0 deletions cli/connect/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package connect

// EtcdUsernameEnv is an environment variable with a username for etcd.
const EtcdUsernameEnv = "TT_CLI_ETCD_USERNAME"

// EtcdPasswordEnv is an environment variable with a password for etcd.
const EtcdPasswordEnv = "TT_CLI_ETCD_PASSWORD"

// setLanguagePrefix is used to set a language in the Tarantool console.
const setLanguagePrefix = "\\set language"

Expand Down
Loading

0 comments on commit 6d50cd4

Please sign in to comment.