Skip to content

Commit

Permalink
Extract method config_get_password
Browse files Browse the repository at this point in the history
This extract duplicated code in the backens that deals with parsing
secret config fields (passwords/tokens).
  • Loading branch information
gdetrez committed Feb 18, 2016
1 parent 544dae6 commit cfd85ab
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 81 deletions.
13 changes: 12 additions & 1 deletion bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from taskw.task import Task

from bugwarrior.config import asbool
from bugwarrior.config import asbool, get_service_password
from bugwarrior.db import MARKUP, URLShortener, ABORT_PROCESSING


Expand Down Expand Up @@ -124,6 +124,9 @@ def get_templates(self):
templates[key] = self.config.get(self.target, template_key)
return templates

def config_has(self, key):
return self.config.has_option(self.target, self._get_key(key))

def config_get_default(self, key, default=None, to_type=None):
try:
return self.config_get(key, to_type=to_type)
Expand All @@ -136,6 +139,14 @@ def config_get(self, key=None, to_type=None):
return to_type(value)
return value

def config_get_password(self, key, keyring_service, login):
password = self.config_get_default(key)
if not password or password.startswith("@oracle:"):
password = get_service_password(
keyring_service, login, oracle=password,
interactive=self.config.interactive)
return password

@classmethod
def _get_key(cls, key):
return '%s.%s' % (cls.CONFIG_PREFIX, key)
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from twiggy import log

from bugwarrior.services import IssueService, Issue
from bugwarrior.config import asbool, die, get_service_password
from bugwarrior.config import asbool, die


class BitbucketIssue(Issue):
Expand Down Expand Up @@ -68,13 +68,10 @@ def __init__(self, *args, **kw):
self.auth = None
if self.config_get_default('login'):
login = self.config_get('login')
password = self.config_get_default('password')
if not password or password.startswith('@oracle:'):
username = self.config_get('username')
password = get_service_password(
self.get_keyring_service(self.config, self.target),
login, oracle=password,
interactive=self.config.interactive)
password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
login)

self.auth = (login, password)

Expand Down
12 changes: 5 additions & 7 deletions bugwarrior/services/bz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime
import six

from bugwarrior.config import die, asbool, get_service_password
from bugwarrior.config import die, asbool
from bugwarrior.services import IssueService, Issue


Expand Down Expand Up @@ -124,12 +124,10 @@ def __init__(self, *args, **kw):
# to pass that argument or not.
self.advanced = asbool(self.config_get_default('advanced', 'no'))

if not self.password or self.password.startswith("@oracle:"):
self.password = get_service_password(
self.get_keyring_service(self.config, self.target),
self.username, oracle=self.password,
interactive=self.config.interactive
)
self.password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
self.username)

url = 'https://%s/xmlrpc.cgi' % self.base_uri
self.bz = bugzilla.Bugzilla(url=url)
Expand Down
27 changes: 10 additions & 17 deletions bugwarrior/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jinja2 import Template
from twiggy import log

from bugwarrior.config import asbool, die, get_service_password
from bugwarrior.config import asbool, die
from bugwarrior.services import IssueService, Issue

from . import githubutils
Expand Down Expand Up @@ -134,24 +134,17 @@ def __init__(self, *args, **kw):

login = self.config_get('login')
token = self.config_get_default('token')
if token:
if token.startswith('@oracle:'):
username = self.config_get('username')
token = get_service_password(
self.get_keyring_service(self.config, self.target),
login, oracle=token,
interactive=self.config.interactive
)
if self.config_has('token'):
token = self.config_get_password(
'token',
self.get_keyring_service(self.config, self.target),
login)
self.auth['token'] = token
else:
password = self.config_get_default('password')
if not password or password.startswith('@oracle:'):
username = self.config_get('username')
password = get_service_password(
self.get_keyring_service(self.config, self.target),
login, oracle=password,
interactive=self.config.interactive
)
password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
login)
self.auth['basic'] = (login, password)

self.exclude_repos = []
Expand Down
11 changes: 3 additions & 8 deletions bugwarrior/services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from jinja2 import Template
from twiggy import log

from bugwarrior.config import asbool, die, get_service_password
from bugwarrior.config import asbool, die
from bugwarrior.services import IssueService, Issue


Expand Down Expand Up @@ -189,13 +189,8 @@ def __init__(self, *args, **kw):
host = self.config_get_default(
'host', default='gitlab.com', to_type=six.text_type)
login = self.config_get('login')
token = self.config_get('token')
if token.startswith('@oracle:'):
token = get_service_password(
self.get_keyring_service(login, host),
login, oracle=token,
interactive=self.config.interactive
)
token = self.config_get_password(
'token', self.get_keyring_service(login, host), login)
self.auth = (host, token)

if self.config_get_default('use_https', default=True, to_type=asbool):
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from jira.client import JIRA
import six

from bugwarrior.config import asbool, die, get_service_password
from bugwarrior.config import asbool, die
from bugwarrior.services import IssueService, Issue


Expand Down Expand Up @@ -131,13 +131,10 @@ def __init__(self, *args, **kw):
super(JiraService, self).__init__(*args, **kw)
self.username = self.config_get('username')
self.url = self.config_get('base_uri')
password = self.config_get('password')
if not password or password.startswith("@oracle:"):
password = get_service_password(
self.get_keyring_service(self.config, self.target),
self.username, oracle=password,
interactive=self.config.interactive
)
password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
self.username)

default_query = 'assignee=' + self.username + \
' AND resolution is null'
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/mplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import megaplan
from twiggy import log

from bugwarrior.config import die, get_service_password
from bugwarrior.config import die
from bugwarrior.services import IssueService, Issue


Expand Down Expand Up @@ -73,13 +73,10 @@ def __init__(self, *args, **kw):

self.hostname = self.config_get('hostname')
_login = self.config_get('login')
_password = self.config_get('password')
if not _password or _password.startswith("@oracle:"):
_password = get_service_password(
self.get_keyring_service(self.config, self.target),
_login, oracle=_password,
interactive=self.config.interactive
)
_password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
_login)

self.client = megaplan.Client(self.hostname)
self.client.authenticate(_login, _password)
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/teamlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import six
from twiggy import log

from bugwarrior.config import die, get_service_password
from bugwarrior.config import die
from bugwarrior.services import Issue, IssueService


Expand Down Expand Up @@ -122,13 +122,10 @@ def __init__(self, *args, **kw):

self.hostname = self.config_get('hostname')
_login = self.config_get('login')
_password = self.config_get('password')
if not _password or _password.startswith("@oracle:"):
_password = get_service_password(
self.get_keyring_service(self.config, self.target),
_login, oracle=_password,
interactive=self.config.interactive
)
_password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
_login)

self.client = TeamLabClient(self.hostname)
self.client.authenticate(_login, _password)
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/trac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from twiggy import log
import urllib

from bugwarrior.config import die, get_service_password
from bugwarrior.config import die
from bugwarrior.services import Issue, IssueService


Expand Down Expand Up @@ -80,13 +80,10 @@ def __init__(self, *args, **kw):
scheme = self.config_get_default('scheme', default='https')
username = self.config_get_default('username', default=None)
if username:
password = self.config_get('password')
if not password or password.startswith('@oracle:'):
password = get_service_password(
self.get_keyring_service(self.config, self.target),
username, oracle=password,
interactive=self.config.interactive
)
password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
username)

auth = urllib.quote_plus('%s:%s' % (username, password)) + '@'
else:
Expand Down
13 changes: 5 additions & 8 deletions bugwarrior/services/versionone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from six.moves.urllib import parse

from bugwarrior.services import IssueService, Issue, LOCAL_TIMEZONE
from bugwarrior.config import die, get_service_password
from bugwarrior.config import die


class VersionOneIssue(Issue):
Expand Down Expand Up @@ -186,13 +186,10 @@ def __init__(self, *args, **kw):
self.address = parsed_address.netloc
self.instance = parsed_address.path.strip('/')
self.username = self.config_get('username')
self.password = self.config_get_default('password')
if not self.password or self.password.startswith('@oracle:'):
self.password = get_service_password(
self.get_keyring_service(self.config, self.target),
self.username, oracle=self.password,
interactive=self.config.interactive
)
self.password = self.config_get_password(
'password',
self.get_keyring_service(self.config, self.target),
self.username)

self.timezone = self.config_get_default(
'timezone',
Expand Down

0 comments on commit cfd85ab

Please sign in to comment.