5
5
# Used by discoverDomain and prepareModel.
6
6
import re
7
7
import os
8
+ from java .io import File
8
9
9
10
from oracle .weblogic .deploy .util import FileUtils
10
11
18
19
from wlsdeploy .tool .util import k8s_helper
19
20
from wlsdeploy .tool .util import variable_injector_functions
20
21
from wlsdeploy .tool .util .targets import additional_output_helper
22
+ from wlsdeploy .tool .util .targets import file_template_helper
21
23
from wlsdeploy .util import dictionary_utils
22
24
from wlsdeploy .util .cla_utils import CommandLineArgUtil
23
25
60
62
SECURITY_NM_PATTERN = re .compile ('^SecurityConfig.NodeManager' )
61
63
SECURITY_NM_REPLACEMENT = 'SecurityConfig.NodeManager.'
62
64
65
+ K8S_SCRIPT_NAME = 'create_k8s_secrets.sh'
66
+ K8S_SCRIPT_RESOURCE_PATH = 'oracle/weblogic/deploy/k8s/' + K8S_SCRIPT_NAME
67
+
63
68
64
69
def process_target_arguments (argument_map ):
65
70
"""
@@ -86,12 +91,13 @@ def process_target_arguments(argument_map):
86
91
argument_map [CommandLineArgUtil .VARIABLE_FILE_SWITCH ] = path
87
92
88
93
89
- def generate_k8s_script (model_context , token_dictionary , model_dictionary ):
94
+ def generate_k8s_script (model_context , token_dictionary , model_dictionary , exception_type ):
90
95
"""
91
96
Generate a shell script for creating k8s secrets.
92
97
:param model_context: used to determine output directory
93
98
:param token_dictionary: contains every token
94
99
:param model_dictionary: used to determine domain UID
100
+ :param exception_type: type of exception to throw
95
101
"""
96
102
97
103
# determine the domain name and UID
@@ -101,46 +107,8 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
101
107
domain_name = DEFAULT_WLS_DOMAIN_NAME
102
108
103
109
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 }
144
112
145
113
# build a map of secret names (jdbc-generic1) to keys (username, password)
146
114
secret_map = {}
@@ -150,7 +118,7 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
150
118
if len (halves ) == 2 :
151
119
secret_name = halves [0 ]
152
120
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
154
122
if secret_name == WEBLOGIC_CREDENTIALS_SECRET_NAME :
155
123
continue
156
124
@@ -160,28 +128,35 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
160
128
secret_keys = secret_map [secret_name ]
161
129
secret_keys [secret_key ] = value
162
130
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
+
163
135
secret_names = secret_map .keys ()
164
136
secret_names .sort ()
165
-
166
137
for secret_name in secret_names :
167
138
secret_keys = secret_map [secret_name ]
168
139
user_name = dictionary_utils .get_element (secret_keys , SECRET_USERNAME_KEY )
169
-
170
140
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 ))
174
142
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 ))
178
144
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}' )
182
148
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 )
185
160
186
161
187
162
def format_as_secret_token (secret_id , target_config ):
@@ -285,3 +260,19 @@ def create_secret_name(variable_name, suffix=None):
285
260
# if empty, just return "x".
286
261
secret = '-' .join (secret_keys ).strip ('-' )
287
262
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