2017-09-08 16:11:41 +02:00
|
|
|
// Package gcs implements remote storage of state on Google Cloud Storage (GCS).
|
|
|
|
package gcs
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-18 01:59:10 +01:00
|
|
|
"encoding/base64"
|
2017-12-07 00:27:00 +01:00
|
|
|
"encoding/json"
|
2017-07-19 11:07:24 +02:00
|
|
|
"fmt"
|
2017-09-12 12:15:13 +02:00
|
|
|
"os"
|
2017-09-08 09:25:20 +02:00
|
|
|
"strings"
|
2017-09-07 13:10:56 +02:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2017-07-19 11:07:24 +02:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2018-02-28 17:40:43 +01:00
|
|
|
"github.com/hashicorp/terraform/httpclient"
|
2019-06-18 14:47:13 +02:00
|
|
|
"golang.org/x/oauth2"
|
2017-12-07 00:27:00 +01:00
|
|
|
"golang.org/x/oauth2/jwt"
|
2017-07-19 11:07:24 +02:00
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
// Backend implements "backend".Backend for GCS.
|
2017-09-08 13:50:07 +02:00
|
|
|
// Input(), Validate() and Configure() are implemented by embedding *schema.Backend.
|
|
|
|
// State(), DeleteState() and States() are implemented explicitly.
|
2018-07-04 12:11:35 +02:00
|
|
|
type Backend struct {
|
2017-09-07 10:08:39 +02:00
|
|
|
*schema.Backend
|
|
|
|
|
|
|
|
storageClient *storage.Client
|
2017-09-08 14:14:00 +02:00
|
|
|
storageContext context.Context
|
2017-09-07 10:08:39 +02:00
|
|
|
|
2017-09-08 14:32:00 +02:00
|
|
|
bucketName string
|
2017-09-11 08:25:42 +02:00
|
|
|
prefix string
|
2017-09-08 14:32:00 +02:00
|
|
|
defaultStateFile string
|
2017-09-13 15:48:36 +02:00
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
encryptionKey []byte
|
2017-09-07 10:08:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 11:07:24 +02:00
|
|
|
func New() backend.Backend {
|
2018-07-04 12:11:35 +02:00
|
|
|
b := &Backend{}
|
|
|
|
b.Backend = &schema.Backend{
|
|
|
|
ConfigureFunc: b.configure,
|
2017-07-19 11:07:24 +02:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"bucket": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Description: "The name of the Google Cloud Storage bucket",
|
|
|
|
},
|
|
|
|
|
2017-09-08 14:32:00 +02:00
|
|
|
"path": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-09-12 10:47:10 +02:00
|
|
|
Description: "Path of the default state file",
|
|
|
|
Deprecated: "Use the \"prefix\" option instead",
|
2017-09-08 14:32:00 +02:00
|
|
|
},
|
|
|
|
|
2017-09-11 08:25:42 +02:00
|
|
|
"prefix": {
|
2017-07-19 11:07:24 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "The directory where state files will be saved inside the bucket",
|
|
|
|
},
|
|
|
|
|
|
|
|
"credentials": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Google Cloud JSON Account Key",
|
|
|
|
Default: "",
|
|
|
|
},
|
2017-09-12 13:57:42 +02:00
|
|
|
|
2019-06-18 14:47:13 +02:00
|
|
|
"access_token": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
|
|
|
"GOOGLE_OAUTH_ACCESS_TOKEN",
|
|
|
|
}, nil),
|
|
|
|
Description: "An OAuth2 token used for GCP authentication",
|
|
|
|
},
|
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
"encryption_key": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "A 32 byte base64 encoded 'customer supplied encryption key' used to encrypt all state.",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
|
2017-09-12 13:57:42 +02:00
|
|
|
"project": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Google Cloud Project ID",
|
|
|
|
Default: "",
|
2018-11-05 13:49:33 +01:00
|
|
|
Removed: "Please remove this attribute. It is not used since the backend no longer creates the bucket if it does not yet exist.",
|
2017-09-12 13:57:42 +02:00
|
|
|
},
|
2017-09-13 15:52:39 +02:00
|
|
|
|
|
|
|
"region": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Description: "Region / location in which to create the bucket",
|
|
|
|
Default: "",
|
2018-11-05 13:49:33 +01:00
|
|
|
Removed: "Please remove this attribute. It is not used since the backend no longer creates the bucket if it does not yet exist.",
|
2017-09-13 15:52:39 +02:00
|
|
|
},
|
2017-07-19 11:07:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
return b
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Backend) configure(ctx context.Context) error {
|
2017-07-19 11:07:24 +02:00
|
|
|
if b.storageClient != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 14:09:14 +02:00
|
|
|
// ctx is a background context with the backend config added.
|
2017-09-08 13:36:40 +02:00
|
|
|
// Since no context is passed to remoteClient.Get(), .Lock(), etc. but
|
2017-09-07 14:09:14 +02:00
|
|
|
// one is required for calling the GCP API, we're holding on to this
|
|
|
|
// context here and re-use it later.
|
|
|
|
b.storageContext = ctx
|
|
|
|
|
|
|
|
data := schema.FromContextBackendConfig(b.storageContext)
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-10-05 09:54:41 +02:00
|
|
|
b.bucketName = data.Get("bucket").(string)
|
2017-09-11 08:25:42 +02:00
|
|
|
b.prefix = strings.TrimLeft(data.Get("prefix").(string), "/")
|
2017-11-08 20:24:36 +01:00
|
|
|
if b.prefix != "" && !strings.HasSuffix(b.prefix, "/") {
|
|
|
|
b.prefix = b.prefix + "/"
|
|
|
|
}
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-09-08 14:32:00 +02:00
|
|
|
b.defaultStateFile = strings.TrimLeft(data.Get("path").(string), "/")
|
|
|
|
|
2017-12-07 00:27:00 +01:00
|
|
|
var opts []option.ClientOption
|
|
|
|
|
2019-06-18 14:47:13 +02:00
|
|
|
// Add credential source
|
|
|
|
var creds string
|
|
|
|
var tokenSource oauth2.TokenSource
|
|
|
|
|
|
|
|
if v, ok := data.GetOk("access_token"); ok {
|
|
|
|
tokenSource = oauth2.StaticTokenSource(&oauth2.Token{
|
|
|
|
AccessToken: v.(string),
|
|
|
|
})
|
|
|
|
} else if v, ok := data.GetOk("credentials"); ok {
|
|
|
|
creds = v.(string)
|
2019-12-12 12:35:39 +01:00
|
|
|
} else if v := os.Getenv("GOOGLE_BACKEND_CREDENTIALS"); v != "" {
|
|
|
|
creds = v
|
2019-06-18 14:47:13 +02:00
|
|
|
} else {
|
2017-12-07 00:27:00 +01:00
|
|
|
creds = os.Getenv("GOOGLE_CREDENTIALS")
|
2017-09-12 12:15:13 +02:00
|
|
|
}
|
2017-12-07 00:27:00 +01:00
|
|
|
|
2019-06-18 14:47:13 +02:00
|
|
|
if tokenSource != nil {
|
|
|
|
opts = append(opts, option.WithTokenSource(tokenSource))
|
|
|
|
} else if creds != "" {
|
2017-12-07 00:27:00 +01:00
|
|
|
var account accountFile
|
|
|
|
|
|
|
|
// to mirror how the provider works, we accept the file path or the contents
|
2020-10-07 18:55:43 +02:00
|
|
|
contents, err := backend.ReadPathOrContents(creds)
|
2017-12-07 00:27:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(contents), &account); err != nil {
|
|
|
|
return fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := jwt.Config{
|
|
|
|
Email: account.ClientEmail,
|
|
|
|
PrivateKey: []byte(account.PrivateKey),
|
|
|
|
Scopes: []string{storage.ScopeReadWrite},
|
2019-08-14 00:24:34 +02:00
|
|
|
TokenURL: "https://oauth2.googleapis.com/token",
|
2017-12-07 00:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, option.WithHTTPClient(conf.Client(ctx)))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, option.WithScopes(storage.ScopeReadWrite))
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:40:43 +01:00
|
|
|
opts = append(opts, option.WithUserAgent(httpclient.UserAgentString()))
|
2017-09-12 12:15:13 +02:00
|
|
|
client, err := storage.NewClient(b.storageContext, opts...)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-12 12:15:13 +02:00
|
|
|
return fmt.Errorf("storage.NewClient() failed: %v", err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b.storageClient = client
|
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
key := data.Get("encryption_key").(string)
|
|
|
|
if key == "" {
|
|
|
|
key = os.Getenv("GOOGLE_ENCRYPTION_KEY")
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != "" {
|
2020-10-07 18:55:43 +02:00
|
|
|
kc, err := backend.ReadPathOrContents(key)
|
2017-12-18 01:59:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading encryption key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The GCS client expects a customer supplied encryption key to be
|
|
|
|
// passed in as a 32 byte long byte slice. The byte slice is base64
|
|
|
|
// encoded before being passed to the API. We take a base64 encoded key
|
|
|
|
// to remain consistent with the GCS docs.
|
|
|
|
// https://cloud.google.com/storage/docs/encryption#customer-supplied
|
|
|
|
// https://github.com/GoogleCloudPlatform/google-cloud-go/blob/def681/storage/storage.go#L1181
|
|
|
|
k, err := base64.StdEncoding.DecodeString(kc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error decoding encryption key: %s", err)
|
|
|
|
}
|
|
|
|
b.encryptionKey = k
|
|
|
|
}
|
|
|
|
|
2017-12-06 22:47:29 +01:00
|
|
|
return nil
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-12-07 00:27:00 +01:00
|
|
|
|
|
|
|
// accountFile represents the structure of the account file JSON file.
|
|
|
|
type accountFile struct {
|
|
|
|
PrivateKeyId string `json:"private_key_id"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
|
|
|
ClientEmail string `json:"client_email"`
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
}
|