Skip to content

Commit

Permalink
fix(lambda-runtime): Init value must be empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
jfagoagas committed Feb 6, 2023
1 parent f19cf21 commit 6e56e46
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ class awslambda_function_using_supported_runtimes(Check):
def execute(self):
findings = []
for function in awslambda_client.functions.values():
report = Check_Report_AWS(self.metadata())
report.region = function.region
report.resource_id = function.name
report.resource_arn = function.arn
if function.runtime:
report = Check_Report_AWS(self.metadata())
report.region = function.region
report.resource_id = function.name
report.resource_arn = function.arn

if function.runtime in get_config_var("obsolete_lambda_runtimes"):
report.status = "FAIL"
report.status_extended = f"Lambda function {function.name} is using {function.runtime} which is obsolete"
else:
report.status = "PASS"
report.status_extended = f"Lambda function {function.name} is using {function.runtime} which is supported"
if function.runtime in get_config_var("obsolete_lambda_runtimes"):
report.status = "FAIL"
report.status_extended = f"Lambda function {function.name} is using {function.runtime} which is obsolete"
else:
report.status = "PASS"
report.status_extended = f"Lambda function {function.name} is using {function.runtime} which is supported"

findings.append(report)
findings.append(report)

return findings
10 changes: 4 additions & 6 deletions prowler/providers/aws/services/awslambda/awslambda_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import zipfile
from enum import Enum
from typing import Any
from typing import Any, Optional

import requests
from botocore.client import ClientError
Expand Down Expand Up @@ -59,17 +59,15 @@ def __list_functions__(self, regional_client):
function["FunctionArn"], self.audit_resources
)
):
lambda_runtime = None
if "Runtime" in function:
lambda_runtime = function["Runtime"]
lambda_name = function["FunctionName"]
lambda_arn = function["FunctionArn"]
self.functions[lambda_name] = Function(
name=lambda_name,
arn=lambda_arn,
runtime=lambda_runtime,
region=regional_client.region,
)
if "Runtime" in function:
self.functions[lambda_name].runtime = function["Runtime"]
if "Environment" in function:
lambda_environment = function["Environment"]["Variables"]
self.functions[lambda_name].environment = lambda_environment
Expand Down Expand Up @@ -179,7 +177,7 @@ class URLConfig(BaseModel):
class Function(BaseModel):
name: str
arn: str
runtime: str
runtime: Optional[str]
environment: dict = None
region: str
policy: dict = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,32 @@ def test_function_supported_runtime(self):
result[0].status_extended
== f"Lambda function {function_name} is using {function_runtime} which is supported"
)

def test_function_no_runtime(self):
lambda_client = mock.MagicMock
function_name = "test-lambda"
function_arn = (
f"arn:aws:lambda:{AWS_REGION}:{DEFAULT_ACCOUNT_ID}:function/{function_name}"
)
lambda_client.functions = {
"function_name": Function(
name=function_name, arn=function_arn, region=AWS_REGION
)
}

with mock.patch(
"prowler.providers.aws.services.awslambda.awslambda_service.Lambda",
new=lambda_client,
), mock.patch(
"prowler.providers.aws.services.awslambda.awslambda_function_using_supported_runtimes.awslambda_function_using_supported_runtimes.get_config_var",
new=mock_get_config_var,
):
# Test Check
from prowler.providers.aws.services.awslambda.awslambda_function_using_supported_runtimes.awslambda_function_using_supported_runtimes import (
awslambda_function_using_supported_runtimes,
)

check = awslambda_function_using_supported_runtimes()
result = check.execute()

assert len(result) == 0

0 comments on commit 6e56e46

Please sign in to comment.