Skip to content

Commit c2f547d

Browse files
schnoddelbotzJan Hacker
authored andcommitted
initial migration of bosh-s3cli patch proposal: support config from env
1 parent 37839cd commit c2f547d

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ func main() {
9999
// configure storage-cli config
100100
common.InitConfig(parseLogLevel(*logLevel))
101101

102-
// check client config file exists
103-
configFile, err := os.Open(*configPath)
104-
if err != nil {
105-
fatalLog("", err)
102+
// try reading config file. if not provided, env will be tried as source.
103+
var configFile *os.File
104+
if *configPath != "" {
105+
configFile, err := os.Open(*configPath)
106+
if err != nil {
107+
fatalLog("", err)
108+
}
109+
defer configFile.Close() //nolint:errcheck
106110
}
107-
defer configFile.Close() //nolint:errcheck
108111

109112
// create client
110113
client, err := storage.NewStorageClient(*storageType, configFile)

s3/client/sdk.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func NewAwsS3ClientWithApiOptions(
9494
}
9595
o.BaseEndpoint = aws.String(endpoint)
9696
}
97+
if c.Debug {
98+
o.ClientLogMode = aws.LogResponse | aws.LogRequest
99+
}
97100
// Apply custom middlewares if provided
98101
o.APIOptions = append(o.APIOptions, apiOptions...)
99102
})

s3/config/config.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package config
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
9+
"os"
10+
"strconv"
811
"strings"
912
)
1013

@@ -27,6 +30,7 @@ type S3Cli struct {
2730
HostStyle bool `json:"host_style"`
2831
SwiftAuthAccount string `json:"swift_auth_account"`
2932
SwiftTempURLKey string `json:"swift_temp_url_key"`
33+
Debug bool `json:"debug"`
3034
RequestChecksumCalculationEnabled bool
3135
ResponseChecksumCalculationEnabled bool
3236
UploaderRequestChecksumCalculationEnabled bool
@@ -74,6 +78,44 @@ func newStaticCredentialsPresentError(desiredSource string) error {
7478
return errorStaticCredentialsPresent{credentialsSource: desiredSource}
7579
}
7680

81+
// NewReader provides an io.Reader on given configFile, using environment as fall-back.
82+
func NewReader(configFile *os.File) (io.Reader, error) {
83+
if configFile != nil {
84+
return configFile, nil
85+
}
86+
// Try reading config from env if no config file handle was provided
87+
port := 443
88+
if altPort, isset := os.LookupEnv("S3_PORT"); isset {
89+
var err error
90+
port, err = strconv.Atoi(altPort)
91+
if err != nil {
92+
return nil, err
93+
}
94+
}
95+
c := S3Cli{
96+
AccessKeyID: os.Getenv("S3_ACCESS_KEY_ID"),
97+
BucketName: os.Getenv("S3_BUCKET_NAME"),
98+
// Fixate CredentialSource to StaticCredentialsSource, making S3_ACCESS_KEY_ID & S3_SECRET_ACCESS_KEY required
99+
CredentialsSource: StaticCredentialsSource,
100+
Host: os.Getenv("S3_HOST"),
101+
Port: port,
102+
Region: os.Getenv("S3_REGION"),
103+
SecretAccessKey: os.Getenv("S3_SECRET_ACCESS_KEY"),
104+
SSLVerifyPeer: !(os.Getenv("S3_INSECURE_SSL") != ""),
105+
// Use SSL/TLS/https:// unless S3_DISABLE_SSL is set
106+
UseSSL: !(os.Getenv("S3_DISABLE_SSL") != ""),
107+
// Use PathStyle=true by default (endpoint/bucket instead of DNS bucket.endpoint), if S3_USE_HOSTSTYLE unset. See client/sdk.go
108+
HostStyle: os.Getenv("S3_USE_HOSTSTYLE") != "",
109+
// Enable HTTP(S) request debugging if S3_DEBUG has non-empty value
110+
Debug: os.Getenv("S3_DEBUG") != "",
111+
}
112+
json, err := json.Marshal(c)
113+
if err != nil {
114+
return nil, err
115+
}
116+
return bytes.NewReader(json), nil
117+
}
118+
77119
// NewFromReader returns a new s3cli configuration struct from the contents of reader.
78120
// reader.Read() is expected to return valid JSON
79121
func NewFromReader(reader io.Reader) (S3Cli, error) {

storage/factory.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ var newGcsClient = func(configFile *os.File) (Storager, error) {
7272
}
7373

7474
var newS3Client = func(configFile *os.File) (Storager, error) {
75-
s3Config, err := s3config.NewFromReader(configFile)
75+
configReader, err := s3config.NewReader(configFile)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
s3Config, err := s3config.NewFromReader(configReader)
7681
if err != nil {
7782
return nil, err
7883
}

0 commit comments

Comments
 (0)