1
1
#!/usr/bin/env python
2
2
#
3
3
# Copyright 2014-2017 Spotify AB
4
- # Copyright 2016-2018 The Last Pickle Ltd
4
+ # Copyright 2016-2019 The Last Pickle Ltd
5
5
#
6
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
7
# you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import getpass
20
20
import json
21
21
import logging
22
22
import os
23
+ import subprocess
23
24
import urllib
24
25
import urlparse
25
26
@@ -88,7 +89,8 @@ class ReaperCaller(object):
88
89
http_method , r .status_code , len (str (r .text )))
89
90
log .debug ("Response content:\n %s" , r .text )
90
91
if str (r .status_code ) == "403" :
91
- printq ("Access to this operation seems to be restricted. You may need to login and pass a JWT to access it." )
92
+ printq ("Access to this operation seems to be restricted. Please login before making any call to the API:" )
93
+ printq ("spreaper login <username>" )
92
94
if not str (r .status_code ).startswith ("2" ):
93
95
print r .text
94
96
r .raise_for_status ()
@@ -147,7 +149,7 @@ def _global_arguments(parser, command):
147
149
action = "store_true" )
148
150
group .add_argument ("-vv" , help = "extra output verbosity" , action = "store_true" )
149
151
group .add_argument ("-q" , "--quiet" , help = "unix mode" , action = "store_true" )
150
- group .add_argument ("--jwt" , default = None , help = "Session JSON Web Token obtained at login " )
152
+ group .add_argument ("--jwt" , default = None , help = "JSON Web Token (JWT) to authenticate with " )
151
153
parser .add_argument (command )
152
154
153
155
@@ -329,7 +331,7 @@ def _arguments_for_delete_snapshots(parser):
329
331
330
332
def _arguments_for_login (parser ):
331
333
"""Arguments needed to login"""
332
- parser .add_argument ("-- username" , help = "Username to login with" )
334
+ parser .add_argument ("username" , help = "Username to login with" )
333
335
334
336
def _parse_arguments (command , description , usage = None , extra_arguments = None ):
335
337
"""Generic argument parsing done by every command"""
@@ -418,24 +420,49 @@ class ReaperCLI(object):
418
420
def prepare_reaper (command , description , usage = None , extra_arguments = None ):
419
421
args = _parse_arguments (command , description , usage , extra_arguments )
420
422
reaper = ReaperCaller (args .reaper_host , args .reaper_port , args .reaper_use_ssl )
421
- ReaperCLI .addJwtHeader (args )
423
+ jwt = ReaperCLI .get_jwt (args )
424
+ ReaperCLI .addJwtHeader (jwt )
422
425
return reaper , args
423
426
424
427
@staticmethod
425
- def addJwtHeader (args ):
426
- if (args . jwt != None ):
427
- HEADERS ['Authorization' ] = 'Bearer ' + args . jwt
428
+ def addJwtHeader (jwt ):
429
+ if (jwt != None ):
430
+ HEADERS ['Authorization' ] = 'Bearer ' + jwt
428
431
429
432
@staticmethod
430
433
def get_password ():
431
434
password = None
432
435
# Use the password from the file if it exists or prompt the user for it
433
- if (os .path .exists (os .path .expanduser ('~/.reaper' ))):
434
- password = file (os .path .expanduser ('~/.reaper' ),'rU' ).readline ().rstrip ("\r \n " )
436
+ if (os .path .exists (os .path .expanduser ('~/.reaper/credentials ' ))):
437
+ password = file (os .path .expanduser ('~/.reaper/credentials ' ),'rU' ).readline ().rstrip ("\r \n " )
435
438
else :
436
439
password = getpass .getpass ()
437
440
438
441
return password
442
+
443
+ @staticmethod
444
+ def get_jwt (args ):
445
+ jwt = None
446
+ if args .jwt == None :
447
+ # Use the password from the file if it exists or prompt the user for it
448
+ if (os .path .exists (os .path .expanduser ('~/.reaper/jwt' ))):
449
+ jwt = file (os .path .expanduser ('~/.reaper/jwt' ),'rU' ).readline ().rstrip ("\r \n " )
450
+ else :
451
+ jwt = args .jwt
452
+
453
+ return jwt
454
+
455
+ @staticmethod
456
+ def save_jwt (jwt ):
457
+ try :
458
+ os .makedirs (os .path .expanduser ('~/.reaper' ))
459
+ os .chmod (os .path .expanduser ('~/.reaper' ),0700 )
460
+ except OSError :
461
+ pass
462
+ with open (os .path .expanduser ('~/.reaper/jwt' ),'w+' ) as f :
463
+ f .write (jwt )
464
+ os .chmod (os .path .expanduser ('~/.reaper/jwt' ),0600 )
465
+ printq ("# JWT saved" )
439
466
440
467
def login (self ):
441
468
reaper , args = ReaperCLI .prepare_reaper (
@@ -451,7 +478,8 @@ class ReaperCLI(object):
451
478
# Use shiro's session id to request a JWT
452
479
COOKIES ["JSESSIONID" ] = reply .cookies ["JSESSIONID" ]
453
480
jwt = reaper .get ("jwt" )
454
- printq ("Add the following flag to all subsequent spreaper calls to use your authenticated session : --jwt {}" .format (jwt ))
481
+ printq ("You are now authenticated to Reaper." )
482
+ ReaperCLI .save_jwt (jwt )
455
483
456
484
# remove the session id and set the auth header with the JWT
457
485
COOKIES .pop ("JSESSIONID" , None )
@@ -461,6 +489,7 @@ class ReaperCLI(object):
461
489
"ping" ,
462
490
"Test connectivity to the Reaper service."
463
491
)
492
+ printq ("# JWT: {}" .format (os .environ .get ("REAPER_JWT" )))
464
493
printq ("# Sending PING to Reaper..." )
465
494
answer = reaper .get ("ping" )
466
495
printq ("# [Reply] {}" .format (answer ))
0 commit comments