Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "rda_python_common"
version = "2.0.2"
version = "2.0.3"
authors = [
{ name="Zaihua Ji", email="[email protected]" },
]
Expand Down Expand Up @@ -33,4 +33,3 @@ pythonpath = [

[project.scripts]
pgpassword = "rda_python_common.pgpassword:main"
pg_pass = "rda_python_common.pg_pass:main"
91 changes: 0 additions & 91 deletions src/rda_python_common/pg_pass.py

This file was deleted.

92 changes: 92 additions & 0 deletions src/rda_python_common/pg_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
#
##################################################################################
#
# Title: pgpassword
# Author: Zaihua Ji, [email protected]
# Date: 2025-10-27
# Purpose: python script to retrieve passwords for postgrsql login to connect a
# gdex database from inside an python application
#
# Github: https://github.com/NCAR/rda-python-common.git
#
##################################################################################

import os
import sys
import re
import pwd
import hvac
from . import PgLOG
from . import PgDBI

DBFLDS = {
'd' : 'dbname',
'c' : 'scname',
'h' : 'dbhost',
'p' : 'dbport',
'u' : 'lnname'
}

DBINFO = {
'dbname' : "",
'scname' : "",
'lnname' : "",
'dbhost' : "",
'dbport' : 5432
}

#
# main function to excecute this script
#
def main():

permit = False
aname = 'pgpassword'
argv = sys.argv[1:]
opt = None
dohelp = True
dbopt = False

for arg in argv:
if re.match(r'^-\w+$', arg):
opt = arg[1:]
elif opt:
if opt == 'l':
PgDBI.PGDBI['BAOURL'] = arg
elif opt == 'k':
PgDBI.PGDBI['BAOTOKEN'] = arg
elif opt in DBFLDS:
dbopt = True
DBINFO[DBFLDS[opt]] = arg
else:
PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX)
dohelp = False
else:
PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX)

if dohelp:
print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
print(" -l OpenBao URL to retrieve passwords")
print(" -k OpenBao Token Name to retrieve passwords")
print(" -d PostgreSQL Database Name")
print(" -c PostgreSQL Schema Name")
print(" -u PostgreSQL Login User Name")
print(" -h PostgreSQL Server Host Name")
print(" -p PostgreSQL Port Number")
sys.exit(0)

if dbopt:
PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'],
DBINFO['lnname'], None, DBINFO['dbport'])

pwname = PgDBI.get_baopassword()
if not pwname: pwname = PgDBI.get_pgpassword()
print(pwname)
sys.exit(0)

#
# call main() to start program
#
if __name__ == "__main__": main()
133 changes: 66 additions & 67 deletions src/rda_python_common/pgpassword.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,90 @@
#
##################################################################################
#
# Title: pgpassword
# Title: pg_pass
# Author: Zaihua Ji, [email protected]
# Date: 2025-10-27
# 2025-12-02 convert to class PgPassword
# Purpose: python script to retrieve passwords for postgrsql login to connect a
# gdex database from inside an python application
#
# Github: https://github.com/NCAR/rda-python-common.git
#
##################################################################################

import os
import sys
import re
import pwd
import hvac
from . import PgLOG
from . import PgDBI
from .pg_dbi import PgDBI

DBFLDS = {
'd' : 'dbname',
'c' : 'scname',
'h' : 'dbhost',
'p' : 'dbport',
'u' : 'lnname'
}
class PgPassword(PgDBI):

DBINFO = {
'dbname' : "",
'scname' : "",
'lnname' : "",
'dbhost' : "",
'dbport' : 5432
}
def __init__(self):
super().__init__() # initialize parent class
self.DBFLDS = {
'd' : 'dbname',
'c' : 'scname',
'h' : 'dbhost',
'p' : 'dbport',
'u' : 'lnname'
}
self.DBINFO = {
'dbname' : "",
'scname' : "",
'lnname' : "",
'dbhost' : "",
'dbport' : 5432
}
self.dbopt = False
self.password = ''

#
# main function to excecute this script
#
def main():

permit = False
aname = 'pgpassword'
argv = sys.argv[1:]
opt = None
dohelp = True
dbopt = False

for arg in argv:
if re.match(r'^-\w+$', arg):
opt = arg[1:]
elif opt:
if opt == 'l':
PgDBI.PGDBI['BAOURL'] = arg
elif opt == 'k':
PgDBI.PGDBI['BAOTOKEN'] = arg
elif opt in DBFLDS:
dbopt = True
DBINFO[DBFLDS[opt]] = arg
# read in command line parameters
def read_parameters(self):
argv = sys.argv[1:]
opt = None
dohelp = True
for arg in argv:
if re.match(r'^-\w+$', arg):
opt = arg[1:]
elif opt:
if opt == 'l':
self.PGDBI['BAOURL'] = arg
elif opt == 'k':
self.PGDBI['BAOTOKEN'] = arg
elif opt in self.DBFLDS:
self.dbopt = True
self.DBINFO[self.DBFLDS[opt]] = arg
else:
self.pglog(arg + ": Unknown option", self.LGEREX)
dohelp = False
else:
PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX)
dohelp = False
else:
PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX)
self.pglog(arg + ": Value provided without option", self.LGEREX)
if dohelp:
print("Usage: pg_pass [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
print(" -l OpenBao URL to retrieve passwords")
print(" -k OpenBao Token Name to retrieve passwords")
print(" -d PostgreSQL Database Name")
print(" -c PostgreSQL Schema Name")
print(" -u PostgreSQL Login User Name")
print(" -h PostgreSQL Server Host Name")
print(" -p PostgreSQL Port Number")
sys.exit(0)

if dohelp:
print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\")
print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]")
print(" -l OpenBao URL to retrieve passwords")
print(" -k OpenBao Token Name to retrieve passwords")
print(" -d PostgreSQL Database Name")
print(" -c PostgreSQL Schema Name")
print(" -u PostgreSQL Login User Name")
print(" -h PostgreSQL Server Host Name")
print(" -p PostgreSQL Port Number")
sys.exit(0)
# get the pgpassword
def start_actions(self):
if self.dbopt:
self.default_scinfo(self.DBINFO['dbname'], self.DBINFO['scname'], self.DBINFO['dbhost'],
self.DBINFO['lnname'], None, self.DBINFO['dbport'])
self.password = self.get_baopassword()
if not self.password: self.password = self.get_pg_pass()

if dbopt:
PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'],
DBINFO['lnname'], None, DBINFO['dbport'])

pwname = PgDBI.get_baopassword()
if not pwname: pwname = PgDBI.get_pgpassword()
print(pwname)
# main function to excecute this script
def main():
object = PgPassword()
object.read_parameters()
object.start_actions()
print(object.password)
sys.exit(0)

#
# call main() to start program
#
if __name__ == "__main__": main()
3 changes: 1 addition & 2 deletions test/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_common():
import rda_python_common.PgSIG
import rda_python_common.PgSplit
import rda_python_common.PgUtil
import rda_python_common.pgpassword
import rda_python_common.pg_cmd
import rda_python_common.pg_dbi
import rda_python_common.pg_file
Expand All @@ -24,4 +23,4 @@ def test_common():
import rda_python_common.pg_sig
import rda_python_common.pg_split
import rda_python_common.pg_util
import rda_python_common.pg_pass
import rda_python_common.pgpassword