Merge lp:~dave-cheney/juju-core/go-juju-update-secrets into lp:~juju/juju-core/trunk

Proposed by Dave Cheney
Status: Merged
Approved by: Gustavo Niemeyer
Approved revision: no longer in the source branch.
Merged at revision: 338
Proposed branch: lp:~dave-cheney/juju-core/go-juju-update-secrets
Merge into: lp:~juju/juju-core/trunk
Diff against target: 133 lines (+75/-4)
2 files modified
juju/conn.go (+29/-0)
juju/conn_test.go (+46/-4)
To merge this branch: bzr merge lp:~dave-cheney/juju-core/go-juju-update-secrets
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+116694@code.launchpad.net

Description of the change

juju: update secrets when calling conn.State()

Calls to conn.State() push the secrets to the state
behind the scenes.

https://codereview.appspot.com/6452044/

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

https://codereview.appspot.com/6452044/diff/11001/juju/conn.go
File juju/conn.go (right):

https://codereview.appspot.com/6452044/diff/11001/juju/conn.go#newcode92
juju/conn.go:92: env.Update(secrets)
As we discussed live, we want to only do this if there are actually no
secrets in state yet.

https://codereview.appspot.com/6452044/

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'juju/conn.go'
2--- juju/conn.go 2012-07-23 13:35:59 +0000
3+++ juju/conn.go 2012-07-26 16:56:19 +0000
4@@ -1,7 +1,9 @@
5 package juju
6
7 import (
8+ "fmt"
9 "launchpad.net/juju-core/environs"
10+ "launchpad.net/juju-core/log"
11 "launchpad.net/juju-core/state"
12 "regexp"
13 "sync"
14@@ -69,10 +71,37 @@
15 return nil, err
16 }
17 c.state = st
18+ if err := c.updateSecrets(); err != nil {
19+ c.state = nil
20+ return nil, fmt.Errorf("unable to push secrets: %v", err)
21+ }
22 }
23 return c.state, nil
24 }
25
26+// updateSecrets updates the sensitive parts of the environment
27+// from the local configuration.
28+func (c *Conn) updateSecrets() error {
29+ cfg := c.Environ.Config()
30+ env, err := c.state.EnvironConfig()
31+ if err != nil {
32+ return err
33+ }
34+ // This is wrong. This will _always_ overwrite the secrets
35+ // in the state with the local secrets. To fix this properly
36+ // we need to ensure that the config, minus secrets, is always
37+ // pushed on bootstrap, then we can fill in the secrets here.
38+ env.Update(cfg.AllAttrs())
39+ n, err := env.Write()
40+ if err != nil {
41+ return err
42+ }
43+ if len(n) > 0 {
44+ log.Debugf("Updating %d secret(s) in environment %q", len(n), c.Environ.Name())
45+ }
46+ return nil
47+}
48+
49 // Close terminates the connection to the environment and releases
50 // any associated resources.
51 func (c *Conn) Close() error {
52
53=== modified file 'juju/conn_test.go'
54--- juju/conn_test.go 2012-07-24 17:36:58 +0000
55+++ juju/conn_test.go 2012-07-26 16:56:19 +0000
56@@ -3,24 +3,37 @@
57 import (
58 "io/ioutil"
59 . "launchpad.net/gocheck"
60- _ "launchpad.net/juju-core/environs/dummy"
61+ "launchpad.net/juju-core/environs/dummy"
62 "launchpad.net/juju-core/juju"
63- "launchpad.net/juju-core/testing"
64+ "launchpad.net/juju-core/state/testing"
65+ coretesting "launchpad.net/juju-core/testing"
66 "os"
67 "path/filepath"
68 stdtesting "testing"
69 )
70
71 func Test(t *stdtesting.T) {
72- testing.ZkTestPackage(t)
73+ coretesting.ZkTestPackage(t)
74 }
75
76 type ConnSuite struct {
77- testing.ZkSuite
78+ coretesting.LoggingSuite
79+ testing.StateSuite
80 }
81
82 var _ = Suite(&ConnSuite{})
83
84+func (cs *ConnSuite) SetUpTest(c *C) {
85+ cs.LoggingSuite.SetUpTest(c)
86+ cs.StateSuite.SetUpTest(c)
87+}
88+
89+func (cs *ConnSuite) TearDownTest(c *C) {
90+ dummy.Reset()
91+ cs.StateSuite.TearDownTest(c)
92+ cs.LoggingSuite.TearDownTest(c)
93+}
94+
95 func (*ConnSuite) TestNewConn(c *C) {
96 home := c.MkDir()
97 defer os.Setenv("HOME", os.Getenv("HOME"))
98@@ -85,6 +98,35 @@
99 c.Assert(err, ErrorMatches, "dummy environment not bootstrapped")
100 }
101
102+func (cs *ConnSuite) TestConnStateSecretsSideEffect(c *C) {
103+ env, err := cs.State.EnvironConfig()
104+ c.Assert(err, IsNil)
105+ // verify we have no secret in the environ config
106+ _, ok := env.Get("secret")
107+ c.Assert(ok, Equals, false)
108+ attrs := map[string]interface{}{
109+ "name": "erewhemos",
110+ "type": "dummy",
111+ "zookeeper": true,
112+ "authorized-keys": "i-am-a-key",
113+ }
114+ conn, err := juju.NewConnFromAttrs(attrs)
115+ c.Assert(err, IsNil)
116+ defer conn.Close()
117+ err = conn.Bootstrap(false)
118+ c.Assert(err, IsNil)
119+ // fetch a state connection via the conn, which will
120+ // push the secrets.
121+ _, err = conn.State()
122+ c.Assert(err, IsNil)
123+ err = env.Read()
124+ c.Assert(err, IsNil)
125+ // check that the secret has been populated
126+ secret, ok := env.Get("secret")
127+ c.Assert(ok, Equals, true)
128+ c.Assert(secret, Equals, "pork")
129+}
130+
131 func (*ConnSuite) TestValidRegexps(c *C) {
132 assertService := func(s string, expect bool) {
133 c.Assert(juju.ValidService.MatchString(s), Equals, expect)

Subscribers

People subscribed via source and target branches