Merge lp:~mikemc/ubuntuone-storage-protocol/fix-1025950-cert-locs into lp:ubuntuone-storage-protocol

Proposed by Mike McCracken
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 161
Merged at revision: 154
Proposed branch: lp:~mikemc/ubuntuone-storage-protocol/fix-1025950-cert-locs
Merge into: lp:ubuntuone-storage-protocol
Prerequisite: lp:~mikemc/ubuntuone-storage-protocol/fix-run-tests
Diff against target: 146 lines (+73/-23)
3 files modified
setup.py (+2/-2)
tests/test_context.py (+44/-0)
ubuntuone/storageprotocol/context.py (+27/-21)
To merge this branch: bzr merge lp:~mikemc/ubuntuone-storage-protocol/fix-1025950-cert-locs
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
dobey (community) Approve
Review via email: mp+118603@code.launchpad.net

Commit message

- Add support for finding SSL certs in packaged mac app. (LP: #1025950)

Description of the change

- Add support for finding SSL certs in packaged mac app. (LP: #1025950)

This branch also adds tests for the ssl cert path finding code for windows and linux.

Tested on all three.

TO TEST:

1. run-tests or run-tests.bat
2. on darwin, use setup-mac.py to create a packaged mac app and run it from command line using something like this:

% U1_DEBUG=1 dist/ubuntuone-control-panel-qt/UbuntuOne.app/Contents/MacOS/ubuntuone-control-panel-qt

Look for any errors regarding the certs path -- there should be none.
(Prior to this branch, you would see something about /etc/ssl/certs not existing)

To post a comment you must log in.
Revision history for this message
dobey (dobey) wrote :

153 + if sys.platform == "win32":
154 + # First open the registry hive
155 + hive = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
156 + # Open the registry key where Windows stores the Shell Folder locations
157 + key = _winreg.OpenKey(hive, str(
158 + "Software\\Microsoft\\Windows\\CurrentVersion"
159 + "\\Explorer\\Shell Folders"))
160 +
161 + ssl_cert_location = None
162 + for i in range(0, _winreg.QueryInfoKey(key)[1]):
163 + name, value, val_type = _winreg.EnumValue(key, i)
164 + # Common AppData will always be present unless the user
165 + # played with regedit
166 + if name == "Common AppData":
167 + ssl_cert_location = os.path.join(value,
168 + "ubuntuone-storageprotocol")
169 + break

Instead of duplicating all this registry poking code, would it not be better here to use the load_config_paths()[0] value from dirspec.basedir, as it already pokes at the registry to find the standard paths?

review: Needs Information
157. By Mike McCracken

Remove env vars due to formerly broken buildout

158. By Mike McCracken

Add back SYSNAME so we skip cpp tests

159. By Mike McCracken

merge with branch

160. By Mike McCracken

simplify windows code using dirspec

Revision history for this message
Mike McCracken (mikemc) wrote :

> Instead of duplicating all this registry poking code, would it not be better
> here to use the load_config_paths()[0] value from dirspec.basedir, as it
> already pokes at the registry to find the standard paths?

I looked into it and dirspec pokes differently, but apparently comes up with the same answer. So, I'm not sure why this code loops over keys like this.

I've updated it to use dirspec on win32.

Revision history for this message
dobey (dobey) :
review: Approve
161. By Mike McCracken

merge with fix-run-tests

Revision history for this message
Alejandro J. Cura (alecu) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py'
2--- setup.py 2012-06-12 17:03:26 +0000
3+++ setup.py 2012-08-15 19:40:28 +0000
4@@ -26,7 +26,7 @@
5 from distutils.core import setup
6 from distutils.spawn import find_executable
7 from distutils.command import clean, build
8-from ubuntuone.storageprotocol.context import ssl_cert_location
9+from ubuntuone.storageprotocol.context import get_cert_location
10
11
12 class StorageProtocolBuild(build.build):
13@@ -74,7 +74,7 @@
14 packages=['ubuntuone',
15 'ubuntuone.storageprotocol'],
16 extra_path='ubuntuone-storage-protocol',
17- data_files=[(ssl_cert_location,
18+ data_files=[(get_cert_location(),
19 ['data/UbuntuOne-Go_Daddy_CA.pem',
20 'data/ValiCert_Class_2_VA.pem',
21 'data/UbuntuOne-Go_Daddy_Class_2_CA.pem'])],
22
23=== modified file 'tests/test_context.py'
24--- tests/test_context.py 2012-08-15 19:40:28 +0000
25+++ tests/test_context.py 2012-08-15 19:40:28 +0000
26@@ -28,6 +28,7 @@
27 # files in the program, then also delete it here.
28
29 import os
30+import sys
31
32 from OpenSSL import crypto, SSL
33 from twisted.internet import defer, error, reactor, ssl
34@@ -197,3 +198,46 @@
35 hostname="localhost")
36
37 yield self.verify_context(server_context, client_context)
38+
39+
40+class SSLCertLocationTestCase(unittest.TestCase):
41+ """Test determining the cert location."""
42+
43+ @defer.inlineCallbacks
44+ def setUp(self):
45+ yield super(SSLCertLocationTestCase, self).setUp()
46+
47+ def test_win(self):
48+ """Test geting a path when Common AppData is defined."""
49+ self.patch(context, "load_config_paths",
50+ lambda x: [os.path.join("returned-value", x)])
51+ self.patch(sys, "platform", "win32")
52+ path = context.get_cert_location()
53+ self.assertEqual(path, os.path.join("returned-value",
54+ "ubuntuone-storageprotocol"))
55+
56+ def test_darwin_frozen(self):
57+ """Test that we get a path with .app in it on frozen darwin."""
58+ self.patch(sys, "platform", "darwin")
59+ sys.frozen = "macosx-app"
60+ self.addCleanup(delattr, sys, "frozen")
61+ self.patch(context, "__file__",
62+ os.path.join("path", "to", "Main.app", "ignore"))
63+ path = context.get_cert_location()
64+ self.assertEqual(path, os.path.join("path", "to", "Main.app",
65+ "Contents", "Resources"))
66+
67+ def test_darwin_unfrozen(self):
68+ """Test that we get a source-relative path on unfrozen darwin."""
69+ self.patch(sys, "platform", "darwin")
70+ self.patch(context, "__file__",
71+ os.path.join("path", "to", "ubuntuone",
72+ "storageprotocol", "context.py"))
73+ path = context.get_cert_location()
74+ self.assertEqual(path, os.path.join("path", "to", "data"))
75+
76+ def test_linux(self):
77+ """Test that linux gets the right path."""
78+ self.patch(sys, "platform", "linux2")
79+ path = context.get_cert_location()
80+ self.assertEqual(path, "/etc/ssl/certs")
81
82=== modified file 'ubuntuone/storageprotocol/context.py'
83--- ubuntuone/storageprotocol/context.py 2012-06-12 16:32:06 +0000
84+++ ubuntuone/storageprotocol/context.py 2012-08-15 19:40:28 +0000
85@@ -36,27 +36,32 @@
86 from twisted.internet import error, ssl
87 from twisted.python import log
88
89-if sys.platform == "win32":
90- # diable pylint warning, as it may be the wrong platform
91- # pylint: disable=F0401
92- import _winreg
93-
94- # First open the registry hive
95- hive = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
96- # Open the registry key where Windows stores the Shell Folder locations
97- key = _winreg.OpenKey(hive, str(
98- "Software\\Microsoft\\Windows\\CurrentVersion"
99- "\\Explorer\\Shell Folders"))
100- for i in range(0, _winreg.QueryInfoKey(key)[1]):
101- name, value, val_type = _winreg.EnumValue(key, i)
102- # Common AppData will always be present unless the user
103- # played with regedit
104- if name == "Common AppData":
105- ssl_cert_location = os.path.join(value,
106- "ubuntuone-storageprotocol")
107- break
108-else:
109- ssl_cert_location = '/etc/ssl/certs'
110+from dirspec.basedir import load_config_paths
111+
112+
113+def get_cert_location():
114+ """Return path to certificate files."""
115+
116+ if sys.platform == "win32":
117+ ssl_cert_location = list(load_config_paths(
118+ "ubuntuone-storageprotocol"))[0]
119+
120+ elif sys.platform == "darwin":
121+ if getattr(sys, "frozen", None) is not None:
122+ main_app_dir = "".join(__file__.partition(".app")[:-1])
123+ main_app_resources_dir = os.path.join(main_app_dir,
124+ "Contents",
125+ "Resources")
126+ ssl_cert_location = main_app_resources_dir
127+ else:
128+ pkg_dir = os.path.dirname(__file__)
129+ src_tree_path = os.path.dirname(os.path.dirname(pkg_dir))
130+ ssl_cert_location = os.path.join(src_tree_path,
131+ "data")
132+ else:
133+ ssl_cert_location = '/etc/ssl/certs'
134+
135+ return ssl_cert_location
136
137
138 class HostnameVerifyContextFactory(ssl.CertificateOptions):
139@@ -88,6 +93,7 @@
140
141 def get_certificates():
142 """Get a list of certificate paths."""
143+ ssl_cert_location = get_cert_location()
144 ca_file = ssl.Certificate.loadPEM(file(os.path.join(ssl_cert_location,
145 'UbuntuOne-Go_Daddy_Class_2_CA.pem'), 'r').read())
146 ca_file_2 = ssl.Certificate.loadPEM(file(os.path.join(ssl_cert_location,

Subscribers

People subscribed via source and target branches