Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b459928

Browse files
d-w-moorealanking
authored andcommitted
[irods#635] Free fcns now have keyword parameter to dictate overwriting of old .irodsA
1 parent 5901a21 commit b459928

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ Note, in the `pam_password` case, this involves sending the cleartext password
178178
to the server (SSL should thus be enabled!) and then writing the scrambled token that
179179
returns from the transaction.
180180

181-
If an .irodsA file exists already, it will be overwritten.
181+
If an .irodsA file exists already, it will be overwritten by default; however, if these functions'
182+
overwrite parameter is set to `False`, an exception of type `irods.client_init.irodsA_already_exists`
183+
will be raised to indicate the older .irodsA file is present.
182184

183185
Examples:
184186
For the `native` authentication scheme, we can use the currently set iRODS password to create .irodsA file from Python thus:

irods/client_init.py

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,65 @@
1+
import contextlib
2+
import getpass
3+
import os
4+
import sys
5+
16
from irods import (env_filename_from_keyword_args, derived_auth_filename)
27
import irods.client_configuration as cfg
38
import irods.password_obfuscation as obf
49
import irods.helpers as h
5-
import getpass
6-
import os
7-
import sys
810

9-
def write_native_credentials_to_secrets_file(password, **kw):
10-
env_file = env_filename_from_keyword_args(kw)
11-
auth_file = derived_auth_filename(env_file)
12-
old_mask = None
11+
@contextlib.contextmanager
12+
def _open_file_for_protected_contents(file_path, *arg, **kw):
13+
f = old_mask = None
1314
try:
1415
old_mask = os.umask(0o77)
15-
open(auth_file,'w').write(obf.encode(password))
16+
f = open(file_path, *arg, **kw)
17+
yield f
1618
finally:
1719
if old_mask is not None:
1820
os.umask(old_mask)
19-
20-
return True
21+
if f is not None:
22+
f.close()
23+
24+
class irodsA_already_exists(Exception):
25+
pass
26+
27+
def _write_encoded_auth_value(auth_file, encode_input, overwrite):
28+
if not auth_file:
29+
raise RuntimeError(f'Path to irodsA ({auth_file}) is null.')
30+
if not overwrite and os.path.exists(auth_file):
31+
raise irodsA_already_exists(f'Overwriting not enabled and {auth_file} already exists.')
32+
with _open_file_for_protected_contents(auth_file, 'w') as irodsA:
33+
irodsA.write(obf.encode(encode_input))
34+
35+
def write_native_credentials_to_secrets_file(password, overwrite = True, **kw):
36+
"""Write the credentials to an .irodsA file that will enable logging in with native authentication
37+
using the given cleartext password.
38+
39+
If overwrite is False, irodsA_already_exists will be raised if an .irodsA is found at the
40+
expected path.
41+
"""
42+
env_file = env_filename_from_keyword_args(kw)
43+
auth_file = derived_auth_filename(env_file)
44+
_write_encoded_auth_value(auth_file, password, overwrite)
45+
46+
def write_pam_credentials_to_secrets_file(password, overwrite = True, **kw):
47+
"""Write the credentials to an .irodsA file that will enable logging in with PAM authentication
48+
using the given cleartext password.
2149
22-
def write_pam_credentials_to_secrets_file( password ,**kw):
50+
If overwrite is False, irodsA_already_exists will be raised if an .irodsA is found at the
51+
expected path.
52+
"""
2353
s = h.make_session()
2454
s.pool.account.password = password
55+
to_encode = []
2556
with cfg.loadlines( [dict(setting='legacy_auth.pam.password_for_auto_renew',value=None),
2657
dict(setting='legacy_auth.pam.store_password_to_environment',value=False)] ):
2758
to_encode = s.pam_pw_negotiated
28-
if to_encode:
29-
open(s.pool.account.derived_auth_file,'w').write(obf.encode(to_encode[0]))
30-
return True
31-
return False
59+
if not to_encode:
60+
raise RuntimeError(f'Password token was not passed from server.')
61+
auth_file = s.pool.account.derived_auth_file
62+
_write_encoded_auth_value(auth_file, to_encode[0], overwrite)
3263

3364
if __name__ == '__main__':
3465
vector = {

0 commit comments

Comments
 (0)