Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor code
  • Loading branch information
Johnny Shum committed May 18, 2020
commit fc21ef1b9b3aa6ab3115bb468da80670795502dc
2 changes: 1 addition & 1 deletion core/src/main/python/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __process_args(args):
cla_helper.verify_required_args_present(_program_name, __required_arguments, required_arg_map)
__wlst_mode = cla_helper.process_online_args(optional_arg_map)

__process_target_arg(optional_arg_map, required_arg_map)
__process_target_arg(optional_arg_map)
__process_archive_filename_arg(required_arg_map)
__process_variable_filename_arg(optional_arg_map)
__process_java_home(optional_arg_map)
Expand Down
62 changes: 54 additions & 8 deletions core/src/main/python/prepare_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import java.io.FileOutputStream as JFileOutputStream
import java.io.PrintWriter as JPrintWriter
import os
import os, re
import sets
import sys
import traceback
Expand Down Expand Up @@ -115,8 +115,8 @@ def __init__(self, model_files, model_context, logger, output_dir=None):
self.model_files = model_files
self.output_dir = output_dir
self.model_context = model_context
aliases = Aliases(model_context=model_context, wlst_mode=WlstModes.OFFLINE)
self._alias_helper = AliasHelper(aliases, logger, ExceptionType.COMPARE)
self._aliases = Aliases(model_context=model_context, wlst_mode=WlstModes.OFFLINE)
self._alias_helper = AliasHelper(self._aliases, logger, ExceptionType.COMPARE)
self._logger = logger
self._name_tokens_location = LocationContext()
self._name_tokens_location.add_name_token('DOMAIN', "testdomain")
Expand Down Expand Up @@ -291,19 +291,65 @@ def __walk_property(self, property_name, property_value, valid_prop_infos, model
if expected_data_type == 'password':
self.__substitute_password_with_token(model_folder_path, property_name, validation_location)


def __format_variable_name(self, location, attribute):
_method_name = '__format_variable_name'
def __traverse_location(iterate_location, name_list, last_folder=None, last_folder_short=None):
current_folder = iterate_location.get_current_model_folder()
if current_folder == model_constants.DOMAIN:
if last_folder is not None:
# If a short name is not defined for the top level folder, use the full name
if len(last_folder_short) == 0:
last_folder_short = last_folder
name_list.insert(0, last_folder_short)
else:
current_folder = iterate_location.get_current_model_folder()
short_folder = self._aliases.get_folder_short_name(iterate_location)
if last_folder_short is not None:
name_list.insert(0, last_folder_short)
try:
if not self._aliases.is_artificial_type_folder(location) and \
(self._aliases.supports_multiple_mbean_instances(iterate_location) or
self._aliases.is_custom_folder_allowed(iterate_location)):
name_token = self._aliases.get_name_token(iterate_location)
name = iterate_location.get_name_for_token(name_token)
name_list.insert(0, name)
iterate_location.remove_name_token(name_token)
iterate_location.pop_location()
except AliasException, ae:
self._logger.warning('WLSDPLY-19531', str(location), attribute, ae.getLocalizedMessage(),
class_name=_class_name, method_name=_method_name)
__traverse_location(iterate_location, name_list, current_folder, short_folder)
return name_list

short_list = __traverse_location(LocationContext(location), list())
short_name = ''
for node in short_list:
if node is not None and len(node) > 0:
short_name += node + '.'
short_name += attribute
_fake_name_replacement = re.compile('.fakename')
_white_space_replacement = re.compile('\s')
short_name = short_name.replace('/', '.')
short_name = _white_space_replacement.sub('-', short_name)
short_name = _fake_name_replacement.sub('', short_name)

return short_name


def __substitute_password_with_token(self, model_path, attribute_name, validation_location, model_context=None):
# TODO: move the logic to target_configuration_helper and reuse in variable_injector.process_attribute

model_path_tokens = model_path.split('/')
tokens_length = len(model_path_tokens)

variable_name = self.__format_variable_name(validation_location, attribute_name)
if tokens_length > 1:
# For AdminPassword
if model_path_tokens[0] == 'domainInfo:' and model_path_tokens[1] == '':
password_name = "@@SECRET:@@DOAMIN-UID@@-weblogic-credentials:%s@@" % (attribute_name.lower())
self.cache[attribute_name] = ''
else:
password_name = "@@SECRET:@@DOMAIN-UID@@-%s:%s@@" % ('-'.join(model_path_tokens[1:]).lower(),
attribute_name.lower())
self.cache['.'.join(model_path_tokens[1:]) + '-' + attribute_name] = ''
password_name = target_configuration_helper.format_as_secret(variable_name)
self.cache[variable_name] = ''

p_dict = self.current_dict

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,3 @@ def __cleanup_topology(model):
server_template['AutoMigrationEnabled'] = False


def __subtitute_secrets(current_dictionary, stack=None, recursive=False):
for key in current_dictionary:
if not recursive:
stack = []
current_node = current_dictionary[key]
if isinstance(current_node, dict):
stack.append(key)
__subtitute_secrets(current_node, stack, True)
else:
if isinstance(current_node, str):
if '--FIX ME--' == current_node:
current_dictionary[key] = '@@SECRET:' + stack[1] + ':' + key + '@@'


# fh = open('/tmp/domain_model.json')
# mydict = eval(fh.read())
# filter_model(mydict)
18 changes: 2 additions & 16 deletions core/src/main/python/wlsdeploy/tool/util/variable_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from wlsdeploy.aliases.wlst_modes import WlstModes
from wlsdeploy.json.json_translator import JsonToPython
from wlsdeploy.logging.platform_logger import PlatformLogger
import wlsdeploy.util.target_configuration_helper as target_configuration_helper

WEBLOGIC_DEPLOY_HOME_TOKEN = '@@WLSDEPLOY@@'

Expand Down Expand Up @@ -397,7 +398,6 @@ def __traverse_location(iterate_location, name_list, last_folder=None, last_fold
class_name=_class_name, method_name=_method_name)
__traverse_location(iterate_location, name_list, current_folder, short_folder)
return name_list

short_list = __traverse_location(LocationContext(location), list())

short_name = ''
Expand All @@ -424,10 +424,9 @@ def _process_attribute(self, model, attribute, location, injector_values):
if not _already_property(attribute_value):
variable_name = self.__format_variable_name(location, attribute)
variable_value = _format_variable_value(attribute_value)

if self.__model_context is not None and self.__model_context.is_target_k8s() \
and variable_value == alias_constants.PASSWORD_TOKEN:
model[attribute] = _format_as_secret(variable_name)
model[attribute] = target_configuration_helper.format_as_secret(variable_name)
else:
model[attribute] = _format_as_property(variable_name)

Expand Down Expand Up @@ -874,19 +873,6 @@ def _already_property(check_string):
def _format_as_property(prop_name):
return '@@PROP:%s@@' % prop_name

def _format_as_secret(prop_name):
"""
Format as X.Y.A.B This is the pattern of the name.
:param prop_name: property name
:return: formatted name
"""
name_lower_tokens = prop_name.lower().split('.')
if len(name_lower_tokens) == 1:
if name_lower_tokens[0] == 'adminusername' or 'adminpassword' == name_lower_tokens[0]:
return '@@SECRET:@@ENV:DOMAIN_UID@@-%s:%s@@' % ('weblogic-credentials', name_lower_tokens[0])

return '@@SECRET:@@ENV:DOMAIN_UID@@-%s:%s@@' % ( '-'.join(name_lower_tokens[:-1]), name_lower_tokens[-1])

def _split_injector(injector_path):
"""
Split the injector path into an mbean list and an attribute name from the injector path string
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/python/wlsdeploy/util/target_configuration_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def generate_k8s_script(file_location, token_dictionary):
k8s_create_script_handle.write(NL)
k8s_create_script_handle.write(NL)
for property_name in token_dictionary:
if property_name in [ 'AdminUserName', 'AdminPassword', 'SecurityConfiguration-NodeManagerPasswordEncrypted']:
# AdminPassword handle differently and SecurityConfig.NodeManagerPasswordEncrypted is the short name
# which filters out
if property_name in [ 'AdminPassword', 'SecurityConfig.NodeManagerPasswordEncrypted']:
continue
secret_names = property_name.lower().split('-')
secret_names = property_name.lower().split('.')
command_string = "create_k8s_secret %s %s %s " %( '-'.join(secret_names[:-1]), secret_names[-1],
"<changeme>")
k8s_create_script_handle.write(command_string)
Expand All @@ -60,3 +62,16 @@ def generate_k8s_script(file_location, token_dictionary):
'weblogic.domainUID=${DOMAIN_UID}')
k8s_create_script_handle.write(NL)
k8s_create_script_handle.close()

def format_as_secret(prop_name):
"""
Format as X.Y.A.B This is the pattern of the name.
:param prop_name: property name
:return: formatted name
"""
name_lower_tokens = prop_name.lower().split('.')
if len(name_lower_tokens) == 1:
if name_lower_tokens[0] == 'adminusername' or 'adminpassword' == name_lower_tokens[0]:
return '@@SECRET:@@ENV:DOMAIN_UID@@-%s:%s@@' % ('weblogic-credentials', name_lower_tokens[0])

return '@@SECRET:@@ENV:DOMAIN_UID@@-%s:%s@@' % ( '-'.join(name_lower_tokens[:-1]), name_lower_tokens[-1])