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

Subscribers

People subscribed via source and target branches