Skip to content

Commit

Permalink
Add support for retrieving password using command
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Oct 4, 2023
1 parent 1b1cf51 commit 5f0cd0d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"strings"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
Expand Down Expand Up @@ -50,6 +52,7 @@ var (
osAuthURL string
osUsername string
osPassword string
osPwCmd string
osUserDomainID string
osUserDomainName string
osProjectID string
Expand Down Expand Up @@ -77,6 +80,7 @@ func newRootCmd(v *VersionInfo) *cobra.Command {
cmd.PersistentFlags().StringVar(&osAuthURL, "os-auth-url", "", "authentication URL")
cmd.PersistentFlags().StringVar(&osUsername, "os-username", "", "username")
cmd.PersistentFlags().StringVar(&osPassword, "os-password", "", "user's Password")
cmd.PersistentFlags().StringVar(&osPwCmd, "os-pw-cmd", "", "command from which to retrieve the user's password")
cmd.PersistentFlags().StringVar(&osUserDomainID, "os-user-domain-id", "", "user's domain ID")
cmd.PersistentFlags().StringVar(&osUserDomainName, "os-user-domain-name", "", "user's domain name")
cmd.PersistentFlags().StringVar(&osProjectID, "os-project-id", "", "project ID to scope to")
Expand Down Expand Up @@ -105,6 +109,15 @@ func authenticate() (*gophercloud.ProviderClient, error) {
// Update OpenStack environment variables, if value(s) provided as flag.
updateOpenStackEnvVars()

pwCmd := os.Getenv("OS_PW_CMD")
if pwCmd != "" && os.Getenv("OS_PASSWORD") == "" {
// Retrieve user's password from external command.
out, err := exec.Command("sh", "-c", pwCmd).Output()
if err != nil {
return nil, util.WrapError(err, fmt.Sprintf("could not retrieve user password using: %s", pwCmd))
}
setenvIfVal("OS_PASSWORD", strings.TrimSuffix(string(out), "\n"))
}
ao, err := clientconfig.AuthOptions(nil)
if err != nil {
return nil, util.WrapError(err, "could not get auth variables")
Expand Down Expand Up @@ -190,6 +203,7 @@ func updateOpenStackEnvVars() {
setenvIfVal("OS_AUTH_URL", osAuthURL)
setenvIfVal("OS_USERNAME", osUsername)
setenvIfVal("OS_PASSWORD", osPassword)
setenvIfVal("OS_PW_CMD", osPwCmd)
setenvIfVal("OS_USER_DOMAIN_ID", osUserDomainID)
setenvIfVal("OS_USER_DOMAIN_NAME", osUserDomainName)
setenvIfVal("OS_PROJECT_ID", osProjectID)
Expand Down

0 comments on commit 5f0cd0d

Please sign in to comment.