Skip to content

Commit 46bddc3

Browse files
authored
Check for token syntax errors after substitutions (#575)
* JIRA OWLS-80571 Check for token syntax errors after substitutions * JIRA OWLS-80571 Revised variable name
1 parent 792bac0 commit 46bddc3

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
_secret_pattern = re.compile("(@@SECRET:([\\w.-]+):([\\w.-]+)@@)")
3131
_file_nested_variable_pattern = re.compile("@@FILE:@@[\w]+@@[\w.\\\/:-]+@@")
3232

33+
# if this pattern is found, token substitution was incomplete
34+
_unresolved_token_pattern = re.compile("(@@(PROP|FILE|ENV|SECRET):)")
35+
3336
_secret_dirs_variable = "WDT_MODEL_SECRETS_DIRS"
3437
_secret_dirs_default = "/weblogic-operator/config-overrides-secrets"
3538
_secret_dir_pairs_variable="WDT_MODEL_SECRETS_NAME_DIR_PAIRS"
@@ -276,6 +279,17 @@ def _substitute(text, variables, model_context):
276279
value = _read_value_from_file(path, model_context)
277280
text = text.replace(token, value)
278281

282+
# if any @@TOKEN: remains in the value, throw an exception
283+
matches = _unresolved_token_pattern.findall(text)
284+
if matches:
285+
match = matches[0]
286+
token = match[1]
287+
sample = "@@" + token + ":<name>"
288+
if token == "SECRET":
289+
sample += ":<key>"
290+
sample += "@@"
291+
_report_token_issue("WLSDPLY-01745", method_name, model_context, token, sample)
292+
279293
return text
280294

281295

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ WLSDPLY-01739=Error: Could not resolve secret token "{0}". Known secrets include
301301
WLSDPLY-01740=Encryption failed: Unable to locate SerializedSystemIni
302302
WLSDPLY-01741=Encryption failed: Unable to initialize encryption service
303303

304+
# wlsdeploy/util/variables.py (cont'd)
305+
WLSDPLY-01745=Invalid syntax for token {0}, should be "{1}"
306+
304307
# oracle.weblogic.deploy.util.WebLogicDeployToolingVersion.java (in src/main/resources/templates)
305308
WLSDPLY-01750=The WebLogic Deploy Tooling {0} version is {1}
306309

core/src/test/python/variables_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ def testSecretTokenNotFound(self):
144144
else:
145145
self.fail('Test must raise VariableException when secret token is not found')
146146

147+
def testTokenSyntaxErrors(self):
148+
"""
149+
Test that exceptions are thrown for token syntax errors.
150+
"""
151+
self._test_token_syntax_error("@@SECRET:my-secret/my-key@@")
152+
self._test_token_syntax_error("@@SECRET:@@ENVmy-secret:-my-key@@-some-more-text")
153+
self._test_token_syntax_error("@@ENV:my-secret!@@")
154+
self._test_token_syntax_error("@@FILE:@@ORACLE_HOME@@/my-file")
155+
self._test_token_syntax_error("@@PROP:")
156+
157+
def _test_token_syntax_error(self, value):
158+
"""
159+
Test that an exception is thrown for a token syntax error in the specified value.
160+
:param value: the text value to be checked
161+
"""
162+
try:
163+
model = {'domainInfo': {'AdminUserName': value}}
164+
variables.substitute(model, {}, self.model_context)
165+
except VariableException, e:
166+
pass
167+
else:
168+
self.fail('Test must raise VariableException when token has a syntax error')
169+
147170

148171
if __name__ == '__main__':
149172
unittest.main()

0 commit comments

Comments
 (0)