Skip to content

Commit 77f4fda

Browse files
authored
JIRA WDT-475 - Remove unused variables from credential cache (#776)
1 parent 4ab34a1 commit 77f4fda

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

core/src/main/python/discover.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,20 @@ def __check_and_customize_model(model, model_context, aliases, credential_inject
417417

418418
credential_cache = None
419419
if credential_injector is not None:
420+
# filter variables or secrets that are no longer in the model
421+
credential_injector.filter_unused_credentials(model.get_model())
422+
420423
credential_cache = credential_injector.get_variable_cache()
421424

422425
# Generate k8s create secret script
423426
if target_configuration.uses_credential_secrets():
424427
target_configuration_helper.generate_k8s_script(model_context, credential_cache, model.get_model())
425428

429+
# create additional output after filtering, but before variables have been inserted
430+
if model_context.is_targetted_config():
431+
target_configuration_helper.create_additional_output(model, model_context, aliases, credential_injector,
432+
ExceptionType.DISCOVER)
433+
426434
# if target handles credential configuration, clear property cache to keep out of variables file.
427435
if model_context.get_target_configuration().manages_credentials():
428436
credential_cache.clear()
@@ -510,11 +518,6 @@ def main(args):
510518
try:
511519
model = __discover(model_context, aliases, credential_injector, helper)
512520

513-
if model_context.is_targetted_config():
514-
# do this before variables have been inserted into model
515-
target_configuration_helper.create_additional_output(model, model_context, aliases, credential_injector,
516-
ExceptionType.DISCOVER)
517-
518521
model = __check_and_customize_model(model, model_context, aliases, credential_injector)
519522

520523
except DiscoverException, ex:

core/src/main/python/prepare_model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ def walk(self):
239239

240240
model_file_name = None
241241

242+
# create a merged model that is not substituted
243+
merged_model_dictionary = {}
244+
242245
try:
243246
model_file_list = self.model_files.split(',')
244247
for model_file in model_file_list:
@@ -285,6 +288,12 @@ def walk(self):
285288
pty._write_dictionary_to_yaml_file(self.current_dict, writer)
286289
writer.close()
287290

291+
cla_helper.merge_model_dictionaries(merged_model_dictionary, self.current_dict, None)
292+
293+
# filter variables or secrets that are no longer in the merged, filtered model
294+
filter_helper.apply_filters(merged_model_dictionary, "discover", self.model_context)
295+
self.credential_injector.filter_unused_credentials(merged_model_dictionary)
296+
288297
# use a merged, substituted, filtered model to get domain name and create additional target output.
289298
full_model_dictionary = cla_helper.load_model(_program_name, self.model_context, self._aliases,
290299
"discover", WlstModes.OFFLINE)

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from wlsdeploy.util.target_configuration import SECRETS_METHOD
2828
from wlsdeploy.util.target_configuration_helper import SECRET_PASSWORD_KEY
2929
from wlsdeploy.util.target_configuration_helper import SECRET_USERNAME_KEY
30+
from wlsdeploy.util.target_configuration_helper import WEBLOGIC_CREDENTIALS_SECRET_NAME
3031

3132
_class_name = 'CredentialInjector'
3233
_logger = PlatformLogger('wlsdeploy.tool.util')
@@ -60,6 +61,13 @@ class CredentialInjector(VariableInjector):
6061
]
6162
}
6263

64+
# keys that should not be filtered from cache, even if they are not in the model.
65+
# the model may reference admin credentials indirectly if the target type uses wls_credentials_name.
66+
NO_FILTER_KEYS = [
67+
WEBLOGIC_CREDENTIALS_SECRET_NAME + ":" + SECRET_PASSWORD_KEY,
68+
WEBLOGIC_CREDENTIALS_SECRET_NAME + ":" + SECRET_USERNAME_KEY
69+
]
70+
6371
def __init__(self, program_name, model, model_context, version=None, variable_dictionary=None):
6472
"""
6573
Construct an instance of the credential injector.
@@ -178,3 +186,50 @@ def get_variable_token(self, attribute, variable_name):
178186
return target_configuration_helper.format_as_overrides_secret(variable_name)
179187
else:
180188
return VariableInjector.get_variable_token(self, attribute, variable_name)
189+
190+
def filter_unused_credentials(self, model_dictionary):
191+
"""
192+
Remove credentials from the cache that are no longer present in the model.
193+
Check for variables or secrets, depending on target configuration.
194+
:param model_dictionary: the model to be checked
195+
"""
196+
_method_name = 'filter_unused_credentials'
197+
198+
target_config = self._model_context.get_target_configuration()
199+
credentials_method = target_config.get_credentials_method()
200+
201+
if credentials_method == CONFIG_OVERRIDES_SECRETS_METHOD:
202+
_logger.info("WLSDPLY-19650", credentials_method, class_name=_class_name, method_name=_method_name)
203+
return
204+
205+
all_variables = []
206+
self._add_model_variables(model_dictionary, all_variables)
207+
208+
cache_keys = self.get_variable_cache().keys()
209+
for key in cache_keys:
210+
if key in self.NO_FILTER_KEYS:
211+
continue
212+
213+
if credentials_method == SECRETS_METHOD:
214+
variable_name = '@@SECRET:@@ENV:DOMAIN_UID@@-%s@@' % key
215+
else:
216+
variable_name = '@@PROP:%s@@' % key
217+
218+
if variable_name not in all_variables:
219+
_logger.info("WLSDPLY-19651", variable_name, class_name=_class_name, method_name=_method_name)
220+
del self.get_variable_cache()[key]
221+
222+
def _add_model_variables(self, model_dictionary, variables):
223+
"""
224+
Add any variable values found in the model dictionary to the variables list
225+
:param model_dictionary: the dictionary to be examined
226+
:param variables: the list to be appended
227+
"""
228+
for key in model_dictionary:
229+
value = model_dictionary[key]
230+
if isinstance(value, dict):
231+
self._add_model_variables(value, variables)
232+
else:
233+
text = str(value)
234+
if text.startswith('@@'):
235+
variables.append(text)

core/src/main/python/wlsdeploy/util/cla_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from wlsdeploy.util import cla_utils
2626
from wlsdeploy.util import getcreds
2727
from wlsdeploy.util import model_helper
28-
from wlsdeploy.util import model_translator, path_utils
28+
from wlsdeploy.util import model_translator
2929
from wlsdeploy.util import path_utils
3030

3131
from wlsdeploy.util import tool_exit
@@ -314,12 +314,12 @@ def merge_model_files(model_file_value, variable_map=None):
314314

315315
for model_file in model_files:
316316
model = FileToPython(model_file, True).parse()
317-
_merge_dictionaries(merged_model, model, variable_map)
317+
merge_model_dictionaries(merged_model, model, variable_map)
318318

319319
return merged_model
320320

321321

322-
def _merge_dictionaries(dictionary, new_dictionary, variable_map):
322+
def merge_model_dictionaries(dictionary, new_dictionary, variable_map):
323323
"""
324324
Merge the values from the new dictionary to the existing one.
325325
Use variables to resolve keys.
@@ -345,7 +345,7 @@ def _merge_dictionaries(dictionary, new_dictionary, variable_map):
345345
else:
346346
value = dictionary[dictionary_key]
347347
if isinstance(value, dict) and isinstance(new_value, dict):
348-
_merge_dictionaries(value, new_value, variable_map)
348+
merge_model_dictionaries(value, new_value, variable_map)
349349
else:
350350
dictionary[new_key] = new_value
351351

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,10 @@ WLSDPLY-19602=Use variable properties file {0} from command line arguments
15181518
WLSDPLY-19603=Archive file {0} was provided but does not contain a model file
15191519
WLSDPLY-19604=Update model with injected variables
15201520

1521+
# wlsdeploy/tool/util/credential_injector.py
1522+
WLSDPLY-19650=Unused credential variables will not be filtered for target credentials method {0}
1523+
WLSDPLY-19651=Removing unused credential variable {0}
1524+
15211525
# wlsdeploy/tool/util/odl_deployer.py
15221526
WLSDPLY-19700=ODL configuration in online mode is not supported, skipping
15231527
WLSDPLY-19701=Unable to add handler {0} without specifying its class at location {1}

core/src/test/python/wlsdeploy/util/cla_helper_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def testMergeModels(self):
3838
}
3939

4040
variables = {}
41-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
41+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
4242
# print("Merged model: " + str(dictionary))
4343

4444
servers = dictionary['Servers']
@@ -59,7 +59,7 @@ def testMergeMatchingProperties(self):
5959
variables = {}
6060

6161
# no variables are needed to resolve this
62-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
62+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
6363
# print("Merged model: " + str(dictionary))
6464

6565
servers = dictionary['Servers']
@@ -73,7 +73,7 @@ def testMergeDifferentProperties(self):
7373
new_dictionary = _build_model_two('@@PROP:server1b@@')
7474
variables = _build_variable_map()
7575

76-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
76+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
7777
# print("Merged model: " + str(dictionary))
7878

7979
self._check_merged_server(dictionary, '@@PROP:server1a@@')
@@ -84,7 +84,7 @@ def testMergePropertyToName(self):
8484
new_dictionary = _build_model_two('@@PROP:server1b@@')
8585
variables = _build_variable_map()
8686

87-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
87+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
8888
# print("Merged model: " + str(dictionary))
8989

9090
self._check_merged_server(dictionary, 'm1')
@@ -95,7 +95,7 @@ def testMergeNameToProperty(self):
9595
new_dictionary = _build_model_two('m1')
9696
variables = _build_variable_map()
9797

98-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
98+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
9999
# print("Merged model: " + str(dictionary))
100100

101101
self._check_merged_server(dictionary, '@@PROP:server1a@@')
@@ -106,7 +106,7 @@ def testMergeDeletedNameToName(self):
106106
new_dictionary = _build_delete_model('m1')
107107
variables = {}
108108

109-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
109+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
110110
# print("Merged model: " + str(dictionary))
111111

112112
servers = dictionary['Servers']
@@ -118,7 +118,7 @@ def testMergeDeletedNameToDeleteName(self):
118118
new_dictionary = _build_delete_model('m1')
119119
variables = {}
120120

121-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
121+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
122122
# print("Merged model: " + str(dictionary))
123123

124124
server = self._check_single_server(dictionary, '!m1')
@@ -130,7 +130,7 @@ def testMergeNameToDeletedName(self):
130130
new_dictionary = _build_model_two('m1')
131131
variables = {}
132132

133-
cla_helper._merge_dictionaries(dictionary, new_dictionary, variables)
133+
cla_helper.merge_model_dictionaries(dictionary, new_dictionary, variables)
134134
# print("Merged model: " + str(dictionary))
135135

136136
server = self._check_single_server(dictionary, 'm1')

0 commit comments

Comments
 (0)