github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/common/authkeys_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/juju/cmd/cmdtesting"
    13  	jc "github.com/juju/testing/checkers"
    14  	"github.com/juju/utils"
    15  	"github.com/juju/utils/ssh"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/cmd/juju/common"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  type AuthKeysSuite struct {
    23  	testing.BaseSuite
    24  	dotssh string // ~/.ssh
    25  }
    26  
    27  var _ = gc.Suite(&AuthKeysSuite{})
    28  
    29  func (s *AuthKeysSuite) SetUpTest(c *gc.C) {
    30  	s.BaseSuite.SetUpTest(c)
    31  	old := utils.Home()
    32  	newhome := c.MkDir()
    33  	err := utils.SetHome(newhome)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	s.AddCleanup(func(*gc.C) {
    36  		ssh.ClearClientKeys()
    37  		err := utils.SetHome(old)
    38  		c.Assert(err, jc.ErrorIsNil)
    39  	})
    40  
    41  	s.dotssh = filepath.Join(newhome, ".ssh")
    42  	err = os.Mkdir(s.dotssh, 0755)
    43  	c.Assert(err, jc.ErrorIsNil)
    44  }
    45  
    46  func (s *AuthKeysSuite) TestReadAuthorizedKeysErrors(c *gc.C) {
    47  	ctx := cmdtesting.Context(c)
    48  	_, err := common.ReadAuthorizedKeys(ctx, "")
    49  	c.Assert(err, gc.ErrorMatches, "no public ssh keys found")
    50  	c.Assert(err, gc.Equals, common.ErrNoAuthorizedKeys)
    51  	_, err = common.ReadAuthorizedKeys(ctx, filepath.Join(s.dotssh, "notthere.pub"))
    52  	c.Assert(err, gc.ErrorMatches, "no public ssh keys found")
    53  	c.Assert(err, gc.Equals, common.ErrNoAuthorizedKeys)
    54  }
    55  
    56  func writeFile(c *gc.C, filename string, contents string) {
    57  	err := ioutil.WriteFile(filename, []byte(contents), 0644)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  }
    60  
    61  func (s *AuthKeysSuite) TestReadAuthorizedKeys(c *gc.C) {
    62  	ctx := cmdtesting.Context(c)
    63  	writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa")
    64  	writeFile(c, filepath.Join(s.dotssh, "identity.pub"), "identity")
    65  	writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test")
    66  	keys, err := common.ReadAuthorizedKeys(ctx, "")
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(keys, gc.Equals, "id_rsa\nidentity\n")
    69  	keys, err = common.ReadAuthorizedKeys(ctx, "test.pub") // relative to ~/.ssh
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	c.Assert(keys, gc.Equals, "test\n")
    72  }
    73  
    74  func (s *AuthKeysSuite) TestReadAuthorizedKeysClientKeys(c *gc.C) {
    75  	ctx := cmdtesting.Context(c)
    76  	keydir := filepath.Join(s.dotssh, "juju")
    77  	err := ssh.LoadClientKeys(keydir) // auto-generates a key pair
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	pubkeyFiles := ssh.PublicKeyFiles()
    80  	c.Assert(pubkeyFiles, gc.HasLen, 1)
    81  	data, err := ioutil.ReadFile(pubkeyFiles[0])
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	prefix := strings.Trim(string(data), "\n") + "\n"
    84  
    85  	writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa")
    86  	writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test")
    87  	keys, err := common.ReadAuthorizedKeys(ctx, "")
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	c.Assert(keys, gc.Equals, prefix+"id_rsa\n")
    90  	keys, err = common.ReadAuthorizedKeys(ctx, "test.pub")
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(keys, gc.Equals, prefix+"test\n")
    93  	keys, err = common.ReadAuthorizedKeys(ctx, "notthere.pub")
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	c.Assert(keys, gc.Equals, prefix)
    96  }
    97  
    98  func (s *AuthKeysSuite) TestFinalizeAuthorizedKeysNoop(c *gc.C) {
    99  	attrs := map[string]interface{}{"authorized-keys": "meep"}
   100  	err := common.FinalizeAuthorizedKeys(cmdtesting.Context(c), attrs)
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	c.Assert(attrs, jc.DeepEquals, map[string]interface{}{"authorized-keys": "meep"})
   103  }
   104  
   105  func (s *AuthKeysSuite) TestFinalizeAuthorizedKeysPath(c *gc.C) {
   106  	writeFile(c, filepath.Join(s.dotssh, "whatever"), "meep")
   107  	attrs := map[string]interface{}{"authorized-keys-path": "whatever"}
   108  	err := common.FinalizeAuthorizedKeys(cmdtesting.Context(c), attrs)
   109  	c.Assert(err, jc.ErrorIsNil)
   110  	c.Assert(attrs, jc.DeepEquals, map[string]interface{}{"authorized-keys": "meep\n"})
   111  }
   112  
   113  func (s *AuthKeysSuite) TestFinalizeAuthorizedKeysDefault(c *gc.C) {
   114  	writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "meep")
   115  	attrs := map[string]interface{}{}
   116  	err := common.FinalizeAuthorizedKeys(cmdtesting.Context(c), attrs)
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	c.Assert(attrs, jc.DeepEquals, map[string]interface{}{"authorized-keys": "meep\n"})
   119  }
   120  
   121  func (s *AuthKeysSuite) TestFinalizeAuthorizedKeysConflict(c *gc.C) {
   122  	attrs := map[string]interface{}{"authorized-keys": "foo", "authorized-keys-path": "bar"}
   123  	err := common.FinalizeAuthorizedKeys(cmdtesting.Context(c), attrs)
   124  	c.Assert(err, gc.ErrorMatches, `"authorized-keys" and "authorized-keys-path" may not both be specified`)
   125  }