Skip to content

Commit 33e29f8

Browse files
authored
Ensure that secret names follow established rules (#718)
* Ensure that secret names follow established rules; avoid chmod on Windows * Remove ServerStart from Server and ServerTemplate in filters
1 parent 9b2bae1 commit 33e29f8

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

core/src/main/java/oracle/weblogic/deploy/util/FileUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.util;
@@ -831,6 +831,8 @@ static Set<PosixFilePermission> getPermissions(int octals) {
831831
* @throws IOException if permissions update fails
832832
*/
833833
public static void chmod(String path, int octals) throws IOException {
834-
Files.setPosixFilePermissions(Paths.get(path), getPermissions(octals));
834+
if(!WINDOWS) {
835+
Files.setPosixFilePermissions(Paths.get(path), getPermissions(octals));
836+
}
835837
}
836838
}

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
136136
continue
137137

138138
user_name = find_user_name(property_name, model_dictionary)
139-
secret_names = property_name.lower().split('.')
140-
secret_name = '-'.join(secret_names[:-1])
139+
secret_name = _create_secret_name(property_name)
141140

142141
if user_name is None:
143142
message = exception_helper.get_message("WLSDPLY-01663", PASSWORD_TAG, secret_name)
@@ -155,6 +154,7 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
155154
k8s_script.close()
156155
FileUtils.chmod(k8s_file, 0750)
157156

157+
158158
def format_as_secret_token(variable_name, target_config):
159159
"""
160160
Format the variable as a secret name token for use in a model.
@@ -176,8 +176,9 @@ def format_as_secret_token(variable_name, target_config):
176176
return '@@SECRET:%s:%s@@' % (secret_name, admin_token)
177177

178178
# for paired and single secrets, password key is always named "password"
179-
secret_name = "password"
180-
return normal_secret_format % ('-'.join(name_lower_tokens[:-1]), secret_name)
179+
secret_key = "password"
180+
secret_name = _create_secret_name(variable_name)
181+
return normal_secret_format % (secret_name, secret_key)
181182

182183

183184
def get_secret_name_for_location(location, domain_uid, aliases):
@@ -190,8 +191,8 @@ def get_secret_name_for_location(location, domain_uid, aliases):
190191
:return: the secret name
191192
"""
192193
variable_name = variable_injector_functions.format_variable_name(location, '(none)', aliases)
193-
name_lower_tokens = variable_name.lower().split('.')
194-
return domain_uid + '-' + '-'.join(name_lower_tokens[:-1])
194+
secret_name = _create_secret_name(variable_name)
195+
return domain_uid + '-' + secret_name
195196

196197

197198
def create_additional_output(model, model_context, aliases, exception_type):
@@ -243,6 +244,27 @@ def find_user_name(property_name, model_dictionary):
243244
return None
244245

245246

247+
def _create_secret_name(variable_name):
248+
"""
249+
Return the secret name derived from the specified property variable name.
250+
Skip the last element of the variable name, which corresponds to the attribute.
251+
Follow limitations for secret names: only alphanumeric and "-", must start and end with alphanumeric.
252+
For example, "JDBC.Generic1.PasswordEncrypted" becomes "jdbc-generic1".
253+
:param variable_name: the variable name to be converted
254+
:return: the derived secret name
255+
"""
256+
variable_keys = variable_name.lower().split('.')
257+
secret_keys = []
258+
for variable_key in variable_keys[:-1]:
259+
secret_key = re.sub('[^a-z0-9-]', '-', variable_key)
260+
secret_keys.append(secret_key)
261+
262+
# rejoin with hyphens, remove leading and trailing hyphens from final name.
263+
# if empty, just return "x".
264+
secret = '-'.join(secret_keys).strip('-')
265+
return secret or 'x'
266+
267+
246268
def _is_paired_secret(property_name):
247269
"""
248270
Determine if the property name is part of a paired secret with .username and .password .

core/src/main/targetconfigs/k8s/k8s_operator_filter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __cleanup_topology(model):
3838
if topology.has_key('Server'):
3939
servers = topology['Server']
4040
for server in servers:
41-
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled']:
41+
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled', 'ServerStart']:
4242
if servers[server].has_key(delthis):
4343
del servers[server][delthis]
4444

@@ -53,6 +53,9 @@ def __cleanup_topology(model):
5353
server_templates = topology['ServerTemplate']
5454
for server_template in server_templates:
5555
server_templates[server_template]['AutoMigrationEnabled'] = False
56+
for delthis in ['ServerStart']:
57+
if server_templates[server_template].has_key(delthis):
58+
del server_templates[server_template][delthis]
5659
else:
5760
topology['ServerTemplate'] = {}
5861
server_templates = topology['ServerTemplate']

core/src/main/targetconfigs/vz/vz_filter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __cleanup_topology(model):
4242
if topology.has_key('Server'):
4343
servers = topology['Server']
4444
for server in servers:
45-
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled']:
45+
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled', 'ServerStart']:
4646
if servers[server].has_key(delthis):
4747
del servers[server][delthis]
4848

@@ -57,6 +57,9 @@ def __cleanup_topology(model):
5757
server_templates = topology['ServerTemplate']
5858
for server_template in server_templates:
5959
server_templates[server_template]['AutoMigrationEnabled'] = False
60+
for delthis in ['ServerStart']:
61+
if server_templates[server_template].has_key(delthis):
62+
del server_templates[server_template][delthis]
6063
else:
6164
topology['ServerTemplate'] = {}
6265
server_templates = topology['ServerTemplate']

core/src/main/targetconfigs/wko/wko_operator_filter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __cleanup_topology(model):
3838
if topology.has_key('Server'):
3939
servers = topology['Server']
4040
for server in servers:
41-
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled']:
41+
for delthis in ['Machine', 'CandidateMachine', 'AutoMigrationEnabled', 'ServerStart']:
4242
if servers[server].has_key(delthis):
4343
del servers[server][delthis]
4444

@@ -53,6 +53,9 @@ def __cleanup_topology(model):
5353
server_templates = topology['ServerTemplate']
5454
for server_template in server_templates:
5555
server_templates[server_template]['AutoMigrationEnabled'] = False
56+
for delthis in ['ServerStart']:
57+
if server_templates[server_template].has_key(delthis):
58+
del server_templates[server_template][delthis]
5659
else:
5760
topology['ServerTemplate'] = {}
5861
server_templates = topology['ServerTemplate']

core/src/test/python/target_configuration_helper_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,19 @@ def testSecretWithoutWlsCredName(self):
5252
self.assertEqual('@@SECRET:@@ENV:DOMAIN_UID@@-something:password@@',
5353
HELPER.format_as_secret_token('something.else', self.target_with_cred_name))
5454

55+
56+
def testCreateSecretName(self):
57+
self.assertEqual('jdbc-generic1', HELPER._create_secret_name('JDBC.Generic1.PasswordEncrypted'))
58+
59+
self.assertEqual('jdbc--weblogic--credentials',
60+
HELPER._create_secret_name('JDBC.(WebLogic)-credentials.PasswordEncrypted'))
61+
62+
self.assertEqual('jdbc--why', HELPER._create_secret_name('JDBC.-why?-.PasswordEncrypted'))
63+
64+
self.assertEqual('jdbc-abc', HELPER._create_secret_name('-JDBC.abc-.PasswordEncrypted'))
65+
66+
self.assertEqual('x', HELPER._create_secret_name('--??!!'))
67+
68+
5569
if __name__ == '__main__':
5670
unittest.main()

0 commit comments

Comments
 (0)