Skip to content

Commit a72077f

Browse files
committed
feat(tf): Improve resource, update Azure provider, and Public storage query
1 parent 28703f3 commit a72077f

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

ql/lib/codeql/hcl/Resources.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ private import codeql.hcl.AST
1010
class Resource extends Block {
1111
Resource() { this.hasType("resource") }
1212

13+
/**
14+
* Get the name of the resource.
15+
*/
1316
string getName() { result = this.getLabel(1) }
1417

18+
/**
19+
* Get the provider of the resource.
20+
*/
21+
string getProvider() { result = "Unknown Provider" }
22+
1523
/**
1624
* Returns the resource id.
1725
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Azure {
1010
*/
1111
class AzureResource extends Resource, Block {
1212
AzureResource() { this.getResourceType().regexpMatch("^azurerm.*") }
13+
14+
override string getProvider() { result = "Azurerm" }
1315
}
1416

1517
/**
@@ -78,6 +80,11 @@ module Azure {
7880
class StorageContainer extends AzureResource {
7981
StorageContainer() { this.getResourceType() = "azurerm_storage_container" }
8082

83+
/**
84+
* Get the name of the storage container.
85+
*/
86+
override string getName() { result = this.getAttribute("name").(StringLiteral).getValue() }
87+
8188
string getContainerAccessType() {
8289
result = this.getAttribute("container_access_type").(StringLiteral).getValue()
8390
}
@@ -96,6 +103,11 @@ module Azure {
96103
class StorageAccount extends AzureResource {
97104
StorageAccount() { this.getResourceType() = "azurerm_storage_account" }
98105

106+
/**
107+
* Get the name of the storage account.
108+
*/
109+
override string getName() { result = this.getAttribute("name").(StringLiteral).getValue() }
110+
99111
boolean getEnableHttpsTrafficOnly() {
100112
result = this.getAttribute("enable_https_traffic_only").(BooleanLiteral).getBool()
101113
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import iac
22

3-
abstract class PublicStorage extends Expr { }
3+
abstract class PublicStorage extends Expr {
4+
abstract string getName();
5+
}
46

57
/**
68
* Azure Public Storage.
79
*/
8-
class AzurePublicStorage extends PublicStorage {
10+
class AzurePublicStorage extends Azure::AzureResource, PublicStorage {
911
AzurePublicStorage() {
1012
// Azure Storage Container
1113
exists(Azure::StorageContainer storage_container |
@@ -19,4 +21,6 @@ class AzurePublicStorage extends PublicStorage {
1921
storage_acount.getAllowNestedItemsToBePublic() = true
2022
)
2123
}
24+
25+
override string getName() { result = this.getName() }
2226
}

ql/src/security/Terraform/Azure/ManagedDisk/PublicAccess.ql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
*/
1414

1515
import hcl
16+
import codeql.hcl.security.PublicStorage
1617

1718
// https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk
18-
from Azure::StorageContainer managed_disk
19-
where
20-
managed_disk.getContainerAccessType() = "blob" and
21-
managed_disk.getProperty("publicAccess").(StringLiteral).getValue() = "blob"
22-
select managed_disk, "Azure Storage is Unencrypted for '" + managed_disk.getName() + "'"
19+
from AzurePublicStorage public_storage
20+
select public_storage, "Azure Storage is Public for '" + public_storage.getName() + "'"

0 commit comments

Comments
 (0)