Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 4800cc1

Browse files
authored
Merge pull request #359 from secrethub/feature/account-inspect-for-services
account inspect for service accounts
2 parents a8978d0 + 8d01879 commit 4800cc1

2 files changed

Lines changed: 123 additions & 19 deletions

File tree

internals/secrethub/account_inspect.go

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/secrethub/secrethub-go/internals/api"
1111
)
1212

13+
const accountTypeUser string = "user"
14+
const accountTypeService string = "service"
15+
1316
// AccountInspectCommand is a command to inspect account details.
1417
type AccountInspectCommand struct {
1518
io ui.IO
@@ -40,38 +43,81 @@ func (cmd *AccountInspectCommand) Run() error {
4043
return err
4144
}
4245

43-
user, err := client.Users().Me()
46+
account, err := client.Accounts().Me()
4447
if err != nil {
4548
return err
4649
}
47-
48-
output, err := cli.PrettyJSON(newOutputUser(user, cmd.timeFormatter))
49-
if err != nil {
50-
return err
50+
var output string
51+
if account.AccountType == accountTypeUser {
52+
user, err := client.Users().Me()
53+
if err != nil {
54+
return err
55+
}
56+
output, err = cli.PrettyJSON(newOutputUser(user, cmd.timeFormatter))
57+
if err != nil {
58+
return err
59+
}
60+
} else if account.AccountType == accountTypeService {
61+
service, err := client.Services().Get(account.Name.String())
62+
if err != nil {
63+
return err
64+
}
65+
output, err = cli.PrettyJSON(newOutputService(service, account, cmd.timeFormatter))
66+
if err != nil {
67+
return err
68+
}
5169
}
52-
5370
fmt.Fprintln(cmd.io.Output(), output)
5471

5572
return nil
5673
}
5774

58-
// outputUser is a user friendly JSON representation of a user account.
59-
type outputUser struct {
60-
Username string
61-
FullName string
62-
Email string `json:",omitempty"`
63-
EmailVerified bool `json:",omitempty"`
75+
// outputAccount contains the fields common in both outputUser and outputService
76+
type outputAccount struct {
77+
AccountType string
78+
AccountName string
6479
CreatedAt string `json:",omitempty"`
6580
PublicAccountKey []byte `json:",omitempty"`
6681
}
6782

83+
// outputUser is a user friendly JSON representation of a user account.
84+
type outputUser struct {
85+
Username string
86+
FullName string
87+
Email string `json:",omitempty"`
88+
EmailVerified bool `json:",omitempty"`
89+
outputAccount
90+
}
91+
6892
func newOutputUser(user *api.User, timeFormatter TimeFormatter) *outputUser {
6993
return &outputUser{
70-
Username: user.Username,
71-
FullName: user.FullName,
72-
Email: user.Email,
73-
EmailVerified: user.EmailVerified,
74-
CreatedAt: timeFormatter.Format(user.CreatedAt.Local()),
75-
PublicAccountKey: user.PublicKey,
94+
Username: user.Username,
95+
FullName: user.FullName,
96+
Email: user.Email,
97+
EmailVerified: user.EmailVerified,
98+
outputAccount: outputAccount{
99+
AccountType: accountTypeUser,
100+
AccountName: user.Username,
101+
CreatedAt: timeFormatter.Format(user.CreatedAt.Local()),
102+
PublicAccountKey: user.PublicKey,
103+
},
104+
}
105+
}
106+
107+
// outputService is a user friendly JSON representation of a service account.
108+
type outputService struct {
109+
Description string
110+
outputAccount
111+
}
112+
113+
func newOutputService(service *api.Service, account *api.Account, timeFormatter TimeFormatter) *outputService {
114+
return &outputService{
115+
Description: service.Description,
116+
outputAccount: outputAccount{
117+
AccountType: accountTypeService,
118+
AccountName: service.ServiceID,
119+
CreatedAt: timeFormatter.Format(service.CreatedAt.Local()),
120+
PublicAccountKey: account.PublicKey,
121+
},
76122
}
77123
}

internals/secrethub/account_inspect_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package secrethub
22

33
import (
4+
"errors"
45
"testing"
56
"time"
67

@@ -24,10 +25,17 @@ func TestAccountInspect(t *testing.T) {
2425
err error
2526
out string
2627
}{
27-
"success": {
28+
"success user": {
2829
cmd: AccountInspectCommand{
2930
newClient: func() (secrethub.ClientInterface, error) {
3031
return &fakeclient.Client{
32+
AccountService: &fakeclient.AccountService{
33+
MeFunc: func() (*api.Account, error) {
34+
return &api.Account{
35+
AccountType: accountTypeUser,
36+
}, nil
37+
},
38+
},
3139
UserService: &fakeclient.UserService{
3240
MeFunc: func() (*api.User, error) {
3341
return &api.User{
@@ -52,6 +60,49 @@ func TestAccountInspect(t *testing.T) {
5260
"FullName": "Developer Uno",
5361
"Email": "dev1@keylocker.eu",
5462
"EmailVerified": true,
63+
"AccountType": "user",
64+
"AccountName": "dev1",
65+
"CreatedAt": "2018-07-30T10:49:18Z",
66+
"PublicAccountKey": "YWJjZGU="
67+
}
68+
`,
69+
},
70+
"success service": {
71+
cmd: AccountInspectCommand{
72+
newClient: func() (secrethub.ClientInterface, error) {
73+
return &fakeclient.Client{
74+
AccountService: &fakeclient.AccountService{
75+
MeFunc: func() (*api.Account, error) {
76+
return &api.Account{
77+
AccountType: accountTypeService,
78+
Name: "dev1",
79+
PublicKey: []byte("abcde"),
80+
}, nil
81+
},
82+
},
83+
ServiceService: &fakeclient.ServiceService{
84+
GetFunc: func(id string) (*api.Service, error) {
85+
if id == "dev1" {
86+
return &api.Service{
87+
ServiceID: "s-abcdefghijkl",
88+
Description: "Test description.",
89+
CreatedAt: time.Date(2020, 12, 12, 12, 12, 12, 0, time.UTC),
90+
}, nil
91+
}
92+
return nil, errors.New(`expected id to be "dev1"`)
93+
},
94+
},
95+
}, nil
96+
},
97+
timeFormatter: &fakes.TimeFormatter{
98+
Response: "2018-07-30T10:49:18Z",
99+
},
100+
},
101+
err: nil,
102+
out: `{
103+
"Description": "Test description.",
104+
"AccountType": "service",
105+
"AccountName": "s-abcdefghijkl",
55106
"CreatedAt": "2018-07-30T10:49:18Z",
56107
"PublicAccountKey": "YWJjZGU="
57108
}
@@ -61,6 +112,13 @@ func TestAccountInspect(t *testing.T) {
61112
cmd: AccountInspectCommand{
62113
newClient: func() (secrethub.ClientInterface, error) {
63114
return fakeclient.Client{
115+
AccountService: &fakeclient.AccountService{
116+
MeFunc: func() (*api.Account, error) {
117+
return &api.Account{
118+
AccountType: accountTypeUser,
119+
}, nil
120+
},
121+
},
64122
UserService: &fakeclient.UserService{
65123
MeFunc: func() (*api.User, error) {
66124
return nil, api.ErrSignatureNotVerified

0 commit comments

Comments
 (0)