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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud_test
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd/cmdtesting"
    10  	"github.com/juju/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	jujucloud "github.com/juju/juju/cloud"
    15  	"github.com/juju/juju/cmd/juju/cloud"
    16  	"github.com/juju/juju/environs"
    17  	"github.com/juju/juju/jujuclient"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type listCredentialsSuite struct {
    22  	testing.BaseSuite
    23  	store              *jujuclient.MemStore
    24  	personalCloudsFunc func() (map[string]jujucloud.Cloud, error)
    25  	cloudByNameFunc    func(string) (*jujucloud.Cloud, error)
    26  }
    27  
    28  var _ = gc.Suite(&listCredentialsSuite{
    29  	personalCloudsFunc: func() (map[string]jujucloud.Cloud, error) {
    30  		return map[string]jujucloud.Cloud{
    31  			"mycloud": {},
    32  		}, nil
    33  	},
    34  	cloudByNameFunc: func(name string) (*jujucloud.Cloud, error) {
    35  		if name == "missingcloud" {
    36  			return nil, errors.NotValidf(name)
    37  		}
    38  		return &jujucloud.Cloud{Type: "test-provider"}, nil
    39  	},
    40  })
    41  
    42  func (s *listCredentialsSuite) SetUpSuite(c *gc.C) {
    43  	s.BaseSuite.SetUpSuite(c)
    44  	unreg := environs.RegisterProvider("test-provider", &mockProvider{})
    45  	s.AddCleanup(func(_ *gc.C) {
    46  		unreg()
    47  	})
    48  }
    49  
    50  func (s *listCredentialsSuite) SetUpTest(c *gc.C) {
    51  	s.BaseSuite.SetUpTest(c)
    52  	s.store = &jujuclient.MemStore{
    53  		Credentials: map[string]jujucloud.CloudCredential{
    54  			"aws": {
    55  				DefaultRegion:     "ap-southeast-2",
    56  				DefaultCredential: "down",
    57  				AuthCredentials: map[string]jujucloud.Credential{
    58  					"bob": jujucloud.NewCredential(
    59  						jujucloud.AccessKeyAuthType,
    60  						map[string]string{
    61  							"access-key": "key",
    62  							"secret-key": "secret",
    63  						},
    64  					),
    65  					"down": jujucloud.NewCredential(
    66  						jujucloud.UserPassAuthType,
    67  						map[string]string{
    68  							"username": "user",
    69  							"password": "password",
    70  						},
    71  					),
    72  				},
    73  			},
    74  			"google": {
    75  				AuthCredentials: map[string]jujucloud.Credential{
    76  					"default": jujucloud.NewCredential(
    77  						jujucloud.OAuth2AuthType,
    78  						map[string]string{
    79  							"client-id":    "id",
    80  							"client-email": "email",
    81  							"private-key":  "key",
    82  						},
    83  					),
    84  				},
    85  			},
    86  			"azure": {
    87  				AuthCredentials: map[string]jujucloud.Credential{
    88  					"azhja": jujucloud.NewCredential(
    89  						jujucloud.UserPassAuthType,
    90  						map[string]string{
    91  							"application-id":       "app-id",
    92  							"application-password": "app-secret",
    93  							"subscription-id":      "subscription-id",
    94  							"tenant-id":            "tenant-id",
    95  						},
    96  					),
    97  				},
    98  			},
    99  			"mycloud": {
   100  				AuthCredentials: map[string]jujucloud.Credential{
   101  					"me": jujucloud.NewCredential(
   102  						jujucloud.AccessKeyAuthType,
   103  						map[string]string{
   104  							"access-key": "key",
   105  							"secret-key": "secret",
   106  						},
   107  					),
   108  				},
   109  			},
   110  		},
   111  	}
   112  }
   113  
   114  func (s *listCredentialsSuite) TestListCredentialsTabular(c *gc.C) {
   115  	out := s.listCredentials(c)
   116  	c.Assert(out, gc.Equals, `
   117  Cloud    Credentials
   118  aws      down*, bob
   119  azure    azhja
   120  google   default
   121  mycloud  me
   122  
   123  `[1:])
   124  }
   125  
   126  func (s *listCredentialsSuite) TestListCredentialsTabularMissingCloud(c *gc.C) {
   127  	s.store.Credentials["missingcloud"] = jujucloud.CloudCredential{}
   128  	out := s.listCredentials(c)
   129  	c.Assert(out, gc.Equals, `
   130  The following clouds have been removed and are omitted from the results to avoid leaking secrets.
   131  Run with --show-secrets to display these clouds' credentials: missingcloud
   132  
   133  Cloud    Credentials
   134  aws      down*, bob
   135  azure    azhja
   136  google   default
   137  mycloud  me
   138  
   139  `[1:])
   140  }
   141  
   142  func (s *listCredentialsSuite) TestListCredentialsTabularFiltered(c *gc.C) {
   143  	out := s.listCredentials(c, "aws")
   144  	c.Assert(out, gc.Equals, `
   145  Cloud  Credentials
   146  aws    down*, bob
   147  
   148  `[1:])
   149  }
   150  
   151  func (s *listCredentialsSuite) TestListCredentialsYAMLWithSecrets(c *gc.C) {
   152  	s.store.Credentials["missingcloud"] = jujucloud.CloudCredential{
   153  		AuthCredentials: map[string]jujucloud.Credential{
   154  			"default": jujucloud.NewCredential(
   155  				jujucloud.AccessKeyAuthType,
   156  				map[string]string{
   157  					"access-key": "key",
   158  					"secret-key": "secret",
   159  				},
   160  			),
   161  		},
   162  	}
   163  	out := s.listCredentials(c, "--format", "yaml", "--show-secrets")
   164  	c.Assert(out, gc.Equals, `
   165  local-credentials:
   166    aws:
   167      default-credential: down
   168      default-region: ap-southeast-2
   169      bob:
   170        auth-type: access-key
   171        access-key: key
   172        secret-key: secret
   173      down:
   174        auth-type: userpass
   175        password: password
   176        username: user
   177    azure:
   178      azhja:
   179        auth-type: userpass
   180        application-id: app-id
   181        application-password: app-secret
   182        subscription-id: subscription-id
   183        tenant-id: tenant-id
   184    google:
   185      default:
   186        auth-type: oauth2
   187        client-email: email
   188        client-id: id
   189        private-key: key
   190    missingcloud:
   191      default:
   192        auth-type: access-key
   193        access-key: key
   194        secret-key: secret
   195    mycloud:
   196      me:
   197        auth-type: access-key
   198        access-key: key
   199        secret-key: secret
   200  `[1:])
   201  }
   202  
   203  func (s *listCredentialsSuite) TestListCredentialsYAMLNoSecrets(c *gc.C) {
   204  	s.store.Credentials["missingcloud"] = jujucloud.CloudCredential{
   205  		AuthCredentials: map[string]jujucloud.Credential{
   206  			"default": jujucloud.NewCredential(
   207  				jujucloud.AccessKeyAuthType,
   208  				map[string]string{
   209  					"access-key": "key",
   210  					"secret-key": "secret",
   211  				},
   212  			),
   213  		},
   214  	}
   215  	out := s.listCredentials(c, "--format", "yaml")
   216  	c.Assert(out, gc.Equals, `
   217  local-credentials:
   218    aws:
   219      default-credential: down
   220      default-region: ap-southeast-2
   221      bob:
   222        auth-type: access-key
   223        access-key: key
   224      down:
   225        auth-type: userpass
   226        username: user
   227    azure:
   228      azhja:
   229        auth-type: userpass
   230        application-id: app-id
   231        subscription-id: subscription-id
   232        tenant-id: tenant-id
   233    google:
   234      default:
   235        auth-type: oauth2
   236        client-email: email
   237        client-id: id
   238    mycloud:
   239      me:
   240        auth-type: access-key
   241        access-key: key
   242  `[1:])
   243  }
   244  
   245  func (s *listCredentialsSuite) TestListCredentialsYAMLFiltered(c *gc.C) {
   246  	out := s.listCredentials(c, "--format", "yaml", "azure")
   247  	c.Assert(out, gc.Equals, `
   248  local-credentials:
   249    azure:
   250      azhja:
   251        auth-type: userpass
   252        application-id: app-id
   253        subscription-id: subscription-id
   254        tenant-id: tenant-id
   255  `[1:])
   256  }
   257  
   258  func (s *listCredentialsSuite) TestListCredentialsJSONWithSecrets(c *gc.C) {
   259  	out := s.listCredentials(c, "--format", "json", "--show-secrets")
   260  	c.Assert(out, gc.Equals, `
   261  {"local-credentials":{"aws":{"default-credential":"down","default-region":"ap-southeast-2","cloud-credentials":{"bob":{"auth-type":"access-key","details":{"access-key":"key","secret-key":"secret"}},"down":{"auth-type":"userpass","details":{"password":"password","username":"user"}}}},"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","application-password":"app-secret","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}},"google":{"cloud-credentials":{"default":{"auth-type":"oauth2","details":{"client-email":"email","client-id":"id","private-key":"key"}}}},"mycloud":{"cloud-credentials":{"me":{"auth-type":"access-key","details":{"access-key":"key","secret-key":"secret"}}}}}}
   262  `[1:])
   263  }
   264  
   265  func (s *listCredentialsSuite) TestListCredentialsJSONNoSecrets(c *gc.C) {
   266  	out := s.listCredentials(c, "--format", "json")
   267  	c.Assert(out, gc.Equals, `
   268  {"local-credentials":{"aws":{"default-credential":"down","default-region":"ap-southeast-2","cloud-credentials":{"bob":{"auth-type":"access-key","details":{"access-key":"key"}},"down":{"auth-type":"userpass","details":{"username":"user"}}}},"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}},"google":{"cloud-credentials":{"default":{"auth-type":"oauth2","details":{"client-email":"email","client-id":"id"}}}},"mycloud":{"cloud-credentials":{"me":{"auth-type":"access-key","details":{"access-key":"key"}}}}}}
   269  `[1:])
   270  }
   271  
   272  func (s *listCredentialsSuite) TestListCredentialsJSONFiltered(c *gc.C) {
   273  	out := s.listCredentials(c, "--format", "json", "azure")
   274  	c.Assert(out, gc.Equals, `
   275  {"local-credentials":{"azure":{"cloud-credentials":{"azhja":{"auth-type":"userpass","details":{"application-id":"app-id","subscription-id":"subscription-id","tenant-id":"tenant-id"}}}}}}
   276  `[1:])
   277  }
   278  
   279  func (s *listCredentialsSuite) TestListCredentialsEmpty(c *gc.C) {
   280  	s.store = &jujuclient.MemStore{
   281  		Credentials: map[string]jujucloud.CloudCredential{
   282  			"aws": {
   283  				AuthCredentials: map[string]jujucloud.Credential{
   284  					"bob": jujucloud.NewCredential(
   285  						jujucloud.OAuth2AuthType,
   286  						map[string]string{},
   287  					),
   288  				},
   289  			},
   290  		},
   291  	}
   292  	out := strings.Replace(s.listCredentials(c), "\n", "", -1)
   293  	c.Assert(out, gc.Equals, "Cloud  Credentialsaws    bob")
   294  
   295  	out = strings.Replace(s.listCredentials(c, "--format", "yaml"), "\n", "", -1)
   296  	c.Assert(out, gc.Equals, "local-credentials:  aws:    bob:      auth-type: oauth2")
   297  
   298  	out = strings.Replace(s.listCredentials(c, "--format", "json"), "\n", "", -1)
   299  	c.Assert(out, gc.Equals, `{"local-credentials":{"aws":{"cloud-credentials":{"bob":{"auth-type":"oauth2"}}}}}`)
   300  }
   301  
   302  func (s *listCredentialsSuite) TestListCredentialsNone(c *gc.C) {
   303  	listCmd := cloud.NewListCredentialsCommandForTest(jujuclient.NewMemStore(), s.personalCloudsFunc, s.cloudByNameFunc)
   304  	ctx, err := cmdtesting.RunCommand(c, listCmd)
   305  	c.Assert(err, jc.ErrorIsNil)
   306  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
   307  	out := strings.Replace(cmdtesting.Stdout(ctx), "\n", "", -1)
   308  	c.Assert(out, gc.Equals, "No locally stored credentials to display.")
   309  
   310  	ctx, err = cmdtesting.RunCommand(c, listCmd, "--format", "yaml")
   311  	c.Assert(err, jc.ErrorIsNil)
   312  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
   313  	out = strings.Replace(cmdtesting.Stdout(ctx), "\n", "", -1)
   314  	c.Assert(out, gc.Equals, "local-credentials: {}")
   315  
   316  	ctx, err = cmdtesting.RunCommand(c, listCmd, "--format", "json")
   317  	c.Assert(err, jc.ErrorIsNil)
   318  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
   319  	out = strings.Replace(cmdtesting.Stdout(ctx), "\n", "", -1)
   320  	c.Assert(out, gc.Equals, `{"local-credentials":{}}`)
   321  }
   322  
   323  func (s *listCredentialsSuite) listCredentials(c *gc.C, args ...string) string {
   324  	ctx, err := cmdtesting.RunCommand(c, cloud.NewListCredentialsCommandForTest(s.store, s.personalCloudsFunc, s.cloudByNameFunc), args...)
   325  	c.Assert(err, jc.ErrorIsNil)
   326  	c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "")
   327  	return cmdtesting.Stdout(ctx)
   328  }