Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1990975: Enhance mechanism of reading ibm cloud apikey #365

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/cmd/provisioning/ibmcloud/create_shared_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type: Opaque`
manifestsDirName = "manifests"
)

// APIKeyEnvVar is the environment variable name containing an IBM Cloud API key
const APIKeyEnvVar = "IC_API_KEY"
// APIKeyEnvVars is a list of environment variable names containing an IBM Cloud API key
var APIKeyEnvVars = []string{"IC_API_KEY", "IBMCLOUD_API_KEY", "BM_API_KEY", "BLUEMIX_API_KEY"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any kind of precedence criteria here? How ibm cloud cli handles it when all of these environment variables are set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkumatag lmk what you come up with here. i'm guessing we should be consistent with the installer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is missing, we can enhance the code in the installer as well,


var (
// CreateOpts captures the options that affect creation of the generated
Expand All @@ -57,10 +57,20 @@ func NewCreateSharedSecretsCmd() *cobra.Command {
return createSecretsCmd
}

// getEnv reads the content from first found environment variable from the envs list, returns empty string if none found.
func getEnv(envs []string) string {
for _, k := range envs {
if v := os.Getenv(k); v != "" {
return v
}
}
return ""
}

func createSharedSecretsCmd(cmd *cobra.Command, args []string) error {
apiKey := os.Getenv(APIKeyEnvVar)
apiKey := getEnv(APIKeyEnvVars)
if apiKey == "" {
return fmt.Errorf("%s environment variable not set", APIKeyEnvVar)
return fmt.Errorf("%s environment variable not set", APIKeyEnvVars)
}

err := createSharedSecrets(CreateOpts.CredRequestDir, CreateOpts.TargetDir, apiKey)
Expand Down
51 changes: 49 additions & 2 deletions pkg/cmd/provisioning/ibmcloud/create_shared_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestCreateSecretsCmd(t *testing.T) {
{
name: "CreateSecretsCmd should populate secret with API key environment variable",
setup: func(t *testing.T) string {
os.Setenv(APIKeyEnvVar, apiKey)
os.Setenv(APIKeyEnvVars[0], apiKey)
tempDirName, err := ioutil.TempDir(os.TempDir(), testDirPrefix)
require.NoError(t, err, "Failed to create temp directory")

Expand Down Expand Up @@ -67,7 +67,7 @@ func TestCreateSecretsCmd(t *testing.T) {
{
name: "CreateSharedSecretsCmd with unset API key environment variable should fail",
setup: func(t *testing.T) string {
os.Setenv(APIKeyEnvVar, "")
os.Setenv(APIKeyEnvVars[0], "")
tempDirName, err := ioutil.TempDir(os.TempDir(), testDirPrefix)
require.NoError(t, err, "Failed to create temp directory")

Expand Down Expand Up @@ -144,3 +144,50 @@ spec:

return nil
}

func Test_getEnv(t *testing.T) {
type env struct {
variable, value string
}
tests := []struct {
name string
envs []env
want string
}{
{
name: "Return IC_API_KEY value",
envs: []env{
{"IBMCLOUD_API_KEY", "IBMCLOUD_API_KEY_apikey"},
{"BM_API_KEY", "BM_API_KEY_apikey"},
{"IC_API_KEY", "IC_API_KEY_apikey"},
{"BLUEMIX_API_KEY", "BLUEMIX_API_KEY_apikey"},
},
want: "IC_API_KEY_apikey",
},
{
name: "Return IBMCLOUD_API_KEY value",
envs: []env{
{"BM_API_KEY", "BM_API_KEY_apikey"},
{"BLUEMIX_API_KEY", "BLUEMIX_API_KEY_apikey"},
{"IBMCLOUD_API_KEY", "IBMCLOUD_API_KEY_apikey"},
},
want: "IBMCLOUD_API_KEY_apikey",
},
{
name: "Returns empty value",
envs: []env{},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, env := range tt.envs {
os.Setenv(env.variable, env.value)
defer os.Unsetenv(env.variable)
}
if got := getEnv(APIKeyEnvVars); got != tt.want {
t.Errorf("getEnv() = %v, want %v", got, tt.want)
}
})
}
}