2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type VaultAWSEngineOptions
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2013-07-16 00:08:19 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-10-27 09:32:23 -04:00
|
|
|
"crypto/tls"
|
2013-07-29 19:42:35 -04:00
|
|
|
"fmt"
|
2017-06-09 14:24:30 -04:00
|
|
|
"log"
|
2017-10-27 09:32:23 -04:00
|
|
|
"net/http"
|
2018-02-02 23:16:23 -05:00
|
|
|
"strings"
|
2015-05-27 14:35:56 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2016-02-19 20:10:05 -05:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2018-10-23 12:58:27 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-10-24 05:26:53 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2019-01-25 06:59:43 -05:00
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2019-02-04 18:54:14 -05:00
|
|
|
vaultapi "github.com/hashicorp/vault/api"
|
2013-07-16 00:08:19 -04:00
|
|
|
)
|
|
|
|
|
2019-02-04 18:29:45 -05:00
|
|
|
type VaultAWSEngineOptions struct {
|
2019-06-03 11:55:09 -04:00
|
|
|
Name string `mapstructure:"name"`
|
|
|
|
RoleARN string `mapstructure:"role_arn"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Specifies the TTL for the use of the STS token. This
|
2019-06-03 11:55:09 -04:00
|
|
|
// is specified as a string with a duration suffix. Valid only when
|
|
|
|
// credential_type is assumed_role or federation_token. When not
|
|
|
|
// specified, the default_sts_ttl set for the role will be used. If that
|
|
|
|
// is also not set, then the default value of 3600s will be used. AWS
|
|
|
|
// places limits on the maximum TTL allowed. See the AWS documentation on
|
|
|
|
// the DurationSeconds parameter for AssumeRole (for assumed_role
|
|
|
|
// credential types) and GetFederationToken (for federation_token
|
|
|
|
// credential types) for more details.
|
2019-05-28 11:50:58 -04:00
|
|
|
TTL string `mapstructure:"ttl" required:"false"`
|
2019-02-04 18:54:14 -05:00
|
|
|
EngineName string `mapstructure:"engine_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VaultAWSEngineOptions) Empty() bool {
|
|
|
|
return len(v.Name) == 0 && len(v.RoleARN) == 0 &&
|
|
|
|
len(v.EngineName) == 0 && len(v.TTL) == 0
|
2019-02-04 18:29:45 -05:00
|
|
|
}
|
|
|
|
|
2013-07-16 00:08:19 -04:00
|
|
|
// AccessConfig is for common configuration related to AWS access
|
|
|
|
type AccessConfig struct {
|
2019-06-03 11:55:09 -04:00
|
|
|
// The access key used to communicate with AWS. [Learn how to set this]
|
2020-04-01 18:54:21 -04:00
|
|
|
// (/docs/builders/amazon#specifying-amazon-credentials). On EBS, this
|
2019-06-03 11:55:09 -04:00
|
|
|
// is not required if you are using `use_vault_aws_engine` for
|
|
|
|
// authentication instead.
|
|
|
|
AccessKey string `mapstructure:"access_key" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This option is useful if you use a cloud
|
2019-06-03 11:55:09 -04:00
|
|
|
// provider whose API is compatible with aws EC2. Specify another endpoint
|
|
|
|
// like this https://ec2.custom.endpoint.com.
|
|
|
|
CustomEndpointEc2 string `mapstructure:"custom_endpoint_ec2" required:"false"`
|
|
|
|
// Enable automatic decoding of any encoded authorization (error) messages
|
|
|
|
// using the `sts:DecodeAuthorizationMessage` API. Note: requires that the
|
|
|
|
// effective user/role have permissions to `sts:DecodeAuthorizationMessage`
|
|
|
|
// on resource `*`. Default `false`.
|
|
|
|
DecodeAuthZMessages bool `mapstructure:"decode_authorization_messages" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This allows skipping TLS
|
2019-06-03 11:55:09 -04:00
|
|
|
// verification of the AWS EC2 endpoint. The default is false.
|
|
|
|
InsecureSkipTLSVerify bool `mapstructure:"insecure_skip_tls_verify" required:"false"`
|
2020-02-07 10:36:14 -05:00
|
|
|
// This is the maximum number of times an API call is retried, in the case
|
|
|
|
// where requests are being throttled or experiencing transient failures.
|
|
|
|
// The delay between the subsequent API calls increases exponentially.
|
|
|
|
MaxRetries int `mapstructure:"max_retries" required:"false"`
|
2019-08-26 10:56:57 -04:00
|
|
|
// The MFA
|
|
|
|
// [TOTP](https://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm)
|
|
|
|
// code. This should probably be a user variable since it changes all the
|
|
|
|
// time.
|
2019-06-03 11:55:09 -04:00
|
|
|
MFACode string `mapstructure:"mfa_code" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The profile to use in the shared credentials file for
|
2019-06-03 11:55:09 -04:00
|
|
|
// AWS. See Amazon's documentation on [specifying
|
|
|
|
// profiles](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-profiles)
|
|
|
|
// for more details.
|
|
|
|
ProfileName string `mapstructure:"profile" required:"false"`
|
|
|
|
// The name of the region, such as `us-east-1`, in which
|
|
|
|
// to launch the EC2 instance to create the AMI.
|
|
|
|
// When chroot building, this value is guessed from environment.
|
|
|
|
RawRegion string `mapstructure:"region" required:"true"`
|
|
|
|
// The secret key used to communicate with AWS. [Learn how to set
|
2020-04-01 18:54:21 -04:00
|
|
|
// this](/docs/builders/amazon#specifying-amazon-credentials). This is not required
|
2019-06-03 11:55:09 -04:00
|
|
|
// if you are using `use_vault_aws_engine` for authentication instead.
|
|
|
|
SecretKey string `mapstructure:"secret_key" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set to true if you want to skip
|
2019-06-03 11:55:09 -04:00
|
|
|
// validation of the ami_regions configuration option. Default false.
|
|
|
|
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
|
|
|
|
SkipMetadataApiCheck bool `mapstructure:"skip_metadata_api_check"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The access token to use. This is different from the
|
2019-06-03 11:55:09 -04:00
|
|
|
// access key and secret key. If you're not sure what this is, then you
|
|
|
|
// probably don't need it. This will also be read from the AWS_SESSION_TOKEN
|
|
|
|
// environmental variable.
|
|
|
|
Token string `mapstructure:"token" required:"false"`
|
|
|
|
session *session.Session
|
2019-06-06 10:45:37 -04:00
|
|
|
// Get credentials from Hashicorp Vault's aws secrets engine. You must
|
|
|
|
// already have created a role to use. For more information about
|
|
|
|
// generating credentials via the Vault engine, see the [Vault
|
2020-03-31 18:19:18 -04:00
|
|
|
// docs.](https://www.vaultproject.io/api/secret/aws#generate-credentials)
|
2019-06-03 11:55:09 -04:00
|
|
|
// If you set this flag, you must also set the below options:
|
2019-06-06 10:45:37 -04:00
|
|
|
// - `name` (string) - Required. Specifies the name of the role to generate
|
|
|
|
// credentials against. This is part of the request URL.
|
|
|
|
// - `engine_name` (string) - The name of the aws secrets engine. In the
|
|
|
|
// Vault docs, this is normally referred to as "aws", and Packer will
|
|
|
|
// default to "aws" if `engine_name` is not set.
|
|
|
|
// - `role_arn` (string)- The ARN of the role to assume if credential\_type
|
|
|
|
// on the Vault role is assumed\_role. Must match one of the allowed role
|
|
|
|
// ARNs in the Vault role. Optional if the Vault role only allows a single
|
|
|
|
// AWS role ARN; required otherwise.
|
|
|
|
// - `ttl` (string) - Specifies the TTL for the use of the STS token. This
|
|
|
|
// is specified as a string with a duration suffix. Valid only when
|
|
|
|
// credential\_type is assumed\_role or federation\_token. When not
|
|
|
|
// specified, the default\_sts\_ttl set for the role will be used. If that
|
|
|
|
// is also not set, then the default value of 3600s will be used. AWS
|
|
|
|
// places limits on the maximum TTL allowed. See the AWS documentation on
|
|
|
|
// the DurationSeconds parameter for AssumeRole (for assumed\_role
|
|
|
|
// credential types) and GetFederationToken (for federation\_token
|
|
|
|
// credential types) for more details.
|
2019-06-03 11:55:09 -04:00
|
|
|
//
|
2020-03-12 10:05:08 -04:00
|
|
|
// ```json
|
2019-06-06 10:45:37 -04:00
|
|
|
// {
|
|
|
|
// "vault_aws_engine": {
|
|
|
|
// "name": "myrole",
|
|
|
|
// "role_arn": "myarn",
|
|
|
|
// "ttl": "3600s"
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2019-06-03 11:55:09 -04:00
|
|
|
VaultAWSEngine VaultAWSEngineOptions `mapstructure:"vault_aws_engine" required:"false"`
|
2018-10-24 05:26:53 -04:00
|
|
|
|
|
|
|
getEC2Connection func() ec2iface.EC2API
|
2013-07-16 00:08:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
// Config returns a valid aws.Config object for access to AWS services, or
|
|
|
|
// an error if the authentication and region couldn't be resolved
|
2017-03-01 19:43:09 -05:00
|
|
|
func (c *AccessConfig) Session() (*session.Session, error) {
|
|
|
|
if c.session != nil {
|
|
|
|
return c.session, nil
|
|
|
|
}
|
2016-02-01 19:55:59 -05:00
|
|
|
|
2018-03-15 12:49:34 -04:00
|
|
|
config := aws.NewConfig().WithCredentialsChainVerboseErrors(true)
|
2020-02-07 10:36:14 -05:00
|
|
|
if c.MaxRetries > 0 {
|
|
|
|
config = config.WithMaxRetries(c.MaxRetries)
|
|
|
|
}
|
2019-02-04 18:29:45 -05:00
|
|
|
|
2018-03-15 12:49:34 -04:00
|
|
|
staticCreds := credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
|
|
|
|
if _, err := staticCreds.Get(); err != credentials.ErrStaticCredentialsEmpty {
|
|
|
|
config.WithCredentials(staticCreds)
|
2018-01-04 18:04:07 -05:00
|
|
|
}
|
2017-11-07 17:03:52 -05:00
|
|
|
|
2019-01-25 07:02:42 -05:00
|
|
|
if c.RawRegion != "" {
|
|
|
|
config = config.WithRegion(c.RawRegion)
|
|
|
|
}
|
2017-03-01 19:43:09 -05:00
|
|
|
|
2017-03-06 11:10:39 -05:00
|
|
|
if c.CustomEndpointEc2 != "" {
|
2017-06-09 14:01:27 -04:00
|
|
|
config = config.WithEndpoint(c.CustomEndpointEc2)
|
2017-03-06 11:10:39 -05:00
|
|
|
}
|
2017-06-09 13:46:01 -04:00
|
|
|
|
2019-03-01 17:28:16 -05:00
|
|
|
config = config.WithHTTPClient(cleanhttp.DefaultClient())
|
|
|
|
transport := config.HTTPClient.Transport.(*http.Transport)
|
2017-10-27 09:32:23 -04:00
|
|
|
if c.InsecureSkipTLSVerify {
|
|
|
|
transport.TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 17:28:16 -05:00
|
|
|
transport.Proxy = http.ProxyFromEnvironment
|
2017-10-27 09:32:23 -04:00
|
|
|
|
2017-03-01 19:43:09 -05:00
|
|
|
opts := session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
Config: *config,
|
2017-02-26 11:24:34 -05:00
|
|
|
}
|
2017-11-07 17:03:52 -05:00
|
|
|
|
2018-02-15 17:20:50 -05:00
|
|
|
if c.ProfileName != "" {
|
|
|
|
opts.Profile = c.ProfileName
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:43:09 -05:00
|
|
|
if c.MFACode != "" {
|
|
|
|
opts.AssumeRoleTokenProvider = func() (string, error) {
|
|
|
|
return c.MFACode, nil
|
|
|
|
}
|
|
|
|
}
|
2017-11-07 17:03:52 -05:00
|
|
|
|
2019-01-25 07:00:21 -05:00
|
|
|
sess, err := session.NewSessionWithOptions(opts)
|
|
|
|
if err != nil {
|
2017-03-01 19:43:09 -05:00
|
|
|
return nil, err
|
2019-01-25 07:00:21 -05:00
|
|
|
}
|
|
|
|
log.Printf("Found region %s", *sess.Config.Region)
|
|
|
|
c.session = sess
|
|
|
|
|
|
|
|
cp, err := c.session.Config.Credentials.Get()
|
2019-11-12 15:27:47 -05:00
|
|
|
|
|
|
|
if isAWSErr(err, "NoCredentialProviders", "") {
|
|
|
|
return nil, fmt.Errorf("No valid credential sources found for AWS Builder. " +
|
2020-03-31 18:19:18 -04:00
|
|
|
"Please see https://www.packer.io/docs/builders/amazon#specifying-amazon-credentials " +
|
2019-11-12 15:27:47 -05:00
|
|
|
"for more information on providing credentials for the AWS Builder.")
|
|
|
|
}
|
|
|
|
|
2019-01-25 07:00:21 -05:00
|
|
|
if err != nil {
|
2019-11-12 15:27:47 -05:00
|
|
|
return nil, fmt.Errorf("Error loading credentials for AWS Provider: %s", err)
|
2018-02-15 17:20:50 -05:00
|
|
|
}
|
2019-11-12 15:27:47 -05:00
|
|
|
|
2019-01-25 07:00:21 -05:00
|
|
|
log.Printf("[INFO] AWS Auth provider used: %q", cp.ProviderName)
|
2017-10-02 16:12:42 -04:00
|
|
|
|
|
|
|
if c.DecodeAuthZMessages {
|
|
|
|
DecodeAuthZMessages(c.session)
|
|
|
|
}
|
2018-12-06 16:30:20 -05:00
|
|
|
LogEnvOverrideWarnings()
|
2017-10-02 16:12:42 -04:00
|
|
|
|
2017-03-01 19:43:09 -05:00
|
|
|
return c.session, nil
|
2016-02-01 19:55:59 -05:00
|
|
|
}
|
|
|
|
|
2018-02-02 23:16:23 -05:00
|
|
|
func (c *AccessConfig) SessionRegion() string {
|
|
|
|
if c.session == nil {
|
|
|
|
panic("access config session should be set.")
|
|
|
|
}
|
|
|
|
return aws.StringValue(c.session.Config.Region)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AccessConfig) IsGovCloud() bool {
|
|
|
|
return strings.HasPrefix(c.SessionRegion(), "us-gov-")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AccessConfig) IsChinaCloud() bool {
|
|
|
|
return strings.HasPrefix(c.SessionRegion(), "cn-")
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:54:14 -05:00
|
|
|
func (c *AccessConfig) GetCredsFromVault() error {
|
|
|
|
// const EnvVaultAddress = "VAULT_ADDR"
|
|
|
|
// const EnvVaultToken = "VAULT_TOKEN"
|
|
|
|
vaultConfig := vaultapi.DefaultConfig()
|
|
|
|
cli, err := vaultapi.NewClient(vaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error getting Vault client: %s", err)
|
|
|
|
}
|
2019-02-05 17:07:04 -05:00
|
|
|
if c.VaultAWSEngine.EngineName == "" {
|
|
|
|
c.VaultAWSEngine.EngineName = "aws"
|
|
|
|
}
|
2019-02-04 18:54:14 -05:00
|
|
|
path := fmt.Sprintf("/%s/creds/%s", c.VaultAWSEngine.EngineName,
|
|
|
|
c.VaultAWSEngine.Name)
|
|
|
|
secret, err := cli.Logical().Read(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading vault secret: %s", err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return fmt.Errorf("Vault Secret does not exist at the given path.")
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:07:04 -05:00
|
|
|
c.AccessKey = secret.Data["access_key"].(string)
|
|
|
|
c.SecretKey = secret.Data["secret_key"].(string)
|
|
|
|
token := secret.Data["security_token"]
|
|
|
|
if token != nil {
|
|
|
|
c.Token = token.(string)
|
|
|
|
} else {
|
|
|
|
c.Token = ""
|
|
|
|
}
|
2019-02-04 18:54:14 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 14:35:56 -04:00
|
|
|
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
2018-01-04 14:50:27 -05:00
|
|
|
|
2018-03-15 12:49:34 -04:00
|
|
|
if c.SkipMetadataApiCheck {
|
|
|
|
log.Println("(WARN) skip_metadata_api_check ignored.")
|
|
|
|
}
|
2019-02-04 18:54:14 -05:00
|
|
|
|
|
|
|
// Make sure it's obvious from the config how we're getting credentials:
|
2019-07-25 00:49:51 -04:00
|
|
|
// Vault, Packer config, or environment.
|
2019-02-04 18:54:14 -05:00
|
|
|
if !c.VaultAWSEngine.Empty() {
|
2019-02-04 18:29:45 -05:00
|
|
|
if len(c.AccessKey) > 0 {
|
|
|
|
errs = append(errs,
|
|
|
|
fmt.Errorf("If you have set vault_aws_engine, you must not set"+
|
|
|
|
" the access_key or secret_key."))
|
|
|
|
}
|
2019-02-04 18:54:14 -05:00
|
|
|
// Go ahead and grab those credentials from Vault now, so we can set
|
|
|
|
// the keys and token now.
|
|
|
|
err := c.GetCredsFromVault()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2019-02-04 18:29:45 -05:00
|
|
|
}
|
2019-02-04 18:54:14 -05:00
|
|
|
|
2018-01-04 14:50:27 -05:00
|
|
|
if (len(c.AccessKey) > 0) != (len(c.SecretKey) > 0) {
|
|
|
|
errs = append(errs,
|
|
|
|
fmt.Errorf("`access_key` and `secret_key` must both be either set or not set."))
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:03:52 -05:00
|
|
|
return errs
|
2013-07-16 00:08:19 -04:00
|
|
|
}
|
2018-10-24 05:26:53 -04:00
|
|
|
|
|
|
|
func (c *AccessConfig) NewEC2Connection() (ec2iface.EC2API, error) {
|
|
|
|
if c.getEC2Connection != nil {
|
|
|
|
return c.getEC2Connection(), nil
|
|
|
|
}
|
|
|
|
sess, err := c.Session()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-23 19:58:48 -05:00
|
|
|
|
2019-03-01 17:28:16 -05:00
|
|
|
ec2conn := ec2.New(sess)
|
2019-01-23 19:58:48 -05:00
|
|
|
|
|
|
|
return ec2conn, nil
|
2018-10-24 05:26:53 -04:00
|
|
|
}
|