|
2 | 2 | # |
3 | 3 | ################################################################################## |
4 | 4 | # |
5 | | -# Title: pgpassword |
| 5 | +# Title: pg_pass |
6 | 6 | # Author: Zaihua Ji, zji@ucar.edu |
7 | 7 | # Date: 2025-10-27 |
| 8 | +# 2025-12-02 convert to class PgPassword |
8 | 9 | # Purpose: python script to retrieve passwords for postgrsql login to connect a |
9 | 10 | # gdex database from inside an python application |
10 | 11 | # |
11 | 12 | # Github: https://github.com/NCAR/rda-python-common.git |
12 | 13 | # |
13 | 14 | ################################################################################## |
14 | 15 |
|
15 | | -import os |
16 | 16 | import sys |
17 | 17 | import re |
18 | | -import pwd |
19 | | -import hvac |
20 | | -from . import PgLOG |
21 | | -from . import PgDBI |
| 18 | +from .pg_dbi import PgDBI |
22 | 19 |
|
23 | | -DBFLDS = { |
24 | | - 'd' : 'dbname', |
25 | | - 'c' : 'scname', |
26 | | - 'h' : 'dbhost', |
27 | | - 'p' : 'dbport', |
28 | | - 'u' : 'lnname' |
29 | | -} |
| 20 | +class PgPassword(PgDBI): |
30 | 21 |
|
31 | | -DBINFO = { |
32 | | - 'dbname' : "", |
33 | | - 'scname' : "", |
34 | | - 'lnname' : "", |
35 | | - 'dbhost' : "", |
36 | | - 'dbport' : 5432 |
37 | | -} |
| 22 | + def __init__(self): |
| 23 | + super().__init__() # initialize parent class |
| 24 | + self.DBFLDS = { |
| 25 | + 'd' : 'dbname', |
| 26 | + 'c' : 'scname', |
| 27 | + 'h' : 'dbhost', |
| 28 | + 'p' : 'dbport', |
| 29 | + 'u' : 'lnname' |
| 30 | + } |
| 31 | + self.DBINFO = { |
| 32 | + 'dbname' : "", |
| 33 | + 'scname' : "", |
| 34 | + 'lnname' : "", |
| 35 | + 'dbhost' : "", |
| 36 | + 'dbport' : 5432 |
| 37 | + } |
| 38 | + self.dbopt = False |
| 39 | + self.password = '' |
38 | 40 |
|
39 | | -# |
40 | | -# main function to excecute this script |
41 | | -# |
42 | | -def main(): |
43 | | - |
44 | | - permit = False |
45 | | - aname = 'pgpassword' |
46 | | - argv = sys.argv[1:] |
47 | | - opt = None |
48 | | - dohelp = True |
49 | | - dbopt = False |
50 | | - |
51 | | - for arg in argv: |
52 | | - if re.match(r'^-\w+$', arg): |
53 | | - opt = arg[1:] |
54 | | - elif opt: |
55 | | - if opt == 'l': |
56 | | - PgDBI.PGDBI['BAOURL'] = arg |
57 | | - elif opt == 'k': |
58 | | - PgDBI.PGDBI['BAOTOKEN'] = arg |
59 | | - elif opt in DBFLDS: |
60 | | - dbopt = True |
61 | | - DBINFO[DBFLDS[opt]] = arg |
| 41 | + # read in command line parameters |
| 42 | + def read_parameters(self): |
| 43 | + argv = sys.argv[1:] |
| 44 | + opt = None |
| 45 | + dohelp = True |
| 46 | + for arg in argv: |
| 47 | + if re.match(r'^-\w+$', arg): |
| 48 | + opt = arg[1:] |
| 49 | + elif opt: |
| 50 | + if opt == 'l': |
| 51 | + self.PGDBI['BAOURL'] = arg |
| 52 | + elif opt == 'k': |
| 53 | + self.PGDBI['BAOTOKEN'] = arg |
| 54 | + elif opt in self.DBFLDS: |
| 55 | + self.dbopt = True |
| 56 | + self.DBINFO[self.DBFLDS[opt]] = arg |
| 57 | + else: |
| 58 | + self.pglog(arg + ": Unknown option", self.LGEREX) |
| 59 | + dohelp = False |
62 | 60 | else: |
63 | | - PgLOG.pglog(arg + ": Unknown option", PgLOG.LGEREX) |
64 | | - dohelp = False |
65 | | - else: |
66 | | - PgLOG.pglog(arg + ": Value provided without option", PgLOG.LGEREX) |
| 61 | + self.pglog(arg + ": Value provided without option", self.LGEREX) |
| 62 | + if dohelp: |
| 63 | + print("Usage: pg_pass [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") |
| 64 | + print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") |
| 65 | + print(" -l OpenBao URL to retrieve passwords") |
| 66 | + print(" -k OpenBao Token Name to retrieve passwords") |
| 67 | + print(" -d PostgreSQL Database Name") |
| 68 | + print(" -c PostgreSQL Schema Name") |
| 69 | + print(" -u PostgreSQL Login User Name") |
| 70 | + print(" -h PostgreSQL Server Host Name") |
| 71 | + print(" -p PostgreSQL Port Number") |
| 72 | + sys.exit(0) |
67 | 73 |
|
68 | | - if dohelp: |
69 | | - print("Usage: pgpassword [-l OpenBaoURL] [-k TokenName] [-d DBNAME] \\") |
70 | | - print(" [-c SCHEMA] [-u USName] [-h DBHOST] [-p DBPORT]") |
71 | | - print(" -l OpenBao URL to retrieve passwords") |
72 | | - print(" -k OpenBao Token Name to retrieve passwords") |
73 | | - print(" -d PostgreSQL Database Name") |
74 | | - print(" -c PostgreSQL Schema Name") |
75 | | - print(" -u PostgreSQL Login User Name") |
76 | | - print(" -h PostgreSQL Server Host Name") |
77 | | - print(" -p PostgreSQL Port Number") |
78 | | - sys.exit(0) |
| 74 | + # get the pgpassword |
| 75 | + def start_actions(self): |
| 76 | + if self.dbopt: |
| 77 | + self.default_scinfo(self.DBINFO['dbname'], self.DBINFO['scname'], self.DBINFO['dbhost'], |
| 78 | + self.DBINFO['lnname'], None, self.DBINFO['dbport']) |
| 79 | + self.password = self.get_baopassword() |
| 80 | + if not self.password: self.password = self.get_pg_pass() |
79 | 81 |
|
80 | | - if dbopt: |
81 | | - PgDBI.default_scinfo(DBINFO['dbname'], DBINFO['scname'], DBINFO['dbhost'], |
82 | | - DBINFO['lnname'], None, DBINFO['dbport']) |
83 | | - |
84 | | - pwname = PgDBI.get_baopassword() |
85 | | - if not pwname: pwname = PgDBI.get_pgpassword() |
86 | | - print(pwname) |
| 82 | +# main function to excecute this script |
| 83 | +def main(): |
| 84 | + object = PgPassword() |
| 85 | + object.read_parameters() |
| 86 | + object.start_actions() |
| 87 | + print(object.password) |
87 | 88 | sys.exit(0) |
88 | 89 |
|
89 | | -# |
90 | 90 | # call main() to start program |
91 | | -# |
92 | 91 | if __name__ == "__main__": main() |
0 commit comments