Skip to content

Commit dd5876a

Browse files
committed
bugzilla: Prefer caching creds in ~/.cache/python-bugzilla/
We still use the old files in ~/.<filename> if they are present, but otherwise we put new files in ~/.cache/python-bugzilla. Additionally add ~/.config/python-bugzilla/bugzillarc as the preferred rcfile. Resolves #4
1 parent 1fdbbef commit dd5876a

3 files changed

Lines changed: 61 additions & 51 deletions

File tree

bin/bugzilla

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,19 +550,20 @@ should work, like --bug_status becomes %{bug_status}, etc.
550550
.SH AUTHENTICATION COOKIES AND TOKENS
551551
552552
Older bugzilla instances use cookie-based authentication, and
553-
bugzilla.redhat.com uses a non-cookie token system.
553+
newer bugzilla instances (around 5.0) use a non-cookie token system.
554554
555555
When you log into bugzilla with the "login" subcommand or the "--login"
556-
argument, we cache the cookie in ~/.bugzillacookies. If you are using
557-
bugzilla.redhat.com, we also cache the token in ~/.bugzillatoken.
556+
argument, we cache the login credentials in ~/.cache/python-bugzilla/
557+
Previously we cached credentials in ~/.<filename>. If you want to see
558+
which file the tool is using, check --debug output.
558559
559560
To perform an authenticated bugzilla command on a new machine, run a one time
560561
"bugzilla login" to cache credentials before running the desired command. You
561562
can also run "bugzilla --login" and the login process will be initiated before
562563
invoking the command.
563564
564565
Additionally, the --no-cache-credentials option will tell the bugzilla tool to
565-
_not_ save any credentials to ~/.bugzillacookies or ~/.bugzillatoken.
566+
_not_ save any credentials in $HOME, or use any previously cached credentials.
566567
567568
.SH EXAMPLES
568569
.PP

bugzilla/base.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ def _detect_filetype(fname):
7272
return None
7373

7474

75+
def _default_auth_location(filename):
76+
"""
77+
Determine auth location for filename, like 'bugzillacookies'. If
78+
old style ~/.bugzillacookies exists, we use that, otherwise we
79+
use ~/.cache/python-bugzilla/bugzillacookies. Same for bugzillatoken
80+
"""
81+
homepath = os.path.expanduser("~/.%s" % filename)
82+
xdgpath = os.path.expanduser("~/.cache/python-bugzilla/%s" % filename)
83+
if os.path.exists(xdgpath):
84+
return xdgpath
85+
if os.path.exists(homepath):
86+
return homepath
87+
88+
if not os.path.exists(os.path.dirname(xdgpath)):
89+
os.makedirs(os.path.dirname(xdgpath), 0o700)
90+
return xdgpath
91+
92+
7593
def _build_cookiejar(cookiefile):
7694
cj = MozillaCookieJar(cookiefile)
7795
if cookiefile is None:
@@ -137,9 +155,12 @@ class Bugzilla(object):
137155
get cookies this way, you will be considered logged in until the cookie
138156
expires.
139157
140-
You may also specify 'user' and 'password' in a bugzillarc file, either
141-
/etc/bugzillarc or ~/.bugzillarc. The latter will override the former.
142-
The format works like this:
158+
You may also specify 'user' and 'password' in a bugzillarc file. The
159+
locations are preferred in this order:
160+
~/.config/python-bugzilla/bugzillarc
161+
~/.bugzillarc
162+
/etc/bugzillarc
163+
It has content like:
143164
[bugzilla.yoursite.com]
144165
user = username
145166
password = password
@@ -249,16 +270,17 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
249270
self._field_aliases = []
250271
self._init_field_aliases()
251272

252-
self.configpath = ['/etc/bugzillarc', '~/.bugzillarc']
273+
self.configpath = ['/etc/bugzillarc', '~/.bugzillarc',
274+
'~/.config/python-bugzilla/bugzillarc']
253275
if not use_creds:
254276
cookiefile = None
255277
tokenfile = None
256278
self.configpath = []
257279

258280
if cookiefile == -1:
259-
cookiefile = os.path.expanduser('~/.bugzillacookies')
281+
cookiefile = _default_auth_location("bugzillacookies")
260282
if tokenfile == -1:
261-
tokenfile = os.path.expanduser("~/.bugzillatoken")
283+
tokenfile = _default_auth_location("bugzillatoken")
262284
log.debug("Using tokenfile=%s", tokenfile)
263285
self.cookiefile = cookiefile
264286
self.tokenfile = tokenfile

tests/rw_functional.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class RHPartnerTest(BaseTest):
6565
# Despite its name, this instance is simply for bugzilla testing,
6666
# doesn't send out emails and is blown away occasionally. The front
6767
# page has some info.
68-
url = tests.REDHAT_URL or "https://partner-bugzilla.redhat.com/xmlrpc.cgi"
68+
url = tests.REDHAT_URL or "partner-bugzilla.redhat.com"
6969
bzclass = bugzilla.RHBugzilla
7070

7171

@@ -78,22 +78,24 @@ def _check_have_admin(self, bz, funcname):
7878
print("\nNo admin privs, reduced testing of %s" % funcname)
7979
return ret
8080

81-
def _check_rh_privs(self, bz, funcname, authtype, quiet=False):
82-
noprivs = bool(bz.getbugs([184858]) == [None])
83-
if noprivs and not quiet:
84-
print("\nNo RH %s privs, skipping %s" % (authtype, funcname))
85-
return not noprivs
81+
test2 = BaseTest._testBZClass
8682

8783

88-
test1 = BaseTest._testCookieOrToken
89-
test2 = BaseTest._testBZClass
84+
def test00LoginState(self):
85+
bz = self.bzclass(url=self.url)
86+
self.assertTrue(bz.logged_in,
87+
"R/W tests require cached login credentials for url=%s" % self.url)
88+
89+
bz = self.bzclass(url=self.url, use_creds=False)
90+
self.assertFalse(bz.logged_in,
91+
"Login state check failed for logged out user.")
9092

9193

9294
def test03NewBugBasic(self):
9395
"""
9496
Create a bug with minimal amount of fields, then close it
9597
"""
96-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
98+
bz = self.bzclass(url=self.url)
9799
component = "python-bugzilla"
98100
version = "rawhide"
99101
summary = ("python-bugzilla test basic bug %s" %
@@ -131,7 +133,7 @@ def test04NewBugAllFields(self):
131133
"""
132134
Create a bug using all 'new' fields, check some values, close it
133135
"""
134-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
136+
bz = self.bzclass(url=self.url)
135137

136138
summary = ("python-bugzilla test manyfields bug %s" %
137139
datetime.datetime.today())
@@ -195,7 +197,7 @@ def test05ModifyStatus(self):
195197
"""
196198
Modify status and comment fields for an existing bug
197199
"""
198-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
200+
bz = self.bzclass(url=self.url)
199201
bugid = "663674"
200202
cmd = "bugzilla modify %s " % bugid
201203

@@ -282,7 +284,7 @@ def test06ModifyEmails(self):
282284
"""
283285
Modify cc, assignee, qa_contact for existing bug
284286
"""
285-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
287+
bz = self.bzclass(url=self.url)
286288
bugid = "663674"
287289
cmd = "bugzilla modify %s " % bugid
288290

@@ -330,7 +332,7 @@ def test07ModifyMultiFlags(self):
330332
"""
331333
Modify flags and fixed_in for 2 bugs
332334
"""
333-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
335+
bz = self.bzclass(url=self.url)
334336
bugid1 = "461686"
335337
bugid2 = "461687"
336338
cmd = "bugzilla modify %s %s " % (bugid1, bugid2)
@@ -406,7 +408,7 @@ def cleardict(b):
406408
def test07ModifyMisc(self):
407409
bugid = "461686"
408410
cmd = "bugzilla modify %s " % bugid
409-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
411+
bz = self.bzclass(url=self.url)
410412
bug = bz.getbug(bugid)
411413

412414
# modify --dependson
@@ -508,7 +510,7 @@ def _test8Attachments(self):
508510
"""
509511
Get and set attachments for a bug
510512
"""
511-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
513+
bz = self.bzclass(url=self.url)
512514
getallbugid = "663674"
513515
setbugid = "461686"
514516
cmd = "bugzilla attach "
@@ -585,7 +587,7 @@ def _test8Attachments(self):
585587

586588

587589
def test09Whiteboards(self):
588-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
590+
bz = self.bzclass(url=self.url)
589591
bug_id = "663674"
590592
cmd = "bugzilla modify %s " % bug_id
591593
bug = bz.getbug(bug_id)
@@ -687,19 +689,9 @@ def fakegetpass(prompt):
687689
getpass.getpass = oldgetpass
688690

689691

690-
def test10LoginState(self):
691-
bz = self.bzclass(url=self.url, use_creds=False)
692-
self.assertFalse(bz.logged_in,
693-
"Login state check failed for logged out user.")
694-
695-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
696-
self.assertTrue(bz.logged_in,
697-
"Login state check failed for logged in user.")
698-
699-
700692
def test11UserUpdate(self):
701693
# This won't work if run by the same user we are using
702-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
694+
bz = self.bzclass(url=self.url)
703695
704696
group = "fedora_contrib"
705697

@@ -761,7 +753,7 @@ def test11UserUpdate(self):
761753

762754

763755
def test11ComponentEditing(self):
764-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
756+
bz = self.bzclass(url=self.url)
765757
component = ("python-bugzilla-testcomponent-%s" %
766758
str(random.randint(1, 1024 * 1024 * 1024)))
767759
basedata = {
@@ -833,11 +825,7 @@ def compare(data, newid):
833825

834826
def test12SetCookie(self):
835827
bz = self.bzclass("partner-bugzilla.redhat.com",
836-
cookiefile=cf, tokenfile=None)
837-
838-
fn = sys._getframe().f_code.co_name # pylint: disable=protected-access
839-
if not self._check_rh_privs(bz, "cookie", fn):
840-
return
828+
cookiefile=-1, tokenfile=None)
841829

842830
try:
843831
bz.cookiefile = None
@@ -850,11 +838,10 @@ def test12SetCookie(self):
850838
bz.disconnect()
851839
bz.cookiefile = None
852840
bz.connect()
853-
self.assertFalse(bool(self._check_rh_privs(
854-
bz, "", "cookie", quiet=True)))
841+
self.assertFalse(bz.logged_in)
855842

856843
def test13SubComponents(self):
857-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
844+
bz = self.bzclass(url=self.url)
858845
# Long closed RHEL5 lvm2 bug. This component has sub_components
859846
bug = bz.getbug("185526")
860847
bug.autorefresh = True
@@ -871,13 +858,13 @@ def test13SubComponents(self):
871858
self.assertEqual(bug.sub_components, {})
872859

873860
def _deleteAllExistingExternalTrackers(self, bugid):
874-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
861+
bz = self.bzclass(url=self.url)
875862
ids = [bug['id'] for bug in bz.getbug(bugid).external_bugs]
876863
if ids != []:
877864
bz.remove_external_tracker(ids=ids)
878865

879866
def test14ExternalTrackersQuery(self):
880-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
867+
bz = self.bzclass(url=self.url)
881868
bugid = 461686
882869
ext_bug_id = 1234659
883870

@@ -925,7 +912,7 @@ def test14ExternalTrackersQuery(self):
925912
assert bugid in [qr.id for qr in query_results]
926913

927914
def test14ExternalTrackersAddUpdateRemoveQuery(self):
928-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
915+
bz = self.bzclass(url=self.url)
929916
bugid = 461686
930917
ext_bug_id = 380489
931918

@@ -973,14 +960,14 @@ def test14ExternalTrackersAddUpdateRemoveQuery(self):
973960
assert len(ids) == 0
974961

975962
def test15EnsureLoggedIn(self):
976-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
963+
bz = self.bzclass(url=self.url)
977964
comm = "bugzilla --ensure-logged-in query --bug_id 979546"
978965
tests.clicomm(comm, bz)
979966

980967
def test16ModifyTags(self):
981968
bugid = "461686"
982969
cmd = "bugzilla modify %s " % bugid
983-
bz = self.bzclass(url=self.url, cookiefile=cf, tokenfile=tf)
970+
bz = self.bzclass(url=self.url)
984971
bug = bz.getbug(bugid)
985972

986973
if bug.tags:

0 commit comments

Comments
 (0)