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