-
Notifications
You must be signed in to change notification settings - Fork 284
/
executables.go
134 lines (117 loc) Β· 3.85 KB
/
executables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package executables
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/providers/cloudstack/decoder"
)
const (
redactMask = "*****"
)
var redactedEnvKeys = []string{
constants.VSphereUsernameKey,
constants.VSpherePasswordKey,
constants.GovcUsernameKey,
constants.GovcPasswordKey,
decoder.CloudStackCloudConfigB64SecretKey,
eksaGithubTokenEnv,
githubTokenEnv,
config.EksaAccessKeyIdEnv,
config.EksaSecretAccessKeyEnv,
config.AwsAccessKeyIdEnv,
config.AwsSecretAccessKeyEnv,
constants.SnowCredentialsKey,
constants.SnowCertsKey,
constants.NutanixUsernameKey,
constants.NutanixPasswordKey,
constants.RegistryUsername,
constants.RegistryPassword,
}
type executable struct {
cli string
}
type Executable interface {
Execute(ctx context.Context, args ...string) (stdout bytes.Buffer, err error)
ExecuteWithEnv(ctx context.Context, envs map[string]string, args ...string) (stdout bytes.Buffer, err error) // TODO: remove this from interface in favor of Command
ExecuteWithStdin(ctx context.Context, in []byte, args ...string) (stdout bytes.Buffer, err error) // TODO: remove this from interface in favor of Command
Command(ctx context.Context, args ...string) *Command
Run(cmd *Command) (stdout bytes.Buffer, err error)
}
// this should only be called through the executables.builder.
func NewExecutable(cli string) Executable {
return &executable{
cli: cli,
}
}
func (e *executable) Execute(ctx context.Context, args ...string) (stdout bytes.Buffer, err error) {
return e.Command(ctx, args...).Run()
}
func (e *executable) ExecuteWithStdin(ctx context.Context, in []byte, args ...string) (stdout bytes.Buffer, err error) {
return e.Command(ctx, args...).WithStdIn(in).Run()
}
func (e *executable) ExecuteWithEnv(ctx context.Context, envs map[string]string, args ...string) (stdout bytes.Buffer, err error) {
return e.Command(ctx, args...).WithEnvVars(envs).Run()
}
func (e *executable) Command(ctx context.Context, args ...string) *Command {
return NewCommand(ctx, e, args...)
}
func (e *executable) Run(cmd *Command) (stdout bytes.Buffer, err error) {
for k, v := range cmd.envVars {
os.Setenv(k, v)
}
return execute(cmd.ctx, e.cli, cmd.stdIn, cmd.envVars, cmd.args...)
}
func (e *executable) Close(ctx context.Context) error {
return nil
}
func RedactCreds(cmd string, envMap map[string]string) string {
redactedEnvs := []string{}
for _, redactedEnvKey := range redactedEnvKeys {
if env, found := os.LookupEnv(redactedEnvKey); found && env != "" {
redactedEnvs = append(redactedEnvs, env)
} else if env, found := envMap[redactedEnvKey]; found && env != "" {
redactedEnvs = append(redactedEnvs, env)
}
}
for _, redactedEnv := range redactedEnvs {
cmd = strings.ReplaceAll(cmd, redactedEnv, redactMask)
}
return cmd
}
func execute(ctx context.Context, cli string, in []byte, envVars map[string]string, args ...string) (stdout bytes.Buffer, err error) {
var stderr bytes.Buffer
cmd := exec.CommandContext(ctx, cli, args...)
logger.V(6).Info("Executing command", "cmd", RedactCreds(cmd.String(), envVars))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if len(in) != 0 {
cmd.Stdin = bytes.NewReader(in)
}
err = cmd.Run()
if err != nil {
if stderr.Len() > 0 {
if logger.MaxLogging() {
logger.V(logger.MaxLogLevel).Info(cli, "stderr", stderr.String())
}
return stdout, errors.New(stderr.String())
} else {
if !logger.MaxLogging() {
logger.V(8).Info(cli, "stdout", stdout.String())
logger.V(8).Info(cli, "stderr", stderr.String())
}
return stdout, errors.New(fmt.Sprint(err))
}
}
if !logger.MaxLogging() {
logger.V(8).Info(cli, "stdout", stdout.String())
logger.V(8).Info(cli, "stderr", stderr.String())
}
return stdout, nil
}