Skip to content

Commit 2eb9e71

Browse files
authored
Tokenize credential attributes even if it's value is already a property (#952)
* change method input parameters instead of using class attribute to store variable keys for removal * rename method * add check for none
1 parent dde23db commit 2eb9e71

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

core/src/main/python/prepare_model.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def walk(self):
313313
full_model_dictionary = cla_helper.load_model(_program_name, self.model_context, self._aliases,
314314
"discover", WlstModes.OFFLINE)
315315

316+
316317
target_config = self.model_context.get_target_configuration()
317318
if target_config.generate_script_for_secrets():
318319
target_configuration_helper.generate_k8s_script(self.model_context,
@@ -374,8 +375,10 @@ def _apply_filter_and_inject_variable(self, model, model_context):
374375
credential_properties)
375376

376377
# update the variable file with any new values
378+
unused_variable_keys_to_remove = self.credential_injector.get_variable_keys_for_removal();
377379
inserted, variable_model, variable_file_name = \
378-
variable_injector.inject_variables_keyword_file(VARIABLE_FILE_UPDATE)
380+
variable_injector.inject_variables_keyword_file(append_option=VARIABLE_FILE_UPDATE,
381+
variable_keys_to_remove=unused_variable_keys_to_remove)
379382

380383
# return variable_model - if writing the variables file failed, this will be the original model.
381384
# a warning is issued in inject_variables_keyword_file() if that was the case.

core/src/main/python/wlsdeploy/tool/util/credential_injector.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ def get_variable_name(self, attribute_location, attribute, suffix=None):
170170

171171
return VariableInjector.get_variable_name(self, model_location, attribute, suffix=suffix)
172172

173+
def get_variable_keys_for_removal(self):
174+
return VariableInjector.get_variable_removal_keys(self)
175+
173176
def get_variable_token(self, attribute, variable_name):
174177
"""
175178
Override method to possibly create secret tokens instead of property token.

core/src/main/python/wlsdeploy/tool/util/variable_injector.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from wlsdeploy.json.json_translator import JsonToPython
2727
from wlsdeploy.logging.platform_logger import PlatformLogger
2828
from wlsdeploy.util import path_utils
29+
from wlsdeploy.aliases.alias_constants import CREDENTIAL
2930

3031
WEBLOGIC_DEPLOY_HOME_TOKEN = '@@WLSDEPLOY@@'
3132

@@ -103,6 +104,13 @@ def __init__(self, program_name, model, model_context, version=None, variable_di
103104
else:
104105
self.__aliases = Aliases(model_context)
105106
self.__variable_dictionary = variable_dictionary
107+
self.__keys_for_variable_removal = []
108+
109+
def get_variable_removal_keys(self):
110+
return self.__keys_for_variable_removal
111+
112+
def add_key_for_variable_removal(self, key):
113+
self.__keys_for_variable_removal.append(key)
106114

107115
def get_variable_cache(self):
108116
"""
@@ -174,7 +182,7 @@ def custom_injection(self, model, attribute, location, injector_values=OrderedDi
174182
variable_dictionary = self._add_variable_info(model, attribute, location, injector_values)
175183
self.add_to_cache(dictionary=variable_dictionary)
176184

177-
def inject_variables_keyword_file(self, append_option=None):
185+
def inject_variables_keyword_file(self, append_option=None, variable_keys_to_remove=None):
178186
"""
179187
Replace attribute values with variables and generate a variable dictionary.
180188
The variable replacement is driven from the values in the model variable helper file.
@@ -255,8 +263,8 @@ def inject_variables_keyword_file(self, append_option=None):
255263
append = True
256264
if variable_file_location != new_variable_file_location:
257265
shutil.copyfile(variable_file_location, new_variable_file_location)
258-
self._filter_duplicate_properties(new_variable_file_location, variable_dictionary)
259-
266+
self._filter_duplicate_and_unused_properties(new_variable_file_location, variable_dictionary,
267+
variable_keys_to_remove)
260268
variable_file_location = new_variable_file_location
261269

262270
variables_inserted = self._write_variables_file(variable_dictionary, variable_file_location, append)
@@ -272,17 +280,28 @@ def inject_variables_keyword_file(self, append_option=None):
272280
return variables_inserted, return_model, variable_file_location
273281

274282

275-
def _filter_duplicate_properties(self, variable_file_location, variable_dictionary):
283+
def _filter_duplicate_and_unused_properties(self, variable_file_location, variable_dictionary, variable_keys_to_remove):
276284
_method_name = '_filter_duplicate_property'
277285
_logger.entering(class_name=_class_name, method_name=_method_name)
278286
try:
279287
fis = FileInputStream(variable_file_location)
280288
prop = Properties()
281289
prop.load(fis)
282290
fis.close()
291+
292+
# remove from the original properties file and then remove from the variable dictionary
293+
# so that it won't be added back later
294+
if variable_keys_to_remove is not None:
295+
for key in variable_keys_to_remove:
296+
if variable_dictionary.has_key(key):
297+
variable_dictionary.pop(key)
298+
if prop.containsKey(key):
299+
prop.remove(key)
300+
283301
for key in variable_dictionary:
284302
if prop.get(key) is not None:
285303
prop.remove(key)
304+
286305
fos = FileOutputStream(variable_file_location)
287306
prop.store(fos, None)
288307
fos.close()
@@ -426,7 +445,14 @@ def _process_attribute(self, model, attribute, location, injector_values):
426445
variable_name = None
427446
variable_value = None
428447
attribute_value = model[attribute]
429-
if not _already_property(attribute_value):
448+
449+
attribute_type = self.__aliases.get_model_attribute_type(location, attribute)
450+
451+
if _already_property(attribute_value) and attribute_type == CREDENTIAL:
452+
self.add_key_for_variable_removal(attribute_value[7:len(attribute_value) - 2])
453+
454+
if not _already_property(attribute_value) or attribute_type == CREDENTIAL:
455+
430456
variable_name = self.get_variable_name(location, attribute)
431457
variable_value = _format_variable_value(attribute_value)
432458

0 commit comments

Comments
 (0)