|
| 1 | +""" |
| 2 | +Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 4 | +""" |
| 5 | +import os.path |
| 6 | + |
| 7 | +from oracle.weblogic.deploy.util import WLSDeployArchive |
| 8 | + |
| 9 | +from wlsdeploy.logging.platform_logger import PlatformLogger |
| 10 | +from wlsdeploy.util import dictionary_utils |
| 11 | +from wlsdeploy.util import string_utils |
| 12 | + |
| 13 | +DOMAIN_SECURITY_FOLDER = 'security' |
| 14 | +IDP_FILE_PREFIX = 'saml2idppartner' |
| 15 | +IDP_PARTNERS_KEY = 'saml2.idp.partners' |
| 16 | +SP_FILE_PREFIX = 'saml2sppartner' |
| 17 | +SP_PARTNERS_KEY = 'saml2.sp.partners' |
| 18 | + |
| 19 | + |
| 20 | +class Saml2SecurityHelper(object): |
| 21 | + """ |
| 22 | + Supports discover and create/deploy/update of SAML2 security initialization data files |
| 23 | + """ |
| 24 | + _class_name = 'Saml2SecurityHelper' |
| 25 | + |
| 26 | + def __init__(self, domain_home, exception_type): |
| 27 | + """ |
| 28 | + Initialize an instance of Saml2SecurityHelper. |
| 29 | + :param domain_home: used locate security files |
| 30 | + :param exception_type: the type of exception to be thrown |
| 31 | + """ |
| 32 | + self._domain_home = domain_home |
| 33 | + self._domain_security_directory = os.path.join(self._domain_home, DOMAIN_SECURITY_FOLDER) |
| 34 | + self._exception_type = exception_type |
| 35 | + self._logger = PlatformLogger('wlsdeploy.tool.util') |
| 36 | + |
| 37 | + def extract_initialization_files(self, archive_helper): |
| 38 | + """ |
| 39 | + Extract initialization files from the archive to the security directory. |
| 40 | + :param archive_helper: used to find initialization files in archive |
| 41 | + """ |
| 42 | + self._extract_initialization_files(IDP_FILE_PREFIX, IDP_PARTNERS_KEY, archive_helper) |
| 43 | + self._extract_initialization_files(SP_FILE_PREFIX, SP_PARTNERS_KEY, archive_helper) |
| 44 | + |
| 45 | + def _extract_initialization_files(self, prefix, partners_key, archive_helper): |
| 46 | + """ |
| 47 | + Extract initialization files for a specific prefix. |
| 48 | + Don't install any files if the <prefix>initialized file exists in the security directory |
| 49 | + :param prefix: the prefix of the "initialized" and "properties" file names |
| 50 | + :param partners_key: the key in the properties file that contains the partner IDs |
| 51 | + :param archive_helper: used to find initialization files |
| 52 | + """ |
| 53 | + _method_name = '_install_initialization_files' |
| 54 | + |
| 55 | + properties_file_name = prefix + '.properties' |
| 56 | + properties_path = WLSDeployArchive.getSaml2DataArchivePath(properties_file_name) |
| 57 | + if archive_helper and archive_helper.contains_file(properties_path): |
| 58 | + # if the "initialized" file is present, don't extract files |
| 59 | + initialized_file = properties_file_name + '.initialized' |
| 60 | + initialized_path = os.path.join(self._domain_security_directory, initialized_file) |
| 61 | + if os.path.isfile(initialized_path): |
| 62 | + self._logger.info('WLSDPLY-23000', properties_file_name, initialized_file, |
| 63 | + class_name=self._class_name, method_name=_method_name) |
| 64 | + else: |
| 65 | + # extract the properties file, the read it to determine metadata files |
| 66 | + self._logger.info('WLSDPLY-23001', properties_file_name, class_name=self._class_name, |
| 67 | + method_name=_method_name) |
| 68 | + archive_helper.extract_file(properties_path, self._domain_security_directory) |
| 69 | + self._extract_metadata_files(properties_file_name, partners_key, archive_helper) |
| 70 | + |
| 71 | + def _extract_metadata_files(self, properties_file_name, partners_key, archive_helper): |
| 72 | + """ |
| 73 | + Extract metadata files specified in the properties file. |
| 74 | + :param properties_file_name: the name of the properties file containing the metadata file names |
| 75 | + :param partners_key: the key in the properties file that contains the partner IDs |
| 76 | + :param archive_helper: used to find metadata files |
| 77 | + """ |
| 78 | + _method_name = '_install_metadata_files' |
| 79 | + |
| 80 | + properties_file = os.path.join(self._domain_security_directory, properties_file_name) |
| 81 | + metadata_file_names = self._get_metadata_file_names(properties_file, partners_key) |
| 82 | + for metadata_file_name in metadata_file_names: |
| 83 | + metadata_file = WLSDeployArchive.getSaml2DataArchivePath(metadata_file_name) |
| 84 | + |
| 85 | + if archive_helper.contains_file(metadata_file): |
| 86 | + self._logger.info('WLSDPLY-23002', metadata_file_name, class_name=self._class_name, |
| 87 | + method_name=_method_name) |
| 88 | + archive_helper.extract_file(metadata_file, self._domain_security_directory) |
| 89 | + else: |
| 90 | + self._logger.severe('WLSDPLY-23003', metadata_file_name, properties_file, |
| 91 | + class_name=self._class_name, method_name=_method_name) |
| 92 | + |
| 93 | + def discover_initialization_files(self, archive, discoverer): |
| 94 | + """ |
| 95 | + Add initialization files from the security directory to the archive. |
| 96 | + :param archive: WLSDeployArchive instance used to add files |
| 97 | + :param discoverer: used to collect remote files when no archive is specified |
| 98 | + """ |
| 99 | + self._discover_initialization_files(IDP_FILE_PREFIX, IDP_PARTNERS_KEY, archive, discoverer) |
| 100 | + self._discover_initialization_files(SP_FILE_PREFIX, SP_PARTNERS_KEY, archive, discoverer) |
| 101 | + |
| 102 | + def _discover_initialization_files(self, prefix, partners_key, archive, discoverer): |
| 103 | + """ |
| 104 | + Add initialization files for a specific prefix to the archive. |
| 105 | + :param prefix: the prefix of the "properties" file name |
| 106 | + :param partners_key: the key in the properties file that contains the partner IDs |
| 107 | + :param archive: WLSDeployArchive instance used to add files |
| 108 | + :param discoverer: used to collect remote files when no archive is specified |
| 109 | + """ |
| 110 | + _method_name = '_discover_initialization_files' |
| 111 | + |
| 112 | + properties_file_name = prefix + '.properties' |
| 113 | + properties_file = os.path.join(self._domain_security_directory, properties_file_name) |
| 114 | + if os.path.isfile(properties_file): |
| 115 | + if archive: |
| 116 | + self._logger.info('WLSDPLY-23005', properties_file_name, class_name=self._class_name, |
| 117 | + method_name=_method_name) |
| 118 | + archive.addSaml2DataFile(properties_file, True) |
| 119 | + else: |
| 120 | + # if -skip_archive or -remote, add to the remote map for manual addition |
| 121 | + discoverer.add_to_remote_map(properties_file, |
| 122 | + WLSDeployArchive.getSaml2DataArchivePath(properties_file_name), |
| 123 | + WLSDeployArchive.ArchiveEntryType.SAML2_DATA.name()) |
| 124 | + |
| 125 | + # check for metadata files, even if archive not specified |
| 126 | + self._discover_metadata_files(properties_file, partners_key, archive, discoverer) |
| 127 | + |
| 128 | + def _discover_metadata_files(self, properties_file_name, partners_key, archive, discoverer): |
| 129 | + """ |
| 130 | + Add metadata files specified in the properties file to the archive. |
| 131 | + :param properties_file_name: the name of the "properties" file |
| 132 | + :param partners_key: the key in the properties file that contains the partner IDs |
| 133 | + :param archive: WLSDeployArchive instance used to add files |
| 134 | + :param discoverer: used to collect remote files when no archive is specified |
| 135 | + """ |
| 136 | + _method_name = '_discover_metadata_files' |
| 137 | + |
| 138 | + properties_file = os.path.join(self._domain_security_directory, properties_file_name) |
| 139 | + metadata_file_names = self._get_metadata_file_names(properties_file, partners_key) |
| 140 | + for metadata_file_name in metadata_file_names: |
| 141 | + metadata_file = os.path.join(self._domain_security_directory, metadata_file_name) |
| 142 | + if not os.path.isfile(metadata_file): |
| 143 | + self._logger.severe('WLSDPLY-23007', metadata_file_name, properties_file_name, |
| 144 | + class_name=self._class_name, method_name=_method_name) |
| 145 | + elif archive: |
| 146 | + self._logger.info('WLSDPLY-23006', metadata_file_name, class_name=self._class_name, |
| 147 | + method_name=_method_name) |
| 148 | + archive.addSaml2DataFile(metadata_file, True) |
| 149 | + else: |
| 150 | + # if -skip_archive or -remote, add to the remote map for manual addition |
| 151 | + discoverer.add_to_remote_map(metadata_file, |
| 152 | + WLSDeployArchive.getSaml2DataArchivePath(metadata_file_name), |
| 153 | + WLSDeployArchive.ArchiveEntryType.SAML2_DATA.name()) |
| 154 | + |
| 155 | + def _get_metadata_file_names(self, properties_file, partners_key): |
| 156 | + """ |
| 157 | + Get the metadata files names from the specified properties file. |
| 158 | + :param properties_file: the properties file to be examined |
| 159 | + :param partners_key: the key in the properties file that contains the partner IDs |
| 160 | + :return: a list of metadata file names |
| 161 | + """ |
| 162 | + _method_name = '_get_metadata_file_names' |
| 163 | + |
| 164 | + metadata_file_names = [] |
| 165 | + properties = string_utils.load_properties(properties_file, self._exception_type) |
| 166 | + partners_text = dictionary_utils.get_element(properties, partners_key) |
| 167 | + if partners_text: |
| 168 | + partner_ids = partners_text.split(',') |
| 169 | + for partner_id in partner_ids: |
| 170 | + metadata_key = partner_id.strip() + '.metadata.file' |
| 171 | + metadata_file_name = dictionary_utils.get_element(properties, metadata_key) |
| 172 | + if metadata_file_name: |
| 173 | + metadata_file_names.append(metadata_file_name) |
| 174 | + else: |
| 175 | + self._logger.severe('WLSDPLY-23004', metadata_key, properties_file, class_name=self._class_name, |
| 176 | + method_name=_method_name) |
| 177 | + return metadata_file_names |
0 commit comments