forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 22
Add reusable YAML document classification foundation #394
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
Chanel (chanel-y)
wants to merge
2
commits into
main
Choose a base branch
from
iac-p0/yaml-document-foundation
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,86 @@ | ||
| /** | ||
| * Provides YAML document classification shared across IaC YAML query families. | ||
| * | ||
| * This library identifies the broad document kind of a YAML document (Azure | ||
| * DevOps Pipelines, Kubernetes/Helm, Compose, OpenAPI, or CloudFormation) and | ||
| * excludes content that is out of scope for YAML-focused IaC queries even | ||
| * though it may be reachable through the same YAML abstract syntax tree: | ||
| * | ||
| * - JSON files. The extractor represents JSON using the same node types as | ||
| * YAML, since JSON is a syntactic subset of YAML. Callers that only intend | ||
| * to analyze literal YAML syntax must not rely on node type alone. | ||
| * - Azure Resource Manager (ARM) templates. ARM templates are JSON by | ||
| * default (`azuredeploy.json`), but can also carry a `.yaml` extension | ||
| * while keeping the ARM `$schema` marker, so the exclusion is schema-based | ||
| * rather than purely extension-based. | ||
| * | ||
| * Terraform, HCL, and Bicep are not represented as `YamlNode`s at all in this | ||
| * extractor, so no additional exclusion is required for them here. | ||
| */ | ||
|
|
||
| import iac | ||
| private import codeql.iac.YAML | ||
| private import codeql.iac.azure.Pipelines | ||
| private import codeql.iac.helmcharts.HelmChart | ||
| private import codeql.iac.compose.Compose | ||
| private import codeql.iac.openapi.OpenApi | ||
| private import codeql.iac.aws.CloudFormation | ||
|
|
||
| module YamlDocumentClassification { | ||
| /** | ||
| * A YAML document whose source file uses a YAML extension (`.yml` or | ||
| * `.yaml`), as opposed to a JSON file that happens to be representable by | ||
| * the same node types. | ||
| */ | ||
| class YamlSyntaxDocument extends YamlNode, YamlDocument, YamlMapping { | ||
| YamlSyntaxDocument() { this.getFile().getExtension() = ["yml", "yaml"] } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `doc` carries the Azure Resource Manager (ARM) template schema | ||
| * marker. ARM templates are out of scope for YAML-focused IaC queries even | ||
| * when authored with a `.yaml` extension. | ||
| */ | ||
| private predicate hasArmSchemaMarker(YamlSyntaxDocument doc) { | ||
| yamlToString(doc.lookup("$schema")).regexpMatch(".*schema\\.management\\.azure\\.com.*") | ||
| } | ||
|
|
||
| /** | ||
| * The kind of a supported in-scope YAML document. | ||
| */ | ||
| class DocumentKind extends string { | ||
| DocumentKind() { | ||
| this = ["ado-pipeline", "kubernetes-helm", "compose", "openapi", "cloudformation"] | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `doc` is a supported, in-scope YAML document of the given | ||
| * `kind`. | ||
| * | ||
| * A document is only classified once its file extension is `.yml`/`.yaml` | ||
| * and it does not carry the ARM template schema marker. Callers writing | ||
| * YAML-only IaC queries should use this predicate, rather than the | ||
| * underlying per-schema `Document` classes directly, so that ARM/JSON | ||
| * content is consistently excluded. | ||
| */ | ||
| predicate isSupportedYamlDocument(YamlSyntaxDocument doc, DocumentKind kind) { | ||
| not hasArmSchemaMarker(doc) and | ||
| ( | ||
| doc instanceof AzurePipelines::Document and kind = "ado-pipeline" | ||
| or | ||
| doc instanceof HelmChart::Document and kind = "kubernetes-helm" | ||
| or | ||
| doc instanceof Compose::Document and kind = "compose" | ||
| or | ||
| doc instanceof OpenApi::Document and kind = "openapi" | ||
| or | ||
| doc instanceof CloudFormation::Document and kind = "cloudformation" | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Gets a supported, in-scope YAML document of any kind. | ||
| */ | ||
| YamlSyntaxDocument getASupportedYamlDocument() { isSupportedYamlDocument(result, _) } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
iac/ql/test/library-tests/yaml-classification/AST.expected
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,15 @@ | ||
| supportedYamlDocument | ||
| | azure-pipelines.yaml:1:1:8:25 | Azure DevOps Pipeline | ado-pipeline | | ||
| | cloudformation.yaml:1:1:6:29 | CloudFormation Document | cloudformation | | ||
| | compose.yaml:1:1:4:24 | version: "3.9" | compose | | ||
| | deployment.yaml:1:1:8:29 | HelmChart Document | kubernetes-helm | | ||
| | openapi.yaml:1:1:12:26 | OpenApi Document | openapi | | ||
| allYamlDocuments | ||
| | arm-template.yaml:1:1:5:24 | $schema ... .json#" | | ||
| | azure-pipelines.yaml:1:1:8:25 | Azure DevOps Pipeline | | ||
| | cloudformation.yaml:1:1:6:29 | CloudFormation Document | | ||
| | compose.yaml:1:1:4:24 | version: "3.9" | | ||
| | deployment.yaml:1:1:8:29 | HelmChart Document | | ||
| | openapi.json:1:1:8:1 | OpenApi Document | | ||
| | openapi.yaml:1:1:12:26 | OpenApi Document | | ||
| | unrelated.yaml:1:1:4:8 | descrip ... ocument | |
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,10 @@ | ||
| private import iac | ||
| private import codeql.iac.YamlDocumentClassification | ||
|
|
||
| query predicate supportedYamlDocument( | ||
| YamlDocumentClassification::YamlSyntaxDocument doc, YamlDocumentClassification::DocumentKind kind | ||
| ) { | ||
| YamlDocumentClassification::isSupportedYamlDocument(doc, kind) | ||
| } | ||
|
|
||
| query predicate allYamlDocuments(YamlDocument doc) { any() } |
5 changes: 5 additions & 0 deletions
5
iac/ql/test/library-tests/yaml-classification/arm-template.yaml
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,5 @@ | ||
| $schema: "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#" | ||
| contentVersion: "1.0.0.0" | ||
| resources: | ||
| - type: Microsoft.Storage/storageAccounts | ||
| name: samplestorage |
8 changes: 8 additions & 0 deletions
8
iac/ql/test/library-tests/yaml-classification/azure-pipelines.yaml
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,8 @@ | ||
| trigger: | ||
| - main | ||
|
|
||
| pool: | ||
| vmImage: ubuntu-latest | ||
|
|
||
| steps: | ||
| - script: echo "hello" |
6 changes: 6 additions & 0 deletions
6
iac/ql/test/library-tests/yaml-classification/cloudformation.yaml
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,6 @@ | ||
| AWSTemplateFormatVersion: "2010-09-09" | ||
| Resources: | ||
| Bucket: | ||
| Type: AWS::S3::Bucket | ||
| Properties: | ||
| AccessControl: Private |
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,4 @@ | ||
| version: "3.9" | ||
| services: | ||
| web: | ||
| image: nginx:latest |
8 changes: 8 additions & 0 deletions
8
iac/ql/test/library-tests/yaml-classification/deployment.yaml
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,8 @@ | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: sample | ||
| spec: | ||
| containers: | ||
| - name: app | ||
| image: example/app:1.0 |
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,8 @@ | ||
| { | ||
| "openapi": "3.0.0", | ||
| "info": { | ||
| "title": "Sample API", | ||
| "version": "1.0" | ||
| }, | ||
| "paths": {} | ||
| } |
12 changes: 12 additions & 0 deletions
12
iac/ql/test/library-tests/yaml-classification/openapi.yaml
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,12 @@ | ||
| openapi: "3.0.0" | ||
| info: | ||
| title: Sample API | ||
| version: "1.0" | ||
| servers: | ||
| - url: https://example.com | ||
| paths: | ||
| /items: | ||
| get: | ||
| responses: | ||
| "200": | ||
| description: OK |
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,4 @@ | ||
| description: an unrelated YAML document | ||
| values: | ||
| - one | ||
| - two |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use a good old
matcheshere instead, can't we? That would be much faster