Skip to content

Commit c2f10db

Browse files
authored
Merge pull request #181 from ViktorLindstrm/new-rules
ECS Fargate CloudFormation Rules
2 parents b1ebb70 + a69ecc8 commit c2f10db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1493
-1
lines changed

ql/lib/codeql/iac/aws/CloudFormation.qll

Lines changed: 362 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,365 @@ module CloudFormation {
158158
// )
159159
}
160160
}
161-
}
161+
class LambdaFunction extends Resource {
162+
LambdaFunction() { this.getType() = "AWS::Lambda::Function" }
163+
164+
override string toString() { result = "CloudFormation Lambda Function" }
165+
166+
/**
167+
* Get the Lambda function runtime.
168+
*/
169+
string getRuntime() {
170+
result = this.getProperties().getProperty("Runtime").(YamlString).getValue()
171+
}
172+
/**
173+
* get principal
174+
*/
175+
string getPrincipal() {
176+
result = this.getProperties().getProperty("Principal").(YamlString).getValue()
177+
}
178+
179+
}
180+
181+
class EC2SecurityGroup extends Resource {
182+
EC2SecurityGroup() { this.getType() = "AWS::EC2::SecurityGroup" }
183+
184+
override string toString() { result = "CloudFormation EC2 Security Group" }
185+
186+
/**
187+
* Get the security group egress rules.
188+
*/
189+
YamlNode getSgEgress() {
190+
result = this.getProperties().getProperty("SecurityGroupEgress")
191+
}
192+
YamlNode getEgressCidrIp() {
193+
result = this.getSgEgress().getAChildNode().(YamlMapping).lookup("CidrIp")
194+
}
195+
YamlNode getEgressFromPort() {
196+
result = this.getSgEgress().getAChildNode().(YamlMapping).lookup("FromPort")
197+
}
198+
YamlNode getEgressToPort() {
199+
result = this.getSgEgress().getAChildNode().(YamlMapping).lookup("ToPort")
200+
}
201+
YamlNode getEgressDesc() {
202+
result = this.getSgEgress().getAChildNode().(YamlMapping).lookup("Description")
203+
}
204+
205+
/**
206+
* Get the security group ingress rules.
207+
*/
208+
YamlNode getSgIngress() {
209+
result = this.getProperties().getProperty("SecurityGroupIngress")
210+
}
211+
YamlNode getIngressCidrIp() {
212+
result = this.getSgIngress().getAChildNode().(YamlMapping).lookup("CidrIp")
213+
}
214+
YamlNode getIngressFromPort() {
215+
result = this.getSgIngress().getAChildNode().(YamlMapping).lookup("FromPort")
216+
}
217+
YamlNode getIngressToPort() {
218+
result = this.getSgIngress().getAChildNode().(YamlMapping).lookup("ToPort")
219+
}
220+
YamlNode getIngressDesc() {
221+
result = this.getSgIngress().getAChildNode().(YamlMapping).lookup("Description")
222+
}
223+
}
224+
225+
class EC2SecurityGroupEgress extends Resource {
226+
EC2SecurityGroupEgress() { this.getType() = "AWS::EC2::SecurityGroupEgress" }
227+
228+
override string toString() { result = "CloudFormation EC2 Security Group Egress" }
229+
230+
/**
231+
* Get the security group ingress CIDR IP.
232+
*/
233+
YamlNode getCidrIp() {
234+
result = this.getProperties().getProperty("CidrIp")
235+
}
236+
237+
/**
238+
* Get the security group ingress from port.
239+
*/
240+
YamlNode getFromPort() {
241+
result = this.getProperties().getProperty("FromPort")
242+
}
243+
YamlNode getToPort() {
244+
result = this.getProperties().getProperty("ToPort")
245+
}
246+
}
247+
248+
249+
250+
class EC2SecurityGroupIngress extends Resource {
251+
EC2SecurityGroupIngress() { this.getType() = "AWS::EC2::SecurityGroupIngress" }
252+
253+
override string toString() { result = "CloudFormation EC2 Security Group Ingress" }
254+
255+
/**
256+
* Get the security group ingress CIDR IP.
257+
*/
258+
YamlNode getCidrIp() {
259+
result = this.getProperties().getProperty("CidrIp")
260+
}
261+
262+
/**
263+
* Get the security group ingress from port.
264+
*/
265+
YamlNode getFromPort() {
266+
result = this.getProperties().getProperty("FromPort")
267+
}
268+
YamlNode getToPort() {
269+
result = this.getProperties().getProperty("ToPort")
270+
}
271+
}
272+
273+
class IAMRole extends Resource {
274+
IAMRole() { this.getType() = "AWS::IAM::Role" }
275+
276+
override string toString() { result = "CloudFormation IAM Role" }
277+
278+
string getProperty(string key) { result = this.getProperties().getProperty(key).toString() }
279+
280+
/**
281+
* Get the IAM role policies.
282+
*/
283+
IAMStatement getPolicy() {
284+
result = this.getProperties().getProperty("Policies").getAChild().getAChild()
285+
286+
/*
287+
exists(YamlNode policies
288+
| policies = this.getProperties().getAChildNode()
289+
| result = policies and policies.toString() = "Statement" )
290+
*/
291+
}
292+
}
293+
class IAMStatement extends YamlNode {
294+
IAMStatement(){ this.getAChild().toString() = "Statement"}
295+
296+
YamlNode getAction() {
297+
result = this.getAChild().getAChild().(YamlMapping).lookup("Action")
298+
}
299+
YamlNode getEffect() {
300+
result = this.getAChild().getAChild().(YamlMapping).lookup("Effect")
301+
}
302+
YamlNode getResource() {
303+
result = this.getAChild().getAChild().(YamlMapping).lookup("Resource")
304+
}
305+
}
306+
307+
class ECSService extends Resource {
308+
ECSService() { this.getType() = "AWS::ECS::Service" }
309+
YamlNode getNetworkConfiguration() {
310+
result = this.getProperties().getProperty("NetworkConfiguration")
311+
}
312+
313+
/**
314+
* Get ecs service platform version
315+
*/
316+
YamlNode getPlatformVersion() {
317+
result = this.getProperties().getProperty("PlatformVersion")
318+
}
319+
320+
TaskDefinition getTaskDefinition() {
321+
result = this.getProperties().getProperty("TaskDefinition")
322+
}
323+
}
324+
325+
class ECSTaskSet extends Resource {
326+
ECSTaskSet() { this.getType() = "AWS::ECS::TaskSet" }
327+
328+
override string toString() { result = "CloudFormation ECS Task Set" }
329+
330+
/**
331+
* Get the task set network configuration.
332+
*/
333+
YamlNode getNetworkConfiguration() {
334+
result = this.getProperties().getProperty("NetworkConfiguration")
335+
}
336+
337+
YamlNode getAssignPublicIp() {
338+
result = this.getNetworkConfiguration().getAChild().(YamlMapping).lookup("AssignPublicIp")
339+
}
340+
}
341+
342+
class ECSNetworkConfiguration extends YamlNode {
343+
ECSNetworkConfiguration() { this.getAChild().toString() = "NetworkConfiguration" }
344+
345+
YamlNode getAwsvpcConfiguration() {
346+
result = this.getAChild().(YamlMapping).lookup("AwsvpcConfiguration")
347+
}
348+
YamlNode getAssignPublicIp() {
349+
result = this.getAwsvpcConfiguration().(YamlMapping).lookup("AssignPublicIp")
350+
}
351+
}
352+
353+
class TaskDefinition extends Resource {
354+
TaskDefinition() { this.getType() = "AWS::ECS::TaskDefinition" }
355+
356+
override string toString() { result = "CloudFormation ECS Task Definition" }
357+
358+
/**
359+
* Get the task definition container definitions.
360+
*/
361+
ContainerDefinition getContainerDefinitions() {
362+
result = this.getProperties().getProperty("ContainerDefinitions")
363+
}
364+
/**
365+
* Get network mode
366+
*/
367+
YamlNode getNetworkMode() {
368+
result = this.getProperties().getProperty("NetworkMode")
369+
}
370+
371+
/**
372+
* get PidMode
373+
*
374+
*/
375+
YamlNode getPidMode() {
376+
result = this.getProperties().getProperty("PidMode")
377+
}
378+
/**
379+
* get IPCMode
380+
*/
381+
YamlNode getIpcMode() {
382+
result = this.getProperties().getProperty("IpcMode")
383+
}
384+
/**
385+
* get Volumes
386+
*/
387+
YamlNode getVolumes() {
388+
result = this.getProperties().getProperty("Volumes")
389+
}
390+
/**
391+
* get PlacementConstraints
392+
*/
393+
YamlNode getPlacementConstraints() {
394+
result = this.getProperties().getProperty("PlacementConstraints")
395+
}
396+
/**
397+
* get RequiresCompatibilities
398+
*/
399+
YamlNode getRequiresCompatibilities() {
400+
result = this.getProperties().getProperty("RequiresCompatibilities")
401+
}
402+
/**
403+
* get Cpu
404+
*/
405+
YamlNode getCpu() {
406+
result = this.getProperties().getProperty("Cpu")
407+
}
408+
/**
409+
* get Memory
410+
*/
411+
YamlNode getMemory() {
412+
result = this.getProperties().getProperty("Memory")
413+
}
414+
/**
415+
* get ExecutionRoleArn
416+
*/
417+
YamlNode getExecutionRoleArn() {
418+
result = this.getProperties().getProperty("ExecutionRoleArn")
419+
}
420+
421+
/**
422+
* get logConfiguration
423+
*/
424+
YamlNode getLogConfiguration() {
425+
result = this.getProperties().getProperty("LogConfiguration")
426+
}
427+
428+
/**
429+
* get Secrets from ContainerDefinitions
430+
*/
431+
YamlNode getSecrets() {
432+
result = this.getContainerDefinitions().getAChild().(YamlMapping).lookup("Secrets")
433+
}
434+
YamlNode getRuntimePlatform() {
435+
result = this.getProperties().getProperty("RuntimePlatform").(YamlMapping).lookup("OperatingSystemFamily")
436+
}
437+
}
438+
439+
class ECSCluster extends Resource {
440+
ECSCluster() { this.getType() = "AWS::ECS::Cluster" }
441+
442+
override string toString() { result = "CloudFormation ECS Cluster" }
443+
444+
/** checks if container insights is enabled in container settings */
445+
YamlNode getContainerInsights() {
446+
result = this.getProperties().getProperty("ClusterSettings").getAChild().(YamlMapping).lookup("Value")
447+
}
448+
}
449+
450+
class ContainerDefinition extends YamlNode
451+
{
452+
ContainerDefinition() { this.getAChild().toString() = "ContainerDefinitions" }
453+
454+
YamlNode getName() {
455+
result = this.getAChild().getAChild().(YamlMapping).lookup("Name")
456+
}
457+
458+
YamlNode getNetworkConfiguration() {
459+
result = this.getAChild().getAChild().(YamlMapping).lookup("NetworkConfiguration")
460+
}
461+
YamlNode getnetworkconfigurationAwsvpcConfiguration() {
462+
result = this.getAChild().getAChild().(YamlMapping).lookup("AwsvpcConfiguration")
463+
}
464+
YamlNode getImage() {
465+
result = this.getAChild().getAChild().(YamlMapping).lookup("Image")
466+
}
467+
YamlNode getMemory() {
468+
result = this.getAChild().getAChild().(YamlMapping).lookup("Memory")
469+
}
470+
YamlNode getMemoryReservation() {
471+
result = this.getAChild().getAChild().(YamlMapping).lookup("MemoryReservation")
472+
}
473+
YamlNode getCpu() {
474+
result = this.getAChild().getAChild().(YamlMapping).lookup("Cpu")
475+
}
476+
YamlNode getEssential() {
477+
result = this.getAChild().getAChild().(YamlMapping).lookup("Essential")
478+
}
479+
YamlNode getPortMappings() {
480+
result = this.getAChild().getAChild().(YamlMapping).lookup("PortMappings")
481+
}
482+
YamlNode getVolumesFrom() {
483+
result = this.getAChild().getAChild().(YamlMapping).lookup("VolumesFrom")
484+
}
485+
YamlNode getEnvironment() {
486+
result = this.getAChild().getAChild().(YamlMapping).lookup("Environment")
487+
}
488+
YamlNode getSecrets() {
489+
result = this.getAChild().getAChild().(YamlMapping).lookup("Secrets")
490+
}
491+
YamlNode getLogConfiguration() {
492+
result = this.getAChild().getAChild().(YamlMapping).lookup("LogConfiguration")
493+
}
494+
YamlNode getHealthCheck() {
495+
result = this.getAChild().getAChild().(YamlMapping).lookup("HealthCheck")
496+
}
497+
YamlNode getEntryPoint() {
498+
result = this.getAChild().getAChild().(YamlMapping).lookup("EntryPoint")
499+
}
500+
YamlNode getCommand() {
501+
result = this.getAChild().getAChild().(YamlMapping).lookup("Command")
502+
}
503+
YamlNode getWorkingDirectory() {
504+
result = this.getAChild().getAChild().(YamlMapping).lookup("WorkingDirectory")
505+
}
506+
string getPrivileged() {
507+
result = this.getAChild().getAChild().(YamlMapping).lookup("Privileged").toString()
508+
}
509+
510+
string getReadOnlyRootFilesystem() {
511+
result = this.getAChild().getAChild().(YamlMapping).lookup("ReadOnlyRootFilesystem").toString()
512+
}
513+
YamlNode getLinuxParametersCapabilities() {
514+
result = this.getAChild().getAChild().(YamlMapping).lookup("LinuxParameters")
515+
}
516+
517+
YamlNode getUser() {
518+
result = this.getAChild().getAChild().(YamlMapping).lookup("User")
519+
}
520+
}
521+
522+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name ECS clusters should use Container Insights
3+
* @kind problem
4+
* @problem.severity warning
5+
* @id iac/ecs/container-insights
6+
* @tags security
7+
* aws/ecs/12
8+
* NIST/800-53/AU-6(3)
9+
* NIST/800-53/AU-6(4)
10+
* NIST/800-53/CA-7
11+
* NIST/800-53/SI-2
12+
*/
13+
14+
import iac
15+
16+
from CloudFormation::ECSCluster cluster
17+
where not cluster.getContainerInsights().toString() = "'enabled'"
18+
select cluster, "ECS Cluster should have cluster settings enabled"

0 commit comments

Comments
 (0)