2016-02-01 19:55:59 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-03 16:53:01 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-02-01 19:55:59 -05:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-02-03 16:53:01 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sts"
|
|
|
|
"github.com/go-ini/ini"
|
2016-02-03 11:43:48 -05:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2016-02-01 19:55:59 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type CLIConfig struct {
|
2016-02-04 15:56:22 -05:00
|
|
|
ProfileName string
|
2016-02-03 16:53:01 -05:00
|
|
|
SourceProfile string
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
AssumeRoleInput *sts.AssumeRoleInput
|
|
|
|
SourceCredentials *credentials.Credentials
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
profileCfg *ini.Section
|
|
|
|
profileCred *ini.Section
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
// Return a new CLIConfig with stored profile settings
|
|
|
|
func NewFromProfile(name string) (*CLIConfig, error) {
|
|
|
|
c := &CLIConfig{}
|
2016-02-04 15:56:22 -05:00
|
|
|
c.AssumeRoleInput = new(sts.AssumeRoleInput)
|
2016-02-03 16:53:01 -05:00
|
|
|
err := c.Prepare(name)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
2016-02-03 16:53:01 -05:00
|
|
|
return nil, err
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
2016-02-04 15:56:22 -05:00
|
|
|
sessName, err := c.getSessionName(c.profileCfg.Key("role_session_name").Value())
|
2016-02-03 16:53:01 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-04 15:56:22 -05:00
|
|
|
c.AssumeRoleInput.RoleSessionName = aws.String(sessName)
|
|
|
|
arn := c.profileCfg.Key("role_arn").Value()
|
|
|
|
if arn != "" {
|
|
|
|
c.AssumeRoleInput.RoleArn = aws.String(arn)
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
id := c.profileCfg.Key("external_id").Value()
|
|
|
|
if id != "" {
|
|
|
|
c.AssumeRoleInput.ExternalId = aws.String(id)
|
|
|
|
}
|
|
|
|
c.SourceCredentials = credentials.NewStaticCredentials(
|
|
|
|
c.profileCred.Key("aws_access_key_id").Value(),
|
|
|
|
c.profileCred.Key("aws_secret_access_key").Value(),
|
|
|
|
c.profileCred.Key("aws_session_token").Value(),
|
|
|
|
)
|
|
|
|
return c, nil
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-04 15:56:22 -05:00
|
|
|
// Return AWS Credentials using current profile. Must supply source config.
|
2016-02-03 16:53:01 -05:00
|
|
|
func (c *CLIConfig) CredentialsFromProfile(conf *aws.Config) (*credentials.Credentials, error) {
|
2016-02-04 15:56:22 -05:00
|
|
|
// If the profile name is equal to the source profile, there is no role to assume so return
|
|
|
|
// the source credentials as they were captured.
|
|
|
|
if c.ProfileName == c.SourceProfile {
|
|
|
|
return c.SourceCredentials, nil
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
srcCfg := aws.NewConfig().Copy(conf).WithCredentials(c.SourceCredentials)
|
2016-11-01 18:53:04 -04:00
|
|
|
session, err := session.NewSession(srcCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
svc := sts.New(session)
|
2016-02-03 16:53:01 -05:00
|
|
|
res, err := svc.AssumeRole(c.AssumeRoleInput)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return credentials.NewStaticCredentials(
|
|
|
|
*res.Credentials.AccessKeyId,
|
|
|
|
*res.Credentials.SecretAccessKey,
|
|
|
|
*res.Credentials.SessionToken,
|
|
|
|
), nil
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
// Sets params in the struct based on the file section
|
|
|
|
func (c *CLIConfig) Prepare(name string) error {
|
|
|
|
var err error
|
2016-02-04 15:56:22 -05:00
|
|
|
c.ProfileName = name
|
|
|
|
c.profileCfg, err = configFromName(c.ProfileName)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
c.SourceProfile = c.profileCfg.Key("source_profile").Value()
|
|
|
|
if c.SourceProfile == "" {
|
2016-02-04 15:56:22 -05:00
|
|
|
c.SourceProfile = c.ProfileName
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
c.profileCred, err = credsFromName(c.SourceProfile)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2016-02-04 15:56:22 -05:00
|
|
|
func (c *CLIConfig) getSessionName(rawName string) (string, error) {
|
|
|
|
if rawName == "" {
|
|
|
|
name := "packer-"
|
2016-02-03 16:53:01 -05:00
|
|
|
host, err := os.Hostname()
|
|
|
|
if err != nil {
|
2016-02-04 15:56:22 -05:00
|
|
|
return name, err
|
2016-02-03 16:53:01 -05:00
|
|
|
}
|
2016-02-04 15:56:22 -05:00
|
|
|
return fmt.Sprintf("%s%s", name, host), nil
|
2016-02-03 16:53:01 -05:00
|
|
|
} else {
|
|
|
|
return rawName, nil
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
func configFromName(name string) (*ini.Section, error) {
|
|
|
|
filePath := os.Getenv("AWS_CONFIG_FILE")
|
|
|
|
if filePath == "" {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filePath = path.Join(home, ".aws", "config")
|
|
|
|
}
|
|
|
|
file, err := readFile(filePath)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
profileName := fmt.Sprintf("profile %s", name)
|
|
|
|
cfg, err := file.GetSection(profileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cfg, nil
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
func credsFromName(name string) (*ini.Section, error) {
|
|
|
|
filePath := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
|
|
|
|
if filePath == "" {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filePath = path.Join(home, ".aws", "credentials")
|
|
|
|
}
|
|
|
|
file, err := readFile(filePath)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
cfg, err := file.GetSection(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cfg, nil
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 16:53:01 -05:00
|
|
|
func readFile(path string) (*ini.File, error) {
|
|
|
|
cfg, err := ini.Load(path)
|
2016-02-01 19:55:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-03 16:53:01 -05:00
|
|
|
return cfg, nil
|
|
|
|
}
|