-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
59 lines (49 loc) · 1.75 KB
/
config.go
File metadata and controls
59 lines (49 loc) · 1.75 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
package conf
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"github.com/drycc/builder/pkg/sys"
)
const (
storageLookupEnvVar = "DRYCC_STORAGE_LOOKUP"
storageBucketEnvVar = "DRYCC_STORAGE_BUCKET"
storageEndpointEnvVar = "DRYCC_STORAGE_ENDPOINT"
storageAccesskeyEnvVar = "DRYCC_STORAGE_ACCESSKEY"
storageSecretkeyEnvVar = "DRYCC_STORAGE_SECRETKEY"
)
// BuilderKeyLocation holds the path of the builder key secret.
var BuilderKeyLocation = "/var/run/secrets/api/auth/builder-key"
// Parameters is map which contains storage params
type Parameters map[string]interface{}
// GetBuilderKey returns the key to be used as token to interact with drycc-controller
func GetBuilderKey() (string, error) {
builderKeyBytes, err := os.ReadFile(BuilderKeyLocation)
if err != nil {
return "", fmt.Errorf("couldn't get builder key from %s (%s)", BuilderKeyLocation, err)
}
builderKey := strings.Trim(string(builderKeyBytes), "\n")
return builderKey, nil
}
// GetStorageParams returns the credentials required for connecting to object storage
func GetStorageParams(env sys.Env) (Parameters, error) {
params := make(map[string]interface{})
mEndpoint := env.Get(storageEndpointEnvVar)
params["regionendpoint"] = mEndpoint
region := "us-east-1" //region is required in distribution
if endpointURL, err := url.Parse(mEndpoint); err == nil {
if endpointURL.Hostname() != "" && net.ParseIP(endpointURL.Hostname()) == nil {
region = strings.Split(endpointURL.Hostname(), ".")[0]
}
}
params["region"] = region
params["accesskey"] = env.Get(storageAccesskeyEnvVar)
params["secretkey"] = env.Get(storageSecretkeyEnvVar)
params["bucket"] = env.Get(storageBucketEnvVar)
if env.Get(storageLookupEnvVar) == "path" {
params["forcepathstyle"] = "true"
}
return params, nil
}