From 483bc3ff349fb4bf4f319f9c59206f00b452d2f7 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 18 Oct 2023 19:18:09 +0545 Subject: [PATCH] fix: use new CmdEnv --- models/connections.go | 15 ++++++++++++++- models/connections_test.go | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/models/connections.go b/models/connections.go index a8883358..d4a6f1ea 100644 --- a/models/connections.go +++ b/models/connections.go @@ -188,11 +188,17 @@ func (c Connection) AsEnv(ctx context.Context) EnvPrep { if v, ok := c.Properties["region"]; ok { envPrep.Env = append(envPrep.Env, fmt.Sprintf("AWS_DEFAULT_REGION=%s", v)) + credentialFile.WriteString(fmt.Sprintf("region = %s\n", v)) + + envPrep.CmdEnvs = append(envPrep.CmdEnvs, fmt.Sprintf("AWS_DEFAULT_REGION=%s", v)) } envPrep.Files[credentialFilePath] = credentialFile + envPrep.CmdEnvs = append(envPrep.CmdEnvs, "AWS_EC2_METADATA_DISABLED=true") // https://github.com/aws/aws-cli/issues/5262#issuecomment-705832151 + envPrep.CmdEnvs = append(envPrep.CmdEnvs, fmt.Sprintf("AWS_SHARED_CREDENTIALS_FILE=%s", credentialFilePath)) + case ConnectionTypeAzure: args := []string{"login", "--service-principal", "--username", c.Username, "--password", c.Password} if v, ok := c.Properties["tenant"]; ok { @@ -214,6 +220,8 @@ func (c Connection) AsEnv(ctx context.Context) EnvPrep { // we need to explicitly activate it envPrep.PreRuns = append(envPrep.PreRuns, exec.CommandContext(ctx, "gcloud", "auth", "activate-service-account", "--key-file", credentialFilePath)) envPrep.Files[credentialFilePath] = credentialFile + + envPrep.CmdEnvs = append(envPrep.CmdEnvs, fmt.Sprintf("GOOGLE_APPLICATION_CREDENTIALS=%s", credentialFilePath)) } return envPrep @@ -223,6 +231,11 @@ type EnvPrep struct { // Env is the connection credentials in environment variables Env []string + // CmdEnvs is a list of env vars that will be passed to the command + CmdEnvs []string + + // List of commands that need to be run before the actual command. + // These commands will setup the connection. PreRuns []*exec.Cmd // File contains the content of the configuration file based on the connection @@ -237,7 +250,7 @@ func (c *EnvPrep) Inject(ctx context.Context, cmd *exec.Cmd) ([]*exec.Cmd, error } } - cmd.Env = append(cmd.Env, c.Env...) + cmd.Env = append(cmd.Env, c.CmdEnvs...) return c.PreRuns, nil } diff --git a/models/connections_test.go b/models/connections_test.go index 347a0afd..29252b5a 100644 --- a/models/connections_test.go +++ b/models/connections_test.go @@ -31,7 +31,7 @@ func Test_Connection_AsGoGetterURL(t *testing.T) { Certificate: "cert123", Properties: map[string]string{"ref": "main"}, }, - expectedURL: "https://github.com/repo.git?ref=main&sshkey=cert123", + expectedURL: "https://github.com/repo.git?ref=main&sshkey=Y2VydDEyMw%3D%3D", expectedError: nil, }, }