|
| 1 | +import contextlib |
| 2 | +import getpass |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
1 | 6 | from irods import (env_filename_from_keyword_args, derived_auth_filename)
|
2 | 7 | import irods.client_configuration as cfg
|
3 | 8 | import irods.password_obfuscation as obf
|
4 | 9 | import irods.helpers as h
|
5 |
| -import getpass |
6 |
| -import os |
7 |
| -import sys |
8 | 10 |
|
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 |
13 | 14 | try:
|
14 | 15 | old_mask = os.umask(0o77)
|
15 |
| - open(auth_file,'w').write(obf.encode(password)) |
| 16 | + f = open(file_path, *arg, **kw) |
| 17 | + yield f |
16 | 18 | finally:
|
17 | 19 | if old_mask is not None:
|
18 | 20 | 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. |
21 | 49 |
|
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 | + """ |
23 | 53 | s = h.make_session()
|
24 | 54 | s.pool.account.password = password
|
| 55 | + to_encode = [] |
25 | 56 | with cfg.loadlines( [dict(setting='legacy_auth.pam.password_for_auto_renew',value=None),
|
26 | 57 | dict(setting='legacy_auth.pam.store_password_to_environment',value=False)] ):
|
27 | 58 | 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) |
32 | 63 |
|
33 | 64 | if __name__ == '__main__':
|
34 | 65 | vector = {
|
|
0 commit comments