Skip to content

Commit 310185f

Browse files
committed
Fixed a bug where a secret is created without a value and the rotation function is assigned, causing it to be call without a AWSCURRENT secret value.
1 parent 4bf25ea commit 310185f

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Management-Utilities/fsxn-rotate-secret/fsxn_rotate_secret.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import boto3
1010
import logging
11+
import os
1112

1213
charactersToExcludeInPassword = '/"\'\\'
1314

@@ -30,7 +31,10 @@ def create_secret(secretsClient, arn, token):
3031
global logger
3132
#
3233
# Make sure the current secret exists
33-
secretsClient.get_secret_value(SecretId=arn, VersionStage="AWSCURRENT")
34+
#
35+
# *NOTE:* The next line is commented out since it breaks if a secret is created
36+
# without a value.
37+
# secretsClient.get_secret_value(SecretId=arn, VersionStage="AWSCURRENT")
3438
#
3539
# Now try to get the secret version, if that fails, put a new secret
3640
try:
@@ -128,8 +132,12 @@ def lambda_handler(event, context):
128132
token = event['ClientRequestToken']
129133
step = event['Step']
130134

131-
logger = logging.getLogger()
132-
logger.setLevel(logging.INFO)
135+
logger = logging.getLogger('fsxn_rotate_secret')
136+
loggingLevel = os.environ.get("loggingLevel")
137+
if loggingLevel is not None:
138+
logger.setLevel(loggingLevel)
139+
else:
140+
logger.setLevel(logging.WARNING)
133141
#
134142
# Set the logging level higher for these noisy modules to mute thier messages.
135143
logging.getLogger("boto3").setLevel(logging.WARNING)
@@ -157,13 +165,15 @@ def lambda_handler(event, context):
157165
#
158166
# Now check that the version hasn't already been promoted to AWSCURRENT and if not
159167
# that a AWSPENDING staging exist.
160-
if "AWSCURRENT" in versions[token]:
161-
logger.info(f"Secret version {token} already set as AWSCURRENT for secret {arn}.")
162-
return
163-
elif "AWSPENDING" not in versions[token]:
164-
message = f"Secret version {token} not set as AWSPENDING for rotation of secret {arn}."
165-
logger.error(message)
166-
raise Exception(message)
168+
# *NOTE:* The following is commented out since it breaks if the secret was created
169+
# without a value and this Lambda function is called before a value is set.
170+
#if "AWSCURRENT" in versions[token]:
171+
# logger.info(f"Secret version {token} already set as AWSCURRENT for secret {arn}.")
172+
# return
173+
#elif "AWSPENDING" not in versions[token]:
174+
# message = f"Secret version {token} not set as AWSPENDING for rotation of secret {arn}."
175+
# logger.error(message)
176+
# raise Exception(message)
167177
#
168178
# At this point we are ready to process the request.
169179
if step == "createSecret":

0 commit comments

Comments
 (0)