Merge lp:~hopem/charm-helpers/lp1518975 into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Superseded
Proposed branch: lp:~hopem/charm-helpers/lp1518975
Merge into: lp:charm-helpers
Diff against target: 175 lines (+84/-21)
2 files modified
charmhelpers/contrib/openstack/utils.py (+45/-13)
tests/contrib/openstack/test_openstack_utils.py (+39/-8)
To merge this branch: bzr merge lp:~hopem/charm-helpers/lp1518975
Reviewer Review Type Date Requested Status
James Page Needs Fixing
Review via email: mp+285734@code.launchpad.net

This proposal has been superseded by a proposal from 2016-02-11.

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Needs Fixing
lp:~hopem/charm-helpers/lp1518975 updated
530. By Edward Hope-Morley

import_pgp_key() > import_key()

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/contrib/openstack/utils.py'
--- charmhelpers/contrib/openstack/utils.py 2016-01-13 21:13:51 +0000
+++ charmhelpers/contrib/openstack/utils.py 2016-02-11 15:34:16 +0000
@@ -25,6 +25,7 @@
25import re25import re
2626
27import six27import six
28import tempfile
28import traceback29import traceback
29import uuid30import uuid
30import yaml31import yaml
@@ -41,6 +42,7 @@
41 config,42 config,
42 log as juju_log,43 log as juju_log,
43 charm_dir,44 charm_dir,
45 DEBUG,
44 INFO,46 INFO,
45 related_units,47 related_units,
46 relation_ids,48 relation_ids,
@@ -347,12 +349,42 @@
347349
348350
349def import_key(keyid):351def import_key(keyid):
350 cmd = "apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 " \352 key = keyid.strip()
351 "--recv-keys %s" % keyid353 if (key.startswith('-----BEGIN PGP PUBLIC KEY BLOCK-----') and
352 try:354 key.endswith('-----END PGP PUBLIC KEY BLOCK-----')):
353 subprocess.check_call(cmd.split(' '))355 juju_log("PGP key found (looks like ASCII Armor format)", level=DEBUG)
354 except subprocess.CalledProcessError:356 juju_log("Importing ASCII Armor PGP key", level=DEBUG)
355 error_out("Error importing repo key %s" % keyid)357 with tempfile.NamedTemporaryFile() as keyfile:
358 with open(keyfile.name, 'w') as fd:
359 fd.write(key)
360 fd.write("\n")
361
362 cmd = ['apt-key', 'add', keyfile.name]
363 try:
364 subprocess.check_call(cmd)
365 except subprocess.CalledProcessError:
366 error_out("Error importing PGP key '%s'" % key)
367 else:
368 juju_log("PGP key found (looks like Radix64 format)", level=DEBUG)
369 juju_log("Importing PGP key from keyserver", level=DEBUG)
370 cmd = ['apt-key', 'adv', '--keyserver',
371 'hkp://keyserver.ubuntu.com:80', '--recv-keys', key]
372 try:
373 subprocess.check_call(cmd)
374 except subprocess.CalledProcessError:
375 error_out("Error importing PGP key '%s'" % key)
376
377
378def get_source_and_pgp_key(input):
379 """Look for a pgp key ID or ascii-armor key in the given input."""
380 index = input.strip()
381 index = input.rfind('|')
382 if index < 0:
383 return input, None
384
385 key = input[index + 1:].strip('|')
386 source = input[:index]
387 return source, key
356388
357389
358def configure_installation_source(rel):390def configure_installation_source(rel):
@@ -364,16 +396,16 @@
364 with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:396 with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:
365 f.write(DISTRO_PROPOSED % ubuntu_rel)397 f.write(DISTRO_PROPOSED % ubuntu_rel)
366 elif rel[:4] == "ppa:":398 elif rel[:4] == "ppa:":
367 src = rel399 src, key = get_source_and_pgp_key(rel)
400 if key:
401 import_key(key)
402
368 subprocess.check_call(["add-apt-repository", "-y", src])403 subprocess.check_call(["add-apt-repository", "-y", src])
369 elif rel[:3] == "deb":404 elif rel[:3] == "deb":
370 l = len(rel.split('|'))405 src, key = get_source_and_pgp_key(rel)
371 if l == 2:406 if key:
372 src, key = rel.split('|')
373 juju_log("Importing PPA key from keyserver for %s" % src)
374 import_key(key)407 import_key(key)
375 elif l == 1:408
376 src = rel
377 with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:409 with open('/etc/apt/sources.list.d/juju_deb.list', 'w') as f:
378 f.write(src)410 f.write(src)
379 elif rel[:6] == 'cloud:':411 elif rel[:6] == 'cloud:':
380412
=== modified file 'tests/contrib/openstack/test_openstack_utils.py'
--- tests/contrib/openstack/test_openstack_utils.py 2016-01-13 21:13:51 +0000
+++ tests/contrib/openstack/test_openstack_utils.py 2016-02-11 15:34:16 +0000
@@ -1,6 +1,7 @@
1import io1import io
2import os2import os
3import subprocess3import subprocess
4import tempfile
4import unittest5import unittest
5from copy import copy6from copy import copy
6from testtools import TestCase7from testtools import TestCase
@@ -114,6 +115,21 @@
114115
115# Mock python-dnspython resolver used by get_host_ip()116# Mock python-dnspython resolver used by get_host_ip()
116117
118PGP_KEY_ASCII_ARMOR = """-----BEGIN PGP PUBLIC KEY BLOCK-----
119Version: SKS 1.1.5
120Comment: Hostname: keyserver.ubuntu.com
121
122mI0EUCEyTAEEAMuUxyfiegCCwn4J/c0nw5PUTSJdn5FqiUTq6iMfij65xf1vl0g/Mxqw0gfg
123AJIsCDvO9N9dloLAwF6FUBMg5My7WyhRPTAKF505TKJboyX3Pp4J1fU1LV8QFVOp87vUh1Rz
124B6GU7cSglhnbL85gmbJTllkzkb3h4Yw7W+edjcQ/ABEBAAG0K0xhdW5jaHBhZCBQUEEgZm9y
125IFVidW50dSBDbG91ZCBBcmNoaXZlIFRlYW2IuAQTAQIAIgUCUCEyTAIbAwYLCQgHAwIGFQgC
126CQoLBBYCAwECHgECF4AACgkQimhEop9oEE7kJAP/eTBgq3Mhbvo0d8elMOuqZx3nmU7gSyPh
127ep0zYIRZ5TJWl/7PRtvp0CJA6N6ZywYTQ/4ANHhpibcHZkh8K0AzUvsGXnJRSFoJeqyDbD91
128EhoO+4ZfHs2HvRBQEDZILMa2OyuB497E5Mmyua3HDEOrG2cVLllsUZzpTFCx8NgeMHk=
129=jLBm
130-----END PGP PUBLIC KEY BLOCK-----
131"""
132
117133
118class FakeAnswer(object):134class FakeAnswer(object):
119 def __init__(self, ip):135 def __init__(self, ip):
@@ -490,25 +506,40 @@
490 _e = 'Invalid Cloud Archive release specified: foo-bar'506 _e = 'Invalid Cloud Archive release specified: foo-bar'
491 mocked_error.assert_called_with(_e)507 mocked_error.assert_called_with(_e)
492508
493 def test_import_apt_key(self):509 @patch.object(openstack, 'juju_log', lambda *args, **kwargs: None)
510 def test_import_apt_key_radix(self):
494 '''Ensure shell out apt-key during key import'''511 '''Ensure shell out apt-key during key import'''
495 with patch('subprocess.check_call') as _subp:512 with patch('subprocess.check_call') as _subp:
496 openstack.import_key('foo')513 openstack.import_key('foo')
497 cmd = ['apt-key', 'adv', '--keyserver', 'hkp://keyserver.ubuntu.com:80',514 cmd = ['apt-key', 'adv', '--keyserver',
498 '--recv-keys', 'foo']515 'hkp://keyserver.ubuntu.com:80', '--recv-keys', 'foo']
499 _subp.assert_called_with(cmd)516 _subp.assert_called_with(cmd)
500517
518 @patch.object(openstack, 'juju_log', lambda *args, **kwargs: None)
519 def test_import_apt_key_ascii_armor(self):
520 with tempfile.NamedTemporaryFile() as tmp:
521 with patch.object(openstack, 'tempfile') as \
522 mock_tmpfile:
523 tmpfile = mock_tmpfile.NamedTemporaryFile.return_value
524 tmpfile.__enter__.return_value = tmpfile
525 tmpfile.name = tmp.name
526 with patch('subprocess.check_call') as _subp:
527 openstack.import_key(PGP_KEY_ASCII_ARMOR)
528 cmd = ['apt-key', 'add', tmp.name]
529 _subp.assert_called_with(cmd)
530
531 @patch.object(openstack, 'juju_log', lambda *args, **kwargs: None)
501 @patch('charmhelpers.contrib.openstack.utils.error_out')532 @patch('charmhelpers.contrib.openstack.utils.error_out')
502 def test_import_bad_apt_key(self, mocked_error):533 def test_import_bad_apt_key(self, mocked_error):
503 '''Ensure error when importing apt key fails'''534 '''Ensure error when importing apt key fails'''
504 with patch('subprocess.check_call') as _subp:535 with patch('subprocess.check_call') as _subp:
505 cmd = ['apt-key', 'adv', '--keyserver', 'hkp://keyserver.ubuntu.com:80',536 cmd = ['apt-key', 'adv', '--keyserver',
506 '--recv-keys', 'foo']537 'hkp://keyserver.ubuntu.com:80', '--recv-keys', 'foo']
507 _subp.side_effect = subprocess.CalledProcessError(1, cmd, '')538 _subp.side_effect = subprocess.CalledProcessError(1, cmd, '')
508 openstack.import_key('foo')539 openstack.import_key('foo')
509 cmd = ['apt-key', 'adv', '--keyserver', 'hkp://keyserver.ubuntu.com:80',540 cmd = ['apt-key', 'adv', '--keyserver',
510 '--recv-keys', 'foo']541 'hkp://keyserver.ubuntu.com:80', '--recv-keys', 'foo']
511 mocked_error.assert_called_with('Error importing repo key foo')542 mocked_error.assert_called_with("Error importing PGP key 'foo'")
512543
513 @patch('os.mkdir')544 @patch('os.mkdir')
514 @patch('os.path.exists')545 @patch('os.path.exists')

Subscribers

People subscribed via source and target branches