Skip to content

Commit ed56400

Browse files
jshum2479Johnny
authored andcommitted
refactoring
1 parent e78b39b commit ed56400

File tree

3 files changed

+254
-168
lines changed

3 files changed

+254
-168
lines changed

core/src/main/python/create.py

Lines changed: 18 additions & 115 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
@@ -54,6 +48,7 @@
5448
from wlsdeploy.util.model_context import ModelContext
5549
from wlsdeploy.util.model_translator import FileToPython
5650
from wlsdeploy.util.weblogic_helper import WebLogicHelper
51+
from wlsdeploy.tool.create import atp_helper
5752

5853
wlst_extended.wlst_functions = globals()
5954

@@ -282,15 +277,9 @@ def __process_rcu_args(optional_arg_map, domain_type, domain_typedef):
282277
ex.setExitCode(CommandLineArgUtil.USAGE_ERROR_EXIT_CODE)
283278
__logger.throwing(ex, class_name=_class_name, method_name=_method_name)
284279
raise ex
285-
# elif CommandLineArgUtil.ATP_PROPERTIES_FILE_SWITCH in optional_arg_map:
286-
# pass
287-
# else:
288-
# ex = exception_helper.create_cla_exception('WLSDPLY-12408', domain_type, rcu_schema_count,
289-
# CommandLineArgUtil.RCU_DB_SWITCH,
290-
# CommandLineArgUtil.RCU_PREFIX_SWITCH)
291-
# ex.setExitCode(CommandLineArgUtil.USAGE_ERROR_EXIT_CODE)
292-
# __logger.throwing(ex, class_name=_class_name, method_name=_method_name)
293-
# raise ex
280+
281+
# Delay the checking later for rcu related parameters
282+
294283
return
295284

296285

@@ -347,86 +336,22 @@ def validate_model(model_dictionary, model_context, aliases):
347336
__clean_up_temp_files()
348337
tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
349338

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

421340
def validateRCUArgsAndModel(model_context, model):
422341
has_atpdbinfo = 0
423342
domain_info = model[model_constants.DOMAIN_INFO]
424343
if model_constants.RCU_DB_INFO in domain_info:
425-
has_tns_admin = model_constants.DRIVER_PARAMS_NET_TNS_ADMIN in domain_info[
426-
model_constants.RCU_DB_INFO]
427-
has_regular_db = model_constants.RCU_DB_CONN in domain_info[model_constants.RCU_DB_INFO]
344+
rcu_db_info = domain_info[model_constants.RCU_DB_INFO]
345+
has_tns_admin = atp_helper.has_tns_admin(rcu_db_info)
428346

429-
if model_constants.ATP_TNS_ENTRY in domain_info[model_constants.RCU_DB_INFO]:
347+
# has_tns_admin = model_constants.DRIVER_PARAMS_NET_TNS_ADMIN in domain_info[
348+
# model_constants.RCU_DB_INFO]
349+
# has_regular_db = model_constants.RCU_DB_CONN in domain_info[model_constants.RCU_DB_INFO]
350+
351+
has_regular_db = atp_helper.is_regular_db(rcu_db_info)
352+
353+
# if model_constants.ATP_TNS_ENTRY in domain_info[model_constants.RCU_DB_INFO]:
354+
if atp_helper.has_atpdbinfo(rcu_db_info):
430355
has_atpdbinfo = 1
431356

432357
if model_context.get_archive_file_name() and not has_regular_db:
@@ -436,16 +361,9 @@ def validateRCUArgsAndModel(model_context, model):
436361
if not has_tns_admin:
437362
# extract the wallet first
438363
archive_file = WLSDeployArchive(model_context.get_archive_file_name())
439-
atp_path = archive_file.getATPWallet()
440-
if atp_path and model[model_constants.TOPOLOGY]['Name']:
441-
domain_path = model_context.get_domain_parent_dir() + os.sep + model[model_constants.TOPOLOGY][
442-
'Name']
443-
extract_path = domain_path + os.sep + 'atpwallet'
444-
extract_dir = File(extract_path)
445-
extract_dir.mkdirs()
446-
wallet_zip = archive_file.extractFile(atp_path, File(domain_path))
447-
unzip_atp_wallet(wallet_zip, extract_path)
448-
os.remove(wallet_zip)
364+
atp_wallet_zipentry = archive_file.getATPWallet()
365+
if atp_wallet_zipentry and model[model_constants.TOPOLOGY]['Name']:
366+
extract_path = atp_helper.extract_walletzip(model, model_context, archive_file, atp_wallet_zipentry)
449367
# update the model to add the tns_admin
450368
model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
451369
model_constants.DRIVER_PARAMS_NET_TNS_ADMIN] = extract_path
@@ -494,8 +412,6 @@ def main(args):
494412
model_file = model_context.get_model_file()
495413
try:
496414
model = FileToPython(model_file, True).parse()
497-
# if model_context.get_archive_file():
498-
# os.environ['oracle.net.fanEnabled'] = 'false'
499415
except TranslateException, te:
500416
__logger.severe('WLSDPLY-20009', _program_name, model_file, te.getLocalizedMessage(), error=te,
501417
class_name=_class_name, method_name=_method_name)
@@ -519,27 +435,14 @@ def main(args):
519435
if filter_helper.apply_filters(model, "create"):
520436
# if any filters were applied, re-validate the model
521437
validate_model(model, model_context, aliases)
522-
523438
try:
524439

525440
has_atp = validateRCUArgsAndModel(model_context, model)
526441
creator = DomainCreator(model, model_context, aliases)
527442
creator.create()
528443

529444
if has_atp:
530-
#print model[model_constants.DOMAIN_INFO][model_constants.ATP_DB_INFO]
531-
tns_admin = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
532-
model_constants.DRIVER_PARAMS_NET_TNS_ADMIN]
533-
keystore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
534-
model_constants.DRIVER_PARAMS_KEYSTOREPWD_PROPERTY]
535-
536-
truststore_password = model[model_constants.DOMAIN_INFO][model_constants.RCU_DB_INFO][
537-
model_constants.DRIVER_PARAMS_TRUSTSTOREPWD_PROPERTY]
538-
539-
jsp_config = model_context.get_domain_home() + '/config/fmwconfig/jps-config.xml'
540-
jsp_config_jse = model_context.get_domain_home() + '/config/fmwconfig/jps-config-jse.xml'
541-
set_ssl_properties(jsp_config, tns_admin, keystore_password, truststore_password)
542-
set_ssl_properties(jsp_config_jse, tns_admin, keystore_password, truststore_password)
445+
atp_helper.fix_jsp_config(model, model_context)
543446

544447
except (IOException | CreateException), ex:
545448
__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)