Skip to content

Commit d33fbc2

Browse files
committed
Merge branch 'saml-tokens' into 'develop-4.0'
Allow variable tokens in SAML2 initialization files See merge request weblogic-cloud/weblogic-deploy-tooling!1613
2 parents 5f539a0 + 17f15d1 commit d33fbc2

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

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

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from wlsdeploy.logging.platform_logger import PlatformLogger
1010
from wlsdeploy.util import dictionary_utils
1111
from wlsdeploy.util import string_utils
12+
from wlsdeploy.util import variables
1213

1314
DOMAIN_SECURITY_FOLDER = 'security'
1415
IDP_FILE_PREFIX = 'saml2idppartner'
@@ -39,19 +40,27 @@ def extract_initialization_files(self, archive_helper, deployer=None):
3940
"""
4041
Extract initialization files from the archive to the security directory.
4142
:param archive_helper: used to find initialization files in archive
43+
:param deployer: used to transfer files for online deployment
4244
"""
43-
self._extract_initialization_files(IDP_FILE_PREFIX, IDP_PARTNERS_KEY, archive_helper, deployer)
44-
self._extract_initialization_files(SP_FILE_PREFIX, SP_PARTNERS_KEY, archive_helper, deployer)
45+
variable_map = {}
46+
variable_file = self._model_context.get_variable_file()
47+
if variable_file is not None and os.path.exists(variable_file):
48+
variable_map = variables.load_variables(variable_file)
4549

46-
def _extract_initialization_files(self, prefix, partners_key, archive_helper, deployer):
50+
self._extract_initialization_files(IDP_FILE_PREFIX, IDP_PARTNERS_KEY, archive_helper, deployer, variable_map)
51+
self._extract_initialization_files(SP_FILE_PREFIX, SP_PARTNERS_KEY, archive_helper, deployer, variable_map)
52+
53+
def _extract_initialization_files(self, prefix, partners_key, archive_helper, deployer, variable_map):
4754
"""
4855
Extract initialization files for a specific prefix.
4956
Don't install any files if the <prefix>initialized file exists in the security directory
5057
:param prefix: the prefix of the "initialized" and "properties" file names
5158
:param partners_key: the key in the properties file that contains the partner IDs
5259
:param archive_helper: used to find initialization files
60+
:param deployer: used to transfer files for online deployment
61+
:param variable_map: used for token replacement
5362
"""
54-
_method_name = '_install_initialization_files'
63+
_method_name = '_extract_initialization_files'
5564

5665
properties_file_name = prefix + '.properties'
5766
properties_path = WLSDeployArchive.getSaml2DataArchivePath(properties_file_name)
@@ -61,28 +70,67 @@ def _extract_initialization_files(self, prefix, partners_key, archive_helper, de
6170
initialized_path = os.path.join(self._domain_security_directory, initialized_file)
6271
if self._model_context.is_ssh():
6372
extracted_file_path = archive_helper.extract_file(properties_path, deployer.upload_temporary_dir)
64-
deployer.upload_specific_file_to_remote_server(extracted_file_path, self._domain_security_directory)
73+
if self._detokenize_file(extracted_file_path, variable_map):
74+
deployer.upload_specific_file_to_remote_server(extracted_file_path, self._domain_security_directory)
75+
self._extract_metadata_files(extracted_file_path, partners_key, archive_helper, deployer)
6576
elif not self._model_context.is_ssh() and os.path.isfile(initialized_path):
6677
self._logger.info('WLSDPLY-23000', properties_file_name, initialized_file,
6778
class_name=self._class_name, method_name=_method_name)
6879
else:
6980
# extract the properties file, the read it to determine metadata files
7081
self._logger.info('WLSDPLY-23001', properties_file_name, class_name=self._class_name,
7182
method_name=_method_name)
83+
extracted_file_path = archive_helper.extract_file(properties_path, self._domain_security_directory)
84+
if self._detokenize_file(extracted_file_path, variable_map):
85+
self._extract_metadata_files(extracted_file_path, partners_key, archive_helper, deployer)
86+
87+
def _detokenize_file(self, file_path, variable_map):
88+
"""
89+
Replace tokens in the specified file from the variable map
90+
:param file_path: the file to detokenize
91+
:param variable_map: variables to resolve property tokens
92+
"""
93+
_method_name = '_detokenize_file'
94+
95+
file_reader = None
96+
file_writer = None
97+
error_total = 0
7298

73-
archive_helper.extract_file(properties_path, self._domain_security_directory)
74-
self._extract_metadata_files(properties_file_name, partners_key, archive_helper, deployer)
99+
try:
100+
file_reader = open(file_path, 'r')
101+
lines = file_reader.readlines()
102+
file_reader.close()
103+
file_reader = None
75104

76-
def _extract_metadata_files(self, properties_file_name, partners_key, archive_helper, deployer):
105+
new_lines = []
106+
for line in lines:
107+
line, error_count = variables.substitute_text(line, variable_map, self._model_context)
108+
new_lines.append(line)
109+
error_total += error_count
110+
111+
if error_total:
112+
self._logger.severe('WLSDPLY-23008', error_total, file_path,
113+
class_name=self._class_name, method_name=_method_name)
114+
file_writer = open(file_path, 'w')
115+
file_writer.writelines(new_lines)
116+
finally:
117+
if file_reader is not None:
118+
file_reader.close()
119+
if file_writer is not None:
120+
file_writer.close()
121+
122+
return error_total == 0
123+
124+
def _extract_metadata_files(self, properties_file, partners_key, archive_helper, deployer):
77125
"""
78126
Extract metadata files specified in the properties file.
79-
:param properties_file_name: the name of the properties file containing the metadata file names
127+
:param properties_file: the properties file containing the metadata file names
80128
:param partners_key: the key in the properties file that contains the partner IDs
81129
:param archive_helper: used to find metadata files
130+
:param deployer: used to transfer files for online deployment
82131
"""
83-
_method_name = '_install_metadata_files'
132+
_method_name = '_extract_metadata_files'
84133

85-
properties_file = os.path.join(self._domain_security_directory, properties_file_name)
86134
metadata_file_names = self._get_metadata_file_names(properties_file, partners_key)
87135
for metadata_file_name in metadata_file_names:
88136
metadata_file = WLSDeployArchive.getSaml2DataArchivePath(metadata_file_name)
@@ -94,7 +142,7 @@ def _extract_metadata_files(self, properties_file_name, partners_key, archive_he
94142
if self._model_context.is_ssh():
95143
extracted_file_path = archive_helper.extract_file(metadata_file, deployer.upload_temporary_dir)
96144
deployer.upload_specific_file_to_remote_server(extracted_file_path,
97-
self._domain_security_directory)
145+
self._domain_security_directory)
98146
else:
99147
archive_helper.extract_file(metadata_file, self._domain_security_directory)
100148
else:
@@ -128,7 +176,8 @@ def _discover_initialization_files(self, prefix, partners_key, archive, discover
128176
results = self._model_context.get_ssh_context().get_directory_contents(self._domain_security_directory, True)
129177
for result in results:
130178
if result.lower().endswith(properties_file_name.lower()):
131-
properties_file = discoverer.download_deployment_from_remote_server(properties_file,
179+
properties_file = discoverer.download_deployment_from_remote_server(
180+
properties_file,
132181
discoverer.download_temporary_dir, "samlInitFile")
133182
if os.path.isfile(properties_file):
134183
if archive:

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2023, Oracle Corporation and/or its affiliates.
2+
Copyright (c) 2017, 2024, Oracle and/or its affiliates.
33
Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import os
@@ -188,6 +188,20 @@ def substitute_value(text, variables, model_context):
188188
return result
189189

190190

191+
def substitute_text(text, variables, model_context):
192+
"""
193+
Perform token substitutions on a single text value.
194+
Return a tuple with the revised text and the number of errors reported.
195+
:param text: the original text
196+
:param variables: a dictionary of variables for substitution
197+
:param model_context: used to resolve variables in file paths
198+
:return the revised text and the number of errors reported
199+
"""
200+
error_info = {'errorCount': 0}
201+
result = _substitute(text, variables, model_context, error_info)
202+
return result, error_info['errorCount']
203+
204+
191205
def substitute(dictionary, variables, model_context):
192206
"""
193207
Substitute fields in the specified dictionary with variable values.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,7 @@ WLSDPLY-23004=Metadata key {0} was not found in SAML2 initialization file {1}
20622062
WLSDPLY-23005=Adding SAML2 initialization file {0} to archive
20632063
WLSDPLY-23006=Adding SAML2 initialization metadata file {0} to archive
20642064
WLSDPLY-23007=SAML2 initialization metadata file {0} specified in properties file {1} was not found
2065+
WLSDPLY-23008=Found {0} unresolved variable token(s) in file {1}
20652066

20662067
####################################################################
20672068
# Message number 30000 - 30999 Archive Helper #

0 commit comments

Comments
 (0)