Skip to content

Commit f8665ef

Browse files
committed
feat: Add KeyVault framework support
1 parent 98dce32 commit f8665ef

File tree

5 files changed

+207
-1
lines changed

5 files changed

+207
-1
lines changed

ql/lib/codeql/bicep/Frameworks.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import frameworks.Microsoft.Compute
22
import frameworks.Microsoft.Network
33
import frameworks.Microsoft.Storage
4-
import frameworks.Microsoft.Databases
4+
import frameworks.Microsoft.Databases
5+
import frameworks.Microsoft.KeyVault
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
private import bicep
2+
private import codeql.bicep.Concepts
3+
4+
module KeyVault {
5+
class VaultResource extends Resource {
6+
/**
7+
* Constructs a VaultResource for any Microsoft.KeyVault resource type.
8+
*/
9+
VaultResource() { this.getResourceType().regexpMatch("^Microsoft.KeyVault/.*") }
10+
11+
string tenantId() { result = this.getProperties().getTenantId().getValue() }
12+
13+
KeyVaultProperties::Properties getProperties() { result = this.getProperty("properties") }
14+
15+
KeyVaultProperties::AccessPolicy getAccessPolicies() {
16+
result = this.getProperties().getAccessPolicies()
17+
}
18+
19+
override string toString() { result = "Key Vault Resource" }
20+
}
21+
22+
class PublicVaultResource extends PublicResource {
23+
private VaultResource vaultResource;
24+
25+
/**
26+
* Constructs a PublicVaultResource for any Microsoft.KeyVault resource type
27+
* that has public network access enabled.
28+
*/
29+
PublicVaultResource() {
30+
vaultResource.getProperties().publicNetworkAccess() = "Enabled" and
31+
this = vaultResource
32+
}
33+
34+
override Expr getPublicAccessProperty() {
35+
result = vaultResource.getProperties().getPublicNetworkAccess()
36+
}
37+
38+
override string toString() { result = "Public Key Vault Resource" }
39+
}
40+
41+
module KeyVaultProperties {
42+
/**
43+
* The properties object for the Microsoft.KeyVault/vaults type.
44+
*/
45+
class Properties extends Object {
46+
private VaultResource vaultResource;
47+
48+
/**
49+
* Constructs a Properties object for the given Key Vault resource.
50+
*/
51+
Properties() { this = vaultResource.getProperty("properties") }
52+
53+
/**
54+
* Returns the parent VaultResource.
55+
*/
56+
VaultResource getVaultResource() { result = vaultResource }
57+
58+
StringLiteral getTenantId() { result = this.getProperty("tenantId") }
59+
60+
string tenantId() { result = this.getTenantId().getValue() }
61+
62+
StringLiteral getCreateMode() { result = this.getProperty("createMode") }
63+
64+
string createMode() { result = this.getCreateMode().getValue() }
65+
66+
Boolean getEnabledForDeployment() { result = this.getProperty("enabledForDeployment") }
67+
68+
boolean enabledForDeployment() { result = this.getEnabledForDeployment().getBool() }
69+
70+
Boolean getEnabledForDiskEncryption() {
71+
result = this.getProperty("enabledForDiskEncryption")
72+
}
73+
74+
boolean enabledForDiskEncryption() { result = this.getEnabledForDiskEncryption().getBool() }
75+
76+
Boolean getEnabledForTemplateDeployment() {
77+
result = this.getProperty("enabledForTemplateDeployment")
78+
}
79+
80+
boolean enabledForTemplateDeployment() {
81+
result = this.getEnabledForTemplateDeployment().getBool()
82+
}
83+
84+
Boolean getSoftDeleteEnabled() { result = this.getProperty("softDeleteEnabled") }
85+
86+
boolean softDeleteEnabled() { result = this.getSoftDeleteEnabled().getBool() }
87+
88+
Boolean getPurgeProtectionEnabled() { result = this.getProperty("purgeProtectionEnabled") }
89+
90+
boolean purgeProtectionEnabled() { result = this.getPurgeProtectionEnabled().getBool() }
91+
92+
StringLiteral getPublicNetworkAccess() { result = this.getProperty("publicNetworkAccess") }
93+
94+
string publicNetworkAccess() { result = this.getPublicNetworkAccess().getValue() }
95+
96+
AccessPolicy getAccessPolicies() {
97+
result = this.getProperty("accessPolicies").(Array).getElements()
98+
}
99+
100+
AccessPolicy getAccessPolicy(int index) {
101+
result = this.getProperty("accessPolicies").(Array).getElement(index)
102+
}
103+
}
104+
105+
class AccessPolicy extends Object {
106+
private KeyVaultProperties::Properties properties;
107+
108+
/**
109+
* Constructs an AccessPolicy object for the given Key Vault properties.
110+
*/
111+
AccessPolicy() { this = properties.getProperty("accessPolicies").(Array).getElements() }
112+
113+
/**
114+
* Returns the tenant ID of the access policy.
115+
*/
116+
string getTenantId() { result = this.getProperty("tenantId").(StringLiteral).getValue() }
117+
118+
/**
119+
* Returns the object ID of the access policy.
120+
*/
121+
string getObjectId() { result = this.getProperty("objectId").(StringLiteral).getValue() }
122+
123+
string toString() { result = "AccessPolicy" }
124+
}
125+
126+
class AccessPolicyPermissions extends Object {
127+
private AccessPolicy accessPolicy;
128+
129+
/**
130+
* Constructs an AccessPolicyPermissions object for the given access policy.
131+
*/
132+
AccessPolicyPermissions() { this = accessPolicy.getProperty("permissions") }
133+
134+
Array getCertificates() { result = this.getProperty("certificates") }
135+
136+
StringLiteral getCertificate(int index) { result = this.getCertificates().getElement(index) }
137+
138+
Array getKeys() { result = this.getProperty("keys") }
139+
140+
StringLiteral getKey(int index) { result = this.getKeys().getElement(index) }
141+
142+
Array getSecrets() { result = this.getProperty("secrets") }
143+
144+
StringLiteral getSecret(int index) { result = this.getSecrets().getElement(index) }
145+
146+
Array getStorages() { result = this.getProperty("storage") }
147+
148+
StringLiteral getStorage(int index) { result = this.getStorages().getElement(index) }
149+
150+
string toString() { result = "AccessPolicyPermissions" }
151+
}
152+
}
153+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
keyvault
2+
| app.bicep:1:1:37:1 | Key Vault Resource |
3+
keyvaultPolicies
4+
| app.bicep:1:1:37:1 | Key Vault Resource | app.bicep:11:7:19:7 | AccessPolicy |
5+
| app.bicep:1:1:37:1 | Key Vault Resource | app.bicep:20:7:28:7 | AccessPolicy |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import bicep
2+
3+
query predicate keyvault(KeyVault::VaultResource vault) {
4+
any()
5+
}
6+
7+
query predicate keyvaultPolicies(KeyVault::VaultResource vault, KeyVault::KeyVaultProperties::AccessPolicy policy) {
8+
policy = vault.getAccessPolicies()
9+
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource keyVault 'Microsoft.KeyVault/vaults@2021-06-01-preview' = {
2+
name: 'mykeyvault'
3+
location: 'eastus'
4+
properties: {
5+
tenantId: '00000000-0000-0000-0000-000000000000'
6+
sku: {
7+
family: 'A'
8+
name: 'standard'
9+
}
10+
accessPolicies: [
11+
{
12+
tenantId: '00000000-0000-0000-0000-000000000000'
13+
objectId: '11111111-1111-1111-1111-111111111111'
14+
permissions: {
15+
keys: [ 'get', 'list' ]
16+
secrets: [ 'get' ]
17+
certificates: []
18+
}
19+
},
20+
{
21+
tenantId: '00000000-0000-0000-0000-000000000000'
22+
objectId: '22222222-2222-2222-2222-222222222222'
23+
permissions: {
24+
keys: [ 'get' ]
25+
secrets: [ 'get', 'set' ]
26+
certificates: [ 'get' ]
27+
}
28+
}
29+
]
30+
enabledForDeployment: false
31+
enabledForDiskEncryption: false
32+
enabledForTemplateDeployment: false
33+
enableSoftDelete: true
34+
enablePurgeProtection: true
35+
publicNetworkAccess: 'Disabled' // Recommended: restrict public access
36+
}
37+
}

0 commit comments

Comments
 (0)