Merge lp:~alecu/ubuntu-sso-client/changed-secrets-spec into lp:ubuntu-sso-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 703
Merged at revision: 711
Proposed branch: lp:~alecu/ubuntu-sso-client/changed-secrets-spec
Merge into: lp:ubuntu-sso-client
Diff against target: 257 lines (+100/-41)
2 files modified
ubuntu_sso/utils/tests/test_txsecrets.py (+87/-38)
ubuntu_sso/utils/txsecrets.py (+13/-3)
To merge this branch: bzr merge lp:~alecu/ubuntu-sso-client/changed-secrets-spec
Reviewer Review Type Date Requested Status
Shane Fagan (community) Approve
Natalia Bidart (community) Approve
Review via email: mp+57947@code.launchpad.net

Commit message

Content_type field was added to the Secret DBus struct. (LP: #745540)

Description of the change

Content_type field was added to the Secret DBus struct. (LP: #745540)

To post a comment you must log in.
Revision history for this message
Alejandro J. Cura (alecu) wrote :

To field test this branch, you should install the gnome-keyring from gnome3 ppa.

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Code looks good, current keyring secret dbus service still works as expected.

review: Approve
Revision history for this message
Shane Fagan (shanepatrickfagan) wrote :

looks good to me for code review

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/utils/tests/test_txsecrets.py'
2--- ubuntu_sso/utils/tests/test_txsecrets.py 2011-04-12 14:16:56 +0000
3+++ ubuntu_sso/utils/tests/test_txsecrets.py 2011-04-15 20:07:58 +0000
4@@ -96,18 +96,6 @@
5 return True
6
7
8-class AltItemMock(ItemMock):
9- """The secret in this item has a content_type."""
10-
11- @dbus.service.method(dbus_interface=txsecrets.ITEM_IFACE,
12- in_signature="o", out_signature="(oayays)")
13- def GetSecret(self, session):
14- """Retrieve the secret for this item."""
15- if self.get_secret_fail:
16- raise SampleMiscException()
17- return (session, "", self.value, SAMPLE_CONTENT_TYPE)
18-
19-
20 class PromptMock(dbus.service.Object):
21 """A prompt necessary to complete an operation."""
22
23@@ -130,8 +118,8 @@
24 """The prompt and operation completed."""
25
26
27-class CollectionMock(dbus.service.Object):
28- """A collection of items containing secrets."""
29+class BaseCollectionMock(dbus.service.Object):
30+ """Base collection of items containing secrets."""
31 SUPPORTS_MULTIPLE_OBJECT_PATHS = True
32 SUPPORTS_MULTIPLE_CONNECTIONS = True
33 create_item_prompt = False
34@@ -143,14 +131,11 @@
35
36 def __init__(self, label, *args, **kwargs):
37 """Initialize this instance."""
38- super(CollectionMock, self).__init__(*args, **kwargs)
39+ super(BaseCollectionMock, self).__init__(*args, **kwargs)
40 self.items = []
41 self.label = label
42
43- @dbus.service.method(dbus_interface=txsecrets.COLLECTION_IFACE,
44- in_signature="a{sv}(oayay)b", out_signature="oo",
45- byte_arrays=True)
46- def CreateItem(self, properties, secret, replace):
47+ def _create_item(self, properties, secret, replace):
48 """Create an item with the given attributes, secret and label.
49
50 If replace is set, then it replaces an item already present with the
51@@ -162,7 +147,7 @@
52 raise SampleMiscException(ERROR_CREATE_BUT_LOCKED)
53 attributes = properties[txsecrets.ATTRIBUTES_PROPERTY]
54 item_label = properties[txsecrets.LABEL_PROPERTY]
55- session, parameters, value = secret
56+ value = secret[2]
57 item_path = create_object_path(make_coll_path(self.label))
58 item = self.dbus_publish(item_path, self.item_mock_class, self,
59 item_label, attributes, value)
60@@ -184,6 +169,18 @@
61 return dbus.String(self.label)
62
63
64+class CollectionMock(BaseCollectionMock):
65+ """Collection of items containing secrets."""
66+
67+ @dbus.service.method(dbus_interface=txsecrets.COLLECTION_IFACE,
68+ in_signature="a{sv}(oayay)b", out_signature="oo",
69+ byte_arrays=True)
70+ def CreateItem(self, properties, secret, replace):
71+ """Expose the _create_item method on DBus."""
72+ assert len(secret) == 3
73+ return self._create_item(properties, secret, replace)
74+
75+
76 class SessionMock(dbus.service.Object):
77 """A session tracks state between the service and a client application."""
78
79@@ -203,6 +200,7 @@
80 create_collection_fail = False
81 open_session_fail = False
82 dismissed = False
83+ collection_mock_class = CollectionMock
84
85 def __init__(self, *args, **kwargs):
86 """Initialize this instance."""
87@@ -230,7 +228,8 @@
88 raise SampleMiscException()
89 label = str(properties[txsecrets.LABEL_PROPERTY])
90 coll_path = make_coll_path(label)
91- collection = self.dbus_publish(coll_path, CollectionMock, label)
92+ collection = self.dbus_publish(coll_path, self.collection_mock_class,
93+ label)
94 self.collections[label] = collection
95
96 if self.create_collection_prompt:
97@@ -319,6 +318,38 @@
98 return dbus.Array(coll_paths, signature="o", variant_level=1)
99
100
101+class AltItemMock(ItemMock):
102+ """The secret in this item has a content_type."""
103+
104+ @dbus.service.method(dbus_interface=txsecrets.ITEM_IFACE,
105+ in_signature="o", out_signature="(oayays)")
106+ def GetSecret2(self, session):
107+ """Retrieve the secret for this item."""
108+ if self.get_secret_fail:
109+ raise SampleMiscException()
110+ return (session, "", self.value, SAMPLE_CONTENT_TYPE)
111+
112+
113+class AltCollectionMock(BaseCollectionMock):
114+ """The secrets in this collection have a content_type field."""
115+
116+ item_mock_class = AltItemMock
117+
118+ @dbus.service.method(dbus_interface=txsecrets.COLLECTION_IFACE,
119+ in_signature="a{sv}(oayays)b", out_signature="oo",
120+ byte_arrays=True)
121+ def CreateItem(self, properties, secret, replace):
122+ """Expose the _create_item method on DBus."""
123+ assert len(secret) == 4
124+ return self._create_item(properties, secret, replace)
125+
126+
127+class AltSecretServiceMock(SecretServiceMock):
128+ """The secrets in this service have a content_type field."""
129+
130+ collection_mock_class = AltCollectionMock
131+
132+
133 def create_object_path(base):
134 """Create a random object path given a base path."""
135 random = uuid.uuid4().hex
136@@ -328,12 +359,13 @@
137 class BaseTestCase(DBusTestCase):
138 """Base class for DBus tests."""
139 timeout = 10
140+ secret_service_class = SecretServiceMock
141
142 def setUp(self):
143 super(BaseTestCase, self).setUp()
144 self.session_bus = dbus.SessionBus()
145 self.mock_service = self.dbus_publish(txsecrets.SECRETS_SERVICE,
146- SecretServiceMock)
147+ self.secret_service_class)
148 self.secretservice = txsecrets.SecretService()
149
150 def dbus_publish(self, object_path, object_class, *args, **kwargs):
151@@ -760,22 +792,6 @@
152 self.assertEqual(value, sample_secret)
153
154 @inlineCallbacks
155- def test_get_value_four_items_per_secret(self):
156- """The code works fine when the secret dbus struct has 4 items."""
157- yield self.secretservice.open_session()
158- collection_label = "sample_keyring"
159- coll = yield self.create_sample_collection(collection_label)
160- mock_coll = self.mock_service.collections[collection_label]
161- mock_coll.item_mock_class = AltItemMock
162- attr = {"key-type": "Ubuntu SSO credentials"}
163- sample_secret = "secret83!"
164- yield coll.create_item("Cucaracha", attr, sample_secret)
165- items = yield self.secretservice.search_items(attr)
166- self.assertEqual(len(items), 1)
167- value = yield items[0].get_value()
168- self.assertEqual(value, sample_secret)
169-
170- @inlineCallbacks
171 def test_get_value_throws_dbus_error(self):
172 """The secret value is not retrieved if DBus fails."""
173 yield self.secretservice.open_session()
174@@ -853,3 +869,36 @@
175 mock_item.delete_fail = True
176 yield self.assertFailure(items[0].delete(),
177 dbus.exceptions.DBusException)
178+
179+
180+class AltItemTestCase(BaseTestCase):
181+ """Test the Item class with 4 fields in the secret struct."""
182+ secret_service_class = AltSecretServiceMock
183+
184+ @inlineCallbacks
185+ def test_create_item_four_fields_per_secret(self):
186+ """The collection creates an item when the dbus struct has 4 fields."""
187+ yield self.secretservice.open_session()
188+ collection_label = "sample_keyring"
189+ yield self.create_sample_collection(collection_label)
190+ coll = yield self.secretservice.get_default_collection()
191+ mock_collection = self.mock_service.collections[collection_label]
192+ attr = {"key-type": "Ubuntu 242 credentials"}
193+ sample_secret = "secret!"
194+ yield coll.create_item("Cucaracha", attr, sample_secret)
195+ self.assertEqual(len(mock_collection.items), 1)
196+ self.assertEqual(mock_collection.items[0].value, sample_secret)
197+
198+ @inlineCallbacks
199+ def test_get_value_four_fields_per_secret(self):
200+ """The code works fine when the secret dbus struct has 4 fields."""
201+ yield self.secretservice.open_session()
202+ collection_label = "sample_keyring"
203+ coll = yield self.create_sample_collection(collection_label)
204+ attr = {"key-type": "Ubuntu SSO credentials"}
205+ sample_secret = "secret83!"
206+ yield coll.create_item("Cucaracha", attr, sample_secret)
207+ items = yield self.secretservice.search_items(attr)
208+ self.assertEqual(len(items), 1)
209+ value = yield items[0].get_value()
210+ self.assertEqual(value, sample_secret)
211
212=== modified file 'ubuntu_sso/utils/txsecrets.py'
213--- ubuntu_sso/utils/txsecrets.py 2011-04-12 14:16:56 +0000
214+++ ubuntu_sso/utils/txsecrets.py 2011-04-15 20:07:58 +0000
215@@ -41,6 +41,7 @@
216 SECRETS_SERVICE = "/org/freedesktop/secrets"
217 DEFAULT_COLLECTION = "/org/freedesktop/secrets/aliases/default"
218 SESSION_COLLECTION = "/org/freedesktop/secrets/collection/session"
219+SECRET_CONTENT_TYPE = "application/octet-stream"
220
221 ALGORITHM = "plain"
222 ALGORITHM_PARAMS = ""
223@@ -257,7 +258,8 @@
224 """Initialize a new collection."""
225 self.service = service
226 self.object_path = object_path
227- collection_object = service.bus.get_object(BUS_NAME, object_path)
228+ collection_object = service.bus.get_object(BUS_NAME, object_path,
229+ introspect=False)
230 self.collection_iface = dbus.Interface(collection_object,
231 dbus_interface=COLLECTION_IFACE)
232 self.properties = dbus.Interface(collection_object,
233@@ -285,16 +287,24 @@
234 else:
235 d.callback(item)
236
237+ def createitem_error(error):
238+ """An error creating the item, try older signature."""
239+ secret = (self.service.session, parameters, value_bytes)
240+ self.collection_iface.CreateItem(properties, secret, replace,
241+ reply_handler=createitem_handler,
242+ error_handler=d.errback)
243+
244 properties = dbus.Dictionary(signature="sv")
245 properties[LABEL_PROPERTY] = label
246 attributes = dbus.Dictionary(attr, signature="ss")
247 properties[ATTRIBUTES_PROPERTY] = attributes
248 parameters = dbus.ByteArray(ALGORITHM_PARAMS)
249 value_bytes = dbus.ByteArray(value)
250- secret = (self.service.session, parameters, value_bytes)
251+ secret = (self.service.session, parameters, value_bytes,
252+ SECRET_CONTENT_TYPE)
253 self.collection_iface.CreateItem(properties, secret, replace,
254 reply_handler=createitem_handler,
255- error_handler=d.errback)
256+ error_handler=createitem_error)
257 return d
258
259

Subscribers

People subscribed via source and target branches