2022-09-26 09:52:31 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
/*
|
|
|
|
Package terraform handles creation/destruction of a Constellation cluster using Terraform.
|
|
|
|
|
|
|
|
Since Terraform does not provide a stable Go API, we use the `terraform-exec` package to interact with Terraform.
|
|
|
|
|
|
|
|
The Terraform templates are located in the "terraform" subdirectory. The templates are embedded into the CLI binary using `go:embed`.
|
|
|
|
On use the relevant template is extracted to the working directory and the user customized variables are written to a `terraform.tfvars` file.
|
|
|
|
*/
|
2022-09-26 09:52:31 -04:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-04-14 08:15:07 -04:00
|
|
|
"fmt"
|
2022-11-14 12:18:58 -05:00
|
|
|
"path/filepath"
|
2022-09-26 09:52:31 -04:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2023-04-14 08:15:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-09-26 09:52:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
install "github.com/hashicorp/hc-install"
|
|
|
|
"github.com/hashicorp/hc-install/fs"
|
|
|
|
"github.com/hashicorp/hc-install/product"
|
|
|
|
"github.com/hashicorp/hc-install/releases"
|
|
|
|
"github.com/hashicorp/hc-install/src"
|
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
tfjson "github.com/hashicorp/terraform-json"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tfVersion = ">= 1.2.0"
|
|
|
|
terraformVarsFile = "terraform.tfvars"
|
|
|
|
)
|
|
|
|
|
2022-11-16 10:33:51 -05:00
|
|
|
// ErrTerraformWorkspaceExistsWithDifferentVariables is returned when existing Terraform files differ from the version the CLI wants to extract.
|
|
|
|
var ErrTerraformWorkspaceExistsWithDifferentVariables = errors.New("creating cluster: a Terraform workspace already exists with different variables")
|
|
|
|
|
2022-09-26 09:52:31 -04:00
|
|
|
// Client manages interaction with Terraform.
|
|
|
|
type Client struct {
|
|
|
|
tf tfInterface
|
|
|
|
|
2022-11-14 12:18:58 -05:00
|
|
|
file file.Handler
|
|
|
|
workingDir string
|
|
|
|
remove func()
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New sets up a new Client for Terraform.
|
2022-11-14 12:18:58 -05:00
|
|
|
func New(ctx context.Context, workingDir string) (*Client, error) {
|
|
|
|
file := file.NewHandler(afero.NewOsFs())
|
|
|
|
if err := file.MkdirAll(workingDir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tf, remove, err := GetExecutable(ctx, workingDir)
|
2022-09-26 09:52:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2022-11-14 12:18:58 -05:00
|
|
|
tf: tf,
|
|
|
|
remove: remove,
|
|
|
|
file: file,
|
|
|
|
workingDir: workingDir,
|
2022-09-26 09:52:31 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 05:36:41 -05:00
|
|
|
// Show reads the default state path and outputs the state.
|
|
|
|
func (c *Client) Show(ctx context.Context) (*tfjson.State, error) {
|
|
|
|
return c.tf.Show(ctx)
|
|
|
|
}
|
|
|
|
|
2022-11-15 08:00:44 -05:00
|
|
|
// PrepareWorkspace prepares a Terraform workspace for a Constellation cluster.
|
2022-12-07 05:48:54 -05:00
|
|
|
func (c *Client) PrepareWorkspace(path string, vars Variables) error {
|
|
|
|
if err := prepareWorkspace(path, c.file, c.workingDir); err != nil {
|
2022-11-15 08:00:44 -05:00
|
|
|
return err
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:06:51 -04:00
|
|
|
return c.writeVars(vars)
|
2022-11-15 08:00:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCluster creates a Constellation cluster using Terraform.
|
2023-04-14 08:15:07 -04:00
|
|
|
func (c *Client) CreateCluster(ctx context.Context, logLevel LogLevel) (CreateOutput, error) {
|
|
|
|
if err := c.setLogLevel(logLevel); err != nil {
|
|
|
|
return CreateOutput{}, fmt.Errorf("set terraform log level %s: %w", logLevel.String(), err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 05:18:52 -05:00
|
|
|
if err := c.tf.Init(ctx); err != nil {
|
2023-04-14 08:15:07 -04:00
|
|
|
return CreateOutput{}, fmt.Errorf("terraform init: %w", err)
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.tf.Apply(ctx); err != nil {
|
2023-04-14 08:15:07 -04:00
|
|
|
return CreateOutput{}, fmt.Errorf("terraform apply: %w", err)
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tfState, err := c.tf.Show(ctx)
|
|
|
|
if err != nil {
|
2023-04-14 08:15:07 -04:00
|
|
|
return CreateOutput{}, fmt.Errorf("terraform show: %w", err)
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ipOutput, ok := tfState.Values.Outputs["ip"]
|
|
|
|
if !ok {
|
2023-01-19 04:41:07 -05:00
|
|
|
return CreateOutput{}, errors.New("no IP output found")
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
ip, ok := ipOutput.Value.(string)
|
|
|
|
if !ok {
|
2023-01-19 04:41:07 -05:00
|
|
|
return CreateOutput{}, errors.New("invalid type in IP output: not a string")
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
secretOutput, ok := tfState.Values.Outputs["initSecret"]
|
|
|
|
if !ok {
|
2023-01-19 04:41:07 -05:00
|
|
|
return CreateOutput{}, errors.New("no initSecret output found")
|
2022-11-26 13:44:34 -05:00
|
|
|
}
|
|
|
|
secret, ok := secretOutput.Value.(string)
|
|
|
|
if !ok {
|
2023-01-19 04:41:07 -05:00
|
|
|
return CreateOutput{}, errors.New("invalid type in initSecret output: not a string")
|
2022-11-26 13:44:34 -05:00
|
|
|
}
|
|
|
|
|
2023-01-19 04:41:07 -05:00
|
|
|
uidOutput, ok := tfState.Values.Outputs["uid"]
|
|
|
|
if !ok {
|
|
|
|
return CreateOutput{}, errors.New("no uid output found")
|
|
|
|
}
|
|
|
|
uid, ok := uidOutput.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return CreateOutput{}, errors.New("invalid type in uid output: not a string")
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:33:04 -04:00
|
|
|
var attestationURL string
|
|
|
|
if attestationURLOutput, ok := tfState.Values.Outputs["attestationURL"]; ok {
|
|
|
|
if attestationURLString, ok := attestationURLOutput.Value.(string); ok {
|
|
|
|
attestationURL = attestationURLString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:41:07 -05:00
|
|
|
return CreateOutput{
|
2023-03-20 08:33:04 -04:00
|
|
|
IP: ip,
|
|
|
|
Secret: secret,
|
|
|
|
UID: uid,
|
|
|
|
AttestationURL: attestationURL,
|
2023-01-19 04:41:07 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOutput contains the Terraform output values of a cluster creation.
|
|
|
|
type CreateOutput struct {
|
|
|
|
IP string
|
|
|
|
Secret string
|
|
|
|
UID string
|
2023-03-20 08:33:04 -04:00
|
|
|
// AttestationURL is the URL of the attestation provider.
|
|
|
|
// It is only set if the cluster is created on Azure.
|
|
|
|
AttestationURL string
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
2022-12-07 05:48:54 -05:00
|
|
|
// IAMOutput contains the output information of the Terraform IAM operations.
|
|
|
|
type IAMOutput struct {
|
|
|
|
GCP GCPIAMOutput
|
|
|
|
Azure AzureIAMOutput
|
|
|
|
AWS AWSIAMOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
// GCPIAMOutput contains the output information of the Terraform IAM operation on GCP.
|
|
|
|
type GCPIAMOutput struct {
|
|
|
|
SaKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AzureIAMOutput contains the output information of the Terraform IAM operation on Microsoft Azure.
|
|
|
|
type AzureIAMOutput struct {
|
|
|
|
SubscriptionID string
|
|
|
|
TenantID string
|
|
|
|
ApplicationID string
|
|
|
|
UAMIID string
|
|
|
|
ApplicationClientSecretValue string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AWSIAMOutput contains the output information of the Terraform IAM operation on GCP.
|
|
|
|
type AWSIAMOutput struct {
|
|
|
|
ControlPlaneInstanceProfile string
|
|
|
|
WorkerNodeInstanceProfile string
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIAMConfig creates an IAM configuration using Terraform.
|
2023-04-14 08:15:07 -04:00
|
|
|
func (c *Client) CreateIAMConfig(ctx context.Context, provider cloudprovider.Provider, logLevel LogLevel) (IAMOutput, error) {
|
|
|
|
if err := c.setLogLevel(logLevel); err != nil {
|
|
|
|
return IAMOutput{}, fmt.Errorf("set terraform log level %s: %w", logLevel.String(), err)
|
|
|
|
}
|
|
|
|
|
2022-12-07 05:48:54 -05:00
|
|
|
if err := c.tf.Init(ctx); err != nil {
|
|
|
|
return IAMOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.tf.Apply(ctx); err != nil {
|
|
|
|
return IAMOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tfState, err := c.tf.Show(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return IAMOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch provider {
|
|
|
|
case cloudprovider.GCP:
|
|
|
|
saKeyOutputRaw, ok := tfState.Values.Outputs["sa_key"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no service account key output found")
|
|
|
|
}
|
|
|
|
saKeyOutput, ok := saKeyOutputRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in service account key output: not a string")
|
|
|
|
}
|
|
|
|
return IAMOutput{
|
|
|
|
GCP: GCPIAMOutput{
|
|
|
|
SaKey: saKeyOutput,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case cloudprovider.Azure:
|
|
|
|
subscriptionIDRaw, ok := tfState.Values.Outputs["subscription_id"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no subscription id output found")
|
|
|
|
}
|
|
|
|
subscriptionIDOutput, ok := subscriptionIDRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in subscription id output: not a string")
|
|
|
|
}
|
|
|
|
tenantIDRaw, ok := tfState.Values.Outputs["tenant_id"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no tenant id output found")
|
|
|
|
}
|
|
|
|
tenantIDOutput, ok := tenantIDRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in tenant id output: not a string")
|
|
|
|
}
|
|
|
|
applicationIDRaw, ok := tfState.Values.Outputs["application_id"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no application id output found")
|
|
|
|
}
|
|
|
|
applicationIDOutput, ok := applicationIDRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in application id output: not a string")
|
|
|
|
}
|
|
|
|
uamiIDRaw, ok := tfState.Values.Outputs["uami_id"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no UAMI id output found")
|
|
|
|
}
|
|
|
|
uamiIDOutput, ok := uamiIDRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in UAMI id output: not a string")
|
|
|
|
}
|
|
|
|
appClientSecretRaw, ok := tfState.Values.Outputs["application_client_secret_value"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no application client secret value output found")
|
|
|
|
}
|
|
|
|
appClientSecretOutput, ok := appClientSecretRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in application client secret valueoutput: not a string")
|
|
|
|
}
|
|
|
|
return IAMOutput{
|
|
|
|
Azure: AzureIAMOutput{
|
|
|
|
SubscriptionID: subscriptionIDOutput,
|
|
|
|
TenantID: tenantIDOutput,
|
|
|
|
ApplicationID: applicationIDOutput,
|
|
|
|
UAMIID: uamiIDOutput,
|
|
|
|
ApplicationClientSecretValue: appClientSecretOutput,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case cloudprovider.AWS:
|
|
|
|
controlPlaneProfileRaw, ok := tfState.Values.Outputs["control_plane_instance_profile"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no control plane instance profile output found")
|
|
|
|
}
|
|
|
|
controlPlaneProfileOutput, ok := controlPlaneProfileRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in control plane instance profile output: not a string")
|
|
|
|
}
|
|
|
|
workerNodeProfileRaw, ok := tfState.Values.Outputs["worker_nodes_instance_profile"]
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("no worker node instance profile output found")
|
|
|
|
}
|
|
|
|
workerNodeProfileOutput, ok := workerNodeProfileRaw.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return IAMOutput{}, errors.New("invalid type in worker node instance profile output: not a string")
|
|
|
|
}
|
|
|
|
return IAMOutput{
|
|
|
|
AWS: AWSIAMOutput{
|
|
|
|
ControlPlaneInstanceProfile: controlPlaneProfileOutput,
|
|
|
|
WorkerNodeInstanceProfile: workerNodeProfileOutput,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return IAMOutput{}, errors.New("unsupported cloud provider")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 02:42:54 -05:00
|
|
|
// Destroy destroys Terraform-created cloud resources.
|
2023-04-14 08:15:07 -04:00
|
|
|
func (c *Client) Destroy(ctx context.Context, logLevel LogLevel) error {
|
|
|
|
if err := c.setLogLevel(logLevel); err != nil {
|
|
|
|
return fmt.Errorf("set terraform log level %s: %w", logLevel.String(), err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 06:50:17 -05:00
|
|
|
if err := c.tf.Init(ctx); err != nil {
|
2023-04-14 08:15:07 -04:00
|
|
|
return fmt.Errorf("terraform init: %w", err)
|
2022-11-15 06:50:17 -05:00
|
|
|
}
|
2022-09-26 09:52:31 -04:00
|
|
|
return c.tf.Destroy(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveInstaller removes the Terraform installer, if it was downloaded for this command.
|
|
|
|
func (c *Client) RemoveInstaller() {
|
|
|
|
c.remove()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUpWorkspace removes terraform files from the current directory.
|
|
|
|
func (c *Client) CleanUpWorkspace() error {
|
2023-03-20 06:06:51 -04:00
|
|
|
return cleanUpWorkspace(c.file, c.workingDir)
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetExecutable returns a Terraform executable either from the local filesystem,
|
|
|
|
// or downloads the latest version fulfilling the version constraint.
|
|
|
|
func GetExecutable(ctx context.Context, workingDir string) (terraform *tfexec.Terraform, remove func(), err error) {
|
|
|
|
inst := install.NewInstaller()
|
|
|
|
|
|
|
|
version, err := version.NewConstraint(tfVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadVersion := &releases.LatestVersion{
|
|
|
|
Product: product.Terraform,
|
|
|
|
Constraints: version,
|
|
|
|
}
|
|
|
|
localVersion := &fs.Version{
|
|
|
|
Product: product.Terraform,
|
|
|
|
Constraints: version,
|
|
|
|
}
|
|
|
|
|
|
|
|
execPath, err := inst.Ensure(ctx, []src.Source{localVersion, downloadVersion})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, err := tfexec.NewTerraform(workingDir, execPath)
|
|
|
|
|
|
|
|
return tf, func() { _ = inst.Remove(context.Background()) }, err
|
|
|
|
}
|
|
|
|
|
2022-11-15 05:18:52 -05:00
|
|
|
// writeVars tries to write the Terraform variables file or, if it exists, checks if it is the same as we are expecting.
|
|
|
|
func (c *Client) writeVars(vars Variables) error {
|
|
|
|
if vars == nil {
|
|
|
|
return errors.New("creating cluster: vars is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
pathToVarsFile := filepath.Join(c.workingDir, terraformVarsFile)
|
|
|
|
if err := c.file.Write(pathToVarsFile, []byte(vars.String())); errors.Is(err, afero.ErrFileExists) {
|
|
|
|
// If a variables file already exists, check if it's the same as we're expecting, so we can continue using it.
|
|
|
|
varsContent, err := c.file.Read(pathToVarsFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vars.String() != string(varsContent) {
|
2022-11-16 10:33:51 -05:00
|
|
|
return ErrTerraformWorkspaceExistsWithDifferentVariables
|
2022-11-15 05:18:52 -05:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-14 08:15:07 -04:00
|
|
|
// setLogLevel sets the log level for Terraform.
|
|
|
|
func (c *Client) setLogLevel(logLevel LogLevel) error {
|
|
|
|
if logLevel.String() != "" {
|
|
|
|
if err := c.tf.SetLog(logLevel.String()); err != nil {
|
|
|
|
return fmt.Errorf("set log level %s: %w", logLevel.String(), err)
|
|
|
|
}
|
|
|
|
if err := c.tf.SetLogPath(filepath.Join("..", constants.TerraformLogFile)); err != nil {
|
|
|
|
return fmt.Errorf("set log path: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:52:31 -04:00
|
|
|
type tfInterface interface {
|
|
|
|
Apply(context.Context, ...tfexec.ApplyOption) error
|
|
|
|
Destroy(context.Context, ...tfexec.DestroyOption) error
|
|
|
|
Init(context.Context, ...tfexec.InitOption) error
|
|
|
|
Show(context.Context, ...tfexec.ShowOption) (*tfjson.State, error)
|
2023-04-14 08:15:07 -04:00
|
|
|
SetLog(level string) error
|
|
|
|
SetLogPath(path string) error
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|