2017-03-21 18:43:31 +01:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
|
|
|
terraformAWS "github.com/hashicorp/terraform/builtin/providers/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new backend for S3 remote state.
|
|
|
|
func New() backend.Backend {
|
|
|
|
s := &schema.Backend{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-31 18:59:29 +02:00
|
|
|
"bucket": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "The name of the S3 bucket",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"key": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "The path to the state file inside the bucket",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"region": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "The region of the S3 bucket.",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AWS_DEFAULT_REGION", nil),
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"endpoint": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A custom endpoint for the S3 API",
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AWS_S3_ENDPOINT", ""),
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"encrypt": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Whether to enable server side encryption of the state file",
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"acl": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Canned ACL to be applied to the state file",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"access_key": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "AWS access key",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"secret_key": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "AWS secret key",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"kms_key_id": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The ARN of a KMS Key to use for encrypting the state",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"lock_table": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "DynamoDB table for state locking",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"profile": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "AWS profile name",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"shared_credentials_file": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Path to a shared credentials file",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"token": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "MFA token",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-03-31 18:59:29 +02:00
|
|
|
"role_arn": {
|
2017-03-21 18:43:31 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The role to be assumed",
|
|
|
|
Default: "",
|
|
|
|
},
|
2017-03-31 18:59:29 +02:00
|
|
|
|
|
|
|
"session_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The session name to use when assuming the role.",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
"external_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The external ID to use when assuming the role",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
"assume_role_policy": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The permissions applied when assuming a role.",
|
|
|
|
Default: "",
|
|
|
|
},
|
2017-03-21 18:43:31 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result := &Backend{Backend: s}
|
|
|
|
result.Backend.ConfigureFunc = result.configure
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type Backend struct {
|
|
|
|
*schema.Backend
|
|
|
|
|
|
|
|
// The fields below are set from configure
|
2017-03-22 20:52:55 +01:00
|
|
|
s3Client *s3.S3
|
|
|
|
dynClient *dynamodb.DynamoDB
|
|
|
|
|
|
|
|
bucketName string
|
|
|
|
keyName string
|
|
|
|
serverSideEncryption bool
|
|
|
|
acl string
|
|
|
|
kmsKeyID string
|
|
|
|
lockTable string
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) configure(ctx context.Context) error {
|
2017-03-22 20:52:55 +01:00
|
|
|
if b.s3Client != nil {
|
2017-03-21 18:43:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the resource data
|
|
|
|
data := schema.FromContextBackendConfig(ctx)
|
|
|
|
|
2017-03-22 20:52:55 +01:00
|
|
|
b.bucketName = data.Get("bucket").(string)
|
|
|
|
b.keyName = data.Get("key").(string)
|
|
|
|
b.serverSideEncryption = data.Get("encrypt").(bool)
|
|
|
|
b.acl = data.Get("acl").(string)
|
|
|
|
b.kmsKeyID = data.Get("kms_key_id").(string)
|
|
|
|
b.lockTable = data.Get("lock_table").(string)
|
2017-03-21 18:43:31 +01:00
|
|
|
|
|
|
|
var errs []error
|
|
|
|
creds, err := terraformAWS.GetCredentials(&terraformAWS.Config{
|
2017-03-31 18:59:29 +02:00
|
|
|
AccessKey: data.Get("access_key").(string),
|
|
|
|
SecretKey: data.Get("secret_key").(string),
|
|
|
|
Token: data.Get("token").(string),
|
|
|
|
Profile: data.Get("profile").(string),
|
|
|
|
CredsFilename: data.Get("shared_credentials_file").(string),
|
|
|
|
AssumeRoleARN: data.Get("role_arn").(string),
|
|
|
|
AssumeRoleSessionName: data.Get("session_name").(string),
|
|
|
|
AssumeRoleExternalID: data.Get("external_id").(string),
|
|
|
|
AssumeRolePolicy: data.Get("assume_role_policy").(string),
|
2017-03-21 18:43:31 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Get to check for credential provider. If nothing found, we'll get an
|
|
|
|
// error, and we can present it nicely to the user
|
|
|
|
_, err = creds.Get()
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
|
|
|
|
errs = append(errs, fmt.Errorf(`No valid credential sources found for AWS S3 remote.
|
|
|
|
Please see https://www.terraform.io/docs/state/remote/s3.html for more information on
|
|
|
|
providing credentials for the AWS S3 remote`))
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("Error loading credentials for AWS S3 remote: %s", err))
|
|
|
|
}
|
|
|
|
return &multierror.Error{Errors: errs}
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:52:55 +01:00
|
|
|
endpoint := data.Get("endpoint").(string)
|
|
|
|
region := data.Get("region").(string)
|
|
|
|
|
2017-03-21 18:43:31 +01:00
|
|
|
awsConfig := &aws.Config{
|
|
|
|
Credentials: creds,
|
|
|
|
Endpoint: aws.String(endpoint),
|
|
|
|
Region: aws.String(region),
|
|
|
|
HTTPClient: cleanhttp.DefaultClient(),
|
|
|
|
}
|
|
|
|
sess := session.New(awsConfig)
|
2017-03-22 20:52:55 +01:00
|
|
|
b.s3Client = s3.New(sess)
|
|
|
|
b.dynClient = dynamodb.New(sess)
|
|
|
|
|
2017-03-21 18:43:31 +01:00
|
|
|
return nil
|
|
|
|
}
|