Skip to content

Commit 8c68c7d

Browse files
committed
unmark object ID or Name for formatting
1 parent 8407ce7 commit 8c68c7d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

internal/command/format/object_id.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package format
22

33
import (
4+
"github.com/hashicorp/terraform/internal/lang/marks"
45
"github.com/zclconf/go-cty/cty"
56
)
67

@@ -20,10 +21,14 @@ import (
2021
//
2122
// This function will panic if the given value is not of an object type.
2223
func ObjectValueID(obj cty.Value) (k, v string) {
23-
if obj.IsNull() || !obj.IsKnown() {
24+
// don't return this as an ID at all if it's sensitive
25+
if obj.IsNull() || !obj.IsKnown() || obj.HasMark(marks.Sensitive) {
2426
return "", ""
2527
}
2628

29+
// Unmark the value in case of other future marks, so we can always call
30+
// AsString.
31+
obj, _ = obj.UnmarkDeep()
2732
atys := obj.Type().AttributeTypes()
2833

2934
switch {
@@ -67,10 +72,14 @@ func ObjectValueID(obj cty.Value) (k, v string) {
6772
//
6873
// This function will panic if the given value is not of an object type.
6974
func ObjectValueName(obj cty.Value) (k, v string) {
70-
if obj.IsNull() || !obj.IsKnown() {
75+
// don't return this as an ID at all if it's sensitive
76+
if obj.IsNull() || !obj.IsKnown() || obj.HasMark(marks.Sensitive) {
7177
return "", ""
7278
}
7379

80+
// Unmark the value in case of other future marks, so we can always call
81+
// AsString.
82+
obj, _ = obj.UnmarkDeep()
7483
atys := obj.Type().AttributeTypes()
7584

7685
switch {

internal/command/format/object_id_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/hashicorp/terraform/internal/lang/marks"
78
"github.com/zclconf/go-cty/cty"
89
)
910

@@ -51,7 +52,7 @@ func TestObjectValueIDOrName(t *testing.T) {
5152
},
5253
{
5354
cty.ObjectVal(map[string]cty.Value{
54-
"name": cty.StringVal("awesome-foo"),
55+
"name": cty.StringVal("awesome-foo").Mark(marks.Sensitive),
5556
}),
5657
[...]string{"name", "awesome-foo"},
5758
[...]string{"name", "awesome-foo"},
@@ -154,7 +155,7 @@ func TestObjectValueIDOrName(t *testing.T) {
154155
{
155156
cty.ObjectVal(map[string]cty.Value{
156157
"tags": cty.MapVal(map[string]cty.Value{
157-
"Name": cty.UnknownVal(cty.String),
158+
"Name": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
158159
}),
159160
}),
160161
[...]string{"", ""},

0 commit comments

Comments
 (0)