-
Notifications
You must be signed in to change notification settings - Fork 53
Add BillingDataInPropertiesBag linter rule to disallow the reserved 'BillingData' property in a resource's properties bag #850
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
Open
vayada
wants to merge
2
commits into
Azure:main
Choose a base branch
from
vayada:vayada/billingDataLinterRule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # BillingDataInPropertiesBag | ||
|
|
||
| ## Category | ||
|
|
||
| ARM Error | ||
|
|
||
| ## Applies to | ||
|
|
||
| ARM OpenAPI(swagger) specs | ||
|
|
||
| ## Description | ||
|
|
||
| A property named `BillingData` (matched case-insensitively) must not be present in a resource's properties bag. If billing-related data is required, model it under a dedicated, appropriately named property or a separate model definition instead of placing a `BillingData` property directly in the resource properties bag. | ||
|
|
||
| ## How to fix the violation | ||
|
|
||
| Remove the `BillingData` property from the resource properties bag. Represent the information using a differently named property or a dedicated model definition as appropriate. | ||
|
|
||
| ### Valid/Good Example | ||
|
|
||
| ```json | ||
| "Resource": { | ||
| "properties": { | ||
| "properties": { | ||
| "provisioningState": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Invalid/Bad Example | ||
|
|
||
| ```json | ||
| "Resource": { | ||
| "properties": { | ||
| "properties": { | ||
| "billingData": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```json | ||
| "Resource": { | ||
| "properties": { | ||
| "properties": { | ||
| "billingData": { | ||
| "$ref": "#/definitions/BillingData" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/rulesets/src/spectral/functions/billing-data-in-properties-bag.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // BillingDataInPropertiesBag | ||
| // A property named 'BillingData' (matched case-insensitively) must not be defined in a | ||
| // resource's properties bag. | ||
|
|
||
| import _ from "lodash" | ||
| import { getProperties } from "./utils" | ||
|
|
||
| const BILLING_DATA = "billingdata" // compared case-insensitively | ||
| const PROPERTIES = "properties" | ||
| const ERROR_MESSAGE = "The 'BillingData' property is not allowed in the resource properties bag." | ||
|
|
||
| // Recursively collect the path to every property named "BillingData" (case-insensitive) that is | ||
| // defined within the given schema. Traversal is restricted to schema-structure keywords | ||
| // (properties, allOf/anyOf/oneOf, items, additionalProperties) so that only actual property | ||
| // definitions are inspected. Values inside non-structural metadata (e.g. default values, enum | ||
| // values, examples, or vendor extensions) are intentionally ignored to avoid false positives. | ||
| function collectBillingDataPropertyPaths( | ||
| schema: any, | ||
| basePath: (string | number)[], | ||
| matches: (string | number)[][], | ||
| visited: WeakSet<object> | ||
| ): void { | ||
| if (!_.isObject(schema) || visited.has(schema)) { | ||
| return | ||
| } | ||
| visited.add(schema) | ||
|
|
||
| const s = schema as { [key: string]: any } | ||
|
|
||
| // properties: a map of property name -> property schema | ||
| if (_.isObject(s.properties)) { | ||
| for (const [name, propertySchema] of Object.entries(s.properties as { [key: string]: any })) { | ||
| if (name.toLowerCase() === BILLING_DATA) { | ||
| matches.push([...basePath, PROPERTIES, name]) | ||
| } | ||
| collectBillingDataPropertyPaths(propertySchema, [...basePath, PROPERTIES, name], matches, visited) | ||
| } | ||
| } | ||
|
|
||
| // allOf / anyOf / oneOf: arrays of subschemas | ||
| for (const keyword of ["allOf", "anyOf", "oneOf"]) { | ||
| const subschemas = s[keyword] | ||
| if (Array.isArray(subschemas)) { | ||
| subschemas.forEach((subschema: any, index: number) => { | ||
| collectBillingDataPropertyPaths(subschema, [...basePath, keyword, index], matches, visited) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // items: a single subschema or an array of subschemas | ||
| if (Array.isArray(s.items)) { | ||
| s.items.forEach((itemSchema: any, index: number) => { | ||
| collectBillingDataPropertyPaths(itemSchema, [...basePath, "items", index], matches, visited) | ||
| }) | ||
| } else if (_.isObject(s.items)) { | ||
| collectBillingDataPropertyPaths(s.items, [...basePath, "items"], matches, visited) | ||
| } | ||
|
|
||
| // additionalProperties: a subschema (when it is an object rather than a boolean) | ||
| if (_.isObject(s.additionalProperties)) { | ||
| collectBillingDataPropertyPaths(s.additionalProperties, [...basePath, "additionalProperties"], matches, visited) | ||
| } | ||
| } | ||
|
|
||
| export const billingDataInPropertiesBag = (definition: any, _opts: any, ctx: any) => { | ||
| const bag = getProperties(definition) | ||
| const matches: (string | number)[][] = [] | ||
| collectBillingDataPropertyPaths(bag, [], matches, new WeakSet<object>()) | ||
|
|
||
| return matches.map((path) => ({ | ||
| message: ERROR_MESSAGE, | ||
| path: _.concat(ctx.path, PROPERTIES, path), | ||
| })) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.