Repository navigation
Conversation
e1bd0ee to
7228590
Compare
7228590 to
a4ef24d
Compare
Finished sorry in Python without testing I don't have sudo, so I don't know if calling ldapmodify like this/shutil.chown/chmod/etc... will work, but I have tested everything I can test for sure. Added newline Applied fydai's suggestions Applied fydai's suggestions - change return values to pythonic raises, and send mail to root@o.b.e instead of trying to edit a log file that does not exist anymore. Finally, formatted automatically so that it looks better made some change to bypass shell availbility check More slight changes ran make test again
a4ef24d to
fd63436
Compare
jvperrin
left a comment
There was a problem hiding this comment.
Thanks for doing this and apologies on the quite late review, it's definitely a tough one to do, especially since sorry (and unsorry for that matter) are pretty old and semi-unmaintained. I think I've probably left too many comments, but let me know if you have any questions on any of them!
| return subprocess.call(['kdestroy']) | ||
|
|
||
|
|
||
| def get_user_real_mail(username): |
There was a problem hiding this comment.
I've written a function for this in update-email, but maybe it should be put into ocflib so that both that and this script can use it? It does need some kerberos-related stuff to be run first though, similar to this script.
There was a problem hiding this comment.
TODO: Probably write kerberos_auth and update_email stuff in ocflib...?
| print('Changing permissions on home directory to 500') | ||
| os.chmod(user_dir, 0o500) | ||
|
|
||
| # WTF is final system check for? |
There was a problem hiding this comment.
Honestly I think it's just to show some debugging information since this bash script is not the most stable and often has errors as it goes, so that's why that part is there and it can probably be removed on this python version.
| os.chmod(http_dir, 0o000) | ||
|
|
||
| print('Changing permissions on home directory to 500') | ||
| os.chmod(user_dir, 0o500) |
There was a problem hiding this comment.
We have a couple ocflib functions that might help a little bit here:
home_dirfromocflib.account.utilsto get a user's home directory pathweb_dirfromocflib.account.utilsfor a user's web directory
| def kerberos_credentials(staff_name): | ||
| if ('SORRY_KRB5CCNAME' in os.environ): | ||
| print('SORRY_KRB5CCNAME set in environment. Assuming this file contains current admin credentials.') | ||
| os.environ['KRB5CCNAME'] = os.environ['SORRY_KRB5CCNAME'] |
There was a problem hiding this comment.
In practice I'm not sure this path is ever used, or at least I've never heard of it being used by anyone in recent history. Maybe it can be removed in favor of just running kinit each time if a kerberos ticket doesn't already exist?
There was a problem hiding this comment.
It's in the original file which I have no clue what it does. May be an ocf legacy...
There was a problem hiding this comment.
I can't think of anything that uses this either; it's probably safe to remove. If we perchance discover that something actually uses it we can add it back...
There was a problem hiding this comment.
This is probably a legacy before ocf transit all its code to github
|
|
||
|
|
||
| def log(root_staffer, sorried_user): | ||
| with open('/opt/acct/sorry.log', 'a') as log_file: |
There was a problem hiding this comment.
I think that we should move this to a root-owned 600 file in ~staff or something like that.
|
|
||
|
|
||
| def send_log_mail(root_staffer, sorried_user, reason): | ||
| email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>' |
There was a problem hiding this comment.
Anthrax won't deliver mail from this address, because no such user exists. You should probably just use
| email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>' | |
| email_from = 'Sorry Log <root@ocf.berkeley.edu>' |
There was a problem hiding this comment.
well... it actually works right now, but that's not intended behavior and is not permanent.
There was a problem hiding this comment.
It’s technically just a header which you can even set to donald@wh.gov... The real sender is still root@o.b.e
There was a problem hiding this comment.
iirc our mailserver will filter based on that header, and reject mail coming from addresses that don't exist
There was a problem hiding this comment.
Maybe that's only for inbox?
| print('Sorry file {}.'.format(file_path)) | ||
|
|
||
| sorry_user(args.sorriedUser, file_path) | ||
| print('NOW RUN note -u {0} reason. NOW'.format(args.sorriedUser)) |
There was a problem hiding this comment.
It would be good to do this automatically. We should keep in mind that note does not have a way to read the reason from stdin by default, and the reason here should usually have more detail than the reason given in the sorryfile.
|
|
||
|
|
||
| def send_log_mail(root_staffer, sorried_user, reason): | ||
| email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>' |
There was a problem hiding this comment.
well... it actually works right now, but that's not intended behavior and is not permanent.
kpengboy
left a comment
There was a problem hiding this comment.
Joining in the party here. Thanks for working on this; I've left some comments, mostly on style.
Several improvements according to jvperrin/cooperc/kpengboy's comments, And added some TODOs
|
Sadly, I gotta do my own works and do the TODOs a moment later, probably.... |
cg505
left a comment
There was a problem hiding this comment.
Excellent work, this is looking great so far.
|
I'll mess around with ocflib soon |
e.g. Automated note Changed structure for logging And a lot of changes that I cannot remember, and fix stupid issues
|
|
||
| # Initiates a kerberos ticket for current user | ||
| # Returns True when a new ticket is created via kinit so that we destroy it afterwards | ||
| # Raises OSError is kinit screwed up |
There was a problem hiding this comment.
| # Raises OSError is kinit screwed up | |
| # Raises OSError if kinit screwed up |
| REASON_PATH = pathlib.Path('/opt/share/utils/staff/acct/sorry/') | ||
| SORRY_SHELL = pathlib.Path('/opt/share/utils/bin/sorried') | ||
| # TODO: This sorry log file does not exist anymore, do we still log to file? | ||
| SORRY_LOG = pathlib.Path('/opt/acct/sorry.log') |
There was a problem hiding this comment.
i think we should probably reinstate this file, it would be useful
|
|
||
| # Omitting final system check for now | ||
| with open(sorry_file, 'r') as filep: | ||
| log(rootstaffer, username, filep.read()) |
There was a problem hiding this comment.
filep.read() can only be called once; any subsequent calls would return ''. You probably want to save its result to a variable and use that.
| send_sorry_mail(user_mail, username, filep.read()) | ||
| choice = input("Do you want this script to run 'note -u {0} reason' for you? [Y/n]".format(username)) | ||
| if choice not in ['n', 'N']: | ||
| subprocess.run(['sudo', '-u', rootstaffer, 'note', '-u', format(username), filep.read()]) |
There was a problem hiding this comment.
This might be problematic if the sorry reason is more than one line. (And even for one-line reasons, you probably want to strip off any end-of-line character that you read.)
There was a problem hiding this comment.
Will change this, I personally don't think that is what things are supposed to be - this might actually should be the filename of sorry reason
There was a problem hiding this comment.
Yeah, I think using the filename makes more sense.
| print("The sorry.log is much more useful when it logs who you are rather than simply 'root'.") | ||
| username = input('Please enter your username: ') |
There was a problem hiding this comment.
Perhaps just ask "Please enter your username (for logging purposes): "?
There was a problem hiding this comment.
That's copied from the original script, but will do
|
|
||
|
|
||
| def kerberos_init(staff_name): | ||
| if ('SORRY_KRB5CCNAME' in os.environ): |
| def kerberos_credentials(staff_name): | ||
| if ('SORRY_KRB5CCNAME' in os.environ): | ||
| print('SORRY_KRB5CCNAME set in environment. Assuming this file contains current admin credentials.') | ||
| os.environ['KRB5CCNAME'] = os.environ['SORRY_KRB5CCNAME'] |
There was a problem hiding this comment.
I can't think of anything that uses this either; it's probably safe to remove. If we perchance discover that something actually uses it we can add it back...
| parser = argparse.ArgumentParser(usage='%(prog)s sorried_user sorry_reason', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| description=sorry_description) | ||
| parser.add_argument('sorried_user', metavar='sorried_user', help='Username of sorried user') |
There was a problem hiding this comment.
Nit: would put the metavars in ALL_CAPS.
| raise PermissionError('You must be root to run this.') | ||
|
|
||
| args = parser.parse_args() | ||
| file_path = find_sorry_file(args.sorryReason) |
There was a problem hiding this comment.
sorryReason
Shouldn't this be sorry_reason?
There was a problem hiding this comment.
Let me double check, i moved some code from main() to there
| print('Sorry file {}.'.format(file_path)) | ||
|
|
||
| # TODO: Help user to note... by sub-processing note -u user reason, maybe? | ||
| sorry_user(args.sorriedUser, file_path) |
|
|
||
| def send_sorry_mail(sorried_user_email, username, reason): | ||
| email_subject = '[OCF] Account disabled' | ||
| email_body = ''' |
There was a problem hiding this comment.
Nit: optionally consider using textwrap.dedent.
| {2} | ||
| '''.format(username, reason, ocflib.misc.mail.MAIL_SIGNATURE) |
There was a problem hiding this comment.
| {2} | |
| '''.format(username, reason, ocflib.misc.mail.MAIL_SIGNATURE) | |
| {2}'''.format(username, reason, ocflib.misc.mail.MAIL_SIGNATURE) |
Avoids empty space at the end of the message.
|
Would you be interested in resurrecting this PR? If not I can take a look at it. |
Absolutely! |
|
Closing #130 #152 ocf/ocflib#210 #153 altogether as I created a looping "merge dependency" almost impossible to review and merge/do CICD; I'll rewrite everything again this semester |
I don't have sudo, so I don't know if calling ldapmodify like this/shutil.chown/chmod/etc... will work, but I have tested (almost) everything I can test for sure.
#121
This issue is meant to make bash script more clear and manageable and maintainable, however it seems that the script becomes more unreadable and uglier.