Skip to content

Commit

Permalink
fix: update lint with revive
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Nov 15, 2022
1 parent 6773d69 commit ce7400c
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 203 deletions.
29 changes: 14 additions & 15 deletions accesscontroller/ipfs/accesscontroller_ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
"github.com/polydawn/refmt/obj/atlas"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -50,7 +49,7 @@ func (i *ipfsAccessController) CanAppend(entry logac.LogEntry, p identityprovide
}
}

return errors.New("not allowed")
return fmt.Errorf("not allowed")
}

func (i *ipfsAccessController) GetAuthorizedByRole(role string) ([]string, error) {
Expand All @@ -65,46 +64,46 @@ func (i *ipfsAccessController) GetAuthorizedByRole(role string) ([]string, error
}

func (i *ipfsAccessController) Grant(ctx context.Context, capability string, keyID string) error {
return errors.New("not implemented - does not exist in JS version")
return fmt.Errorf("not implemented - does not exist in JS version")
}

func (i *ipfsAccessController) Revoke(ctx context.Context, capability string, keyID string) error {
return errors.New("not implemented - does not exist in JS version")
return fmt.Errorf("not implemented - does not exist in JS version")
}

func (i *ipfsAccessController) Load(ctx context.Context, address string) error {
i.logger.Debug(fmt.Sprintf("reading IPFS access controller write access on hash %s", address))

c, err := cid.Decode(address)
if err != nil {
return errors.Wrap(err, "unable to parse cid")
return fmt.Errorf("unable to parse cid: %w", err)
}

res, err := io.ReadCBOR(ctx, i.ipfs, c)
if err != nil {
return errors.Wrap(err, "unable to load access controller manifest data")
return fmt.Errorf("unable to load access controller manifest data: %w", err)
}

manifest := &accesscontroller.Manifest{}
err = cbornode.DecodeInto(res.RawData(), manifest)
if err != nil {
return errors.Wrap(err, "unable to unmarshal access controller manifest data")
return fmt.Errorf("unable to unmarshal access controller manifest data: %w", err)
}

res, err = io.ReadCBOR(ctx, i.ipfs, manifest.Params.GetAddress())
if err != nil {
return errors.Wrap(err, "unable to load access controller data")
return fmt.Errorf("unable to load access controller data: %w", err)
}

writeAccessData := &cborWriteAccess{}
err = cbornode.DecodeInto(res.RawData(), writeAccessData)
if err != nil {
return errors.Wrap(err, "unable to unmarshal access controller data")
return fmt.Errorf("unable to unmarshal access controller data: %w", err)
}

var writeAccess []string
if err := json.Unmarshal([]byte(writeAccessData.Write), &writeAccess); err != nil {
return errors.Wrap(err, "unable to unmarshal json write access")
return fmt.Errorf("unable to unmarshal json write access: %w", err)
}

i.muWriteAccess.Lock()
Expand All @@ -120,12 +119,12 @@ func (i *ipfsAccessController) Save(ctx context.Context) (accesscontroller.Manif
i.muWriteAccess.RUnlock()

if err != nil {
return nil, errors.Wrap(err, "unable to serialize write access")
return nil, fmt.Errorf("unable to serialize write access: %w", err)
}

c, err := io.WriteCBOR(ctx, i.ipfs, &cborWriteAccess{Write: string(writeAccess)}, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to save access controller")
return nil, fmt.Errorf("unable to save access controller: %w", err)
}

i.logger.Debug(fmt.Sprintf("saved IPFS access controller write access on hash %s", c.String()))
Expand All @@ -134,17 +133,17 @@ func (i *ipfsAccessController) Save(ctx context.Context) (accesscontroller.Manif
}

func (i *ipfsAccessController) Close() error {
return errors.New("not implemented - does not exist in JS version")
return fmt.Errorf("not implemented - does not exist in JS version")
}

// NewIPFSAccessController Returns an access controller for IPFS
func NewIPFSAccessController(_ context.Context, db iface.BaseOrbitDB, params accesscontroller.ManifestParams, options ...accesscontroller.Option) (accesscontroller.Interface, error) {
if params == nil {
return &ipfsAccessController{}, errors.New("an options object must be passed")
return &ipfsAccessController{}, fmt.Errorf("an options object must be passed")
}

if db == nil {
return &ipfsAccessController{}, errors.New("an OrbitDB instance is required")
return &ipfsAccessController{}, fmt.Errorf("an OrbitDB instance is required")
}

if len(params.GetAccess("write")) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions accesscontroller/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package accesscontroller

import (
"context"
"fmt"
"strings"
"sync"

"berty.tech/go-ipfs-log/io"
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
"github.com/polydawn/refmt/obj/atlas"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -160,7 +160,7 @@ func CreateManifest(ctx context.Context, ipfs coreapi.CoreAPI, controllerType st
func ResolveManifest(ctx context.Context, ipfs coreapi.CoreAPI, manifestAddress string, params ManifestParams) (*Manifest, error) {
if params.GetSkipManifest() {
if params.GetType() == "" {
return nil, errors.New("no manifest, access-controller type required")
return nil, fmt.Errorf("no manifest, access-controller type required")
}

manifest := &Manifest{
Expand All @@ -177,18 +177,18 @@ func ResolveManifest(ctx context.Context, ipfs coreapi.CoreAPI, manifestAddress

c, err := cid.Decode(manifestAddress)
if err != nil {
return nil, errors.Wrap(err, "unable to parse CID")
return nil, fmt.Errorf("unable to parse CID: %w", err)
}

node, err := io.ReadCBOR(ctx, ipfs, c)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch manifest data")
return nil, fmt.Errorf("unable to fetch manifest data: %w", err)
}

manifest := &Manifest{}
err = cbornode.DecodeInto(node.RawData(), &manifest)
if err != nil {
return nil, errors.Wrap(err, "unable to unmarshal")
return nil, fmt.Errorf("unable to unmarshal: %w", err)
}

return manifest, nil
Expand Down
46 changes: 23 additions & 23 deletions accesscontroller/orbitdb/accesscontroller_orbitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package orbitdb
import (
"context"
"encoding/json"
"fmt"
"sync"

logac "berty.tech/go-ipfs-log/accesscontroller"
Expand All @@ -15,7 +16,6 @@ import (

cid "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/event"
"github.com/pkg/errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func (o *orbitDBAccessController) Address() address.Address {
func (o *orbitDBAccessController) GetAuthorizedByRole(role string) ([]string, error) {
authorizations, err := o.getAuthorizations()
if err != nil {
return nil, errors.Wrap(err, "unable to get authorizations")
return nil, fmt.Errorf("unable to get authorizations: %w", err)
}

return authorizations[role], nil
Expand All @@ -81,7 +81,7 @@ func (o *orbitDBAccessController) getAuthorizations() (map[string][]string, erro
authorizations[role] = map[string]struct{}{}

if err := json.Unmarshal(keyBytes, &authorizedKeys); err != nil {
return nil, errors.Wrap(err, "unable to unmarshal json")
return nil, fmt.Errorf("unable to unmarshal json: %w", err)
}

for _, key := range authorizedKeys {
Expand Down Expand Up @@ -114,12 +114,12 @@ func (o *orbitDBAccessController) getAuthorizations() (map[string][]string, erro
func (o *orbitDBAccessController) CanAppend(entry logac.LogEntry, p identityprovider.Interface, additionalContext accesscontroller.CanAppendAdditionalContext) error {
writeAccess, err := o.GetAuthorizedByRole("write")
if err != nil {
return errors.Wrap(err, "unable to get keys with write access")
return fmt.Errorf("unable to get keys with write access: %w", err)
}

adminAccess, err := o.GetAuthorizedByRole("admin")
if err != nil {
return errors.Wrap(err, "unable to get keys with admin access")
return fmt.Errorf("unable to get keys with admin access: %w", err)
}

access := append(writeAccess, adminAccess...)
Expand All @@ -130,25 +130,25 @@ func (o *orbitDBAccessController) CanAppend(entry logac.LogEntry, p identityprov
}
}

return errors.New("unauthorized")
return fmt.Errorf("unauthorized")
}

func (o *orbitDBAccessController) Grant(ctx context.Context, capability string, keyID string) error {
capabilities, err := o.GetAuthorizedByRole(capability)
if err != nil {
return errors.Wrap(err, "unable to fetch capabilities")
return fmt.Errorf("unable to fetch capabilities: %w", err)
}

capabilities = append(capabilities, keyID)

capabilitiesJSON, err := json.Marshal(capabilities)
if err != nil {
return errors.Wrap(err, "unable to marshal capabilities")
return fmt.Errorf("unable to marshal capabilities: %w", err)
}

_, err = o.kvStore.Put(ctx, capability, capabilitiesJSON)
if err != nil {
return errors.Wrap(err, "unable to put data in store")
return fmt.Errorf("unable to put data in store: %w", err)
}

return nil
Expand All @@ -157,7 +157,7 @@ func (o *orbitDBAccessController) Grant(ctx context.Context, capability string,
func (o *orbitDBAccessController) Revoke(ctx context.Context, capability string, keyID string) error {
capabilities, err := o.GetAuthorizedByRole(capability)
if err != nil {
return errors.Wrap(err, "unable to get capability")
return fmt.Errorf("unable to get capability: %w", err)
}

for idx, existingKeyID := range capabilities {
Expand All @@ -170,17 +170,17 @@ func (o *orbitDBAccessController) Revoke(ctx context.Context, capability string,
if len(capabilities) > 0 {
capabilitiesJSON, err := json.Marshal(capabilities)
if err != nil {
return errors.Wrap(err, "unable to marshal capabilities")
return fmt.Errorf("unable to marshal capabilities: %w", err)
}

_, err = o.kvStore.Put(ctx, capability, capabilitiesJSON)
if err != nil {
return errors.Wrap(err, "unable to persist capabilities")
return fmt.Errorf("unable to persist capabilities: %w", err)
}
} else {
_, err := o.kvStore.Delete(ctx, capability)
if err != nil {
return errors.Wrap(err, "unable to remove capabilities")
return fmt.Errorf("unable to remove capabilities: %w", err)
}
}

Expand All @@ -191,7 +191,7 @@ func (o *orbitDBAccessController) Load(ctx context.Context, address string) erro
if o.kvStore != nil {
err := o.kvStore.Close()
if err != nil {
return errors.Wrap(err, "unable to close opened store")
return fmt.Errorf("unable to close opened store: %w", err)
}
}

Expand All @@ -208,7 +208,7 @@ func (o *orbitDBAccessController) Load(ctx context.Context, address string) erro
AccessController: ipfsAccessController,
})
if err != nil {
return errors.Wrap(err, "unable to open key value store for access controller")
return fmt.Errorf("unable to open key value store for access controller: %w", err)
}

o.kvStore = store
Expand All @@ -219,7 +219,7 @@ func (o *orbitDBAccessController) Load(ctx context.Context, address string) erro
new(stores.EventReplicated),
})
if err != nil {
return errors.Wrap(err, "unable subscribe to store events")
return fmt.Errorf("unable subscribe to store events: %w", err)
}

go func() {
Expand All @@ -242,7 +242,7 @@ func (o *orbitDBAccessController) Load(ctx context.Context, address string) erro

err = o.kvStore.Load(ctx, -1)
if err != nil {
return errors.Wrap(err, "unable to fetch store data")
return fmt.Errorf("unable to fetch store data: %w", err)
}

return nil
Expand All @@ -254,7 +254,7 @@ func (o *orbitDBAccessController) Save(ctx context.Context) (accesscontroller.Ma

func (o *orbitDBAccessController) Close() error {
if err := o.kvStore.Close(); err != nil {
return errors.Wrap(err, "error while closing store")
return fmt.Errorf("error while closing store: %w", err)
}

return nil
Expand All @@ -269,12 +269,12 @@ func (o *orbitDBAccessController) onUpdate(ctx context.Context) {
// NewIPFSAccessController Returns a default access controller for OrbitDB database
func NewOrbitDBAccessController(ctx context.Context, db iface.BaseOrbitDB, params accesscontroller.ManifestParams, options ...accesscontroller.Option) (accesscontroller.Interface, error) {
if db == nil {
return &orbitDBAccessController{}, errors.New("an OrbitDB instance is required")
return &orbitDBAccessController{}, fmt.Errorf("an OrbitDB instance is required")
}

kvDB, ok := db.(iface.OrbitDBKVStoreProvider)
if !ok {
return &orbitDBAccessController{}, errors.New("the OrbitDB instance must provide a key value store")
return &orbitDBAccessController{}, fmt.Errorf("the OrbitDB instance must provide a key value store")
}

addr := "default-access-controller"
Expand All @@ -286,12 +286,12 @@ func NewOrbitDBAccessController(ctx context.Context, db iface.BaseOrbitDB, param

kvStore, err := kvDB.KeyValue(ctx, addr, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to init key value store")
return nil, fmt.Errorf("unable to init key value store: %w", err)
}

emitter, err := db.EventBus().Emitter(new(EventUpdated))
if err != nil {
return nil, errors.Wrap(err, "unable to init emitter")
return nil, fmt.Errorf("unable to init emitter: %w", err)
}

controller := &orbitDBAccessController{
Expand All @@ -306,7 +306,7 @@ func NewOrbitDBAccessController(ctx context.Context, db iface.BaseOrbitDB, param

for _, writeAccess := range params.GetAccess("write") {
if err := controller.Grant(ctx, "write", writeAccess); err != nil {
return nil, errors.Wrap(err, "unable to grant write access")
return nil, fmt.Errorf("unable to grant write access: %w", err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions accesscontroller/simple/accesscontroller_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simple

import (
"context"
"fmt"
"sync"

logac "berty.tech/go-ipfs-log/accesscontroller"
Expand All @@ -10,7 +11,6 @@ import (
"berty.tech/go-orbit-db/address"
"berty.tech/go-orbit-db/iface"
cid "github.com/ipfs/go-cid"
"github.com/pkg/errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -73,13 +73,13 @@ func (o *simpleAccessController) CanAppend(e logac.LogEntry, p identityprovider.
}
}

return errors.New("not allowed to write entry")
return fmt.Errorf("not allowed to write entry")
}

// NewSimpleAccessController Returns a non configurable access controller
func NewSimpleAccessController(_ context.Context, _ iface.BaseOrbitDB, params accesscontroller.ManifestParams, options ...accesscontroller.Option) (accesscontroller.Interface, error) {
if params == nil {
return &simpleAccessController{}, errors.New("an options object is required")
return &simpleAccessController{}, fmt.Errorf("an options object is required")
}

ac := &simpleAccessController{
Expand Down
Loading

0 comments on commit ce7400c

Please sign in to comment.