Skip to content

Commit 9d47c5d

Browse files
committed
[_621] free functions & associated changes (squash)
including: review chgs and codacy recommendations
1 parent afac630 commit 9d47c5d

5 files changed

Lines changed: 56 additions & 14 deletions

File tree

irods/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
import logging
44
import os
55

6+
def env_filename_from_keyword_args(kwargs):
7+
try:
8+
env_file = kwargs.pop('irods_env_file')
9+
except KeyError:
10+
try:
11+
env_file = os.environ['IRODS_ENVIRONMENT_FILE']
12+
except KeyError:
13+
env_file = os.path.expanduser('~/.irods/irods_environment.json')
14+
return env_file
15+
16+
def derived_auth_filename(env_filename):
17+
if not env_filename:
18+
return ''
19+
default_irods_authentication_file = os.path.join(os.path.dirname(env_filename),'.irodsA')
20+
return os.environ.get('IRODS_AUTHENTICATION_FILE', default_irods_authentication_file)
21+
622
# This has no effect if basicConfig() was previously called.
723
logging.basicConfig()
824

irods/account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import os
1+
from irods import derived_auth_filename
22

33
class iRODSAccount(object):
44

55
@property
66
def derived_auth_file(self):
7-
return '' if not self.env_file else os.path.join(os.path.dirname(self.env_file),'.irodsA')
7+
return derived_auth_filename(self.env_file)
88

99
def __init__(self, irods_host, irods_port, irods_user_name, irods_zone_name,
1010
irods_authentication_scheme='native',

irods/client_init.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from irods import (env_filename_from_keyword_args, derived_auth_filename)
2+
import irods.client_configuration as cfg
3+
import irods.password_obfuscation as obf
4+
import irods.helpers as h
5+
import getpass
6+
import sys
7+
8+
def write_native_credentials_to_secrets_file(password, **kw):
9+
env_file = env_filename_from_keyword_args(kw)
10+
auth_file = derived_auth_filename(env_file)
11+
open(auth_file,'w').write(obf.encode(password))
12+
return True
13+
14+
def write_pam_credentials_to_secrets_file( password ,**kw):
15+
s = h.make_session()
16+
s.pool.account.password = password
17+
with cfg.loadlines( [dict(setting='legacy_auth.pam.password_for_auto_renew',value=None),
18+
dict(setting='legacy_auth.pam.store_password_to_environment',value=False)] ):
19+
to_encode = s.pam_pw_negotiated
20+
if to_encode:
21+
open(s.pool.account.derived_auth_file,'w').write(obf.encode(to_encode[0]))
22+
return True
23+
return False
24+
25+
if __name__ == '__main__':
26+
vector = {
27+
'pam_password': write_pam_credentials_to_secrets_file,
28+
'native': write_native_credentials_to_secrets_file
29+
}
30+
31+
if sys.argv[1] in vector:
32+
vector[sys.argv[1]](getpass.getpass(prompt=f'{sys.argv[1]} password: '))
33+
else:
34+
print('did not recognize authentication scheme argument',file = sys.stderr)

irods/connection.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,16 +461,14 @@ def _login_pam(self):
461461

462462
import irods.client_configuration as cfg
463463
inline_password = (self.account.authentication_scheme == self.account._original_authentication_scheme)
464-
# By default, let server determine the TTL.
465-
time_to_live_in_hours = 0
464+
time_to_live_in_hours = cfg.legacy_auth.pam.time_to_live_in_hours
466465
# For certain characters in the pam password, if they need escaping with '\' then do so.
467466
new_pam_password = PAM_PW_ESC_PATTERN.sub(lambda m: '\\'+m.group(1), self.account.password)
468-
if not inline_password:
467+
if not inline_password and cfg.legacy_auth.pam.password_for_auto_renew is not None:
469468
# Login using PAM password from .irodsA
470469
try:
471470
self._login_native()
472471
except (ex.CAT_PASSWORD_EXPIRED, ex.CAT_INVALID_USER, ex.CAT_INVALID_AUTHENTICATION) as exc:
473-
time_to_live_in_hours = cfg.legacy_auth.pam.time_to_live_in_hours
474472
if cfg.legacy_auth.pam.password_for_auto_renew:
475473
new_pam_password = cfg.legacy_auth.pam.password_for_auto_renew
476474
# Fall through and retry the native login later, after creating a new PAM password

irods/test/helpers.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from irods.session import iRODSSession
2222
from irods.message import (iRODSMessage, IRODS_VERSION)
2323
from irods.password_obfuscation import encode
24+
from irods import env_filename_from_keyword_args
2425
from six.moves import range
2526

2627
class iRODSUserLogins(object):
@@ -148,7 +149,6 @@ def recast(k):
148149
os.chmod(auth,0o600)
149150
return (config, auth)
150151

151-
152152
# Create a connection for test, based on ~/.irods environment by default.
153153

154154
def make_session(test_server_version = True, **kwargs):
@@ -166,13 +166,7 @@ def make_session(test_server_version = True, **kwargs):
166166
**kwargs: Keyword arguments. Fed directly to the iRODSSession
167167
constructor. """
168168

169-
try:
170-
env_file = kwargs.pop('irods_env_file')
171-
except KeyError:
172-
try:
173-
env_file = os.environ['IRODS_ENVIRONMENT_FILE']
174-
except KeyError:
175-
env_file = os.path.expanduser('~/.irods/irods_environment.json')
169+
env_file = env_filename_from_keyword_args( kwargs )
176170
session = iRODSSession( irods_env_file = env_file, **kwargs )
177171
if test_server_version:
178172
connected_version = session.server_version[:3]

0 commit comments

Comments
 (0)