Skip to content

Commit

Permalink
Merge pull request #855 from SoftLayer/issues854
Browse files Browse the repository at this point in the history
Reworked use permission lists to be more inline with cloud.ibm.com
  • Loading branch information
allmightyspiff authored and GitHub Enterprise committed Jun 10, 2024
2 parents 38fee5a + 90cff70 commit 5462e9a
Show file tree
Hide file tree
Showing 16 changed files with 1,364 additions and 119 deletions.
16 changes: 14 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
"files": "plugin/i18n/v1Resources/|plugin/i18n/v2Resources/|(.*test.*)|(vendor)|(go.sum)|bin/|^.secrets.baseline$",
"lines": null
},
"generated_at": "2024-05-23T21:29:26Z",
"generated_at": "2024-06-06T23:07:46Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
},
{
"name": "ArtifactoryDetector"
},
{
"name": "AzureStorageKeyDetector"
},
{
"base64_limit": 4.5,
"name": "Base64HighEntropyString"
Expand All @@ -28,6 +31,9 @@
"ghe_instance": "github.ibm.com",
"name": "GheDetector"
},
{
"name": "GitHubTokenDetector"
},
{
"hex_limit": 3,
"name": "HexHighEntropyString"
Expand All @@ -48,6 +54,9 @@
{
"name": "MailchimpDetector"
},
{
"name": "NpmDetector"
},
{
"name": "PrivateKeyDetector"
},
Expand All @@ -57,6 +66,9 @@
{
"name": "SoftlayerDetector"
},
{
"name": "SquareOAuthDetector"
},
{
"name": "StripeDetector"
},
Expand Down Expand Up @@ -312,7 +324,7 @@
"hashed_secret": "c2a6b03f190dfb2b4aa91f8af8d477a9bc3401dc",
"is_secret": false,
"is_verified": false,
"line_number": 373,
"line_number": 386,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,15 @@ By default, every API call made to the SoftLayer API will load in the approprait
To force errors:

```go
fakeHandler := testhelpers.FakeTransportHandler{}
fakeHandler.AddApiError("SoftLayer_Tag", "getAttachedTagsForCurrentUser", 500, "BAD")
fakeSLSession := &session.Session{TransportHandler: fakeHandler,}
// In the Top level BeforeEach
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
fakeHandler = testhelpers.GetSessionHandler(fakeSession)

// Then in a BeforeEach for the specific test...
BeforeEach(func() {
fakeHandler.AddApiError("SoftLayer_User_Customer", "getObject",
500, "Internal Server Error")
})
```

To force a non-default JSON file to be loaded
Expand Down
20 changes: 11 additions & 9 deletions bin/buildAndDeploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ def runTests() -> None:
if re.match(r"fixtrues|vendor", mod) is None:
clean_mods.append(mod)

## GO GENERATE
go_generate = ['go', 'generate', './...']
# Not using the 'real' command here because this looks neater.

print(f'[turquoise2]Running: go generate ./...')
try:
subprocess.run(go_generate, check=True)
except subprocess.CalledProcessError as e:
print(f"[red]>>> Go Generate failed <<<")
sys.exit(e.returncode)

## GO VET
go_vet = ['go', 'vet'] + clean_mods
# Not using the 'real' command here because this looks neater.
Expand All @@ -73,16 +84,7 @@ def runTests() -> None:
print(f"[red]>>> Go Vet failed <<<")
sys.exit(e.returncode)

## GO GENERATE
go_generate = ['go', 'generate', './...']
# Not using the 'real' command here because this looks neater.

print(f'[turquoise2]Running: go generate ./...')
try:
subprocess.run(go_generate, check=True)
except subprocess.CalledProcessError as e:
print(f"[red]>>> Go Generate failed <<<")
sys.exit(e.returncode)

## GO TEST
go_test = ['go', 'test'] + clean_mods
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/nicksnyder/go-i18n/v2 v2.4.0
github.com/onsi/ginkgo/v2 v2.17.0
github.com/onsi/gomega v1.31.1
github.com/softlayer/softlayer-go v1.1.3
github.com/softlayer/softlayer-go v1.1.5
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
Expand All @@ -32,16 +32,17 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/tools v0.19.0 // indirect
Expand Down
16 changes: 9 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -36,11 +37,10 @@ github.com/jarcoal/httpmock v1.0.5 h1:cHtVEcTxRSX4J0je7mWPfc9BpDpqzXSJ5HbymZmyHc
github.com/jarcoal/httpmock v1.0.5/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -63,15 +63,17 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.7 h1:I6tZjLXD2Q1kjvNbIzB1wvQBsXmKXiVrhpRE8ZjP5jY=
github.com/smartystreets/goconvey v1.6.7/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/softlayer/softlayer-go v1.1.3 h1:dfFzt5eOKIAyB/b78fHMyDu5ICx0ZtxL9NRhBlf831A=
github.com/softlayer/softlayer-go v1.1.3/go.mod h1:Pc7F57OgUKaAam7TtpqkUeqL7QyKknfiUI4R49h41/U=
github.com/softlayer/softlayer-go v1.1.5 h1:UFFtgKxiw0yIuUw93XBCFIiIMYR5eLgmm4a5DqMHXGg=
github.com/softlayer/softlayer-go v1.1.5/go.mod h1:WeJrBLoTJcaT8nO1azeyHyNpo/fDLtbpbvh+pzts+Qw=
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e h1:3OgWYFw7jxCZPcvAg+4R8A50GZ+CCkARF10lxu2qDsQ=
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e/go.mod h1:fKZCUVdirrxrBpwd9wb+lSoVixvpwAu8eHzbQB2tums=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
Expand Down
1 change: 0 additions & 1 deletion plugin/commands/order/complex_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var TYPEMAP = map[string]interface{}{
"SoftLayer_Container_Product_Order_Network_Interconnect_Upgrade": &datatypes.Container_Product_Order_Network_Interconnect_Upgrade{},
"SoftLayer_Container_Product_Order_Network_LoadBalancer": &datatypes.Container_Product_Order_Network_LoadBalancer{},
"SoftLayer_Container_Product_Order_Network_LoadBalancer_AsAService": &datatypes.Container_Product_Order_Network_LoadBalancer_AsAService{},
"SoftLayer_Container_Product_Order_Network_LoadBalancer_Global": &datatypes.Container_Product_Order_Network_LoadBalancer_Global{},
"SoftLayer_Container_Product_Order_Network_Message_Delivery": &datatypes.Container_Product_Order_Network_Message_Delivery{},
"SoftLayer_Container_Product_Order_Network_PerformanceStorage": &datatypes.Container_Product_Order_Network_PerformanceStorage{},
"SoftLayer_Container_Product_Order_Network_PerformanceStorage_Iscsi": &datatypes.Container_Product_Order_Network_PerformanceStorage_Iscsi{},
Expand Down
3 changes: 0 additions & 3 deletions plugin/commands/user/details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package user_test

import (
"errors"
"fmt"
"time"

"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
Expand Down Expand Up @@ -374,7 +373,6 @@ var _ = Describe("Detail", func() {

Context("user detail with correct id and logins", func() {
It("return a user with logins", func() {
fmt.Println("**")
err := testhelpers.RunCobraCommand(cliCommand.Command, "5555", "--logins")
Expect(err).NotTo(HaveOccurred())
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))
Expand All @@ -401,7 +399,6 @@ var _ = Describe("Detail", func() {

Context("user detail with correct id and events", func() {
It("return a user with events", func() {
fmt.Println("**")
err := testhelpers.RunCobraCommand(cliCommand.Command, "5555", "--events")
Expect(err).NotTo(HaveOccurred())
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))
Expand Down
92 changes: 66 additions & 26 deletions plugin/commands/user/permissions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package user

import (
"bytes"
"strconv"

"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/spf13/cobra"
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
Expand All @@ -22,10 +25,14 @@ func NewPermissionsCommand(sl *metadata.SoftlayerCommand) (cmd *PermissionsComma
SoftlayerCommand: sl,
UserManager: managers.NewUserManager(sl.Session),
}

subs := map[string]interface{}{
"HelpUrl": "https://cloud.ibm.com/docs/account?topic=account-migrated_permissions",
}
cobraCmd := &cobra.Command{
Use: "permissions " + T("USER_ID"),
Short: T("View user permissions"),
Long: T(`Some permissions here may also be managed by the IBM IAM service.
See {{.HelpUrl}} for more details.`, subs),
Args: metadata.OneArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return thisCmd.Run(args)
Expand All @@ -36,7 +43,19 @@ func NewPermissionsCommand(sl *metadata.SoftlayerCommand) (cmd *PermissionsComma
return thisCmd
}

type PermissionCollection struct {
Department string
Permissions []Permission
}

type Permission struct {
KeyName string
Assigned string
Description string
}

func (cmd *PermissionsCommand) Run(args []string) error {
outputFormat := cmd.GetOutputFlag()
id, err := strconv.Atoi(args[0])
if err != nil {
return errors.NewInvalidUsageError(T("User ID should be a number."))
Expand All @@ -48,17 +67,41 @@ func (cmd *PermissionsCommand) Run(args []string) error {
return errors.NewAPIError(T("Failed to get user."), err.Error(), 2)
}

allPermission, err := cmd.UserManager.GetAllPermission()
allPermission, err := cmd.UserManager.GetAllPermissionDepartments()
if err != nil {
return errors.NewAPIError(T("Failed to get permissions."), err.Error(), 2)
}

userPermissions := []PermissionCollection{}

isMasterUser := false
if user.IsMasterUserFlag != nil && *user.IsMasterUserFlag {
cmd.UI.Print(T("This account is the Master User and has all permissions enabled"))
if outputFormat != "JSON" {
cmd.UI.Print(T("This account is the Master User and has all permissions enabled"))
}
isMasterUser = true
}


for _, department := range allPermission {
depPerm := PermissionCollection{Department: *department.KeyName}
for _, perm := range department.Permissions {
assignedPerm := UserHasPermission(user.Permissions, *perm.KeyName) || isMasterUser
thisPerm := Permission{
KeyName: *perm.KeyName,
Description: *perm.Description,
Assigned: strconv.FormatBool(assignedPerm),
}
depPerm.Permissions = append(depPerm.Permissions, thisPerm)
}
userPermissions = append(userPermissions, depPerm)
}

if outputFormat == "JSON" {
return utils.PrintPrettyJSON(cmd.UI, userPermissions)
}


table := cmd.UI.Table([]string{T("ID"), T("Role Name"), T("Description")})

for _, role := range user.Roles {
Expand All @@ -71,31 +114,28 @@ func (cmd *PermissionsCommand) Run(args []string) error {
table.Add("", "", "")
table.Print()

tablePermission := cmd.UI.Table([]string{T("Description"), T("KeyName"), T("Assigned")})
for _, perm := range allPermission {
var assigned bool
// Display master user as having all permissions, even though they have none, technically.
if isMasterUser {
assigned = true
}
for _, userPerm := range user.Permissions {
if perm.KeyName != nil && userPerm.KeyName != nil && *perm.KeyName == *userPerm.KeyName {
assigned = true
}

}
flag := true
arr := []string{"ACCOUNT_SUMMARY_VIEW", "REQUEST_COMPLIANCE_REPORT", "COMPANY_EDIT", "ONE_TIME_PAYMENTS", "UPDATE_PAYMENT_DETAILS",
"EU_LIMITED_PROCESSING_MANAGE", "TICKET_ADD", "TICKET_EDIT", "TICKET_SEARCH", "TICKET_VIEW", "TICKET_VIEW_ALL"}
for i := 0; i < len(arr); i++ {
if *perm.KeyName == arr[i] {
flag = false
}
}
if flag == true {
tablePermission.Add(utils.FormatStringPointer(perm.Name), utils.FormatStringPointer(perm.KeyName), strconv.FormatBool(assigned))
tablePermission := cmd.UI.Table([]string{T("Department"), T("Permissions")})
for _, department := range userPermissions {
buf := new(bytes.Buffer)
headers := []string{T("KeyName"), T("Assigned"), T("Description")}
subTable := terminal.NewTable(buf, headers)
for _, perm := range department.Permissions {
subTable.Add(perm.KeyName, perm.Assigned, perm.Description)
}
subTable.Print()
tablePermission.Add(department.Department, buf.String())
}
tablePermission.Print()
return nil
}


func UserHasPermission(userPermissions []datatypes.User_Customer_CustomerPermission_Permission, keyName string) bool {
assigned := false
for _, userPerm := range userPermissions {
if *userPerm.KeyName == keyName {
assigned = true
}
}
return assigned
}
Loading

0 comments on commit 5462e9a

Please sign in to comment.