-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathprovider.go
More file actions
98 lines (85 loc) · 3.07 KB
/
provider.go
File metadata and controls
98 lines (85 loc) · 3.07 KB
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
// Package envprovider provides utilities for accessing environment variables to pass Mountpoint.
package envprovider
import (
"fmt"
"maps"
"os"
"slices"
)
const (
EnvRegion = "AWS_REGION"
EnvDefaultRegion = "AWS_DEFAULT_REGION"
EnvSTSRegionalEndpoints = "AWS_STS_REGIONAL_ENDPOINTS"
EnvMaxAttempts = "AWS_MAX_ATTEMPTS"
EnvProfile = "AWS_PROFILE"
EnvConfigFile = "AWS_CONFIG_FILE"
EnvSharedCredentialsFile = "AWS_SHARED_CREDENTIALS_FILE"
EnvRoleARN = "AWS_ROLE_ARN"
EnvWebIdentityTokenFile = "AWS_WEB_IDENTITY_TOKEN_FILE"
EnvContainerAuthorizationTokenFile = "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE"
EnvContainerCredentialsFullURI = "AWS_CONTAINER_CREDENTIALS_FULL_URI"
EnvEC2MetadataDisabled = "AWS_EC2_METADATA_DISABLED"
EnvAccessKeyID = "AWS_ACCESS_KEY_ID"
EnvSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
EnvSessionToken = "AWS_SESSION_TOKEN"
EnvMountpointCacheKey = "UNSTABLE_MOUNTPOINT_CACHE_KEY"
)
// Key represents an environment variable name.
type Key = string
// Value represents an environment variable value.
type Value = string
// Environment represents a list of environment variables as key-value pairs.
type Environment map[Key]Value
// envAllowlist is the list of environment variables to pass-by by default.
// If any of these set, it will be returned as-is in [Default].
var envAllowlist = []Key{
EnvRegion,
EnvDefaultRegion,
EnvSTSRegionalEndpoints,
}
// Region returns detected region from environment variables `AWS_REGION` or `AWS_DEFAULT_REGION`.
// It returns an empty string if both is unset.
func Region() Value {
region := os.Getenv(EnvRegion)
if region != "" {
return region
}
return os.Getenv(EnvDefaultRegion)
}
// Default returns list of environment variables to pass Mountpoint.
func Default() Environment {
environment := make(Environment)
for _, key := range envAllowlist {
val := os.Getenv(key)
if val != "" {
environment[key] = val
}
}
return environment
}
// List returns a sorted slice of environment variables in "KEY=VALUE" format.
func (env Environment) List() []string {
list := []string{}
for key, val := range env {
list = append(list, format(key, val))
}
slices.Sort(list)
return list
}
// Delete deletes the environment variable with the specified key.
func (env Environment) Delete(key Key) {
delete(env, key)
}
// Set adds or updates the environment variable with the specified key and value.
func (env Environment) Set(key Key, value Value) {
env[key] = value
}
// Merge adds all key-value pairs from the given environment to the current environment.
// If a key exists in both environments, the value from the given environment takes precedence.
func (env Environment) Merge(other Environment) {
maps.Copy(env, other)
}
// format formats given key and value to be used as an environment variable.
func format(key Key, value Value) string {
return fmt.Sprintf("%s=%s", key, value)
}