2424from ldap3 import Connection , SASL , KERBEROS
2525from ldap3 .core .rdns import ReverseDnsSetting
2626import dns .resolver
27+ import base64
2728
2829"""
2930Constants
3334CONF_FILE_NAME = "/etc/krb5.conf"
3435SECRET_ARN = "secret_arn"
3536DIRECTORY_NAME = "directory_name"
37+ KRB5_CONF = "krb5.conf"
3638REGION_NAME = "region_name"
3739SERVICE_PRINCIPAL_NAME = "service_principal_name"
3840KRB_TICKET_REFRESH_PERIOD = "krb_ticket_refresh_period"
@@ -90,14 +92,15 @@ def get_secret(region_name_arg, secret_arn_arg):
9092 except KeyError as _ :
9193 print ("ERROR* Secret doesn't contain password" , flush = True )
9294 domain = secret_dict .get (DIRECTORY_NAME )
95+ krb5_conf = secret_dict .get (KRB5_CONF )
9396 # Missing values are handled in the caller
94- return username , password , domain
97+ return username , password , domain , krb5_conf
9598 except ClientError as e :
9699 if e .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
97100 print ("The requested secret " + secret_arn_arg + " was not found" ,
98101 flush = True )
99102 # Retry this because the secret can be created later
100- return None , None , None
103+ return None , None , None , None
101104 elif e .response ['Error' ]['Code' ] == 'InvalidRequestException' :
102105 print ("The request was invalid due to:" , e , flush = True )
103106 raise # there is no point to retry because there is nothing that can change
@@ -113,12 +116,12 @@ def get_secret(region_name_arg, secret_arn_arg):
113116 elif e .response ['Error' ]['Code' ] == 'InternalServiceError' :
114117 print ("An error occurred on service side:" , e , flush = True )
115118 # Retry this, the service can fix itself
116- return None , None , None
119+ return None , None , None , None
117120 elif e .response ['Error' ]['Code' ] == 'AccessDeniedException' :
118121 print (f"Access denied when reading secret { secret_arn_arg } . Check your container execution role:" ,
119122 e , flush = True )
120123 # Retry this, they can fix the role without restarting
121- return None , None , None
124+ return None , None , None , None
122125 # All other exceptions will be caught in the caller
123126 raise
124127
@@ -418,9 +421,14 @@ def main():
418421 try :
419422 # get_secret returns None for username and/or password in cases where retry makes sense, like
420423 # secret not found, and returns None for username and password
421- username_new , password_new , domain_new = get_secret (env_vars [REGION_NAME ], env_vars [SECRET_ARN ])
424+ username_new , password_new , domain_new , krb5_conf = get_secret (env_vars [REGION_NAME ], env_vars [SECRET_ARN ])
422425 print (f"Got username { username_new } password { password_new } and domain name { domain_new } from secret" )
423426
427+ # Write krb5.conf if provided via secret
428+ if krb5_conf :
429+ with open (CONF_FILE_NAME , "w" ) as f :
430+ f .write (base64 .b64decode (krb5_conf )
431+
424432 if username_new is not None and password_new is not None :
425433 if domain_new is not None :
426434 env_vars [DIRECTORY_NAME ] = domain_new
0 commit comments