Skip to content

Commit

Permalink
feat(terraform): AWS ensure Sagemaker Notebook users are not Root (#4676
Browse files Browse the repository at this point in the history
)

ensure Sagemaker Notebook users are not Root
  • Loading branch information
JamesWoolfenden committed Mar 23, 2023
1 parent 7eb7246 commit c8e1e13
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
26 changes: 26 additions & 0 deletions checkov/terraform/checks/resource/aws/SagemakerNotebookRoot.py
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()
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 tests/terraform/checks/resource/aws/test_SagemakerNotebookRoot.py
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()

0 comments on commit c8e1e13

Please sign in to comment.