Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7602: rudder-dev self upgrade #108

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions scripts/rudder-dev/rudder-dev
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage:
rudder-dev [-f|--force] merge <first_branch> <next_branch> [-s <strategy>] [-a|--automatic]
rudder-dev [-f|--force] find <command>
rudder-dev [-f|--force] cleanup [--more] [-n|--dry-run]
rudder-dev [-f|--force] update
rudder-dev [-f|--force] <smart_arg> [<PR_message>] [--base=<ticket_id>]

SMART
Expand Down Expand Up @@ -150,6 +151,10 @@ CLEANUP (cleanup your repository
closed tickets that have un unmerged commits are asked to the user
ex: git cleanup

UPDATE rudder-dev
- download last version from http://www.rudder-project.org/tools/rudder-dev
- replace current rudder-dev with it
- use sudo if needed
"""

from __future__ import print_function
Expand Down Expand Up @@ -178,6 +183,8 @@ except ImportError:


## GLOBAL VARIABLES
RUDDER_DEV_ORIGIN = "http://www.rudder-project.org/tools/rudder-dev"
Copy link
Member

Choose a reason for hiding this comment

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

http, seriously ?

Copy link
Member

Choose a reason for hiding this comment

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

in term of security, this is DISASTROUS - any man in the middle, anywhere between the user and our server can modify, tamper or replace the file - this has been a vector of attack recently from hacked router which replaced content of http to inject malware. Here I can't even imagine the impact

WARN_FOR_UPDATE_AFTER = 15 # days

LIFECYCLES = { "rudder": { "detection": r'^\*?\s+remotes/{}/branches/rudder/(.*)',
"format": "branches/rudder/{}",
Expand Down Expand Up @@ -1408,6 +1415,46 @@ def cleanup(more=False, dry=False):
print("keeping: " + branch)


def update():
my_path = os.path.abspath(__file__)
with open(my_path, 'r') as fd:
my_text = fd.read()

data = requests.get(RUDDER_DEV_ORIGIN)
if data.status_code != requests.codes.ok:
logfail("Cannot get last version of rudder-dev sorry!")
exit(14)
new_text = data.text

# No update needed
if my_text == new_text:
print("No update needed!")
(code, x) = shell("touch '" + my_path + "'", "Trying touch to avoid warnings", fail_exit=False)
if code != 0:
shell("sudo touch '" + my_path + "'", "Trying sudo touch to avoid warnings")
exit(0)

# Try to update rudder-dev with our access rights
try:
with open(my_path, 'w') as fd:
fd.write(new_text)
except Exception as e:
# Try with sudo instead
shell("cat <<'EOF' | sudo tee '" + my_path + "' > /dev/null \n" + new_text + "\nEOF\n")

print("rudder-dev has been updated, well done!")


def check_update():
my_path = os.path.abspath(__file__)
# mtime = last modification = content changed
ctime = os.path.getmtime(my_path)
days_ago = (time.time() - ctime) / 60 / 60 / 24
if days_ago > WARN_FOR_UPDATE_AFTER:
print("Your version of rudder-dev is old and probably needs an update, please run 'rudder-dev update'")
# else everything is up to date


# Main loop
if __name__ == "__main__":
arguments = docopt.docopt(__doc__)
Expand Down Expand Up @@ -1440,6 +1487,9 @@ if __name__ == "__main__":
logfail("***** ERROR: Unable to get the current git branch name, this directory is probably not a git repository")
exit(11)

# check if update is needed
check_update()

# standard arguments
if arguments['clone']:
clone(arguments['<repository>'], arguments['--fork'])
Expand Down Expand Up @@ -1485,4 +1535,6 @@ if __name__ == "__main__":
find(arguments['<command>'])
elif arguments['cleanup']:
cleanup(arguments['--more'], arguments['-n'] or arguments['--dry-run'])
elif arguments['update']:
update()