From a48b024c0af6540fc3ba1849e4cef277783ef439 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 16 Aug 2021 16:42:09 -0400 Subject: [PATCH] unmark object ID or Name for formatting --- internal/command/format/object_id.go | 29 ++++++++++++++++++++++- internal/command/format/object_id_test.go | 19 +++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/internal/command/format/object_id.go b/internal/command/format/object_id.go index 85ebbfec5..75b427b8d 100644 --- a/internal/command/format/object_id.go +++ b/internal/command/format/object_id.go @@ -1,6 +1,7 @@ package format import ( + "github.com/hashicorp/terraform/internal/lang/marks" "github.com/zclconf/go-cty/cty" ) @@ -30,6 +31,11 @@ func ObjectValueID(obj cty.Value) (k, v string) { case atys["id"] == cty.String: v := obj.GetAttr("id") + if v.HasMark(marks.Sensitive) { + break + } + v, _ = v.Unmark() + if v.IsKnown() && !v.IsNull() { return "id", v.AsString() } @@ -38,6 +44,11 @@ func ObjectValueID(obj cty.Value) (k, v string) { // "name" isn't always globally unique, but if there isn't also an // "id" then it _often_ is, in practice. v := obj.GetAttr("name") + if v.HasMark(marks.Sensitive) { + break + } + v, _ = v.Unmark() + if v.IsKnown() && !v.IsNull() { return "name", v.AsString() } @@ -77,25 +88,41 @@ func ObjectValueName(obj cty.Value) (k, v string) { case atys["name"] == cty.String: v := obj.GetAttr("name") + if v.HasMark(marks.Sensitive) { + break + } + v, _ = v.Unmark() + if v.IsKnown() && !v.IsNull() { return "name", v.AsString() } case atys["tags"].IsMapType() && atys["tags"].ElementType() == cty.String: tags := obj.GetAttr("tags") - if tags.IsNull() || !tags.IsWhollyKnown() { + if tags.IsNull() || !tags.IsWhollyKnown() || tags.HasMark(marks.Sensitive) { break } + tags, _ = tags.Unmark() switch { case tags.HasIndex(cty.StringVal("name")).RawEquals(cty.True): v := tags.Index(cty.StringVal("name")) + if v.HasMark(marks.Sensitive) { + break + } + v, _ = v.Unmark() + if v.IsKnown() && !v.IsNull() { return "tags.name", v.AsString() } case tags.HasIndex(cty.StringVal("Name")).RawEquals(cty.True): // AWS-style naming convention v := tags.Index(cty.StringVal("Name")) + if v.HasMark(marks.Sensitive) { + break + } + v, _ = v.Unmark() + if v.IsKnown() && !v.IsNull() { return "tags.Name", v.AsString() } diff --git a/internal/command/format/object_id_test.go b/internal/command/format/object_id_test.go index d9c791214..2f13e1366 100644 --- a/internal/command/format/object_id_test.go +++ b/internal/command/format/object_id_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform/internal/lang/marks" "github.com/zclconf/go-cty/cty" ) @@ -57,6 +58,14 @@ func TestObjectValueIDOrName(t *testing.T) { [...]string{"name", "awesome-foo"}, [...]string{"name", "awesome-foo"}, }, + { + cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("awesome-foo").Mark(marks.Sensitive), + }), + [...]string{"", ""}, + [...]string{"", ""}, + [...]string{"", ""}, + }, { cty.ObjectVal(map[string]cty.Value{ "name": cty.StringVal("awesome-foo"), @@ -161,6 +170,16 @@ func TestObjectValueIDOrName(t *testing.T) { [...]string{"", ""}, [...]string{"", ""}, }, + { + cty.ObjectVal(map[string]cty.Value{ + "tags": cty.MapVal(map[string]cty.Value{ + "Name": cty.UnknownVal(cty.String).Mark(marks.Sensitive), + }), + }), + [...]string{"", ""}, + [...]string{"", ""}, + [...]string{"", ""}, + }, { cty.ObjectVal(map[string]cty.Value{ "tags": cty.MapVal(map[string]cty.Value{