Skip to content

Commit 61fcc84

Browse files
authored
Merge pull request #41 from neong83/fix/default_api_key_on_lambda_metric
check api_key had been set before setting the value from env
2 parents d7e0795 + 3585f1a commit 61fcc84

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

datadog_lambda/metric.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,20 @@ def submit_errors_metric(lambda_context):
108108

109109

110110
# Set API Key and Host in the module, so they only set once per container
111-
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
112-
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
113-
DD_API_KEY = os.environ.get("DD_API_KEY", os.environ.get("DATADOG_API_KEY", ""))
114-
if DD_API_KEY_SECRET_ARN:
115-
api._api_key = boto3.client("secretsmanager").get_secret_value(
116-
SecretId=DD_API_KEY_SECRET_ARN
117-
)["SecretString"]
118-
elif DD_KMS_API_KEY:
119-
api._api_key = boto3.client("kms").decrypt(
120-
CiphertextBlob=base64.b64decode(DD_KMS_API_KEY)
121-
)["Plaintext"]
122-
else:
123-
api._api_key = DD_API_KEY
111+
if not api._api_key:
112+
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
113+
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
114+
DD_API_KEY = os.environ.get("DD_API_KEY", os.environ.get("DATADOG_API_KEY", ""))
115+
if DD_API_KEY_SECRET_ARN:
116+
api._api_key = boto3.client("secretsmanager").get_secret_value(
117+
SecretId=DD_API_KEY_SECRET_ARN
118+
)["SecretString"]
119+
elif DD_KMS_API_KEY:
120+
api._api_key = boto3.client("kms").decrypt(
121+
CiphertextBlob=base64.b64decode(DD_KMS_API_KEY)
122+
)["Plaintext"]
123+
else:
124+
api._api_key = DD_API_KEY
124125
logger.debug("Setting DATADOG_API_KEY of length %d", len(api._api_key))
125126

126127
# Set DATADOG_HOST, to send data to a non-default Datadog datacenter

0 commit comments

Comments
 (0)