55# Used by discoverDomain and prepareModel.
66import re
77import os
8+ from java .io import File
89
910from oracle .weblogic .deploy .util import FileUtils
1011
1819from wlsdeploy .tool .util import k8s_helper
1920from wlsdeploy .tool .util import variable_injector_functions
2021from wlsdeploy .tool .util .targets import additional_output_helper
22+ from wlsdeploy .tool .util .targets import file_template_helper
2123from wlsdeploy .util import dictionary_utils
2224from wlsdeploy .util .cla_utils import CommandLineArgUtil
2325
6062SECURITY_NM_PATTERN = re .compile ('^SecurityConfig.NodeManager' )
6163SECURITY_NM_REPLACEMENT = 'SecurityConfig.NodeManager.'
6264
65+ K8S_SCRIPT_NAME = 'create_k8s_secrets.sh'
66+ K8S_SCRIPT_RESOURCE_PATH = 'oracle/weblogic/deploy/k8s/' + K8S_SCRIPT_NAME
67+
6368
6469def process_target_arguments (argument_map ):
6570 """
@@ -86,12 +91,13 @@ def process_target_arguments(argument_map):
8691 argument_map [CommandLineArgUtil .VARIABLE_FILE_SWITCH ] = path
8792
8893
89- def generate_k8s_script (model_context , token_dictionary , model_dictionary ):
94+ def generate_k8s_script (model_context , token_dictionary , model_dictionary , exception_type ):
9095 """
9196 Generate a shell script for creating k8s secrets.
9297 :param model_context: used to determine output directory
9398 :param token_dictionary: contains every token
9499 :param model_dictionary: used to determine domain UID
100+ :param exception_type: type of exception to throw
95101 """
96102
97103 # determine the domain name and UID
@@ -101,46 +107,8 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
101107 domain_name = DEFAULT_WLS_DOMAIN_NAME
102108
103109 domain_uid = k8s_helper .get_domain_uid (domain_name )
104-
105- nl = '\n '
106- file_location = model_context .get_output_dir ()
107- k8s_file = os .path .join (file_location , "create_k8s_secrets.sh" )
108- k8s_script = open (k8s_file , 'w' )
109-
110- k8s_script .write ('#!/bin/bash' + nl )
111-
112- k8s_script .write (nl )
113- k8s_script .write ('set -eu' + nl )
114-
115- k8s_script .write (nl )
116- message = exception_helper .get_message ("WLSDPLY-01665" , ADMIN_USER_TAG , ADMIN_PASSWORD_TAG )
117- k8s_script .write ("# " + message + nl )
118- k8s_script .write ('NAMESPACE=default' + nl )
119- k8s_script .write ('DOMAIN_UID=' + domain_uid + nl )
120-
121- k8s_script .write (nl )
122- k8s_script .write ('function create_k8s_secret {' + nl )
123- k8s_script .write (' kubectl -n $NAMESPACE delete secret ${DOMAIN_UID}-$1 --ignore-not-found' + nl )
124- k8s_script .write (' kubectl -n $NAMESPACE create secret generic ${DOMAIN_UID}-$1 --from-literal=password=$2' + nl )
125- k8s_script .write (' kubectl -n $NAMESPACE label secret ${DOMAIN_UID}-$1 weblogic.domainUID=${DOMAIN_UID}' + nl )
126- k8s_script .write ('}' + nl )
127-
128- k8s_script .write (nl )
129- k8s_script .write ('function create_paired_k8s_secret {' + nl )
130- k8s_script .write (' kubectl -n $NAMESPACE delete secret ${DOMAIN_UID}-$1 --ignore-not-found' + nl )
131- k8s_script .write (' kubectl -n $NAMESPACE create secret generic ${DOMAIN_UID}-$1' +
132- ' --from-literal=username=$2 --from-literal=password=$3' + nl )
133- k8s_script .write (' kubectl -n $NAMESPACE label secret ${DOMAIN_UID}-$1 weblogic.domainUID=${DOMAIN_UID}' + nl )
134- k8s_script .write ('}' + nl )
135-
136- command_string = "create_paired_k8s_secret %s %s %s" \
137- % (WEBLOGIC_CREDENTIALS_SECRET_NAME , ADMIN_USER_TAG , ADMIN_PASSWORD_TAG )
138-
139- k8s_script .write (nl )
140- message = exception_helper .get_message ("WLSDPLY-01664" , ADMIN_USER_TAG , ADMIN_PASSWORD_TAG ,
141- WEBLOGIC_CREDENTIALS_SECRET_NAME )
142- k8s_script .write ("# " + message + nl )
143- k8s_script .write (command_string + nl )
110+ comment = exception_helper .get_message ("WLSDPLY-01665" )
111+ script_hash = {'domainUid' : domain_uid , 'topComment' : comment }
144112
145113 # build a map of secret names (jdbc-generic1) to keys (username, password)
146114 secret_map = {}
@@ -150,7 +118,7 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
150118 if len (halves ) == 2 :
151119 secret_name = halves [0 ]
152120
153- # admin credentials are hard-coded in the script, to be first in the list
121+ # admin credentials are inserted later, at the top of the list
154122 if secret_name == WEBLOGIC_CREDENTIALS_SECRET_NAME :
155123 continue
156124
@@ -160,28 +128,35 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
160128 secret_keys = secret_map [secret_name ]
161129 secret_keys [secret_key ] = value
162130
131+ # update the hash with secrets and paired secrets
132+ secrets = []
133+ paired_secrets = [_build_secret_hash (WEBLOGIC_CREDENTIALS_SECRET_NAME , USER_TAG , PASSWORD_TAG )]
134+
163135 secret_names = secret_map .keys ()
164136 secret_names .sort ()
165-
166137 for secret_name in secret_names :
167138 secret_keys = secret_map [secret_name ]
168139 user_name = dictionary_utils .get_element (secret_keys , SECRET_USERNAME_KEY )
169-
170140 if user_name is None :
171- message = exception_helper .get_message ("WLSDPLY-01663" , PASSWORD_TAG , secret_name )
172- command_string = "create_k8s_secret %s %s " \
173- % (secret_name , PASSWORD_TAG )
141+ secrets .append (_build_secret_hash (secret_name , None , PASSWORD_TAG ))
174142 else :
175- message = exception_helper .get_message ("WLSDPLY-01664" , USER_TAG , PASSWORD_TAG , secret_name )
176- command_string = "create_paired_k8s_secret %s %s %s " \
177- % (secret_name , user_name , PASSWORD_TAG )
143+ paired_secrets .append (_build_secret_hash (secret_name , user_name , PASSWORD_TAG ))
178144
179- k8s_script . write ( nl )
180- k8s_script . write ( "# " + message + nl )
181- k8s_script . write ( command_string + nl )
145+ script_hash [ 'secrets' ] = secrets
146+ script_hash [ 'pairedSecrets' ] = paired_secrets
147+ script_hash [ 'longMessage' ] = exception_helper . get_message ( 'WLSDPLY-01667' , '${LONG_SECRETS_COUNT}' )
182148
183- k8s_script .close ()
184- FileUtils .chmod (k8s_file , 0750 )
149+ long_messages = [
150+ {'text' : exception_helper .get_message ('WLSDPLY-01668' )},
151+ {'text' : exception_helper .get_message ('WLSDPLY-01669' )},
152+ {'text' : exception_helper .get_message ('WLSDPLY-01670' )}
153+ ]
154+ script_hash ['longMessageDetails' ] = long_messages
155+
156+ file_location = model_context .get_output_dir ()
157+ k8s_file = File (file_location , K8S_SCRIPT_NAME )
158+ file_template_helper .create_file_from_resource (K8S_SCRIPT_RESOURCE_PATH , script_hash , k8s_file , exception_type )
159+ FileUtils .chmod (k8s_file .getPath (), 0750 )
185160
186161
187162def format_as_secret_token (secret_id , target_config ):
@@ -285,3 +260,19 @@ def create_secret_name(variable_name, suffix=None):
285260 # if empty, just return "x".
286261 secret = '-' .join (secret_keys ).strip ('-' )
287262 return secret or 'x'
263+
264+
265+ def _build_secret_hash (secret_name , user , password ):
266+ """
267+ Build a hash for a single secret, for use with the create secrets script template.
268+ :param secret_name: the name of the secret
269+ :param user: the associated user name, or None
270+ :param password: the associated password
271+ :return: a secret hash
272+ """
273+ if user :
274+ message = exception_helper .get_message ("WLSDPLY-01664" , USER_TAG , PASSWORD_TAG , secret_name )
275+ return {'secretName' : secret_name , 'user' : user , 'password' : password , 'comment' : message }
276+ else :
277+ message = exception_helper .get_message ("WLSDPLY-01663" , PASSWORD_TAG , secret_name )
278+ return {'secretName' : secret_name , 'password' : password , 'comment' : message }
0 commit comments