Merge lp:~chipaca/ubuntu-push/out-with-the-new-in-with-the-oh-wait into lp:ubuntu-push/automatic

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: 139
Merged at revision: 138
Proposed branch: lp:~chipaca/ubuntu-push/out-with-the-new-in-with-the-oh-wait
Merge into: lp:ubuntu-push/automatic
Diff against target: 374 lines (+55/-140)
11 files modified
Makefile (+0/-2)
README (+0/-4)
client/client.go (+0/-2)
client/client_test.go (+5/-2)
client/session/session.go (+21/-15)
client/session/session_test.go (+29/-17)
debian/control (+0/-4)
dependencies.tsv (+0/-2)
ubuntu-push-client.go (+0/-3)
util/auth.go (+0/-36)
util/auth_test.go (+0/-53)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/out-with-the-new-in-with-the-oh-wait
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+217790@code.launchpad.net

Commit message

Move to an external helper for auth bits.

Description of the change

Move to an external helper for auth bits.

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

123 + if sess.AuthHelper == nil || len(sess.AuthHelper) == 0 {

len() is enough, len of a nil slice is legal in go and also gives 0

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve
139. By John Lenton

len() of nil slice is legal and zero

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2014-04-15 14:43:02 +0000
3+++ Makefile 2014-05-01 09:16:09 +0000
4@@ -11,8 +11,6 @@
5 GODEPS += launchpad.net/go-dbus/v1
6 GODEPS += launchpad.net/go-xdg/v0
7 GODEPS += code.google.com/p/gosqlite/sqlite3
8-GODEPS += gopkg.in/qml.v0
9-GODEPS += gopkg.in/niemeyer/uoneauth.v1
10
11 TOTEST = $(shell env GOPATH=$(GOPATH) go list $(PROJECT)/...|grep -v acceptance|grep -v http13client )
12
13
14=== modified file 'README'
15--- README 2014-04-28 08:12:30 +0000
16+++ README 2014-05-01 09:16:09 +0000
17@@ -10,10 +10,6 @@
18
19 build-essential
20 libsqlite3-dev
21- qtbase5-private-dev
22- qtdeclarative5-dev
23- libqt5opengl5-dev
24- libubuntuoneauth-2.0-dev
25
26 and run:
27
28
29=== modified file 'client/client.go'
30--- client/client.go 2014-04-16 18:17:07 +0000
31+++ client/client.go 2014-05-01 09:16:09 +0000
32@@ -26,7 +26,6 @@
33 "os"
34 "strings"
35
36- "gopkg.in/qml.v0"
37 "launchpad.net/go-dbus/v1"
38
39 "launchpad.net/ubuntu-push/bus"
40@@ -112,7 +111,6 @@
41
42 // later, we'll be specifying more logging options in the config file
43 client.log = logger.NewSimpleLogger(os.Stderr, client.config.LogLevel.Level())
44- qml.SetLogger(client.log)
45
46 // overridden for testing
47 client.idder = identifier.New()
48
49=== modified file 'client/client_test.go'
50--- client/client_test.go 2014-04-18 10:36:20 +0000
51+++ client/client_test.go 2014-05-01 09:16:09 +0000
52@@ -265,8 +265,9 @@
53 ExchangeTimeout: 10 * time.Millisecond,
54 HostsCachingExpiryTime: 1 * time.Hour,
55 ExpectAllRepairedTime: 30 * time.Minute,
56- PEM: cli.pem,
57- Info: info,
58+ PEM: cli.pem,
59+ Info: info,
60+ AuthHelper: []string{},
61 }
62 // sanity check that we are looking at all fields
63 vExpected := reflect.ValueOf(expected)
64@@ -276,6 +277,8 @@
65 // field isn't empty/zero
66 c.Assert(fv.Interface(), Not(DeepEquals), reflect.Zero(fv.Type()).Interface(), Commentf("forgot about: %s", vExpected.Type().Field(i).Name))
67 }
68+ // but AuthHelper really should be nil for now
69+ expected.AuthHelper = nil
70 // finally compare
71 conf := cli.deriveSessionConfig(info)
72 c.Check(conf, DeepEquals, expected)
73
74=== modified file 'client/session/session.go'
75--- client/session/session.go 2014-04-18 16:15:30 +0000
76+++ client/session/session.go 2014-05-01 09:16:09 +0000
77@@ -26,6 +26,7 @@
78 "fmt"
79 "math/rand"
80 "net"
81+ "os/exec"
82 "strings"
83 "sync"
84 "sync/atomic"
85@@ -40,8 +41,6 @@
86
87 var (
88 wireVersionBytes = []byte{protocol.ProtocolWireVersion}
89- getAuthorization = util.GetAuthorization
90- shouldGetAuth = false
91 )
92
93 type Notification struct {
94@@ -88,6 +87,7 @@
95 ExpectAllRepairedTime time.Duration
96 PEM []byte
97 Info map[string]interface{}
98+ AuthHelper []string
99 }
100
101 // ClientSession holds a client<->server session and its configuration.
102@@ -240,18 +240,24 @@
103 return nil
104 }
105
106-// checkAuthorization checks the authorization within the phone
107-func (sess *ClientSession) checkAuthorization() error {
108- // grab the authorization string from the accounts
109- // TODO: remove this condition when we have a way to deal with failing authorizations
110- if shouldGetAuth {
111- auth, err := getAuthorization()
112- if err != nil {
113- // For now we just log the error, as we don't want to block unauthorized users
114- sess.Log.Errorf("unable to get the authorization token from the account: %v", err)
115- }
116- sess.auth = auth
117- }
118+// addAuthorization gets the authorization blob to send to the server
119+// and adds it to the session.
120+func (sess *ClientSession) addAuthorization() error {
121+ sess.Log.Debugf("adding authorization")
122+ // using a helper, for now at least
123+ if len(sess.AuthHelper) == 0 {
124+ // do nothing if helper is unset or empty
125+ return nil
126+ }
127+
128+ auth, err := exec.Command(sess.AuthHelper[0], sess.AuthHelper[1:]...).Output()
129+ if err != nil {
130+ // For now we just log the error, as we don't want to block unauthorized users
131+ sess.Log.Errorf("unable to get the authorization token from the account: %v", err)
132+ } else {
133+ sess.auth = strings.TrimSpace(string(auth))
134+ }
135+
136 return nil
137 }
138
139@@ -553,7 +559,7 @@
140 // keep on trying.
141 panic("can't Dial() without a protocol constructor.")
142 }
143- return sess.run(sess.doClose, sess.checkAuthorization, sess.getHosts, sess.connect, sess.start, sess.loop)
144+ return sess.run(sess.doClose, sess.addAuthorization, sess.getHosts, sess.connect, sess.start, sess.loop)
145 }
146
147 func init() {
148
149=== modified file 'client/session/session_test.go'
150--- client/session/session_test.go 2014-04-18 16:15:30 +0000
151+++ client/session/session_test.go 2014-05-01 09:16:09 +0000
152@@ -169,22 +169,10 @@
153 lvls func() (levelmap.LevelMap, error)
154 }
155
156-func (cs *clientSessionSuite) SetUpSuite(c *C) {
157- getAuthorization = func() (string, error) {
158- return "some auth", nil
159- }
160- shouldGetAuth = true
161-}
162-
163 func (cs *clientSessionSuite) SetUpTest(c *C) {
164 cs.log = helpers.NewTestLogger(c, "debug")
165 }
166
167-func (cs *clientSessionSuite) TearDownSuite(c *C) {
168- getAuthorization = util.GetAuthorization
169- shouldGetAuth = false
170-}
171-
172 // in-memory level map testing
173 var _ = Suite(&clientSessionSuite{lvls: levelmap.NewLevelMap})
174
175@@ -194,7 +182,6 @@
176 var _ = Suite(&clientSqlevelsSessionSuite{})
177
178 func (cs *clientSqlevelsSessionSuite) SetUpSuite(c *C) {
179- cs.clientSessionSuite.SetUpSuite(c)
180 cs.lvls = func() (levelmap.LevelMap, error) { return levelmap.NewSqliteLevelMap(":memory:") }
181 }
182
183@@ -359,17 +346,42 @@
184 }
185
186 /****************************************************************
187- checkAuthorization() tests
188+ addAuthorization() tests
189 ****************************************************************/
190
191-func (cs *clientSessionSuite) TestChecksAuthorizationFromServer(c *C) {
192- sess := &ClientSession{}
193+func (cs *clientSessionSuite) TestAddAuthorizationAddsAuthorization(c *C) {
194+ sess := &ClientSession{Log: cs.log}
195+ sess.AuthHelper = []string{"echo", "some auth"}
196 c.Assert(sess.auth, Equals, "")
197- err := sess.checkAuthorization()
198+ err := sess.addAuthorization()
199 c.Assert(err, IsNil)
200 c.Check(sess.auth, Equals, "some auth")
201 }
202
203+func (cs *clientSessionSuite) TestAddAuthorizationIgnoresErrors(c *C) {
204+ sess := &ClientSession{Log: cs.log}
205+ sess.AuthHelper = []string{"sh", "-c", "echo hello; false"}
206+
207+ c.Assert(sess.auth, Equals, "")
208+ err := sess.addAuthorization()
209+ c.Assert(err, IsNil)
210+ c.Check(sess.auth, Equals, "")
211+}
212+
213+func (cs *clientSessionSuite) TestAddAuthorizationSkipsIfUnsetOrNil(c *C) {
214+ sess := &ClientSession{Log: cs.log}
215+ sess.AuthHelper = nil
216+ c.Assert(sess.auth, Equals, "")
217+ err := sess.addAuthorization()
218+ c.Assert(err, IsNil)
219+ c.Check(sess.auth, Equals, "")
220+
221+ sess.AuthHelper = []string{}
222+ err = sess.addAuthorization()
223+ c.Assert(err, IsNil)
224+ c.Check(sess.auth, Equals, "")
225+}
226+
227 /****************************************************************
228 startConnectionAttempt()/nextHostToTry()/started tests
229 ****************************************************************/
230
231=== modified file 'debian/control'
232--- debian/control 2014-04-15 14:20:09 +0000
233+++ debian/control 2014-05-01 09:16:09 +0000
234@@ -14,10 +14,6 @@
235 libgcrypt11-dev,
236 libglib2.0-dev (>= 2.31.6),
237 libwhoopsie-dev,
238- qtbase5-private-dev,
239- qtdeclarative5-dev,
240- libqt5opengl5-dev,
241- libubuntuoneauth-2.0-dev,
242 Standards-Version: 3.9.5
243 Homepage: http://launchpad.net/ubuntu-push
244 Vcs-Bzr: lp:ubuntu-push
245
246=== modified file 'dependencies.tsv'
247--- dependencies.tsv 2014-04-08 21:37:59 +0000
248+++ dependencies.tsv 2014-05-01 09:16:09 +0000
249@@ -2,5 +2,3 @@
250 launchpad.net/go-dbus/v1 bzr james@jamesh.id.au-20140206110213-pbzcr6ucaz3rqmnw 125
251 launchpad.net/go-xdg/v0 bzr john.lenton@canonical.com-20140208094800-gubd5md7cro3mtxa 10
252 launchpad.net/gocheck bzr gustavo@niemeyer.net-20140127131816-zshobk1qqme626xw 86
253-gopkg.in/qml.v0 git master 8adbc8c2bf2da9f609df366683ad0f47a89c3d49
254-gopkg.in/niemeyer/uoneauth.v1 git v1 0758ba882a143ad2862dbcac85a7ca145750b640
255
256=== modified file 'ubuntu-push-client.go'
257--- ubuntu-push-client.go 2014-04-15 13:01:05 +0000
258+++ ubuntu-push-client.go 2014-05-01 09:16:09 +0000
259@@ -19,7 +19,6 @@
260 import (
261 "log"
262
263- "gopkg.in/qml.v0"
264 "launchpad.net/go-dbus/v1"
265 "launchpad.net/go-xdg/v0"
266
267@@ -60,8 +59,6 @@
268 log.Fatalf("unable to open the levels database: %v", err)
269 }
270
271- qml.Init(nil)
272-
273 cli := client.NewPushClient(cfgFname, lvlFname)
274 err = cli.Start()
275 if err != nil {
276
277=== removed file 'util/auth.go'
278--- util/auth.go 2014-04-09 22:36:14 +0000
279+++ util/auth.go 1970-01-01 00:00:00 +0000
280@@ -1,36 +0,0 @@
281-/*
282- Copyright 2013-2014 Canonical Ltd.
283-
284- This program is free software: you can redistribute it and/or modify it
285- under the terms of the GNU General Public License version 3, as published
286- by the Free Software Foundation.
287-
288- This program is distributed in the hope that it will be useful, but
289- WITHOUT ANY WARRANTY; without even the implied warranties of
290- MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
291- PURPOSE. See the GNU General Public License for more details.
292-
293- You should have received a copy of the GNU General Public License along
294- with this program. If not, see <http://www.gnu.org/licenses/>.
295-*/
296-
297-package util
298-
299-import (
300- "gopkg.in/niemeyer/uoneauth.v1"
301- "gopkg.in/qml.v0"
302-)
303-
304-func GetAuthorization() (string, error) {
305- engine := qml.NewEngine()
306- defer engine.Destroy()
307- authService := uoneauth.NewService(engine)
308- var auth string
309- token, err := authService.Token()
310- if err != nil {
311- return "", err
312- } else {
313- auth = token.HeaderSignature("POST", "https://push.ubuntu.com")
314- }
315- return auth, nil
316-}
317
318=== removed file 'util/auth_test.go'
319--- util/auth_test.go 2014-04-15 13:05:37 +0000
320+++ util/auth_test.go 1970-01-01 00:00:00 +0000
321@@ -1,53 +0,0 @@
322-/*
323- Copyright 2013-2014 Canonical Ltd.
324-
325- This program is free software: you can redistribute it and/or modify it
326- under the terms of the GNU General Public License version 3, as published
327- by the Free Software Foundation.
328-
329- This program is distributed in the hope that it will be useful, but
330- WITHOUT ANY WARRANTY; without even the implied warranties of
331- MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
332- PURPOSE. See the GNU General Public License for more details.
333-
334- You should have received a copy of the GNU General Public License along
335- with this program. If not, see <http://www.gnu.org/licenses/>.
336-*/
337-
338-package util
339-
340-import (
341- "os"
342-
343- "gopkg.in/qml.v0"
344-
345- . "launchpad.net/gocheck"
346-)
347-
348-type authSuite struct{}
349-
350-var _ = Suite(&authSuite{})
351-
352-func (s *authSuite) SetUpSuite(c *C) {
353- if os.Getenv("PUSH_AUTH_TEST") == "1" {
354- qml.Init(nil)
355- }
356-}
357-
358-func (s *authSuite) SetUpTest(c *C) {
359- qml.SetLogger(c)
360-}
361-
362-func (s *authSuite) TestGetAuth(c *C) {
363- /*
364- * This test is only useful when the PUSH_AUTH_TEST environment
365- * variable is set to "1" - in which case the runner should have
366- * a Ubuntu One account setup via system-settings.
367- */
368- if os.Getenv("PUSH_AUTH_TEST") != "1" {
369- c.Skip("PUSH_AUTH_TEST not set to '1'")
370- }
371- auth, err := GetAuthorization()
372- c.Assert(err, IsNil)
373- c.Assert(auth, Matches, "OAuth .*oauth_consumer_key=.*")
374-}

Subscribers

People subscribed via source and target branches