-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameters.go
57 lines (48 loc) · 1.5 KB
/
parameters.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
package elephantine
import (
"context"
"errors"
"fmt"
"github.com/urfave/cli/v2"
)
// ParameterSource should be implemented to support loading of configuration
// paramaters that should be resolved at run time rather than given as
// environment variables or flags for the application. This is useful for
// loading secrets.
type ParameterSource interface {
GetParameterValue(ctx context.Context, name string) (string, error)
}
type noParameterSource struct{}
func (noParameterSource) GetParameterValue(_ context.Context, _ string) (string, error) {
return "", errors.New("no parameter source configured")
}
// GetParameterSource returns a named parameter source.
func GetParameterSource(name string) (ParameterSource, error) {
switch name {
case "":
return noParameterSource{}, nil
case "ssm":
return NewLazySSM(), nil
case "vault":
return NewVault()
default:
return nil, fmt.Errorf("unknown parameter source %q", name)
}
}
// ResolveParameter loads the parameter from the parameter source if
// "[name]-parameter" has been set for the cli.Context, otherwise the value of
// "[name]" will be returned.
func ResolveParameter(
ctx context.Context, c *cli.Context, src ParameterSource, name string,
) (string, error) {
paramName := c.String(name + "-parameter")
if paramName == "" {
return c.String(name), nil
}
value, err := src.GetParameterValue(ctx, paramName)
if err != nil {
return "", fmt.Errorf("failed to fetch %q (%s) parameter value: %w",
paramName, name, err)
}
return value, nil
}