def __check_and_customize_model(model, model_context, aliases):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover"):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    inserted, variable_model, variable_file_name = VariableInjector(_program_name, model.get_model(), model_context,
                                                                    WebLogicHelper(
                                                                        __logger).get_actual_weblogic_version()).\
        inject_variables_keyword_file()
    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context,
                              wlst_mode=__wlst_mode,
                              aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(
            model.get_model(),
            variables_file_name=variable_file_name,
            archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015',
                         ex.getLocalizedMessage(),
                         class_name=_class_name,
                         method_name=_method_name)
    def _apply_filter_and_inject_variable(self, model, model_context, validator):
        """
        Applying filter
        Generate k8s create script
        Inject variable for tokens
        :param model: updated model
        """
        _method_name = '_apply_filter_and_inject_variable'
        self._logger.entering(class_name=_class_name, method_name=_method_name)

        if filter_helper.apply_filters(model, "discover", model_context):
            self._logger.info('WLSDPLY-06014', _class_name=_class_name, method_name=_method_name)

        variable_injector = VariableInjector(_program_name, model, model_context,
                                             WebLogicHelper(self._logger).get_actual_weblogic_version(), self.cache)
        if self.cache is not None:
            # Generate k8s create secret script, after that clear the dictionary to avoid showing up in the variable file
            if model_context.is_targetted_config():

                for item in self.cache:
                    self.secrets_to_generate.add(item)

                self.cache.clear()
                # This is in case the user has specify -variable_file in command line
                # clearing the cache will remove the original entries and the final variable file will miss the original
                # contents
                if os.path.exists(self.model_context.get_variable_file()):
                    variable_map = validator.load_variables(self.model_context.get_variable_file())
                    self.cache.update(variable_map)

        variable_injector.inject_variables_keyword_file()
        return model
Example #3
0
    def _apply_filter_and_inject_variable(self, model, model_context):
        """
        Applying filter
        Inject variable for tokens
        :param model: updated model
        """
        _method_name = '_apply_filter_and_inject_variable'
        self._logger.entering(class_name=_class_name, method_name=_method_name)

        if filter_helper.apply_filters(model, "discover", model_context):
            self._logger.info('WLSDPLY-06014', _class_name=_class_name, method_name=_method_name)

        # include credential properties in the injector map, unless target uses credential secrets
        target_config = model_context.get_target_configuration()
        if target_config.uses_credential_secrets():
            credential_properties = {}
        else:
            credential_properties = self.credential_injector.get_variable_cache()

        variable_injector = VariableInjector(_program_name, model, model_context,
                                             WebLogicHelper(self._logger).get_actual_weblogic_version(),
                                             credential_properties)

        # update the variable file with any new values
        inserted, variable_model, variable_file_name = \
            variable_injector.inject_variables_keyword_file(VARIABLE_FILE_UPDATE)

        # return variable_model - if writing the variables file failed, this will be the original model.
        # a warning is issued in inject_variables_keyword_file() if that was the case.
        return variable_model
Example #4
0
def __check_and_customize_model(model, model_context, aliases,
                                password_injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model, before any tokenization
    :param model_context: configuration from command-line
    :param aliases: used for validation if model changes are made
    :param password_injector: injector created to collect and tokenize passwords, possibly None
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover",
                                   model_context):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    cache = None
    if password_injector is not None:
        cache = password_injector.get_variable_cache()

        # Generate k8s create secret script, possibly using lax validation method
        if model_context.is_targetted_config():
            validation_method = model_context.get_target_configuration(
            ).get_validation_method()
            model_context.set_validation_method(validation_method)
            target_configuration_helper.generate_k8s_script(
                model_context, cache, model.get_model())

            # if target handles password substitution, clear property cache to keep out of variables file.
            if model_context.get_target_configuration().manages_credentials():
                cache.clear()

    # Apply the injectors specified in model_variable_injector.json, or in the target configuration
    variable_injector = VariableInjector(
        _program_name, model.get_model(), model_context,
        WebLogicHelper(__logger).get_actual_weblogic_version(), cache)

    inserted, variable_model, variable_file_name = variable_injector.inject_variables_keyword_file(
    )

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context,
                              wlst_mode=__wlst_mode,
                              aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(
            model.get_model(),
            variables_file_name=variable_file_name,
            archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015',
                         ex.getLocalizedMessage(),
                         class_name=_class_name,
                         method_name=_method_name)
def __perform_model_file_validation(model_file_name, model_context):
    """

    :param model_file_name:
    :param model_context:
    :return:
    :raises ValidationException:
    """

    _method_name = '__perform_model_file_validation'

    __logger.entering(model_file_name,
                      class_name=_class_name,
                      method_name=_method_name)

    try:
        model_validator = Validator(model_context, logger=__logger)
        variable_map = model_validator.load_variables(
            model_context.get_variable_file())
        model_dictionary = cla_helper.merge_model_files(
            model_file_name, variable_map)

        if cla_helper.check_persist_model():
            persist_model_dict = copy.deepcopy(model_dictionary)
            variables.substitute(persist_model_dict, variable_map,
                                 model_context)
            cla_helper.persist_model(model_context, persist_model_dict)

        model_validator.validate_in_standalone_mode(
            model_dictionary, variable_map,
            model_context.get_archive_file_name())

        # substitute variables before filtering
        variables.substitute(model_dictionary, variable_map, model_context)
        # apply filters to merged model
        if filter_helper.apply_filters(model_dictionary, "validate"):
            # persist model after filtering
            cla_helper.persist_model(model_context, model_dictionary)

            # validate model changes after filtering
            model_validator.validate_in_standalone_mode(
                model_dictionary, variable_map,
                model_context.get_archive_file_name())

    except (TranslateException, VariableException), te:
        __logger.severe('WLSDPLY-20009',
                        _program_name,
                        model_file_name,
                        te.getLocalizedMessage(),
                        error=te,
                        class_name=_class_name,
                        method_name=_method_name)
        ex = exception_helper.create_validate_exception(
            te.getLocalizedMessage(), error=te)
        __logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        raise ex
Example #6
0
def __check_and_customize_model(model, model_context, aliases, injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover", model_context):
        __logger.info('WLSDPLY-06014', _class_name=_class_name, method_name=_method_name)

    cache = None
    if injector is not None:
        cache = injector.get_variable_cache()
        # Generate k8s create secret script, after that clear the dictionary to avoid showing up in the variable file
        if model_context.is_targetted_config():
            validation_method = model_context.get_target_configuration()['validation_method']
            model_context.set_validation_method(validation_method)
            target_configuration_helper.generate_k8s_script(model_context.get_kubernetes_variable_file(), cache)
            cache.clear()

    variable_injector = VariableInjector(_program_name, model.get_model(), model_context,
                     WebLogicHelper(__logger).get_actual_weblogic_version(), cache)

    inserted, variable_model, variable_file_name = \
        variable_injector.inject_variables_keyword_file()

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context, wlst_mode=__wlst_mode, aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(model.get_model(), variables_file_name=variable_file_name,
                                        archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015', ex.getLocalizedMessage(), class_name=_class_name, method_name=_method_name)
Example #7
0
    try:
        variables.substitute(model_dictionary, variable_map, model_context)
    except VariableException, ex:
        __logger.severe('WLSDPLY-20004',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    aliases = Aliases(model_context, wlst_mode=__wlst_mode)
    validate_model(model_dictionary, model_context, aliases)

    if filter_helper.apply_filters(model_dictionary, "deploy"):
        # if any filters were applied, re-validate the model
        validate_model(model_dictionary, model_context, aliases)

    try:
        model = Model(model_dictionary)
        exit_code = __deploy(model, model_context, aliases)
    except DeployException, ex:
        __logger.severe('WLSDPLY-09015',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
Example #8
0
        variables.substitute(model, variable_map, model_context)
    except VariableException, ex:
        __logger.severe('WLSDPLY-20004',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        __clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    aliases = Aliases(model_context, wlst_mode=__wlst_mode)
    alias_helper = AliasHelper(aliases, __logger, ExceptionType.CREATE)
    validate_model(model, model_context, aliases)

    if filter_helper.apply_filters(model, "create"):
        # if any filters were applied, re-validate the model
        validate_model(model, model_context, aliases)
    try:

        has_atp = validateRCUArgsAndModel(model_context, model, alias_helper)
        # check if there is an atpwallet and extract in the domain dir
        # it is to support non JRF domain but user wants to use ATP database
        archive_file_name = model_context.get_archive_file_name()
        if not has_atp and archive_file_name and os.path.exists(
                archive_file_name):
            archive_file = WLSDeployArchive(archive_file_name)
            if archive_file:
                atp_wallet_zipentry = archive_file.getATPWallet()
                if atp_wallet_zipentry:
                    atp_helper.extract_walletzip(model, model_context,
def __check_and_customize_model(model, model_context, aliases,
                                credential_injector):
    """
    Customize the model dictionary before persisting. Validate the model after customization for informational
    purposes. Any validation errors will not stop the discovered model to be persisted.
    :param model: completely discovered model, before any tokenization
    :param model_context: configuration from command-line
    :param aliases: used for validation if model changes are made
    :param credential_injector: injector created to collect and tokenize credentials, possibly None
    """
    _method_name = '__check_and_customize_model'
    __logger.entering(class_name=_class_name, method_name=_method_name)

    if filter_helper.apply_filters(model.get_model(), "discover",
                                   model_context):
        __logger.info('WLSDPLY-06014',
                      _class_name=_class_name,
                      method_name=_method_name)

    # target config always present in model context, default config if not declared
    target_configuration = model_context.get_target_configuration()

    # if target config declared, use the validation method it contains (lax, etc.)
    if model_context.is_targetted_config():
        validation_method = target_configuration.get_validation_method()
        model_context.set_validation_method(validation_method)

    credential_cache = None
    if credential_injector is not None:
        # filter variables or secrets that are no longer in the model
        credential_injector.filter_unused_credentials(model.get_model())

        credential_cache = credential_injector.get_variable_cache()

        # Generate k8s create secret script
        if target_configuration.uses_credential_secrets():
            target_configuration_helper.generate_k8s_script(
                model_context, credential_cache, model.get_model(),
                ExceptionType.DISCOVER)

        # create additional output after filtering, but before variables have been inserted
        if model_context.is_targetted_config():
            target_configuration_helper.create_additional_output(
                model, model_context, aliases, credential_injector,
                ExceptionType.DISCOVER)

        # if target handles credential configuration, clear property cache to keep out of variables file.
        if model_context.get_target_configuration().manages_credentials():
            credential_cache.clear()

    # Apply the injectors specified in model_variable_injector.json, or in the target configuration.
    # Include the variable mappings that were collected in credential_cache.
    variable_injector = VariableInjector(
        _program_name, model.get_model(), model_context,
        WebLogicHelper(__logger).get_actual_weblogic_version(),
        credential_cache)

    inserted, variable_model, variable_file_name = variable_injector.inject_variables_keyword_file(
    )

    if inserted:
        model = Model(variable_model)
    try:
        validator = Validator(model_context,
                              wlst_mode=__wlst_mode,
                              aliases=aliases)

        # no variables are generated by the discover tool
        validator.validate_in_tool_mode(
            model.get_model(),
            variables_file_name=variable_file_name,
            archive_file_name=model_context.get_archive_file_name())
    except ValidateException, ex:
        __logger.warning('WLSDPLY-06015',
                         ex.getLocalizedMessage(),
                         class_name=_class_name,
                         method_name=_method_name)
                model_context.get_variable_file())
        variables.substitute(model_dictionary, variable_map, model_context)
    except VariableException, ex:
        __logger.severe('WLSDPLY-20004',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    aliases = Aliases(model_context, wlst_mode=__wlst_mode)
    validate_model(model_dictionary, model_context, aliases)

    if filter_helper.apply_filters(model_dictionary, "update"):
        # if any filters were applied, re-validate the model
        validate_model(model_dictionary, model_context, aliases)

    try:
        model = Model(model_dictionary)
        __update(model, model_context, aliases)
    except DeployException, ex:
        __logger.severe('WLSDPLY-09015',
                        _program_name,
                        ex.getLocalizedMessage(),
                        error=ex,
                        class_name=_class_name,
                        method_name=_method_name)
        cla_helper.clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
        clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    try:
        variables.substitute(model_dictionary, variable_map, model_context)
    except VariableException, ex:
        __logger.severe('WLSDPLY-20004', program_name, ex.getLocalizedMessage(), error=ex,
                        class_name=_class_name, method_name=_method_name)
        clean_up_temp_files()
        tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)

    persist_model(model_context, model_dictionary)

    validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode)

    if filter_helper.apply_filters(model_dictionary, filter_type):
        # if any filters were applied, re-validate the model
        validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode)

    return model_dictionary


def process_online_args(optional_arg_map):
    """
    Determine if we are executing in online mode and if so, validate/prompt for the necessary parameters.
    :param optional_arg_map: the optional arguments map
    :return: the WLST mode
    :raises CLAException: if an error occurs reading input from the user
    """
    _method_name = 'process_online_args'
Example #12
0
    def walk(self):
        """
        Replace password attributes in each model file with secret tokens, and write each model.
        Generate a script to create the required secrets.
        Create any additional output specified for the target environment.
        """
        _method_name = "walk"

        model_file_name = None

        # create a merged model that is not substituted
        merged_model_dictionary = {}

        try:
            model_file_list = self.model_files.split(',')
            for model_file in model_file_list:
                if os.path.splitext(model_file)[1].lower() == ".yaml":
                    model_file_name = model_file
                    FileToPython(model_file_name, True).parse()

                aliases = Aliases(model_context=self.model_context, wlst_mode=WlstModes.OFFLINE)

                validator = Validator(self.model_context, aliases, wlst_mode=WlstModes.OFFLINE)

                # Just merge and validate but without substitution
                model_dictionary = cla_helper.merge_model_files(model_file_name, None)

                variable_file = self.model_context.get_variable_file()
                if not os.path.exists(variable_file):
                    variable_file = None

                return_code = validator.validate_in_tool_mode(model_dictionary,
                                                              variables_file_name=variable_file,
                                                              archive_file_name=None)

                if return_code == Validator.ReturnCode.STOP:
                    self._logger.severe('WLSDPLY-05705', model_file_name)
                    return VALIDATION_FAIL

                self.current_dict = model_dictionary

                self.__walk_model_section(model.get_model_domain_info_key(), self.current_dict,
                                          aliases.get_model_section_top_level_folder_names(DOMAIN_INFO))

                self.__walk_model_section(model.get_model_topology_key(), self.current_dict,
                                          aliases.get_model_topology_top_level_folder_names())

                self.__walk_model_section(model.get_model_resources_key(), self.current_dict,
                                          aliases.get_model_resources_top_level_folder_names())

                self.current_dict = self._apply_filter_and_inject_variable(self.current_dict, self.model_context)

                file_name = os.path.join(self.output_dir, os.path.basename(model_file_name))
                fos = JFileOutputStream(file_name, False)
                writer = JPrintWriter(fos, True)
                pty = PythonToYaml(self.current_dict)
                pty._write_dictionary_to_yaml_file(self.current_dict, writer)
                writer.close()

                cla_helper.merge_model_dictionaries(merged_model_dictionary, self.current_dict, None)

            # filter variables or secrets that are no longer in the merged, filtered model
            filter_helper.apply_filters(merged_model_dictionary, "discover", self.model_context)
            self.credential_injector.filter_unused_credentials(merged_model_dictionary)

            # use a merged, substituted, filtered model to get domain name and create additional target output.
            full_model_dictionary = cla_helper.load_model(_program_name, self.model_context, self._aliases,
                                                          "discover", WlstModes.OFFLINE)

            target_config = self.model_context.get_target_configuration()
            if target_config.uses_credential_secrets():
                target_configuration_helper.generate_k8s_script(self.model_context,
                                                                self.credential_injector.get_variable_cache(),
                                                                full_model_dictionary, ExceptionType.VALIDATE)

            # create any additional outputs from full model dictionary
            target_configuration_helper.create_additional_output(Model(full_model_dictionary), self.model_context,
                                                                 self._aliases, self.credential_injector,
                                                                 ExceptionType.VALIDATE)

        except ValidateException, te:
            self._logger.severe('WLSDPLY-20009', _program_name, model_file_name, te.getLocalizedMessage(),
                                error=te, class_name=_class_name, method_name=_method_name)
            ex = exception_helper.create_compare_exception(te.getLocalizedMessage(), error=te)
            self._logger.throwing(ex, class_name=_class_name, method_name=_method_name)
            return VALIDATION_FAIL