forked from felixb/swamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_writer.go
99 lines (84 loc) · 2.25 KB
/
profile_writer.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
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/go-ini/ini"
"github.com/golang-utils/lockfile"
)
type ProfileWriter struct {
lock lockfile.LockFile
awsPath string
credentialsPath string
lockPath string
}
func NewProfileWriter() *ProfileWriter {
usr, err := user.Current()
if err != nil {
die("Error fetching home dir", err)
}
awsPath := filepath.Join(usr.HomeDir, ".aws")
credentialsPath := filepath.Join(awsPath, "credentials")
lockPath := filepath.Join(awsPath, ".credentials.lock")
return &ProfileWriter{
lock: lockfile.New(),
awsPath: awsPath,
credentialsPath: credentialsPath,
lockPath: lockPath,
}
}
func (pw *ProfileWriter) accuire_lock() {
for {
if err := pw.lock.Lock(pw.lockPath); err == nil {
break
} else {
fmt.Printf("Waiting for lock %s\n", pw.lockPath)
time.Sleep(time.Second)
}
}
}
func (pw *ProfileWriter) release_lock() {
os.Remove(pw.lockPath)
}
func (pw *ProfileWriter) writeProfile(cred *sts.Credentials, targetProfile, region *string) {
pw.accuire_lock()
cfg, err := ini.Load(pw.credentialsPath)
if err != nil {
fmt.Printf("Unable to find credentials file %s. Creating new file.\n", pw.credentialsPath)
if err := os.MkdirAll(pw.awsPath, os.ModePerm); err != nil { // TODO move to init
die("Error creating aws config path", err)
}
cfg = ini.Empty()
}
sec, err := cfg.GetSection(*targetProfile)
if err != nil {
sec, err = cfg.NewSection(*targetProfile)
if err != nil {
die("Error creating new section", err)
}
}
updateKey(sec, "region", region)
updateKey(sec, "aws_access_key_id", cred.AccessKeyId)
updateKey(sec, "aws_secret_access_key", cred.SecretAccessKey)
updateKey(sec, "aws_session_token", cred.SessionToken)
if err := cfg.SaveTo(pw.credentialsPath); err != nil {
die("Error writing credentials file", err)
}
pw.release_lock()
fmt.Printf("Wrote session token for profile %s\n", *targetProfile)
fmt.Printf("Token is valid until: %v\n", cred.Expiration)
}
func updateKey(sec *ini.Section, name string, value *string) {
key, err := sec.GetKey(name)
if err != nil {
_, err := sec.NewKey(name, *value)
if err != nil {
die("Error creating config key", err)
}
} else {
key.SetValue(*value)
}
}