-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreds.go
More file actions
40 lines (35 loc) · 1.31 KB
/
creds.go
File metadata and controls
40 lines (35 loc) · 1.31 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
package storage
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials"
)
const (
accessKeyIDFile = "/var/run/secrets/object/store/access-key-id"
accessSecretKeyFile = "/var/run/secrets/object/store/access-secret-key"
)
var (
errMissingKey = fmt.Errorf("missing %s", accessKeyIDFile)
errMissingSecret = fmt.Errorf("missing %s", accessSecretKeyFile)
)
// getCreds gets storage credentials from accessKeyIDFile and accessSecretKeyFile.
// if either a key exists but not a secret or vice-versa, returns an error.
// if neither exists, returns credentials.AnonymousCredentials
func getCreds() (*credentials.Credentials, error) {
accessKeyIDBytes, accessKeyErr := ioutil.ReadFile(accessKeyIDFile)
accessSecretKeyBytes, accessSecretKeyErr := ioutil.ReadFile(accessSecretKeyFile)
if accessKeyErr == os.ErrNotExist && accessSecretKeyErr == os.ErrNotExist {
return credentials.AnonymousCredentials, nil
}
if accessKeyErr != nil && accessSecretKeyErr == nil {
return nil, errMissingKey
}
if accessKeyErr == nil && accessSecretKeyErr != nil {
return nil, errMissingSecret
}
accessKeyID := strings.TrimSpace(string(accessKeyIDBytes))
accessSecretKey := strings.TrimSpace(string(accessSecretKeyBytes))
return credentials.NewStaticCredentials(accessKeyID, accessSecretKey, ""), nil
}