From 6154aa1272be6e95a599268bf927639ff1dd8b48 Mon Sep 17 00:00:00 2001 From: Craig Furman Date: Mon, 1 Aug 2022 09:22:31 +0100 Subject: [PATCH] Remove vendored terraform RPC diagnostics code Projects that import both regula and terraform would see a runtime panic when both this vendored and the original terraform's init functions try to register duplicate types with gob. --- Makefile | 1 + patches/terraform.patch | 109 ++++++++++++++++++++++++++ pkg/terraform/tfdiags/diagnostics.go | 18 ----- pkg/terraform/tfdiags/rpc_friendly.go | 59 -------------- 4 files changed, 110 insertions(+), 77 deletions(-) create mode 100644 patches/terraform.patch delete mode 100644 pkg/terraform/tfdiags/rpc_friendly.go diff --git a/Makefile b/Makefile index 3bada304..2ceb18e4 100644 --- a/Makefile +++ b/Makefile @@ -95,6 +95,7 @@ terraform_gen: -exec sed -i".bak" 's#github\.com/hashicorp/terraform/version#github.com/fugue/regula/v2/pkg/terraform/version#' '{}' \; find pkg/terraform/ -name '*.bak' -delete find pkg/terraform/ -name '*_test.go' -delete + git apply patches/terraform.patch rm -rf terraform.zip terraform-$(TERRAFORM_VERSION) ############# diff --git a/patches/terraform.patch b/patches/terraform.patch new file mode 100644 index 00000000..6c2c3672 --- /dev/null +++ b/patches/terraform.patch @@ -0,0 +1,109 @@ +diff --git a/pkg/terraform/configs/configload/loader_snapshot.go b/pkg/terraform/configs/configload/loader_snapshot.go +index 243939f..6ca8b4f 100644 +--- a/pkg/terraform/configs/configload/loader_snapshot.go ++++ b/pkg/terraform/configs/configload/loader_snapshot.go +@@ -325,6 +325,10 @@ func (fs snapshotFS) Chmod(name string, mode os.FileMode) error { + return fmt.Errorf("cannot set file mode inside configuration snapshot") + } + ++func (fs snapshotFS) Chown(string, int, int) error { ++ return fmt.Errorf("cannot set file owner inside configuration snapshot") ++} ++ + func (fs snapshotFS) Chtimes(name string, atime, mtime time.Time) error { + return fmt.Errorf("cannot set file times inside configuration snapshot") + } +diff --git a/pkg/terraform/tfdiags/diagnostics.go b/pkg/terraform/tfdiags/diagnostics.go +index 30476ee..25cd135 100644 +--- a/pkg/terraform/tfdiags/diagnostics.go ++++ b/pkg/terraform/tfdiags/diagnostics.go +@@ -107,24 +107,6 @@ func (diags Diagnostics) HasErrors() bool { + return false + } + +-// ForRPC returns a version of the receiver that has been simplified so that +-// it is friendly to RPC protocols. +-// +-// Currently this means that it can be serialized with encoding/gob and +-// subsequently re-inflated. It may later grow to include other serialization +-// formats. +-// +-// Note that this loses information about the original objects used to +-// construct the diagnostics, so e.g. the errwrap API will not work as +-// expected on an error-wrapped Diagnostics that came from ForRPC. +-func (diags Diagnostics) ForRPC() Diagnostics { +- ret := make(Diagnostics, len(diags)) +- for i := range diags { +- ret[i] = makeRPCFriendlyDiag(diags[i]) +- } +- return ret +-} +- + // Err flattens a diagnostics list into a single Go error, or to nil + // if the diagnostics list does not include any error-level diagnostics. + // +diff --git a/pkg/terraform/tfdiags/rpc_friendly.go b/pkg/terraform/tfdiags/rpc_friendly.go +deleted file mode 100644 +index 485063b..0000000 +--- a/pkg/terraform/tfdiags/rpc_friendly.go ++++ /dev/null +@@ -1,59 +0,0 @@ +-package tfdiags +- +-import ( +- "encoding/gob" +-) +- +-type rpcFriendlyDiag struct { +- Severity_ Severity +- Summary_ string +- Detail_ string +- Subject_ *SourceRange +- Context_ *SourceRange +-} +- +-// rpcFriendlyDiag transforms a given diagnostic so that is more friendly to +-// RPC. +-// +-// In particular, it currently returns an object that can be serialized and +-// later re-inflated using gob. This definition may grow to include other +-// serializations later. +-func makeRPCFriendlyDiag(diag Diagnostic) Diagnostic { +- desc := diag.Description() +- source := diag.Source() +- return &rpcFriendlyDiag{ +- Severity_: diag.Severity(), +- Summary_: desc.Summary, +- Detail_: desc.Detail, +- Subject_: source.Subject, +- Context_: source.Context, +- } +-} +- +-func (d *rpcFriendlyDiag) Severity() Severity { +- return d.Severity_ +-} +- +-func (d *rpcFriendlyDiag) Description() Description { +- return Description{ +- Summary: d.Summary_, +- Detail: d.Detail_, +- } +-} +- +-func (d *rpcFriendlyDiag) Source() Source { +- return Source{ +- Subject: d.Subject_, +- Context: d.Context_, +- } +-} +- +-func (d rpcFriendlyDiag) FromExpr() *FromExpr { +- // RPC-friendly diagnostics cannot preserve expression information because +- // expressions themselves are not RPC-friendly. +- return nil +-} +- +-func init() { +- gob.Register((*rpcFriendlyDiag)(nil)) +-} diff --git a/pkg/terraform/tfdiags/diagnostics.go b/pkg/terraform/tfdiags/diagnostics.go index 30476ee2..25cd135f 100644 --- a/pkg/terraform/tfdiags/diagnostics.go +++ b/pkg/terraform/tfdiags/diagnostics.go @@ -107,24 +107,6 @@ func (diags Diagnostics) HasErrors() bool { return false } -// ForRPC returns a version of the receiver that has been simplified so that -// it is friendly to RPC protocols. -// -// Currently this means that it can be serialized with encoding/gob and -// subsequently re-inflated. It may later grow to include other serialization -// formats. -// -// Note that this loses information about the original objects used to -// construct the diagnostics, so e.g. the errwrap API will not work as -// expected on an error-wrapped Diagnostics that came from ForRPC. -func (diags Diagnostics) ForRPC() Diagnostics { - ret := make(Diagnostics, len(diags)) - for i := range diags { - ret[i] = makeRPCFriendlyDiag(diags[i]) - } - return ret -} - // Err flattens a diagnostics list into a single Go error, or to nil // if the diagnostics list does not include any error-level diagnostics. // diff --git a/pkg/terraform/tfdiags/rpc_friendly.go b/pkg/terraform/tfdiags/rpc_friendly.go deleted file mode 100644 index 485063b0..00000000 --- a/pkg/terraform/tfdiags/rpc_friendly.go +++ /dev/null @@ -1,59 +0,0 @@ -package tfdiags - -import ( - "encoding/gob" -) - -type rpcFriendlyDiag struct { - Severity_ Severity - Summary_ string - Detail_ string - Subject_ *SourceRange - Context_ *SourceRange -} - -// rpcFriendlyDiag transforms a given diagnostic so that is more friendly to -// RPC. -// -// In particular, it currently returns an object that can be serialized and -// later re-inflated using gob. This definition may grow to include other -// serializations later. -func makeRPCFriendlyDiag(diag Diagnostic) Diagnostic { - desc := diag.Description() - source := diag.Source() - return &rpcFriendlyDiag{ - Severity_: diag.Severity(), - Summary_: desc.Summary, - Detail_: desc.Detail, - Subject_: source.Subject, - Context_: source.Context, - } -} - -func (d *rpcFriendlyDiag) Severity() Severity { - return d.Severity_ -} - -func (d *rpcFriendlyDiag) Description() Description { - return Description{ - Summary: d.Summary_, - Detail: d.Detail_, - } -} - -func (d *rpcFriendlyDiag) Source() Source { - return Source{ - Subject: d.Subject_, - Context: d.Context_, - } -} - -func (d rpcFriendlyDiag) FromExpr() *FromExpr { - // RPC-friendly diagnostics cannot preserve expression information because - // expressions themselves are not RPC-friendly. - return nil -} - -func init() { - gob.Register((*rpcFriendlyDiag)(nil)) -}