-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(terraform): AWS ensure Sagemaker Notebook users are not Root (#4676
- Loading branch information
1 parent
7eb7246
commit c8e1e13
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
checkov/terraform/checks/resource/aws/SagemakerNotebookRoot.py
This file contains 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,26 @@ | ||
|
||
from checkov.common.models.enums import CheckCategories, CheckResult | ||
from checkov.terraform.checks.resource.base_resource_value_check import BaseResourceValueCheck | ||
|
||
|
||
class SagemakerNotebookRoot(BaseResourceValueCheck): | ||
def __init__(self): | ||
""" | ||
NIST.800-53.r5 AC-2(1), NIST.800-53.r5 AC-3(15), NIST.800-53.r5 AC-3(7), NIST.800-53.r5 AC-6, NIST.800-53.r5 | ||
AC-6(10), NIST.800-53.r5 AC-6(2) | ||
""" | ||
name = "Ensure SageMaker Users should not have root access to SageMaker notebook instances" | ||
id = "CKV_AWS_307" | ||
supported_resources = ['aws_sagemaker_notebook_instance'] | ||
categories = [CheckCategories.GENERAL_SECURITY] | ||
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources, | ||
missing_block_result=CheckResult.FAILED) | ||
|
||
def get_inspected_key(self): | ||
return 'root_access' | ||
|
||
def get_expected_value(self): | ||
return "Disabled" | ||
|
||
|
||
check = SagemakerNotebookRoot() |
32 changes: 32 additions & 0 deletions
32
tests/terraform/checks/resource/aws/example_SagemakerNotebookRoot/main.tf
This file contains 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,32 @@ | ||
resource "aws_sagemaker_notebook_instance" "fail" { | ||
name = "my-notebook-instance" | ||
role_arn = aws_iam_role.role.arn | ||
instance_type = "ml.t2.medium" | ||
default_code_repository = aws_sagemaker_code_repository.example.code_repository_name | ||
root_access = "Enabled" | ||
tags = { | ||
Name = "foo" | ||
} | ||
} | ||
|
||
resource "aws_sagemaker_notebook_instance" "fail2" { | ||
name = "my-notebook-instance" | ||
role_arn = aws_iam_role.role.arn | ||
instance_type = "ml.t2.medium" | ||
default_code_repository = aws_sagemaker_code_repository.example.code_repository_name | ||
tags = { | ||
Name = "foo" | ||
} | ||
} | ||
|
||
resource "aws_sagemaker_notebook_instance" "pass" { | ||
name = "my-notebook-instance" | ||
role_arn = aws_iam_role.role.arn | ||
instance_type = "ml.t2.medium" | ||
subnet_id = aws_subnet.pike.id | ||
default_code_repository = aws_sagemaker_code_repository.example.code_repository_name | ||
root_access = "Disabled" | ||
tags = { | ||
Name = "foo" | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/terraform/checks/resource/aws/test_SagemakerNotebookRoot.py
This file contains 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,39 @@ | ||
import os | ||
import unittest | ||
|
||
from checkov.runner_filter import RunnerFilter | ||
from checkov.terraform.checks.resource.aws.SagemakerNotebookRoot import check | ||
from checkov.terraform.runner import Runner | ||
|
||
|
||
class TestSagemakerNotebookRoot(unittest.TestCase): | ||
def test(self): | ||
runner = Runner() | ||
current_dir = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
test_files_dir = current_dir + "/example_SagemakerNotebookRoot" | ||
report = runner.run(root_folder=test_files_dir, runner_filter=RunnerFilter(checks=[check.id])) | ||
summary = report.get_summary() | ||
|
||
passing_resources = { | ||
"aws_sagemaker_notebook_instance.pass", | ||
} | ||
failing_resources = { | ||
"aws_sagemaker_notebook_instance.fail", | ||
"aws_sagemaker_notebook_instance.fail2", | ||
} | ||
|
||
passed_check_resources = set([c.resource for c in report.passed_checks]) | ||
failed_check_resources = set([c.resource for c in report.failed_checks]) | ||
|
||
self.assertEqual(summary["passed"], len(passing_resources)) | ||
self.assertEqual(summary["failed"], len(failing_resources)) | ||
self.assertEqual(summary["skipped"], 0) | ||
self.assertEqual(summary["parsing_errors"], 0) | ||
|
||
self.assertEqual(passing_resources, passed_check_resources) | ||
self.assertEqual(failing_resources, failed_check_resources) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |