Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve heuristic for detection of ARM Resource References #3832

Merged
merged 8 commits into from
Mar 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make property type checks more robust
theunrepentantgeek committed Feb 28, 2024
commit ac35811e38a4aebec7d628465c09531b7bb54f81
Original file line number Diff line number Diff line change
@@ -180,19 +180,31 @@ var armIDNameRegex = regexp.MustCompile("(?i)(^Id$|ResourceID|ARMID)")
// This can be used for logging/reporting purposes to discover references which we missed.
func DoesPropertyLookLikeARMReference(prop *astmodel.PropertyDefinition) bool {
// The property must be a string, optional string, list of strings, or map[string]string
isString := astmodel.TypeEquals(prop.PropertyType(), astmodel.StringType)
isOptionalString := astmodel.TypeEquals(prop.PropertyType(), astmodel.OptionalStringType)
isStringSlice := astmodel.TypeEquals(prop.PropertyType(), astmodel.NewArrayType(astmodel.StringType))
isStringMap := astmodel.TypeEquals(prop.PropertyType(), astmodel.MapOfStringStringType)
mightBeReference := false
if pt, ok := astmodel.AsPrimitiveType(prop.PropertyType()); ok {
// Might be a reference if we have a primitive type that's a string
mightBeReference = pt == astmodel.StringType
}

if at, ok := astmodel.AsArrayType(prop.PropertyType()); ok {
// Might be references if we have an array of strings
elementType, elementTypeIsPrimitive := astmodel.AsPrimitiveType(at.Element())
mightBeReference = elementTypeIsPrimitive &&
elementType == astmodel.StringType
}

if !isString && !isOptionalString && !isStringSlice && !isStringMap {
return false
if mt, ok := astmodel.AsMapType(prop.PropertyType()); ok {
// Might be references if we have a map of strings to strings
keyType, keyTypeIsPrimitive := astmodel.AsPrimitiveType(mt.KeyType())
valueType, valueTypeIsPrimitive := astmodel.AsPrimitiveType(mt.ValueType())
mightBeReference = keyTypeIsPrimitive && valueTypeIsPrimitive &&
keyType == astmodel.StringType && valueType == astmodel.StringType
}

hasMatchingName := armIDNameRegex.MatchString(prop.PropertyName().String())
hasMatchingDescription := armIDDescriptionRegex.MatchString(prop.Description())

if hasMatchingName || hasMatchingDescription {
if mightBeReference && (hasMatchingName || hasMatchingDescription) {
return true
}