github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/commands/sshkeys_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 "strings" 9 10 jc "github.com/juju/testing/checkers" 11 sshtesting "github.com/juju/utils/ssh/testing" 12 gc "gopkg.in/check.v1" 13 14 keymanagerserver "github.com/juju/juju/apiserver/keymanager" 15 keymanagertesting "github.com/juju/juju/apiserver/keymanager/testing" 16 "github.com/juju/juju/cmd/juju/common" 17 "github.com/juju/juju/juju/osenv" 18 jujutesting "github.com/juju/juju/juju/testing" 19 coretesting "github.com/juju/juju/testing" 20 ) 21 22 type SSHKeysSuite struct { 23 coretesting.FakeJujuXDGDataHomeSuite 24 } 25 26 var _ = gc.Suite(&SSHKeysSuite{}) 27 28 func (s *SSHKeysSuite) assertHelpOutput(c *gc.C, cmd, args string) { 29 if args != "" { 30 args = " " + args 31 } 32 expected := fmt.Sprintf("Usage: juju %s [options]%s", cmd, args) 33 out := badrun(c, 0, cmd, "--help") 34 lines := strings.Split(out, "\n") 35 c.Assert(lines[0], gc.Equals, expected) 36 } 37 38 func (s *SSHKeysSuite) TestHelpList(c *gc.C) { 39 s.assertHelpOutput(c, "list-ssh-keys", "") 40 } 41 42 func (s *SSHKeysSuite) TestHelpAdd(c *gc.C) { 43 s.assertHelpOutput(c, "add-ssh-key", "<ssh key> ...") 44 } 45 46 func (s *SSHKeysSuite) TestHelpRemove(c *gc.C) { 47 s.assertHelpOutput(c, "remove-ssh-key", "<ssh key id> ...") 48 } 49 50 func (s *SSHKeysSuite) TestHelpImport(c *gc.C) { 51 s.assertHelpOutput(c, "import-ssh-key", "<lp|gh>:<user identity> ...") 52 } 53 54 type keySuiteBase struct { 55 jujutesting.JujuConnSuite 56 common.CmdBlockHelper 57 } 58 59 func (s *keySuiteBase) SetUpSuite(c *gc.C) { 60 s.JujuConnSuite.SetUpSuite(c) 61 s.PatchEnvironment(osenv.JujuModelEnvKey, "admin") 62 } 63 64 func (s *keySuiteBase) SetUpTest(c *gc.C) { 65 s.JujuConnSuite.SetUpTest(c) 66 s.CmdBlockHelper = common.NewCmdBlockHelper(s.APIState) 67 c.Assert(s.CmdBlockHelper, gc.NotNil) 68 s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() }) 69 } 70 71 func (s *keySuiteBase) setAuthorizedKeys(c *gc.C, keys ...string) { 72 keyString := strings.Join(keys, "\n") 73 err := s.State.UpdateModelConfig(map[string]interface{}{"authorized-keys": keyString}, nil, nil) 74 c.Assert(err, jc.ErrorIsNil) 75 envConfig, err := s.State.ModelConfig() 76 c.Assert(err, jc.ErrorIsNil) 77 c.Assert(envConfig.AuthorizedKeys(), gc.Equals, keyString) 78 } 79 80 func (s *keySuiteBase) assertEnvironKeys(c *gc.C, expected ...string) { 81 envConfig, err := s.State.ModelConfig() 82 c.Assert(err, jc.ErrorIsNil) 83 keys := envConfig.AuthorizedKeys() 84 c.Assert(keys, gc.Equals, strings.Join(expected, "\n")) 85 } 86 87 type ListKeysSuite struct { 88 keySuiteBase 89 } 90 91 var _ = gc.Suite(&ListKeysSuite{}) 92 93 func (s *ListKeysSuite) TestListKeys(c *gc.C) { 94 key1 := sshtesting.ValidKeyOne.Key + " user@host" 95 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 96 s.setAuthorizedKeys(c, key1, key2) 97 98 context, err := coretesting.RunCommand(c, NewListKeysCommand()) 99 c.Assert(err, jc.ErrorIsNil) 100 output := strings.TrimSpace(coretesting.Stdout(context)) 101 c.Assert(err, jc.ErrorIsNil) 102 c.Assert(output, gc.Matches, "Keys used in model: admin\n.*\\(user@host\\)\n.*\\(another@host\\)") 103 } 104 105 func (s *ListKeysSuite) TestListFullKeys(c *gc.C) { 106 key1 := sshtesting.ValidKeyOne.Key + " user@host" 107 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 108 s.setAuthorizedKeys(c, key1, key2) 109 110 context, err := coretesting.RunCommand(c, NewListKeysCommand(), "--full") 111 c.Assert(err, jc.ErrorIsNil) 112 output := strings.TrimSpace(coretesting.Stdout(context)) 113 c.Assert(err, jc.ErrorIsNil) 114 c.Assert(output, gc.Matches, "Keys used in model: admin\n.*user@host\n.*another@host") 115 } 116 117 func (s *ListKeysSuite) TestTooManyArgs(c *gc.C) { 118 _, err := coretesting.RunCommand(c, NewListKeysCommand(), "foo") 119 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["foo"\]`) 120 } 121 122 type AddKeySuite struct { 123 keySuiteBase 124 } 125 126 var _ = gc.Suite(&AddKeySuite{}) 127 128 func (s *AddKeySuite) TestAddKey(c *gc.C) { 129 key1 := sshtesting.ValidKeyOne.Key + " user@host" 130 s.setAuthorizedKeys(c, key1) 131 132 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 133 context, err := coretesting.RunCommand(c, NewAddKeysCommand(), key2, "invalid-key") 134 c.Assert(err, jc.ErrorIsNil) 135 c.Assert(coretesting.Stderr(context), gc.Matches, `cannot add key "invalid-key".*\n`) 136 s.assertEnvironKeys(c, key1, key2) 137 } 138 139 func (s *AddKeySuite) TestBlockAddKey(c *gc.C) { 140 key1 := sshtesting.ValidKeyOne.Key + " user@host" 141 s.setAuthorizedKeys(c, key1) 142 143 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 144 // Block operation 145 s.BlockAllChanges(c, "TestBlockAddKey") 146 _, err := coretesting.RunCommand(c, NewAddKeysCommand(), key2, "invalid-key") 147 s.AssertBlocked(c, err, ".*TestBlockAddKey.*") 148 } 149 150 type RemoveKeySuite struct { 151 keySuiteBase 152 } 153 154 var _ = gc.Suite(&RemoveKeySuite{}) 155 156 func (s *RemoveKeySuite) TestRemoveKeys(c *gc.C) { 157 key1 := sshtesting.ValidKeyOne.Key + " user@host" 158 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 159 s.setAuthorizedKeys(c, key1, key2) 160 161 context, err := coretesting.RunCommand(c, NewRemoveKeysCommand(), 162 sshtesting.ValidKeyTwo.Fingerprint, "invalid-key") 163 c.Assert(err, jc.ErrorIsNil) 164 c.Assert(coretesting.Stderr(context), gc.Matches, `cannot remove key id "invalid-key".*\n`) 165 s.assertEnvironKeys(c, key1) 166 } 167 168 func (s *RemoveKeySuite) TestBlockRemoveKeys(c *gc.C) { 169 key1 := sshtesting.ValidKeyOne.Key + " user@host" 170 key2 := sshtesting.ValidKeyTwo.Key + " another@host" 171 s.setAuthorizedKeys(c, key1, key2) 172 173 // Block operation 174 s.BlockAllChanges(c, "TestBlockRemoveKeys") 175 _, err := coretesting.RunCommand(c, NewRemoveKeysCommand(), 176 sshtesting.ValidKeyTwo.Fingerprint, "invalid-key") 177 s.AssertBlocked(c, err, ".*TestBlockRemoveKeys.*") 178 } 179 180 type ImportKeySuite struct { 181 keySuiteBase 182 } 183 184 var _ = gc.Suite(&ImportKeySuite{}) 185 186 func (s *ImportKeySuite) SetUpTest(c *gc.C) { 187 s.keySuiteBase.SetUpTest(c) 188 s.PatchValue(&keymanagerserver.RunSSHImportId, keymanagertesting.FakeImport) 189 } 190 191 func (s *ImportKeySuite) TestImportKeys(c *gc.C) { 192 key1 := sshtesting.ValidKeyOne.Key + " user@host" 193 s.setAuthorizedKeys(c, key1) 194 195 context, err := coretesting.RunCommand(c, NewImportKeysCommand(), "lp:validuser", "invalid-key") 196 c.Assert(err, jc.ErrorIsNil) 197 c.Assert(coretesting.Stderr(context), gc.Matches, `cannot import key id "invalid-key".*\n`) 198 s.assertEnvironKeys(c, key1, sshtesting.ValidKeyThree.Key) 199 } 200 201 func (s *ImportKeySuite) TestBlockImportKeys(c *gc.C) { 202 key1 := sshtesting.ValidKeyOne.Key + " user@host" 203 s.setAuthorizedKeys(c, key1) 204 205 // Block operation 206 s.BlockAllChanges(c, "TestBlockImportKeys") 207 _, err := coretesting.RunCommand(c, NewImportKeysCommand(), "lp:validuser", "invalid-key") 208 s.AssertBlocked(c, err, ".*TestBlockImportKeys.*") 209 }