aboutsummaryrefslogtreecommitdiff
path: root/lava_tool/authtoken.py
diff options
context:
space:
mode:
authorStevan Radaković <stevan.radakovic@linaro.org>2016-09-19 11:08:26 +0200
committerNeil Williams <neil.williams@linaro.org>2016-09-19 15:14:45 +0000
commitc5878ba3aa1697eb3bbfa17dcab927ec78fac4c6 (patch)
tree597637a6e9d4019ed3f8e2f63e8da546250c8edc /lava_tool/authtoken.py
parentc2aaff4e6291c746e6bdcf29f617b833d7a9d5fa (diff)
lava-tool auth management.
Add list and delete options for authentication tokens. List can display all or only from specific endpoint. Remove python-keyring dependency and use config parser to handle token storage. Change-Id: I83cb3ff092973769080a0725ef62bbef089b7593 Reviewed-on: https://review.linaro.org/14391 Reviewed-by: lava-bot <lava-bot@linaro.org> Reviewed-by: Neil Williams <neil.williams@linaro.org>
Diffstat (limited to 'lava_tool/authtoken.py')
-rw-r--r--lava_tool/authtoken.py74
1 files changed, 66 insertions, 8 deletions
diff --git a/lava_tool/authtoken.py b/lava_tool/authtoken.py
index 4aa539c..245b263 100644
--- a/lava_tool/authtoken.py
+++ b/lava_tool/authtoken.py
@@ -17,16 +17,24 @@
# along with lava-tool. If not, see <http://www.gnu.org/licenses/>.
import base64
+import errno
+import ConfigParser as configparser
import urllib
import urllib2
import os
import xmlrpclib
-import keyring.core
-
from lava_tool.interface import LavaCommandError
+DEFAULT_KEYRING_FILE = "%s/.local/share/lava-tool/keyring.cfg" % (
+ os.path.expanduser("~"))
+
+
+class TokenDeleteError(Exception):
+ """ Raise when token cannot be deleted. """
+
+
def normalize_xmlrpc_url(uri):
if '://' not in uri:
uri = 'http://' + uri
@@ -48,18 +56,68 @@ class AuthBackend(object):
class KeyringAuthBackend(AuthBackend):
+ def __init__(self, config_file=None):
+ if not config_file:
+ config_file = DEFAULT_KEYRING_FILE
+ self.config_file = config_file
+
+ self.config = configparser.RawConfigParser()
+ try:
+ os.makedirs(os.path.dirname(self.config_file))
+ except OSError as exception:
+ if exception.errno != errno.EEXIST:
+ raise
+ self.config.read(self.config_file)
+
def add_token(self, username, endpoint_url, token):
- keyring.core.set_password(
- "lava-tool-%s" % endpoint_url, username, token)
+ # update the keyring with the token
+ if not self.config.has_section(endpoint_url):
+ self.config.add_section(endpoint_url)
+ self.config.set(endpoint_url, username, token)
+
+ # save the config back to the file
+ with open(self.config_file, 'w') as config_file:
+ self.config.write(config_file)
def get_token_for_endpoint(self, username, endpoint_url):
+ # fetch the token
try:
- token = keyring.core.get_password(
- "lava-tool-%s" % endpoint_url, username)
- except ValueError as exc:
- raise LavaCommandError(exc)
+ token = self.config.get(endpoint_url, username)
+ except (configparser.NoOptionError, configparser.NoSectionError):
+ token = None
return token
+ def remove_token(self, username, endpoint_url):
+ try:
+ if not self.config.remove_option(endpoint_url, username):
+ raise TokenDeleteError("Username not found")
+ if not self.config.items(endpoint_url):
+ self.config.remove_section(endpoint_url)
+ except configparser.NoSectionError:
+ raise TokenDeleteError("Endpoint URL not found")
+
+ with open(self.config_file, 'w') as config_file:
+ self.config.write(config_file)
+
+ def get_all_tokens_uri(self, endpoint_url):
+ items = []
+ try:
+ items = self.config.items(endpoint_url)
+ except configparser.NoSectionError:
+ pass
+
+ return [item[0] for item in items]
+
+ def get_all_tokens(self):
+ items = {}
+
+ for section in self.config.sections():
+ if section not in items.keys():
+ items[section] = [item[0] for item in
+ self.config.items(section)]
+
+ return items
+
class MemoryAuthBackend(AuthBackend):