Skip to content

Commit 4104c72

Browse files
committed
feat(tf): Update Azure provider and PublicStorage
1 parent 405065b commit 4104c72

5 files changed

Lines changed: 57 additions & 3 deletions

File tree

ql/lib/codeql/hcl/Resources.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import codeql.Locations
22
private import codeql.hcl.AST
3+
private import codeql.hcl.Terraform::Terraform
34

45
// Resources are the most important element in the Terraform language.
56
// Each resource block describes one or more infrastructure objects, such as
@@ -18,7 +19,7 @@ class Resource extends Block {
1819
/**
1920
* Get the provider of the resource.
2021
*/
21-
string getProvider() { result = "Unknown Provider" }
22+
RequiredProvider getProvider() { none() }
2223

2324
/**
2425
* Returns the resource id.

ql/lib/codeql/hcl/Terraform.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import codeql.files.FileSystem
22
private import codeql.hcl.AST
3+
private import codeql.iac.Dependencies
34
private import Resources
45

56
module Terraform {
@@ -42,12 +43,21 @@ module Terraform {
4243
*/
4344
abstract string getVersion();
4445

46+
/**
47+
* Gets the semantic version of the provider.
48+
*/
49+
abstract SemanticVersion getSemanticVersion();
50+
4551
/**
4652
* Gets the source of the provider.
4753
*/
4854
abstract string getSource();
4955
}
5056

57+
RequiredProvider getProviderByName(string name) {
58+
exists(RequiredProvider provider | provider.getName() = name)
59+
}
60+
5161
/**
5262
* Basic Terraform required provider String.
5363
*/
@@ -62,6 +72,8 @@ module Terraform {
6272

6373
override string getVersion() { result = this.getValue() }
6474

75+
override SemanticVersion getSemanticVersion() { result = this.getValue() }
76+
6577
/**
6678
* Basic providers are assumed to be from the Hashicorp namespace.
6779
*/
@@ -93,5 +105,7 @@ module Terraform {
93105
override string getVersion() {
94106
result = this.getElementByName("version").(StringLiteral).getValue()
95107
}
108+
109+
override SemanticVersion getSemanticVersion() { result = this.getVersion() }
96110
}
97111
}

ql/lib/codeql/hcl/providers/Azure.qll

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private import codeql.hcl.AST
22
private import codeql.hcl.Resources
33
private import codeql.hcl.Constants
4+
private import codeql.hcl.Terraform::Terraform
45

56
module Azure {
67
/**
@@ -11,7 +12,7 @@ module Azure {
1112
class AzureResource extends Resource, Block {
1213
AzureResource() { this.getResourceType().regexpMatch("^azurerm.*") }
1314

14-
override string getProvider() { result = "Azurerm" }
15+
override RequiredProvider getProvider() { result = getProviderByName("azurerm") }
1516
}
1617

1718
/**
@@ -108,14 +109,36 @@ module Azure {
108109
*/
109110
override string getName() { result = this.getAttribute("name").(StringLiteral).getValue() }
110111

112+
/**
113+
* Get the `allow_blob_public_access` property of the storage account. Only available
114+
* for `azurerm` v2 and not v3 onwards.
115+
*
116+
* https://github.com/hashicorp/terraform-provider-azurerm/blob/main/CHANGELOG-v3.md
117+
*/
118+
boolean getAllowBlobPublicAccess() {
119+
this.getProvider().getSemanticVersion().maybeBefore("3.0.0") and
120+
result = this.getAttribute("allow_blob_public_access").(BooleanLiteral).getBool()
121+
or
122+
result = false
123+
}
124+
125+
/**
126+
* Get the `public_network_access_enabled` property of the storage account.
127+
*/
111128
boolean getEnableHttpsTrafficOnly() {
112129
result = this.getAttribute("enable_https_traffic_only").(BooleanLiteral).getBool()
113130
}
114131

132+
/**
133+
* Get the `public_network_access_enabled` property of the storage account.
134+
*/
115135
boolean getPublicNetworkAccess() {
116136
result = this.getAttribute("public_network_access_enabled").(BooleanLiteral).getBool()
117137
}
118138

139+
/**
140+
* Get the `allow_nested_items_to_be_public` property of the storage account.
141+
*/
119142
boolean getAllowNestedItemsToBePublic() {
120143
result = this.getAttribute("allow_nested_items_to_be_public").(BooleanLiteral).getBool()
121144
}

ql/lib/codeql/hcl/security/PublicStorage.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ class AzurePublicStorage extends Azure::AzureResource, PublicStorage {
1515
storage_container.getProperty("publicAccess").(StringLiteral).getValue() = "blob"
1616
)
1717
or
18-
// Azure Storage Accounts (v3)
18+
// Azure Storage Accounts
1919
exists(Azure::StorageAccount storage_acount |
20+
// v2
21+
storage_acount.getAllowBlobPublicAccess() = true or
22+
// v3
2023
storage_acount.getPublicNetworkAccess() = true or
2124
storage_acount.getAllowNestedItemsToBePublic() = true
2225
)

ql/test/queries-tests/Terraform/Azure/Storage/PublicAccess/storage.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,16 @@ resource "azurerm_storage_container" "insecure" {
1313
"publicAccess" = "blob"
1414
}
1515
}
16+
17+
# insecure (v3)
18+
resource "azurerm_storage_account" "insecure_storage_account" {
19+
name = "insecure-storage-account"
20+
location = var.location
21+
account_kind = var.kind
22+
account_tier = var.tier
23+
account_replication_type = var.replication_type
24+
resource_group_name = var.resource_group_name
25+
public_network_access_enabled = true
26+
allow_nested_items_to_be_public = true
27+
min_tls_version = var.min_tls_version
28+
}

0 commit comments

Comments
 (0)