-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1555 from snyk/feat/state-discovery-hcl
Discover tfstate from hcl
- Loading branch information
Showing
13 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
invalid {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
backend "s3" { | ||
bucket = "terraform-state-prod" | ||
key = "network/terraform.tfstate" | ||
region = "us-east-1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package hcl | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/snyk/driftctl/pkg/iac/config" | ||
"github.com/snyk/driftctl/pkg/iac/terraform/state" | ||
"github.com/snyk/driftctl/pkg/iac/terraform/state/backend" | ||
) | ||
|
||
type BackendBlock struct { | ||
Name string `hcl:"name,label"` | ||
Path string `hcl:"path,optional"` | ||
WorkspaceDir string `hcl:"workspace_dir,optional"` | ||
Bucket string `hcl:"bucket,optional"` | ||
Key string `hcl:"key,optional"` | ||
Region string `hcl:"region,optional"` | ||
Prefix string `hcl:"prefix,optional"` | ||
ContainerName string `hcl:"container_name,optional"` | ||
Remain hcl.Body `hcl:",remain"` | ||
} | ||
|
||
func (b BackendBlock) SupplierConfig() *config.SupplierConfig { | ||
switch b.Name { | ||
case "local": | ||
return b.parseLocalBackend() | ||
case "s3": | ||
return b.parseS3Backend() | ||
case "gcs": | ||
return b.parseGCSBackend() | ||
case "azurerm": | ||
return b.parseAzurermBackend() | ||
} | ||
return nil | ||
} | ||
|
||
func (b BackendBlock) parseLocalBackend() *config.SupplierConfig { | ||
if b.Path == "" { | ||
return nil | ||
} | ||
return &config.SupplierConfig{ | ||
Key: state.TerraformStateReaderSupplier, | ||
Backend: backend.BackendKeyFile, | ||
Path: path.Join(b.WorkspaceDir, b.Path), | ||
} | ||
} | ||
|
||
func (b BackendBlock) parseS3Backend() *config.SupplierConfig { | ||
if b.Bucket == "" || b.Key == "" { | ||
return nil | ||
} | ||
return &config.SupplierConfig{ | ||
Key: state.TerraformStateReaderSupplier, | ||
Backend: backend.BackendKeyS3, | ||
Path: path.Join(b.Bucket, b.Key), | ||
} | ||
} | ||
|
||
func (b BackendBlock) parseGCSBackend() *config.SupplierConfig { | ||
if b.Bucket == "" || b.Prefix == "" { | ||
return nil | ||
} | ||
return &config.SupplierConfig{ | ||
Key: state.TerraformStateReaderSupplier, | ||
Backend: backend.BackendKeyGS, | ||
Path: fmt.Sprintf("%s.tfstate", path.Join(b.Bucket, b.Prefix)), | ||
} | ||
} | ||
|
||
func (b BackendBlock) parseAzurermBackend() *config.SupplierConfig { | ||
if b.ContainerName == "" || b.Key == "" { | ||
return nil | ||
} | ||
return &config.SupplierConfig{ | ||
Key: state.TerraformStateReaderSupplier, | ||
Backend: backend.BackendKeyAzureRM, | ||
Path: path.Join(b.ContainerName, b.Key), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package hcl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/snyk/driftctl/pkg/iac/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBackend_SupplierConfig(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
dir string | ||
want *config.SupplierConfig | ||
wantErr string | ||
}{ | ||
{ | ||
name: "test with no backend block", | ||
dir: "testdata/no_backend_block.tf", | ||
want: nil, | ||
wantErr: "testdata/no_backend_block.tf:1,11-11: Missing backend block; A backend block is required.", | ||
}, | ||
{ | ||
name: "test with local backend block", | ||
dir: "testdata/local_backend_block.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Path: "terraform-state-prod/network/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "test with S3 backend block", | ||
dir: "testdata/s3_backend_block.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Backend: "s3", | ||
Path: "terraform-state-prod/network/terraform.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "test with GCS backend block", | ||
dir: "testdata/gcs_backend_block.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Backend: "gs", | ||
Path: "tf-state-prod/terraform/state.tfstate", | ||
}, | ||
}, | ||
{ | ||
name: "test with Azure backend block", | ||
dir: "testdata/azurerm_backend_block.tf", | ||
want: &config.SupplierConfig{ | ||
Key: "tfstate", | ||
Backend: "azurerm", | ||
Path: "states/prod.terraform.tfstate", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
hcl, err := ParseTerraformFromHCL(tt.dir) | ||
if tt.wantErr == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tt.wantErr) | ||
return | ||
} | ||
|
||
if hcl.Backend.SupplierConfig() == nil { | ||
assert.Nil(t, tt.want) | ||
return | ||
} | ||
|
||
assert.Equal(t, *tt.want, *hcl.Backend.SupplierConfig()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hcl | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2/gohcl" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
) | ||
|
||
type MainBodyBlock struct { | ||
Terraform TerraformBlock `hcl:"terraform,block"` | ||
} | ||
|
||
type TerraformBlock struct { | ||
Backend BackendBlock `hcl:"backend,block"` | ||
} | ||
|
||
func ParseTerraformFromHCL(filename string) (*TerraformBlock, error) { | ||
var v MainBodyBlock | ||
|
||
parser := hclparse.NewParser() | ||
f, diags := parser.ParseHCLFile(filename) | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
|
||
diags = gohcl.DecodeBody(f.Body, nil, &v) | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
|
||
return &v.Terraform, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
terraform { | ||
backend "azurerm" { | ||
resource_group_name = "StorageAccount-ResourceGroup" | ||
storage_account_name = "abcd1234" | ||
container_name = "states" | ||
key = "prod.terraform.tfstate" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
terraform { | ||
backend "gcs" { | ||
bucket = "tf-state-prod" | ||
prefix = "terraform/state" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
terraform { | ||
backend "local" { | ||
path = "terraform-state-prod/network/terraform.tfstate" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
backend "s3" { | ||
bucket = "terraform-state-prod" | ||
key = "network/terraform.tfstate" | ||
region = "us-east-1" | ||
} | ||
} |