Skip to content

Commit 158abcb

Browse files
committed
refactoring
1 parent c657e99 commit 158abcb

File tree

3 files changed

+254
-169
lines changed

3 files changed

+254
-169
lines changed

core/src/main/python/create.py

Lines changed: 18 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77
import javaos as os
88
import sys
99

10-
from xml.dom.minidom import parse
1110

1211
from java.io import IOException
1312
from java.lang import IllegalArgumentException
1413
from java.lang import IllegalStateException
1514
from java.lang import String
1615
from java.io import File
17-
from java.nio.file import Files
18-
from java.io import FileInputStream
19-
from java.io import FileOutputStream
20-
from java.util.zip import ZipInputStream
21-
import jarray
2216

2317
from oracle.weblogic.deploy.create import CreateException
2418
from oracle.weblogic.deploy.deploy import DeployException
@@ -52,7 +46,7 @@
5246
from wlsdeploy.util.model_context import ModelContext
5347
from wlsdeploy.util.model_translator import FileToPython
5448
from wlsdeploy.util.weblogic_helper import WebLogicHelper
55-
49+
from wlsdeploy.tool.create import atp_helper
5650

5751
_program_name = 'createDomain'
5852
_class_name = 'create'
@@ -277,15 +271,9 @@ def __process_rcu_args(optional_arg_map, domain_type, domain_typedef):
277271
ex.setExitCode(CommandLineArgUtil.USAGE_ERROR_EXIT_CODE)
278272
__logger.throwing(ex, class_name=_class_name, method_name=_method_name)
279273
raise ex
280-
# elif CommandLineArgUtil.ATP_PROPERTIES_FILE_SWITCH in optional_arg_map:
281-
# pass
282-
# else:
283-
# ex = exception_helper.create_cla_exception('WLSDPLY-12408', domain_type, rcu_schema_count,
284-
# CommandLineArgUtil.RCU_DB_SWITCH,
285-
# CommandLineArgUtil.RCU_PREFIX_SWITCH)
286-
# ex.setExitCode(CommandLineArgUtil.USAGE_ERROR_EXIT_CODE)
287-
# __logger.throwing(ex, class_name=_class_name, method_name=_method_name)
288-
# raise ex
274+
275+
# Delay the checking later for rcu related parameters
276+
289277
return
290278

291279

@@ -342,86 +330,22 @@ def validate_model(model_dictionary, model_context, aliases):
342330
__clean_up_temp_files()
343331
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
344332

345-
def set_ssl_properties(xmlDoc, atp_creds_path, keystore_password, truststore_password):
346-
'''
347-
Add SSL config properties to the specified XML document.
348-
:param xmlDoc: The XML document
349-
:param db_keystore_password: The DB keystore/truststore password (assumed to be same)
350-
:return: void
351-
'''
352-
DOMTree = parse(xmlDoc)
353-
collection = DOMTree.documentElement
354-
props = collection.getElementsByTagName("propertySet")
355-
356-
for prop in props:
357-
if prop.getAttribute('name') == 'props.db.1':
358-
set_property(DOMTree, prop, 'javax.net.ssl.trustStoreType', 'JKS')
359-
set_property(DOMTree, prop, 'javax.net.ssl.trustStore', atp_creds_path + '/truststore.jks')
360-
set_property(DOMTree, prop, 'oracle.net.tns_admin', atp_creds_path)
361-
set_property(DOMTree, prop, 'javax.net.ssl.keyStoreType', 'JKS')
362-
set_property(DOMTree, prop, 'javax.net.ssl.keyStore', atp_creds_path + '/keystore.jks')
363-
set_property(DOMTree, prop, 'javax.net.ssl.keyStorePassword', keystore_password)
364-
set_property(DOMTree, prop, 'javax.net.ssl.trustStorePassword', truststore_password)
365-
set_property(DOMTree, prop, 'oracle.net.ssl_server_dn_match', 'true')
366-
set_property(DOMTree, prop, 'oracle.net.ssl_version', '1.2')
367-
# Persist the changes in the xml file
368-
file_handle = open(xmlDoc,"w")
369-
DOMTree.writexml(file_handle)
370-
file_handle.close()
371-
372-
def set_property(DOMTree, prop, name, value):
373-
'''
374-
Sets the property child element under prop parent node.
375-
:param DOMTree: The DOM document handle
376-
:param prop: The propertySet parent handle
377-
:param name: The property name
378-
:param value: The property value
379-
:return: void
380-
'''
381-
property = DOMTree.createElement('property')
382-
property.setAttribute("name", name)
383-
property.setAttribute("value", value)
384-
prop.appendChild(property)
385-
newline = DOMTree.createTextNode('\n')
386-
prop.appendChild(newline)
387-
388-
389-
def unzip_atp_wallet(wallet_file, location):
390-
391-
if not os.path.exists(location):
392-
os.mkdir(location)
393-
394-
buffer = jarray.zeros(1024, "b")
395-
fis = FileInputStream(wallet_file)
396-
zis = ZipInputStream(fis)
397-
ze = zis.getNextEntry()
398-
while ze:
399-
fileName = ze.getName()
400-
newFile = File(location + File.separator + fileName)
401-
File(newFile.getParent()).mkdirs()
402-
fos = FileOutputStream(newFile)
403-
len = zis.read(buffer)
404-
while len > 0:
405-
fos.write(buffer, 0, len)
406-
len = zis.read(buffer)
407-
408-
fos.close()
409-
zis.closeEntry()
410-
ze = zis.getNextEntry()
411-
zis.closeEntry()
412-
zis.close()
413-
fis.close()
414-
415333

416334
def validateRCUArgsAndModel(model_context, model):
417335
has_atpdbinfo = 0
418336
domain_info = model[model_constants.DOMAIN_INFO]
419337
if model_constants.RCU_DB_INFO in domain_info:
420-
has_tns_admin = model_constants.DRIVER_PARAMS_NET_TNS_ADMIN in domain_info[
421-
model_constants.RCU_DB_INFO]
422-
has_regular_db = model_constants.RCU_DB_CONN in domain_info[model_constants.RCU_DB_INFO]
338+
rcu_db_info = domain_info[model_constants.RCU_DB_INFO]
339+
has_tns_admin = atp_helper.has_tns_admin(rcu_db_info)
423340

424-
if model_constants.ATP_TNS_ENTRY in domain_info[model_constants.RCU_DB_INFO]:
341+
# has_tns_admin = model_constants.DRIVER_PARAMS_NET_TNS_ADMIN in domain_info[
342+
# model_constants.RCU_DB_INFO]
343+
# has_regular_db = model_constants.RCU_DB_CONN in domain_info[model_constants.RCU_DB_INFO]
344+
345+
has_regular_db = atp_helper.is_regular_db(rcu_db_info)
346+
347+
# if model_constants.ATP_TNS_ENTRY in domain_info[model_constants.RCU_DB_INFO]:
348+
if atp_helper.has_atpdbinfo(rcu_db_info):
425349
has_atpdbinfo = 1
426350

427351
if model_context.get_archive_file_name() and not has_regular_db:
@@ -431,16 +355,9 @@ def validateRCUArgsAndModel(model_context, model):
431355
if not has_tns_admin:
432356
# extract the wallet first
433357
archive_file = WLSDeployArchive(model_context.get_archive_file_name())
434-
atp_path = archive_file.getATPWallet()
435-
if atp_path and model[model_constants.TOPOLOGY]['Name']:
436-
domain_path = model_context.get_domain_parent_dir() + os.sep + model[model_constants.TOPOLOGY][
437-
'Name']
438-
extract_path = domain_path + os.sep + 'atpwallet'
439-
extract_dir = File(extract_path)
440-
extract_dir.mkdirs()
441-
wallet_zip = archive_file.extractFile(atp_path, File(domain_path))
442-
unzip_atp_wallet(wallet_zip, extract_path)
443-
os.remove(wallet_zip)
358+
atp_wallet_zipentry = archive_file.getATPWallet()
359+
if atp_wallet_zipentry and model[model_constants.TOPOLOGY]['Name']:
360+
extract_path = atp_helper.extract_walletzip(model, model_context, archive_file, atp_wallet_zipentry)
444361
# update the model to add the tns_admin
445362
model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
446363
model_constants.DRIVER_PARAMS_NET_TNS_ADMIN] = extract_path
@@ -489,8 +406,6 @@ def main(args):
489406
model_file = model_context.get_model_file()
490407
try:
491408
model = FileToPython(model_file, True).parse()
492-
# if model_context.get_archive_file():
493-
# os.environ['oracle.net.fanEnabled'] = 'false'
494409
except TranslateException, te:
495410
__logger.severe('WLSDPLY-20009', _program_name, model_file, te.getLocalizedMessage(), error=te,
496411
class_name=_class_name, method_name=_method_name)
@@ -514,27 +429,14 @@ def main(args):
514429
if filter_helper.apply_filters(model, "create"):
515430
# if any filters were applied, re-validate the model
516431
validate_model(model, model_context, aliases)
517-
518432
try:
519433

520434
has_atp = validateRCUArgsAndModel(model_context, model)
521435
creator = DomainCreator(model, model_context, aliases)
522436
creator.create()
523437

524438
if has_atp:
525-
#print model[model_constants.DOMAIN_INFO][model_constants.ATP_DB_INFO]
526-
tns_admin = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
527-
model_constants.DRIVER_PARAMS_NET_TNS_ADMIN]
528-
keystore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
529-
model_constants.DRIVER_PARAMS_KEYSTOREPWD_PROPERTY]
530-
531-
truststore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
532-
model_constants.DRIVER_PARAMS_TRUSTSTOREPWD_PROPERTY]
533-
534-
jsp_config = model_context.get_domain_home() + '/config/fmwconfig/jps-config.xml'
535-
jsp_config_jse = model_context.get_domain_home() + '/config/fmwconfig/jps-config-jse.xml'
536-
set_ssl_properties(jsp_config, tns_admin, keystore_password, truststore_password)
537-
set_ssl_properties(jsp_config_jse, tns_admin, keystore_password, truststore_password)
439+
atp_helper.fix_jsp_config(model, model_context)
538440

539441
except (IOException | CreateException), ex:
540442
__logger.severe('WLSDPLY-12409', _program_name, ex.getLocalizedMessage(), error=ex,
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
"""
2+
Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
6+
"""
7+
8+
import os, re
9+
10+
from xml.dom.minidom import parse
11+
12+
from java.io import File
13+
from java.io import FileInputStream
14+
from java.io import FileOutputStream
15+
from java.util.zip import ZipInputStream
16+
import jarray
17+
18+
from wlsdeploy.aliases import model_constants
19+
20+
21+
def set_ssl_properties(xmlDoc, atp_creds_path, keystore_password, truststore_password):
22+
'''
23+
Add SSL config properties to the specified XML document.
24+
:param xmlDoc: The XML document
25+
:param db_keystore_password: The DB keystore/truststore password (assumed to be same)
26+
:return: void
27+
'''
28+
DOMTree = parse(xmlDoc)
29+
collection = DOMTree.documentElement
30+
props = collection.getElementsByTagName("propertySet")
31+
32+
for prop in props:
33+
if prop.getAttribute('name') == 'props.db.1':
34+
set_property(DOMTree, prop, 'javax.net.ssl.trustStoreType', 'JKS')
35+
set_property(DOMTree, prop, 'javax.net.ssl.trustStore', atp_creds_path + '/truststore.jks')
36+
set_property(DOMTree, prop, 'oracle.net.tns_admin', atp_creds_path)
37+
set_property(DOMTree, prop, 'javax.net.ssl.keyStoreType', 'JKS')
38+
set_property(DOMTree, prop, 'javax.net.ssl.keyStore', atp_creds_path + '/keystore.jks')
39+
set_property(DOMTree, prop, 'javax.net.ssl.keyStorePassword', keystore_password)
40+
set_property(DOMTree, prop, 'javax.net.ssl.trustStorePassword', truststore_password)
41+
set_property(DOMTree, prop, 'oracle.net.ssl_server_dn_match', 'true')
42+
set_property(DOMTree, prop, 'oracle.net.ssl_version', '1.2')
43+
# Persist the changes in the xml file
44+
file_handle = open(xmlDoc,"w")
45+
DOMTree.writexml(file_handle)
46+
file_handle.close()
47+
48+
def set_property(DOMTree, prop, name, value):
49+
'''
50+
Sets the property child element under prop parent node.
51+
:param DOMTree: The DOM document handle
52+
:param prop: The propertySet parent handle
53+
:param name: The property name
54+
:param value: The property value
55+
:return: void
56+
'''
57+
property = DOMTree.createElement('property')
58+
property.setAttribute("name", name)
59+
property.setAttribute("value", value)
60+
prop.appendChild(property)
61+
newline = DOMTree.createTextNode('\n')
62+
prop.appendChild(newline)
63+
64+
65+
def unzip_atp_wallet(wallet_file, location):
66+
67+
if not os.path.exists(location):
68+
os.mkdir(location)
69+
70+
buffer = jarray.zeros(1024, "b")
71+
fis = FileInputStream(wallet_file)
72+
zis = ZipInputStream(fis)
73+
ze = zis.getNextEntry()
74+
while ze:
75+
fileName = ze.getName()
76+
newFile = File(location + File.separator + fileName)
77+
File(newFile.getParent()).mkdirs()
78+
fos = FileOutputStream(newFile)
79+
len = zis.read(buffer)
80+
while len > 0:
81+
fos.write(buffer, 0, len)
82+
len = zis.read(buffer)
83+
84+
fos.close()
85+
zis.closeEntry()
86+
ze = zis.getNextEntry()
87+
zis.closeEntry()
88+
zis.close()
89+
fis.close()
90+
91+
def fix_jsp_config(model, model_context):
92+
#print model[model_constants.DOMAIN_INFO][model_constants.ATP_DB_INFO]
93+
tns_admin = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
94+
model_constants.DRIVER_PARAMS_NET_TNS_ADMIN]
95+
keystore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
96+
model_constants.DRIVER_PARAMS_KEYSTOREPWD_PROPERTY]
97+
98+
truststore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
99+
model_constants.DRIVER_PARAMS_TRUSTSTOREPWD_PROPERTY]
100+
101+
jsp_config = model_context.get_domain_home() + '/config/fmwconfig/jps-config.xml'
102+
jsp_config_jse = model_context.get_domain_home() + '/config/fmwconfig/jps-config-jse.xml'
103+
set_ssl_properties(jsp_config, tns_admin, keystore_password, truststore_password)
104+
set_ssl_properties(jsp_config_jse, tns_admin, keystore_password, truststore_password)
105+
106+
107+
def get_atp_connect_string(tnsnames_ora_path, tns_sid_name):
108+
109+
110+
try:
111+
f = open(tnsnames_ora_path, "r+")
112+
try:
113+
text = f.read()
114+
finally:
115+
f.close()
116+
# The regex below looks for the <dbName>_<level><whitespaces>=<whitespaces> and grabs the
117+
# tnsConnectString from the current and the next line as tnsnames.ora file has the connect string
118+
# being printed on 2 lines.
119+
pattern = tns_sid_name + '\s*=\s*([(].*\n.*)'
120+
match = re.search(pattern, text)
121+
if match:
122+
str = match.group(1)
123+
tnsConnectString=str.replace('\r','').replace('\n','')
124+
str = format_connect_string(tnsConnectString)
125+
return str
126+
except:
127+
pass
128+
129+
return None
130+
131+
132+
def format_connect_string(connect_string):
133+
"""
134+
Formats connect string for ATP DB by removing unwanted whitespaces.
135+
Input:
136+
(description= (address=(protocol=tcps)(port=1522)(host=adb-preprod.us-phoenix-1.oraclecloud.com))(connect_data=(service_name=uq7p1eavz8qlvts_watsh01_medium.atp.oraclecloud.com))(security=(ssl_server_cert_dn= "CN=adwc-preprod.uscom-east-1.oraclecloud.com,OU=Oracle BMCS US,O=Oracle Corporation,L=Redwood City,ST=California,C=US")) )
137+
Output Parts:
138+
1. (description=(address=(protocol=tcps)(port=1522)(host=adb-preprod.us-phoenix-1.oraclecloud.com))(connect_data=(service_name=uq7p1eavz8qlvts_watsh01_medium.atp.oraclecloud.com))(security=(
139+
2. ssl_server_cert_dn=
140+
3. "CN=adwc-preprod.uscom-east-1.oraclecloud.com,OU=Oracle BMCS US,O=Oracle Corporation,L=Redwood City,ST=California,C=US"
141+
4. )))
142+
:param connect_string:
143+
:return:
144+
"""
145+
pattern = "(.*)(ssl_server_cert_dn=)\s*(\".*\")(.*)"
146+
match = re.search(pattern, connect_string)
147+
148+
if match:
149+
part1 = match.group(1).replace(' ','')
150+
part2 = match.group(2).replace(' ', '')
151+
# We don't want to remove the spaces from serverDN part.
152+
part3 = match.group(3)
153+
part4 = match.group(4).replace(' ', '')
154+
connect_string = "%s%s%s%s" % (part1, part2, part3, part4)
155+
156+
return connect_string
157+
158+
# has_tns_admin is used to find the extract location if it is already extracted by the user
159+
# its an optional field, so insufficient to determine whether it has atp
160+
161+
162+
def has_tns_admin(rcu_db_info):
163+
return model_constants.DRIVER_PARAMS_NET_TNS_ADMIN in rcu_db_info
164+
165+
166+
def has_atpdbinfo(rcu_db_info):
167+
return model_constants.ATP_TNS_ENTRY in rcu_db_info
168+
169+
170+
def is_regular_db(rcu_db_info):
171+
return model_constants.RCU_DB_CONN in rcu_db_info
172+
173+
174+
def extract_walletzip(model, model_context, archive_file, atp_path):
175+
domain_path = model_context.get_domain_parent_dir() + os.sep + model[model_constants.TOPOLOGY][
176+
'Name']
177+
extract_path = domain_path + os.sep + 'atpwallet'
178+
extract_dir = File(extract_path)
179+
extract_dir.mkdirs()
180+
wallet_zip = archive_file.extractFile(atp_path, File(domain_path))
181+
unzip_atp_wallet(wallet_zip, extract_path)
182+
os.remove(wallet_zip)
183+
return extract_path
184+
# update the model to add the tns_admin

0 commit comments

Comments
 (0)