Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: jans-linux-setup option reset-rdbm-db #2413

Merged
merged 6 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jans-linux-setup/jans_setup/setup_app/installers/rdbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(self):
def install(self):
self.qchar = '`' if Config.rdbm_type in ('mysql', 'spanner') else '"'
self.local_install()
if Config.rdbm_install_type == InstallTypes.REMOTE and base.argsp.reset_rdbm_db:
self.reset_rdbm_db()
jans_schema_files = []
self.jans_attributes = []
for jans_schema_fn in ('jans_schema.json', 'custom_schema.json'):
Expand All @@ -45,6 +47,13 @@ def install(self):
self.create_indexes()
self.rdbmProperties()

def reset_rdbm_db(self):
self.logIt("Resetting DB {}".format(Config.rdbm_db))
self.dbUtils.metadata.reflect(self.dbUtils.engine)
self.dbUtils.metadata.drop_all(self.dbUtils.engine)
self.dbUtils.session.commit()
self.dbUtils.metadata.clear()

def local_install(self):
if not Config.rdbm_password:
Config.rdbm_password = self.getPW()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
parser.add_argument('-rdbm-port', help="RDBM port")
parser.add_argument('-rdbm-db', help="RDBM database")
parser.add_argument('-rdbm-host', help="RDBM host")
parser.add_argument('--reset-rdbm-db', help="Deletes all tables on target database. Warning! You will lose all data on target database.", action='store_true')

parser.add_argument('--shell', help="Drop into interactive shell before starting installation", action='store_true')
parser.add_argument('--dump-config-on-error', help="Dump configuration on error", action='store_true')
Expand Down
58 changes: 30 additions & 28 deletions jans-linux-setup/jans_setup/setup_app/utils/properties_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import psycopg2
import inspect
import ldap3
import tempfile

from setup_app import paths
from setup_app.messages import msg
Expand Down Expand Up @@ -69,7 +70,7 @@ def getPrompt(self, prompt, defaultValue=None, itype=None, indent=0):

except KeyboardInterrupt:
sys.exit()
except:
except Exception:
return None

def check_properties(self):
Expand All @@ -85,9 +86,9 @@ def check_properties(self):
while not Config.orgName:
Config.orgName = input('Organization Name: ').strip()
while not Config.countryCode:
testCode = input('2 Character Country Code: ').strip()
if len(testCode) == 2:
Config.countryCode = testCode
test_code = input('2 Character Country Code: ').strip()
if len(test_code) == 2:
Config.countryCode = test_code
else:
print('Country code should only be two characters. Try again\n')
while not Config.city:
Expand All @@ -98,7 +99,7 @@ def check_properties(self):
tld = None
try:
tld = ".".join(self.hostname.split(".")[-2:])
except:
except Exception:
tld = Config.hostname
Config.admin_email = "support@%s" % tld

Expand Down Expand Up @@ -180,7 +181,6 @@ def load_properties(self, prop_file, no_update=[]):

no_update += ['noPrompt', 'jre_version', 'node_version', 'jetty_version', 'jython_version', 'jreDestinationPath']

cb_install = False
map_db = []

if prop_file.endswith('.enc'):
Expand All @@ -192,7 +192,7 @@ def load_properties(self, prop_file, no_update=[]):

try:
p = base.read_properties_file(prop_file)
except:
except Exception:
self.logIt("Error loading properties", True)

if p.get('ldap_type') == 'openldap':
Expand All @@ -219,14 +219,14 @@ def load_properties(self, prop_file, no_update=[]):
mapping_locations = json.loads(p[prop])
setattr(Config, prop, mapping_locations)
for l in mapping_locations:
if not mapping_locations[l] in map_db:
if mapping_locations[l] not in map_db:
map_db.append(mapping_locations[l])

if p[prop] == 'True':
setattr(Config, prop, True)
elif p[prop] == 'False':
setattr(Config, prop, False)
except:
except Exception:
self.logIt("Error loading property %s" % prop)

if prop_file.endswith('-DEC~'):
Expand All @@ -245,7 +245,7 @@ def load_properties(self, prop_file, no_update=[]):
else:
Config.opendj_install = InstallTypes.NONE

if map_db and not 'ldap' in map_db:
if map_db and 'ldap' not in map_db:
Config.opendj_install = InstallTypes.NONE

if 'couchbase' in map_db:
Expand All @@ -260,12 +260,12 @@ def load_properties(self, prop_file, no_update=[]):

if Config.cb_install == InstallTypes.LOCAL:
available_backends = self.getBackendTypes()
if not 'couchbase' in available_backends:
if 'couchbase' not in available_backends:
print("Couchbase package is not available exiting.")
sys.exit(1)


if (not 'cb_password' in properties_list) and Config.cb_install:
if ('cb_password' not in properties_list) and Config.cb_install:
Config.cb_password = p.get('ldapPass')

if Config.cb_install == InstallTypes.REMOTE:
Expand Down Expand Up @@ -295,7 +295,7 @@ def save_properties(self, prop_fn=None, obj=None):

self.logIt('Saving properties to %s' % prop_fn)

def getString(value):
def get_string(value):
if isinstance(value, str):
return str(value).strip()
elif isinstance(value, bool) or isinstance(value, int) or isinstance(value, float):
Expand All @@ -318,7 +318,7 @@ def getString(value):
if obj_name == 'mapping_locations':
p[obj_name] = json.dumps(obj)
else:
value = getString(obj)
value = get_string(obj)
if value != '':
p[obj_name] = value

Expand All @@ -327,7 +327,7 @@ def getString(value):

self.run([paths.cmd_chmod, '600', prop_fn])

# TODO: uncomment later
# uncomment later
return

self.run([paths.cmd_openssl, 'enc', '-aes-256-cbc', '-in', prop_fn, '-out', prop_fn+'.enc', '-k', Config.admin_password])
Expand All @@ -338,7 +338,7 @@ def getString(value):

self.run(['rm', '-f', prop_fn])

except:
except Exception:
self.logIt("Error saving properties", True)

def getBackendTypes(self):
Expand Down Expand Up @@ -436,11 +436,16 @@ def check_remote_ldap(self, ldap_host, ldap_binddn, ldap_password):
def check_oxd_server(self, oxd_url, error_out=True, log_error=True):

oxd_url = os.path.join(oxd_url, 'health-check')

ctx = ssl.create_default_context()
ctx.check_hostname = True
ctx.verify_mode = ssl.CERT_NONE

try:
result = urllib.request.urlopen(
oxd_url,
timeout = 2,
context=ssl._create_unverified_context()
timeout=2,
context=ctx
)
if result.code == 200:
oxd_status = json.loads(result.read().decode())
Expand All @@ -459,12 +464,13 @@ def check_oxd_server(self, oxd_url, error_out=True, log_error=True):
def check_oxd_ssl_cert(self, oxd_hostname, oxd_port):

oxd_cert = ssl.get_server_certificate((oxd_hostname, oxd_port))
oxd_crt_fn = '/tmp/oxd_{}.crt'.format(str(uuid.uuid4()))
self.writeFile(oxd_crt_fn, oxd_cert)
ssl_subjects = self.get_ssl_subject(oxd_crt_fn)

if ssl_subjects.get('commonName') != oxd_hostname:
return ssl_subjects
with tempfile.TemporaryDirectory() as tmpdirname:
oxd_crt_fn = os.path.join(tmpdirname, 'oxd.crt')
self.writeFile(oxd_crt_fn, oxd_cert)
ssl_subjects = self.get_ssl_subject(oxd_crt_fn)

if ssl_subjects.get('commonName') != oxd_hostname:
return ssl_subjects


def promptForBackendMappings(self):
Expand Down Expand Up @@ -769,10 +775,6 @@ def prompt_for_backend(self):
else:
Config.rdbm_port = 5432
Config.rdbm_type = 'pgsql'
if not Config.rdbm_host:

if not Config.rdbm_password:
Config.rdbm_password = self.getPW(special='.*=+-()[]{}')

elif backend_type_str in (BackendStrings.REMOTE_MYSQL, BackendStrings.REMOTE_PGSQL):
Config.opendj_install = InstallTypes.NONE
Expand Down