Skip to content

Instantly share code, notes, and snippets.

@benjaminws
Last active June 26, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminws/bc4543f3ee1c18650245032939b1cf2a to your computer and use it in GitHub Desktop.
Save benjaminws/bc4543f3ee1c18650245032939b1cf2a to your computer and use it in GitHub Desktop.
spin-login

spin-login

Presuming you use Oauth2 for auth with spinnaker, this will help you extract a Spinnaker session id from your browser. Then you can use it with the cli (roer as of now) and API things, say via curl.

NOTE: This assumes you're using a Chrome based browser on macos or linux.

Install

  1. Make sure you have latest python 3

  2. Get all the files in this gist (hint: Click Download ZIP above)

  3. Run make like this:

$ make deps

Run

$ export SPINNAKER_ADDR=https://your-spinnaker.com 
$ make run

Optionally you could put the python script somewhere in your $PATH, and make it executable. Then you would run spin-login.py.

Eval

$ export SPINNAKER_ADDR=https://your-spinnaker.com 
$ export SPINNAKER_SESSION=$(make run)

Prove It

$ echo $SPINNAKER_SESSION
c95ebebd-20cb-4292-ad20-2cda13b5ceb8

Use It (with roer)

$ roer --as $SPINNAKER_SESSION app list
INFO[0000] Fetching application list
...
STAMP_DEPS := .deps
PHONY: clean run install
clean:
@rm -rf $(STAMP_DEPS)
$(STAMP_DEPS):
@pip3 install -r requirements.txt > /dev/null 2>&1
@touch $@
run: $(STAMP_DEPS)
@python3 spin-login.py
deps: $(STAMP_DEPS)
keyring==10.3.2
pycookiecheat==0.4.3
pycrypto==2.6.1
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import os.path
import socket
import time
import sqlite3
import sys
from subprocess import Popen, check_call, check_output
from sys import stdin, stdout
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
from functools import wraps
from pycookiecheat import chrome_cookies
def retry(exceptions, tries=4, delay=3, backoff=2):
def deco_retry(f):
@wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f(*args, **kwargs)
except exceptions as e:
print('{}, Retrying in {} seconds...'.format(e, mdelay))
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return f(*args, **kwargs)
return f_retry
return deco_retry
@retry((KeyError, sqlite3.OperationalError), delay=1)
def get_session(spinnaker_addr):
return chrome_cookies(url=spinnaker_addr, browser="Chrome").get("SESSION")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--spinnaker_address")
args = parser.parse_args()
env = os.environ.copy()
if args.spinnaker_address:
env["SPINNAKER_ADDR"] = args.spinnaker_address
elif not env.get("SPINNAKER_ADDR", None):
env["SPINNAKER_ADDR"] = "https://localhost:9000/"
Popen(["open", env["SPINNAKER_ADDR"]])
while get_session(env["SPINNAKER_ADDR"]) is None:
continue
print(get_session(env["SPINNAKER_ADDR"]))
if __name__ == '__main__':
main()
@adamresson
Copy link

Added a few scripts to my ~/.zshrc file to make things easier to run:

export SPINNAKER_API=https://spinnaker-api.resson.com
export SPINNAKER_ADDR=$SPINNAKER_API

function spin-auth() {
  export SPINNAKER_SESSION=$(make -C /usr/local/spin-auth run)
  echo "Spinnaker Session: $SPINNAKER_SESSION"
}

function roer() {
  if [[ -z $SPINNAKER_SESSION ]] ; then
    spin-auth
  fi
  /usr/local/bin/roer --as $SPINNAKER_SESSION $@
}

Setup:

  1. Download files and extract to /usr/local/spin-auth
  2. Run make -C /usr/local/spin-auth deps
  3. Install roer to /usr/local/bin/roer

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