Skip to content

Commit

Permalink
helper/schema: Additional validation for schema attribute references
Browse files Browse the repository at this point in the history
Reference: #71
  • Loading branch information
bflad authored and appilon committed Apr 28, 2020
1 parent a4210cb commit ded2641
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
36 changes: 28 additions & 8 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}

if len(v.ConflictsWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v)
if err != nil {
return fmt.Errorf("ConflictsWith: %+v", err)
}
Expand All @@ -784,14 +784,14 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}

if len(v.ExactlyOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v)
if err != nil {
return fmt.Errorf("ExactlyOneOf: %+v", err)
}
}

if len(v.AtLeastOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap)
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v)
if err != nil {
return fmt.Errorf("AtLeastOneOf: %+v", err)
}
Expand Down Expand Up @@ -860,14 +860,20 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
return nil
}

func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap) error {
func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema) error {
for _, key := range keys {
parts := strings.Split(key, ".")
sm := topSchemaMap
var target *Schema
for _, part := range parts {
// Skip index fields
if _, err := strconv.Atoi(part); err == nil {
// Skip index fields if 0
partInt, err := strconv.Atoi(part)

if err == nil {
if partInt != 0 {
return fmt.Errorf("%s configuration block reference (%s) can only use the .0. index for TypeList and MaxItems: 1 configuration blocks", k, key)
}

continue
}

Expand All @@ -876,13 +882,27 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
return fmt.Errorf("%s references unknown attribute (%s) at part (%s)", k, key, part)
}

if subResource, ok := target.Elem.(*Resource); ok {
sm = schemaMap(subResource.Schema)
subResource, ok := target.Elem.(*Resource)

if !ok {
continue
}

if target.Type == TypeSet || target.MaxItems != 1 {
return fmt.Errorf("%s configuration block reference (%s) can only be used with TypeList and MaxItems: 1 configuration blocks", k, key)
}

sm = schemaMap(subResource.Schema)
}

if target == nil {
return fmt.Errorf("%s cannot find target attribute (%s), sm: %#v", k, key, sm)
}

if target == self {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
}

if target.Required {
return fmt.Errorf("%s cannot contain Required attribute (%s)", k, key)
}
Expand Down
38 changes: 31 additions & 7 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3611,14 +3611,15 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as it will always error during runtime
true,
},

"ConflictsWith list index syntax with list configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
Expand Down Expand Up @@ -3660,6 +3661,29 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
true,
},

"ConflictsWith list index syntax with list configuration block missing MaxItems": {
map[string]*Schema{
"config_block_attr": &Schema{
Type: TypeList,
Optional: true,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": &Schema{
Type: TypeString,
Optional: true,
},
},
},
},
"test": &Schema{
Type: TypeBool,
Optional: true,
ConflictsWith: []string{"config_block_attr.0.missing_attr"},
},
},
true,
},

"ConflictsWith list index syntax with set configuration block existing attribute": {
map[string]*Schema{
"config_block_attr": &Schema{
Expand All @@ -3680,7 +3704,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ConflictsWith: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: This should return an error as sets cannot be indexed
true,
},

"ConflictsWith list index syntax with set configuration block missing attribute": {
Expand Down Expand Up @@ -3726,7 +3750,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ConflictsWith: []string{"config_block_attr.nested_attr"},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error
true,
},

"ConflictsWith map key syntax with list configuration block self reference": {
Expand All @@ -3745,7 +3769,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error
true,
},

"ConflictsWith map key syntax with set configuration block existing attribute": {
Expand All @@ -3768,7 +3792,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ConflictsWith: []string{"config_block_attr.nested_attr"},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error and sets cannot be indexed
true,
},

"ConflictsWith map key syntax with set configuration block self reference": {
Expand All @@ -3787,7 +3811,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
false, // TODO: This should return an error as validateConflictingAttributes will never error and sets cannot be indexed
true,
},

"ConflictsWith map key syntax with map attribute": {
Expand Down Expand Up @@ -3830,7 +3854,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ConflictsWith: []string{"test"},
},
},
false, // TODO: This should return an error as it will always error during runtime
true,
},

"Sub-resource invalid": {
Expand Down

0 comments on commit ded2641

Please sign in to comment.