forked from segmentio/aws-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
87 lines (71 loc) · 1.95 KB
/
config.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
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
package lib
import (
"fmt"
"os"
"path/filepath"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/mitchellh/go-homedir"
"github.com/vaughan0/go-ini"
)
type profiles map[string]map[string]string
type config interface {
Parse() (profiles, error)
}
type fileConfig struct {
file string
}
func NewConfigFromEnv() (config, error) {
file := os.Getenv("AWS_CONFIG_FILE")
if file == "" {
home, err := homedir.Dir()
if err != nil {
return nil, err
}
file = filepath.Join(home, "/.aws/config")
if _, err := os.Stat(file); os.IsNotExist(err) {
file = ""
}
}
return &fileConfig{file: file}, nil
}
func (c *fileConfig) Parse() (profiles, error) {
if c.file == "" {
return nil, nil
}
log.Debugf("Parsing config file %s", c.file)
f, err := ini.LoadFile(c.file)
if err != nil {
return nil, fmt.Errorf("Error parsing config file %q: %v", c.file, err)
}
profiles := profiles{"okta": map[string]string{}}
for sectionName, section := range f {
profiles[strings.TrimPrefix(sectionName, "profile ")] = section
}
return profiles, nil
}
// sourceProfile returns either the defined source_profile or p if none exists
func sourceProfile(p string, from profiles) string {
if conf, ok := from[p]; ok {
if source := conf["source_profile"]; source != "" {
return source
}
}
return p
}
func formatCredentialError(p string, from profiles, err error) string {
source := sourceProfile(p, from)
sourceDescr := p
// add custom formatting for source_profile
if source != p {
sourceDescr = fmt.Sprintf("%s (source profile for %s)", source, p)
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
return fmt.Sprintf(
"No credentials found for profile %s.\n"+
"Use 'aws-vault add %s' to set up credentials or 'aws-vault list' to see what credentials exist",
sourceDescr, source)
}
return fmt.Sprintf("Failed to get credentials for %s: %v", err, sourceDescr)
}