[packagedb-cli: 10/10] If the user is already logged in via the session pickle, move on
by Pierre-YvesChibon
commit 516e95327d0369d997ebcebc8488dab84c3c1ae6
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 12:00:52 2014 +0200
If the user is already logged in via the session pickle, move on
pkgdb2client.py | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)
---
diff --git a/pkgdb2client.py b/pkgdb2client.py
index 63ab4df..668290f 100644
--- a/pkgdb2client.py
+++ b/pkgdb2client.py
@@ -226,6 +226,10 @@ class PkgDB(object):
if not motif.match(openid_url):
raise PkgDBException(
'Un-expected openid provider asked: %s' % openid_url)
+ elif 'logged in as' in response.text:
+ # User already logged in via its cookie file by default:
+ # ~/.cache/pkgdb-session.pickle
+ return
else:
data = {}
for resp in response.history:
9 years, 11 months
[packagedb-cli: 9/10] If no password is provided, skip the auth tests
by Pierre-YvesChibon
commit 31e1efa4839f08d43d343efe1cd901a216cce85b
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 11:19:43 2014 +0200
If no password is provided, skip the auth tests
test.py | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
---
diff --git a/test.py b/test.py
index 421aff9..c693fff 100644
--- a/test.py
+++ b/test.py
@@ -23,6 +23,8 @@ if AUTH:
except:
USERNAME = raw_input('FAS username: ')
PASSWORD = getpass.getpass('FAS password: ')
+ if not PASSWORD:
+ AUTH = False
COL_NAME = str(uuid.uuid1())[:30]
PKG_NAME = str(uuid.uuid1())[:30]
9 years, 11 months
[packagedb-cli: 8/10] Merge pull request #9 from tyll/store_cookies
by Pierre-YvesChibon
commit ea7de6a16237c6a9df0095bb42fbc8748be4d6b7
Merge: 15df804 afda46e
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 10:24:34 2014 +0200
Merge pull request #9 from tyll/store_cookies
PkgDB: store cookies
pkgdb2client.py | 39 +++++++++++++++++++++++++++++++++------
1 files changed, 33 insertions(+), 6 deletions(-)
---
9 years, 11 months
[packagedb-cli: 7/10] Merge pull request #14 from tyll/simplify
by Pierre-YvesChibon
commit 15df8041e87e2b53301059e48a9ad233e1f1a6b4
Merge: 2409a12 9cbc4b2
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 10:21:07 2014 +0200
Merge pull request #14 from tyll/simplify
pkgdb2client: fix indentation
pkgdb2client.py | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
---
9 years, 11 months
[packagedb-cli: 6/10] pkgdb2client: fix indentation
by Pierre-YvesChibon
commit 9cbc4b2ecd4716ec8cefb912fcab0fb05a8a35f6
Author: Till Maas <opensource(a)till.name>
Date: Fri May 30 10:19:26 2014 +0200
pkgdb2client: fix indentation
pkgdb2client.py | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
---
diff --git a/pkgdb2client.py b/pkgdb2client.py
index 2e029bf..f464d66 100644
--- a/pkgdb2client.py
+++ b/pkgdb2client.py
@@ -279,8 +279,8 @@ class PkgDB(object):
bad_password = True
if not success:
raise PkgDBAuthException("Too many failed login attempts")
- response = self.__send_request(url=url, method=method, data=data,
- params=params)
+ response = self.__send_request(url=url, method=method, data=data,
+ params=params)
return response
def handle_api_call(self, path, params=None, data=None):
9 years, 11 months
[packagedb-cli: 5/10] Merge pull request #13 from tyll/cli
by Pierre-YvesChibon
commit 2409a12425ecd9c540d913c3cb59c7a930853ad4
Merge: 9e15b95 341896e
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 10:13:13 2014 +0200
Merge pull request #13 from tyll/cli
pkgdb2_cli: Ask for password only if required
pkgdb2_cli.py | 40 ++++++++--------------------------------
pkgdb2client.py | 1 +
2 files changed, 9 insertions(+), 32 deletions(-)
---
9 years, 11 months
[packagedb-cli: 4/10] PkgDB: Store cookies
by Pierre-YvesChibon
commit afda46ea257d3465f8311614cbaff799db8ed041
Author: Till Maas <opensource(a)till.name>
Date: Wed May 28 19:12:07 2014 +0200
PkgDB: Store cookies
Store cookies in pickle file if possible.
pkgdb2client.py | 39 +++++++++++++++++++++++++++++++++------
1 files changed, 33 insertions(+), 6 deletions(-)
---
diff --git a/pkgdb2client.py b/pkgdb2client.py
index 2e029bf..c797dbf 100644
--- a/pkgdb2client.py
+++ b/pkgdb2client.py
@@ -15,8 +15,13 @@
# license.
"""
+try:
+ import cPickle as pickle
+except ImportError:
+ import pickle
import getpass
import logging
+import os
import pkg_resources
import fedora_cert
@@ -90,8 +95,9 @@ class PkgDB(object):
'''
- def __init__(self, url=PKGDB_URL, insecure=False, login_callback=None,
- login_attempts=3):
+ def __init__(self, url=PKGDB_URL, insecure=False, cookies=None,
+ login_callback=None, login_attempts=3,
+ sessionfile="~/.cache/pkgdb-session.pickle"):
''' Constructor for the PkgDB object used to query the package
database.
@@ -118,11 +124,17 @@ class PkgDB(object):
self.url = url
self.session = requests.session()
self.insecure = insecure
- self.__logged_in = False
self.username = None
self.password = None
self.login_callback = login_callback
self.login_attempts = login_attempts
+ self.sessionfile = os.path.expanduser(sessionfile)
+
+ try:
+ with open(self.sessionfile, "rb") as sessionfo:
+ self.session.cookies = pickle.load(sessionfo)["cookies"]
+ except (IOError, KeyError, TypeError):
+ pass
def __send_request(self, url, method, params=None, data=None):
''' Send a http request to the provided URL with the provided
@@ -143,12 +155,29 @@ class PkgDB(object):
data=data,
verify=not self.insecure,
)
+ self._save_cookies()
return req
+ def _save_cookies(self):
+ try:
+ with open(self.sessionfile, 'rb') as sessionfo:
+ data = pickle.load(sessionfo)
+ except:
+ data = {}
+ try:
+ with open(self.sessionfile, 'wb', 0600) as sessionfo:
+ sessionfo.seek(0)
+ data["cookies"] = self.session.cookies
+ pickle.dump(data, sessionfo)
+ except:
+ pass
+
@property
def is_logged_in(self):
''' Return whether the user if logged in or not. '''
- return self.__logged_in
+
+ response = self.session.get(self.url + '/login/')
+ return "logged in as" in response.text
def login(self, username=None, password=None, openid_insecure=False,
response=None):
@@ -229,8 +258,6 @@ class PkgDB(object):
method='POST',
data=output['response'])
- self.__logged_in = True
-
return output
def call_api(self, path, params=None, data=None):
9 years, 11 months
[packagedb-cli: 3/10] pkgdb2_cli: Ask for password only if required
by Pierre-YvesChibon
commit 341896e1ee15d2509fe3a6efe23d25a99bed4228
Author: Till Maas <opensource(a)till.name>
Date: Fri May 30 10:05:00 2014 +0200
pkgdb2_cli: Ask for password only if required
Use login_callback to ask for username/password only when it is
required.
pkgdb2_cli.py | 40 ++++++++--------------------------------
pkgdb2client.py | 1 +
2 files changed, 9 insertions(+), 32 deletions(-)
---
diff --git a/pkgdb2_cli.py b/pkgdb2_cli.py
index e698dc7..8f5be19 100644
--- a/pkgdb2_cli.py
+++ b/pkgdb2_cli.py
@@ -18,17 +18,17 @@
from fedora.client import (AccountSystem, AppError, ServerError)
from bugzilla.rhbugzilla import RHBugzilla
from pkgdb2client import PkgDB, PkgDBException, __version__
+import pkgdb2client
import argparse
import logging
-import getpass
import koji
-import fedora_cert
KOJI_HUB = 'http://koji.fedoraproject.org/kojihub'
RH_BZ_API = 'https://bugzilla.redhat.com/xmlrpc.cgi'
-pkgdbclient = PkgDB('https://admin.fedoraproject.org/pkgdb')
+pkgdbclient = PkgDB('https://admin.fedoraproject.org/pkgdb',
+ login_callback=pkgdb2client.ask_password)
fasclient = AccountSystem('https://admin.fedoraproject.org/accounts')
BOLD = "\033[1m"
RED = "\033[0;31m"
@@ -48,31 +48,6 @@ class ActionError(Exception):
pass
-def __do_login(username=None, password=None):
- ''' Returned a BaseClient with authentification
-
- If the username is None, tries to retrieve it from fedora_cert.
-
- :arg pkgdbclient a PackageDB object to which username and password
- are added
- :karg username FAS username, if None it is asked to the user
- :karg password FAS password, if None it is asked to the user
- '''
- if pkgdbclient.is_logged_in:
- return
- else:
- if username is None and pkgdbclient.username is None:
- try:
- username = fedora_cert.read_user_cert()
- except:
- LOG.debug('Could not read Fedora cert, using login name')
- username = raw_input('FAS username: ')
- pkgdbclient.username = username
- if password is None and pkgdbclient.password is None:
- password = getpass.getpass('FAS password: ')
- pkgdbclient.login(username, password)
-
-
def _get_acls_info(acls):
''' Re-order the ACLs as provided by the PkgDB API in a way that can be
easily printed.
@@ -499,7 +474,7 @@ def do_orphan(args):
else:
branches = [args.branch]
- __do_login(args.username)
+ pkgdbclient.username = args.username
output = pkgdbclient.orphan_packages(pkgs, branches)
for msg in output.get('messages', []):
@@ -528,7 +503,7 @@ def do_unorphan(args):
else:
branches = [args.branch]
- __do_login(args.username)
+ pkgdbclient.username = args.username
username = args.poc or args.username or pkgdbclient.username
LOG.info("new poc : {0}".format(username))
@@ -563,7 +538,7 @@ def do_request(args):
if args.cancel:
status = 'Obsolete'
- __do_login(args.username)
+ pkgdbclient.username = args.username
LOG.info("user : {0}".format(pkgdbclient.username))
output = pkgdbclient.update_acl(
@@ -605,7 +580,7 @@ def do_update(args):
if args.approve:
status = "Approved"
- __do_login(args.username)
+ pkgdbclient.username = args.username
output = pkgdbclient.update_acl(
args.package,
@@ -669,6 +644,7 @@ def main():
insecure=True)
pkgdbclient = PkgDB(
'https://admin.stg.fedoraproject.org/pkgdb',
+ login_callback=pkgdb2client.ask_password,
insecure=True)
return_code = 0
diff --git a/pkgdb2client.py b/pkgdb2client.py
index 2e029bf..e74caf6 100644
--- a/pkgdb2client.py
+++ b/pkgdb2client.py
@@ -79,6 +79,7 @@ def ask_password(username=None, bad_password=False):
try:
username = fedora_cert.read_user_cert()
except fedora_cert.fedora_cert_error:
+ LOG.debug('Could not read Fedora cert, asking for username')
username = raw_input("Username: ")
password = getpass.getpass("FAS password for user {0}: ".format(username))
return username, password
9 years, 11 months
[packagedb-cli: 2/10] Merge pull request #12 from tyll/simplify
by Pierre-YvesChibon
commit 9e15b951c189abbd70180963a4097d4e7d0fcd5b
Merge: 2ef3c4a 210c90b
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Fri May 30 09:40:05 2014 +0200
Merge pull request #12 from tyll/simplify
pkgdb2client: Cleanup on-demand login code
pkgdb2client.py | 21 +++++++++++++++------
1 files changed, 15 insertions(+), 6 deletions(-)
---
9 years, 11 months
[packagedb-cli: 1/10] pkgdb2client: Cleanup on-demand login code
by Pierre-YvesChibon
commit 210c90b9e99d30436c0cab78bd4da1870c2151d7
Author: Till Maas <opensource(a)till.name>
Date: Thu May 29 17:55:57 2014 +0200
pkgdb2client: Cleanup on-demand login code
- Rename login_retries to login_attempts
- Allow to set login_attempts in constructor
- Add docstring for login_callback()
- Fix typo in exception
pkgdb2client.py | 21 +++++++++++++++------
1 files changed, 15 insertions(+), 6 deletions(-)
---
diff --git a/pkgdb2client.py b/pkgdb2client.py
index e2fa32f..2e029bf 100644
--- a/pkgdb2client.py
+++ b/pkgdb2client.py
@@ -90,7 +90,8 @@ class PkgDB(object):
'''
- def __init__(self, url=PKGDB_URL, insecure=False, login_callback=None):
+ def __init__(self, url=PKGDB_URL, insecure=False, login_callback=None,
+ login_attempts=3):
''' Constructor for the PkgDB object used to query the package
database.
@@ -102,6 +103,16 @@ class PkgDB(object):
is. Useful for development but should not be used in
production code.
:type insecure: bool
+ :kwarg login_callback: Function to be called to provide username and/or
+ password, it must accept two keyword arguments, `username` and
+ `bad_password`. Username is the currently known username (can be
+ None) and bad_password is a bool to specifies whether a bad
+ password was supplied earlier. It needs to return a (username,
+ password) tuple. See pkgdb2client.ask_password() for an example.
+ :type login_callback: function
+ :kwarg login_attempts: How often a login might fail after
+ login_callback() was called before a API call fails permanently.
+ :type login_attempts: int
'''
self.url = url
@@ -110,9 +121,8 @@ class PkgDB(object):
self.__logged_in = False
self.username = None
self.password = None
- # should accept username and bad_password
self.login_callback = login_callback
- self.login_retries = 3
+ self.login_attempts = login_attempts
def __send_request(self, url, method, params=None, data=None):
''' Send a http request to the provided URL with the provided
@@ -250,7 +260,7 @@ class PkgDB(object):
password = self.password
username = self.username
success = False
- for count in xrange(0, self.login_retries):
+ for count in xrange(self.login_attempts):
if not username or not password or bad_password:
if self.login_callback:
username, password = self.login_callback(
@@ -258,7 +268,7 @@ class PkgDB(object):
bad_password=bad_password
)
else:
- raise PkgDBAuthException('Authentication retired')
+ raise PkgDBAuthException('Authentication required')
try:
self.login(username=username, password=password,
response=response)
@@ -893,4 +903,3 @@ class PkgDB(object):
'poc': poc,
}
return self.handle_api_call('/package/acl/reassign/', data=args)
-
9 years, 11 months