kopia lustrzana https://github.com/bugout-dev/moonstream
Updated package versions and verify command
rodzic
8bf1915b68
commit
0af43774fc
|
@ -3,6 +3,8 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
@ -234,6 +236,128 @@ var CommonCommands = []*cli.Command{
|
|||
}
|
||||
fmt.Println(string(userAccessesJson))
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "verify",
|
||||
Usage: "Verify accesses in correct state",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "access-token",
|
||||
Aliases: []string{"t"},
|
||||
Usage: "Authorized user access token with granted privileges to get resources in Moonstream Bugout application",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
accessToken := c.String("access-token")
|
||||
|
||||
var clientErr error
|
||||
bugoutClient, clientErr = CreateBugoutClient()
|
||||
if clientErr != nil {
|
||||
return fmt.Errorf("an error occurred during Bugout client creation: %v", clientErr)
|
||||
}
|
||||
|
||||
resources, getResErr := GetAccesses("", "", accessToken)
|
||||
if getResErr != nil {
|
||||
return fmt.Errorf("unable to get Bugout resources, err: %v", getResErr)
|
||||
}
|
||||
|
||||
if len(resources.Resources) == 0 {
|
||||
fmt.Println("[]")
|
||||
return nil
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
sem := make(chan struct{}, 3)
|
||||
errChan := make(chan error, len(resources.Resources))
|
||||
|
||||
var modifiedClientAccesses []ClientAccess
|
||||
var deleteClientAccesses []ClientAccess
|
||||
shareResourceIds := make(map[string]bool)
|
||||
for _, resource := range resources.Resources {
|
||||
wg.Add(1)
|
||||
go func(resourceId string) {
|
||||
defer wg.Done()
|
||||
sem <- struct{}{}
|
||||
|
||||
var isShared bool
|
||||
|
||||
holders, holdErr := CheckAccess(resourceId, accessToken)
|
||||
if holdErr != nil {
|
||||
errChan <- fmt.Errorf("failed get holders for resource ID %s with error %v", resourceId, holdErr)
|
||||
}
|
||||
|
||||
for _, h := range holders.Holders {
|
||||
if h.Id == NB_CONTROLLER_USER_ID {
|
||||
isShared = true
|
||||
}
|
||||
}
|
||||
|
||||
if !isShared {
|
||||
shareResourceIds[resourceId] = true
|
||||
}
|
||||
|
||||
<-sem
|
||||
}(resource.Id)
|
||||
|
||||
var isModified bool
|
||||
clientAccess, parseErr := ParseResourceDataToClientAccess(resource)
|
||||
if parseErr != nil {
|
||||
fmt.Printf("Unable to parse resource data, err: %v", parseErr)
|
||||
continue
|
||||
}
|
||||
|
||||
if clientAccess.ClientResourceData.Name == "" || clientAccess.ClientResourceData.AccessID == "" {
|
||||
deleteClientAccesses = append(deleteClientAccesses, *clientAccess)
|
||||
continue
|
||||
}
|
||||
|
||||
if clientAccess.ClientResourceData.PeriodStartTs == 0 {
|
||||
clientAccess.ClientResourceData.PeriodStartTs = int64(time.Now().Unix())
|
||||
isModified = true
|
||||
}
|
||||
|
||||
if clientAccess.ClientResourceData.PeriodDuration < 3600 {
|
||||
clientAccess.ClientResourceData.PeriodDuration = 3600
|
||||
isModified = true
|
||||
}
|
||||
|
||||
if isModified {
|
||||
modifiedClientAccesses = append(modifiedClientAccesses, *clientAccess)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("There are %d accesses to modify:\n", len(modifiedClientAccesses))
|
||||
for _, a := range modifiedClientAccesses {
|
||||
fmt.Printf(" - resource ID %s with access ID %s\n", a.ResourceID, a.ClientResourceData.AccessID)
|
||||
}
|
||||
fmt.Printf("There are %d accesses to delete\n", len(deleteClientAccesses))
|
||||
for _, a := range deleteClientAccesses {
|
||||
fmt.Printf(" - resource ID %s with access ID %s\n", a.ResourceID, a.ClientResourceData.AccessID)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(sem)
|
||||
close(errChan)
|
||||
|
||||
var errorMessages []string
|
||||
for err := range errChan {
|
||||
errorMessages = append(errorMessages, err.Error())
|
||||
}
|
||||
|
||||
if len(errorMessages) > 0 {
|
||||
fmt.Printf("errors occurred during verification:\n%s", strings.Join(errorMessages, "\n"))
|
||||
}
|
||||
|
||||
fmt.Printf("There are %d accesses not shared with nodebalancer application user\n", len(shareResourceIds))
|
||||
fmt.Printf("There are %d accesses to delete\n", len(deleteClientAccesses))
|
||||
for a := range shareResourceIds {
|
||||
fmt.Printf(" - resource ID %s\n", a)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
|
|
@ -240,6 +240,15 @@ func AddNewAccess(accessId, userId, name, description string, blockchainAccess,
|
|||
return &newUserAccess, nil
|
||||
}
|
||||
|
||||
func CheckAccess(resourceId, accessToken string) (*brood.ResourceHolders, error) {
|
||||
holders, holdErr := bugoutClient.Brood.GetResourceHolders(accessToken, resourceId)
|
||||
if holdErr != nil {
|
||||
return nil, fmt.Errorf("unable to fetch resource holders, err: %v", holdErr)
|
||||
}
|
||||
|
||||
return &holders, nil
|
||||
}
|
||||
|
||||
func ShareAccess(resourceId, userId, holderType string, permissions []string, accessToken string) (*brood.ResourceHolders, error) {
|
||||
resourceHolderPermissions, holdErr := bugoutClient.Brood.AddResourceHolderPermissions(
|
||||
accessToken, resourceId, brood.ResourceHolder{
|
||||
|
|
|
@ -3,9 +3,9 @@ module github.com/bugout-dev/moonstream/nodes/node_balancer
|
|||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/bugout-dev/bugout-go v0.4.3
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/bugout-dev/bugout-go v0.4.5
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20230713220619-2cd74a2b36d7
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/urfave/cli/v2 v2.27.5
|
||||
)
|
||||
|
||||
|
|
|
@ -24,10 +24,12 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
|
|||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
|
||||
github.com/bugout-dev/bugout-go v0.4.3 h1:sTwMgNDZR8mK0BkPVfXIsFWZAkL8ePKr5Xmj35A2O3o=
|
||||
github.com/bugout-dev/bugout-go v0.4.3/go.mod h1:P4+788iHtt/32u2wIaRTaiXTWpvSVBYxZ01qQ8N7eB8=
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205 h1:UQ7XGjvoOVKGRIuTFXgqGtU/UgMOk8+ikpoHWrWefjQ=
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20211206230955-57607cd2d205/go.mod h1:U/NXHfc3tzGeQz+xVfpifXdPZi7p6VV8xdP/4ZKeWJU=
|
||||
github.com/bugout-dev/bugout-go v0.4.4 h1:B6Ke/27tT18l+X21ho3uIGVxVGisWRODg2q16CHIWPE=
|
||||
github.com/bugout-dev/bugout-go v0.4.4/go.mod h1:P4+788iHtt/32u2wIaRTaiXTWpvSVBYxZ01qQ8N7eB8=
|
||||
github.com/bugout-dev/bugout-go v0.4.5 h1:W5VVVaDOpb5QfF949UNZ4KCFQGZ055ewlGKi58fwBVo=
|
||||
github.com/bugout-dev/bugout-go v0.4.5/go.mod h1:P4+788iHtt/32u2wIaRTaiXTWpvSVBYxZ01qQ8N7eB8=
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20230713220619-2cd74a2b36d7 h1:Mn6t3HO056/++m5UESl/06FdSxz84S1p7pfQA+NZwVo=
|
||||
github.com/bugout-dev/humbug/go v0.0.0-20230713220619-2cd74a2b36d7/go.mod h1:U/NXHfc3tzGeQz+xVfpifXdPZi7p6VV8xdP/4ZKeWJU=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
|
||||
|
@ -68,8 +70,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
|
|||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
|
||||
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
|
|
Ładowanie…
Reference in New Issue