-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
48 lines (46 loc) · 1.21 KB
/
aws.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
package aws
import (
"bufio"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"os"
"path/filepath"
"strings"
)
const (
awsCredentialPath = ".aws/credentials"
)
func CreateAwsSessionConfig(provider *Provider) (*aws.Config, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
credentialFile, err := os.Open(filepath.Join(homeDir, awsCredentialPath))
if err != nil {
return nil, err
}
defer credentialFile.Close()
scanner := bufio.NewScanner(credentialFile)
for scanner.Scan() {
if strings.Contains(scanner.Text(), provider.Profile) {
if !scanner.Scan() {
return nil, fmt.Errorf("not founnd credential")
}
accessKeyId := strings.Split(scanner.Text(), " ")[2]
if !scanner.Scan() {
return nil, fmt.Errorf("not founnd credential")
}
accessKeySecret := strings.Split(scanner.Text(), " ")[2]
return &aws.Config{
Credentials: credentials.NewStaticCredentialsFromCreds(
credentials.Value{
AccessKeyID: accessKeyId,
SecretAccessKey: accessKeySecret,
}),
Region: aws.String(provider.Region),
}, nil
}
}
return nil, fmt.Errorf("no such profile")
}