Skip to content

Commit

Permalink
Add IsSensitive to AttributeSchema (#42)
Browse files Browse the repository at this point in the history
So that sensitive attributes can be flagged.
  • Loading branch information
beandrad authored Jun 2, 2021
1 parent b62f3a9 commit ca187e8
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 21 deletions.
19 changes: 10 additions & 9 deletions decoder/attribute_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ func attributeSchemaToCandidate(name string, attr *schema.AttributeSchema, rng h
}

func detailForAttribute(attr *schema.AttributeSchema) string {
var detail string
details := []string{}

if attr.IsRequired {
detail = "Required"
details = append(details, "required")
} else if attr.IsOptional {
detail = "Optional"
details = append(details, "optional")
}

if attr.IsSensitive {
details = append(details, "sensitive")
}

friendlyName := attr.Expr.FriendlyName()
if friendlyName != "" {
if detail != "" {
detail = strings.Join([]string{detail, friendlyName}, ", ")
} else {
detail = friendlyName
}
details = append(details, friendlyName)
}

return detail
return strings.Join(details[:], ", ")
}

func snippetForAttribute(name string, attr *schema.AttributeSchema) string {
Expand Down
2 changes: 1 addition & 1 deletion decoder/body_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestDecoder_CandidateAtPos_computedAttributes(t *testing.T) {
List: []lang.Candidate{
{
Label: "attr2",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down
79 changes: 70 additions & 9 deletions decoder/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,16 @@ func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
"five": {Expr: schema.LiteralTypeOnly(cty.DynamicPseudoType)},
},
},
schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: "sensitive_resource"},
},
}): {
Attributes: map[string]*schema.AttributeSchema{
"six": {Expr: schema.LiteralTypeOnly(cty.Number), IsSensitive: true},
"seven": {Expr: schema.LiteralTypeOnly(cty.Number), IsRequired: true, IsSensitive: true},
},
},
},
}

Expand All @@ -783,6 +793,10 @@ func TestDecoder_CandidatesAtPos_basic(t *testing.T) {
count = 3
}
resource "sensitive_resource" "t" {
count = 2
}
resource "random_resource" "test" {
arg = ""
}
Expand Down Expand Up @@ -877,6 +891,19 @@ resource "random_resource" "test" {
},
Kind: lang.LabelCandidateKind,
},
{
Label: "sensitive_resource",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 11, Byte: 10},
End: hcl.Pos{Line: 1, Column: 25, Byte: 24},
},
NewText: "sensitive_resource",
Snippet: "sensitive_resource",
},
Kind: lang.LabelCandidateKind,
},
}),
},
{
Expand All @@ -885,7 +912,7 @@ resource "random_resource" "test" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "one",
Detail: "Required, string",
Detail: "required, string",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -899,7 +926,7 @@ resource "random_resource" "test" {
},
{
Label: "three",
Detail: "Optional, bool",
Detail: "optional, bool",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -914,7 +941,7 @@ resource "random_resource" "test" {
},
{
Label: "two",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -934,7 +961,7 @@ resource "random_resource" "test" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "one",
Detail: "Required, string",
Detail: "required, string",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -948,7 +975,7 @@ resource "random_resource" "test" {
},
{
Label: "three",
Detail: "Optional, bool",
Detail: "optional, bool",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -963,7 +990,7 @@ resource "random_resource" "test" {
},
{
Label: "two",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand All @@ -977,6 +1004,40 @@ resource "random_resource" "test" {
},
}),
},
{
"second block attribute",
hcl.Pos{Line: 6, Column: 1, Byte: 89},
lang.CompleteCandidates([]lang.Candidate{
{
Label: "seven",
Detail: "required, sensitive, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 6, Column: 1, Byte: 89},
End: hcl.Pos{Line: 6, Column: 1, Byte: 89},
},
NewText: "seven",
Snippet: "seven = ${1:1}",
},
Kind: lang.AttributeCandidateKind,
},
{
Label: "six",
Detail: "sensitive, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 6, Column: 1, Byte: 89},
End: hcl.Pos{Line: 6, Column: 1, Byte: 89},
},
NewText: "six",
Snippet: "six = ${1:1}",
},
Kind: lang.AttributeCandidateKind,
},
}),
},
}

for i, tc := range testCases {
Expand Down Expand Up @@ -1110,7 +1171,7 @@ func TestDecoder_CandidatesAtPos_multipleTypes(t *testing.T) {
expectedCandidates := lang.CompleteCandidates([]lang.Candidate{
{
Label: "for_each",
Detail: "Optional, set of any single type or map of any single type",
Detail: "optional, set of any single type or map of any single type",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down Expand Up @@ -1194,7 +1255,7 @@ resource "any" "ref" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "count",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down Expand Up @@ -1309,7 +1370,7 @@ resource "any" "ref" {
lang.CompleteCandidates([]lang.Candidate{
{
Label: "count",
Detail: "Optional, number",
Detail: "optional, number",
TextEdit: lang.TextEdit{
Range: hcl.Range{
Filename: "test.tf",
Expand Down
22 changes: 20 additions & 2 deletions decoder/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
IsOptional: true,
Description: lang.PlainText("Special attribute"),
},
"bool_attr": {
Expr: schema.LiteralTypeOnly(cty.Bool),
IsSensitive: true,
Description: lang.PlainText("Flag attribute"),
},
},
},
DependentBody: map[schema.SchemaKey]*schema.BodySchema{
Expand Down Expand Up @@ -314,6 +319,7 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
testConfig := []byte(`myblock "sushi" "salmon" {
str_attr = "test"
num_attr = 4
bool_attr = true
}
`)
d := NewDecoder()
Expand All @@ -331,17 +337,29 @@ func TestDecoder_HoverAtPos_basic(t *testing.T) {
expectedData *lang.HoverData
}{
{
"attribute name",
"optional attribute name",
hcl.Pos{Line: 2, Column: 6, Byte: 32},
&lang.HoverData{
Content: lang.Markdown("**str_attr** _Optional, string_\n\nSpecial attribute"),
Content: lang.Markdown("**str_attr** _optional, string_\n\nSpecial attribute"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 2, Column: 3, Byte: 29},
End: hcl.Pos{Line: 2, Column: 20, Byte: 46},
},
},
},
{
"sensitive attribute name",
hcl.Pos{Line: 4, Column: 6, Byte: 68},
&lang.HoverData{
Content: lang.Markdown("**bool_attr** _sensitive, bool_\n\nFlag attribute"),
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 4, Column: 3, Byte: 64},
End: hcl.Pos{Line: 4, Column: 19, Byte: 80},
},
},
},
{
"block type",
hcl.Pos{Line: 1, Column: 3, Byte: 2},
Expand Down
2 changes: 2 additions & 0 deletions schema/attribute_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type AttributeSchema struct {
IsOptional bool
IsDeprecated bool
IsComputed bool
IsSensitive bool

Expr ExprConstraints

Expand Down Expand Up @@ -85,6 +86,7 @@ func (as *AttributeSchema) Copy() *AttributeSchema {
IsOptional: as.IsOptional,
IsDeprecated: as.IsDeprecated,
IsComputed: as.IsComputed,
IsSensitive: as.IsSensitive,
IsDepKey: as.IsDepKey,
Description: as.Description,
Expr: as.Expr.Copy(),
Expand Down
10 changes: 10 additions & 0 deletions schema/attribute_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ func TestAttributeSchema_Validate(t *testing.T) {
},
nil,
},
{
&AttributeSchema{
Expr: ExprConstraints{
TraversalExpr{OfType: cty.Number, OfScopeId: lang.ScopeId("blah")},
},
IsRequired: true,
IsSensitive: true,
},
nil,
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit ca187e8

Please sign in to comment.