Skip to content

Finished sorry in Python without testing - #130

Closed
axmmisaka wants to merge 3 commits into
ocf:masterfrom
axmmisaka:pythonic-sorry
Closed

axmmisaka wants to merge 3 commits into
ocf:masterfrom
axmmisaka:pythonic-sorry

Conversation

@axmmisaka

@axmmisaka axmmisaka commented Oct 5, 2019 •

Copy link
Copy Markdown
Contributor

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.

@axmmisaka
axmmisaka force-pushed the pythonic-sorry branch 3 times, most recently from e1bd0ee to 7228590 Compare October 13, 2019 01:16
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
@cg505
cg505 self-requested a review December 7, 2019 21:06

@jvperrin jvperrin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Outdated
return subprocess.call(['kdestroy'])


def get_user_real_mail(username):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@axmmisaka axmmisaka Feb 14, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Probably write kerberos_auth and update_email stuff in ocflib...?

Comment thread staff/acct/sorry/sorry Outdated
print('Changing permissions on home directory to 500')
os.chmod(user_dir, 0o500)

# WTF is final system check for?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread staff/acct/sorry/sorry
os.chmod(http_dir, 0o000)

print('Changing permissions on home directory to 500')
os.chmod(user_dir, 0o500)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a couple ocflib functions that might help a little bit here:

  • home_dir from ocflib.account.utils to get a user's home directory path
  • web_dir from ocflib.account.utils for a user's web directory

Comment thread staff/acct/sorry/sorry
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']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the original file which I have no clue what it does. May be an ocf legacy...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably a legacy before ocf transit all its code to github

@cg505 cg505 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the progress on this. It's looking really good actually. Agree with @jvperrin's comments but left a few notes of my own.

Comment thread staff/acct/sorry/sorry Outdated


def log(root_staffer, sorried_user):
with open('/opt/acct/sorry.log', 'a') as log_file:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should move this to a root-owned 600 file in ~staff or something like that.

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Outdated


def send_log_mail(root_staffer, sorried_user, reason):
email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anthrax won't deliver mail from this address, because no such user exists. You should probably just use

Suggested change
email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>'
email_from = 'Sorry Log <root@ocf.berkeley.edu>'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well... it actually works right now, but that's not intended behavior and is not permanent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s technically just a header which you can even set to donald@wh.gov... The real sender is still root@o.b.e

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc our mailserver will filter based on that header, and reject mail coming from addresses that don't exist

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that's only for inbox?

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Outdated
print('Sorry file {}.'.format(file_path))

sorry_user(args.sorriedUser, file_path)
print('NOW RUN note -u {0} reason. NOW'.format(args.sorriedUser))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Outdated


def send_log_mail(root_staffer, sorried_user, reason):
email_from = 'Open Computing Facility <sorry_log@ocf.berkeley.edu>'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well... it actually works right now, but that's not intended behavior and is not permanent.

@kpengboy kpengboy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joining in the party here. Thanks for working on this; I've left some comments, mostly on style.

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
Several improvements according to jvperrin/cooperc/kpengboy's comments,
And added some TODOs
@axmmisaka

Copy link
Copy Markdown
Contributor Author

Sadly, I gotta do my own works and do the TODOs a moment later, probably....

@cg505 cg505 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, this is looking great so far.

Comment thread staff/acct/sorry/sorry Resolved
Comment thread staff/acct/sorry/sorry Resolved
@axmmisaka

Copy link
Copy Markdown
Contributor Author

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
Comment thread staff/acct/sorry/sorry

# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Raises OSError is kinit screwed up
# Raises OSError if kinit screwed up

Comment thread staff/acct/sorry/sorry
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')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should probably reinstate this file, it would be useful

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably put it in NFS though

Comment thread staff/acct/sorry/sorry

# Omitting final system check for now
with open(sorry_file, 'r') as filep:
log(rootstaffer, username, filep.read())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread staff/acct/sorry/sorry
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()])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think using the filename makes more sense.

Comment thread staff/acct/sorry/sorry
Comment on lines +94 to +95
print("The sorry.log is much more useful when it logs who you are rather than simply 'root'.")
username = input('Please enter your username: ')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just ask "Please enter your username (for logging purposes): "?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's copied from the original script, but will do

Comment thread staff/acct/sorry/sorry


def kerberos_init(staff_name):
if ('SORRY_KRB5CCNAME' in os.environ):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove parentheses

Comment thread staff/acct/sorry/sorry
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']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Comment thread staff/acct/sorry/sorry
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')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: would put the metavars in ALL_CAPS.

Comment thread staff/acct/sorry/sorry
raise PermissionError('You must be root to run this.')

args = parser.parse_args()
file_path = find_sorry_file(args.sorryReason)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorryReason

Shouldn't this be sorry_reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me double check, i moved some code from main() to there

Comment thread staff/acct/sorry/sorry
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorriedUser

idem

Comment thread staff/acct/sorry/sorry

def send_sorry_mail(sorried_user_email, username, reason):
email_subject = '[OCF] Account disabled'
email_body = '''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: optionally consider using textwrap.dedent.

Comment thread staff/acct/sorry/sorry
Comment on lines +172 to +173
{2}
'''.format(username, reason, ocflib.misc.mail.MAIL_SIGNATURE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{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.

@emmatyping

Copy link
Copy Markdown
Member

Would you be interested in resurrecting this PR? If not I can take a look at it.

@axmmisaka

Copy link
Copy Markdown
Contributor Author

Would you be interested in resurrecting this PR? If not I can take a look at it.

Absolutely!

@axmmisaka

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants