Skip to content

Commit

Permalink
refactor(act): naming and fix remaining PR comments (#42)
Browse files Browse the repository at this point in the history
* Refactor naming and fix remaining PR comments

* use ctx in grantees.get

* remove act_ucs.md
  • Loading branch information
bosi95 authored and aranyia committed May 17, 2024
1 parent 4d69d8d commit afccdbd
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 143 deletions.
86 changes: 0 additions & 86 deletions act_ucs.md

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/api/dynamicaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *Service) actListGranteesHandler(w http.ResponseWriter, r *http.Request)
}
publisher := &s.publicKey
ls := loadsave.NewReadonly(s.storer.Download(cache))
grantees, err := s.dac.GetGrantees(r.Context(), ls, publisher, paths.GranteesAddress)
grantees, err := s.dac.Get(r.Context(), ls, publisher, paths.GranteesAddress)
if err != nil {
logger.Debug("could not get grantees", "error", err)
logger.Error(nil, "could not get grantees")
Expand Down Expand Up @@ -326,7 +326,7 @@ func (s *Service) actGrantRevokeHandler(w http.ResponseWriter, r *http.Request)
publisher := &s.publicKey
ls := loadsave.New(s.storer.ChunkStore(), s.storer.Cache(), requestPipelineFactory(ctx, putter, false, redundancy.NONE))
gls := loadsave.New(s.storer.ChunkStore(), s.storer.Cache(), requestPipelineFactory(ctx, putter, granteeListEncrypt, redundancy.NONE))
granteeref, encryptedglref, historyref, actref, err := s.dac.HandleGrantees(ctx, ls, gls, granteeref, historyAddress, publisher, grantees.Addlist, grantees.Revokelist)
granteeref, encryptedglref, historyref, actref, err := s.dac.UpdateHandler(ctx, ls, gls, granteeref, historyAddress, publisher, grantees.Addlist, grantees.Revokelist)
if err != nil {
logger.Debug("failed to update grantee list", "error", err)
logger.Error(nil, "failed to update grantee list")
Expand Down Expand Up @@ -471,7 +471,7 @@ func (s *Service) actCreateGranteesHandler(w http.ResponseWriter, r *http.Reques
publisher := &s.publicKey
ls := loadsave.New(s.storer.ChunkStore(), s.storer.Cache(), requestPipelineFactory(ctx, putter, false, redundancy.NONE))
gls := loadsave.New(s.storer.ChunkStore(), s.storer.Cache(), requestPipelineFactory(ctx, putter, granteeListEncrypt, redundancy.NONE))
granteeref, encryptedglref, historyref, actref, err := s.dac.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, historyAddress, publisher, list, nil)
granteeref, encryptedglref, historyref, actref, err := s.dac.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, historyAddress, publisher, list, nil)
if err != nil {
logger.Debug("failed to update grantee list", "error", err)
logger.Error(nil, "failed to update grantee list")
Expand Down
34 changes: 15 additions & 19 deletions pkg/dynamicaccess/accesslogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDecryptRef_Success(t *testing.T) {
al := setupAccessLogic()
err := al.AddPublisher(ctx, s, &id0.PublicKey)
if err != nil {
t.Errorf("AddPublisher: expected no error, got %v", err)
t.Fatalf("AddPublisher: expected no error, got %v", err)
}

byteRef, _ := hex.DecodeString("39a5ea87b141fe44aa609c3327ecd896c0e2122897f5f4bbacf74db1033c5559")
Expand All @@ -73,18 +73,16 @@ func TestDecryptRef_Success(t *testing.T) {
encryptedRef, err := al.EncryptRef(ctx, s, &id0.PublicKey, expectedRef)
t.Logf("encryptedRef: %s", encryptedRef.String())
if err != nil {
t.Errorf("There was an error while calling EncryptRef: ")
t.Error(err)
t.Fatalf("There was an error while calling EncryptRef: %v", err)
}

actualRef, err := al.DecryptRef(ctx, s, encryptedRef, &id0.PublicKey)
if err != nil {
t.Errorf("There was an error while calling Get: ")
t.Error(err)
t.Fatalf("There was an error while calling Get: %v", err)
}

if expectedRef.Compare(actualRef) != 0 {
t.Errorf("Get returned a wrong Swarm reference!")
t.Fatalf("Get gave back wrong Swarm reference!")
}
}

Expand All @@ -97,13 +95,13 @@ func TestDecryptRefWithGrantee_Success(t *testing.T) {
s := kvsmock.New()
err := al.AddPublisher(ctx, s, &id0.PublicKey)
if err != nil {
t.Errorf("AddPublisher: expected no error, got %v", err)
t.Fatalf("AddPublisher: expected no error, got %v", err)
}

id1, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
err = al.AddGrantee(ctx, s, &id0.PublicKey, &id1.PublicKey, nil)
if err != nil {
t.Errorf("AddNewGrantee: expected no error, got %v", err)
t.Fatalf("AddNewGrantee: expected no error, got %v", err)
}

byteRef, _ := hex.DecodeString("39a5ea87b141fe44aa609c3327ecd896c0e2122897f5f4bbacf74db1033c5559")
Expand All @@ -114,20 +112,18 @@ func TestDecryptRefWithGrantee_Success(t *testing.T) {
encryptedRef, err := al.EncryptRef(ctx, s, &id0.PublicKey, expectedRef)
t.Logf("encryptedRef: %s", encryptedRef.String())
if err != nil {
t.Errorf("There was an error while calling EncryptRef: ")
t.Error(err)
t.Fatalf("There was an error while calling EncryptRef: %v", err)
}

diffieHellman2 := dynamicaccess.NewDefaultSession(id1)
granteeAccessLogic := dynamicaccess.NewLogic(diffieHellman2)
actualRef, err := granteeAccessLogic.DecryptRef(ctx, s, encryptedRef, &id0.PublicKey)
if err != nil {
t.Errorf("There was an error while calling Get: ")
t.Error(err)
t.Fatalf("There was an error while calling Get: %v", err)
}

if expectedRef.Compare(actualRef) != 0 {
t.Errorf("Get returned a wrong Swarm reference!")
t.Fatalf("Get gave back wrong Swarm reference!")
}
}

Expand All @@ -147,7 +143,7 @@ func TestDecryptRef_Error(t *testing.T) {
r, err := al.DecryptRef(ctx, s, encryptedRef, nil)
if err == nil {
t.Logf("r: %s", r.String())
t.Errorf("Get should return encrypted access key not found error!")
t.Fatalf("Get should return encrypted access key not found error!")
}
}

Expand All @@ -172,10 +168,10 @@ func TestAddPublisher(t *testing.T) {
// A random value is returned, so it is only possible to check the length of the returned value
// We know the lookup key because the generated private key is fixed
if len(decodedEncryptedAccessKey) != 64 {
t.Errorf("AddPublisher: expected encrypted access key length 64, got %d", len(decodedEncryptedAccessKey))
t.Fatalf("AddPublisher: expected encrypted access key length 64, got %d", len(decodedEncryptedAccessKey))
}
if s == nil {
t.Errorf("AddPublisher: expected act, got nil")
t.Fatalf("AddPublisher: expected act, got nil")
}
}

Expand Down Expand Up @@ -206,7 +202,7 @@ func TestAddNewGranteeToContent(t *testing.T) {
result, _ := s.Get(ctx, lookupKeyAsByte)
hexEncodedEncryptedAK := hex.EncodeToString(result)
if len(hexEncodedEncryptedAK) != 64 {
t.Errorf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
t.Fatalf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
}

lookupKeyAsByte, err = hex.DecodeString(firstAddedGranteeLookupKey)
Expand All @@ -215,7 +211,7 @@ func TestAddNewGranteeToContent(t *testing.T) {
result, _ = s.Get(ctx, lookupKeyAsByte)
hexEncodedEncryptedAK = hex.EncodeToString(result)
if len(hexEncodedEncryptedAK) != 64 {
t.Errorf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
t.Fatalf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
}

lookupKeyAsByte, err = hex.DecodeString(secondAddedGranteeLookupKey)
Expand All @@ -224,6 +220,6 @@ func TestAddNewGranteeToContent(t *testing.T) {
result, _ = s.Get(ctx, lookupKeyAsByte)
hexEncodedEncryptedAK = hex.EncodeToString(result)
if len(hexEncodedEncryptedAK) != 64 {
t.Errorf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
t.Fatalf("AddNewGrantee: expected encrypted access key length 64, got %d", len(hexEncodedEncryptedAK))
}
}
20 changes: 10 additions & 10 deletions pkg/dynamicaccess/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
"github.com/ethersphere/bee/v2/pkg/swarm"
)

type GranteeManager interface {
// HandleGrantees manages the grantees for the given publisher, updating the list based on provided public keys to add or remove.
type Grantees interface {
// UpdateHandler manages the grantees for the given publisher, updating the list based on provided public keys to add or remove.
// Only the publisher can make changes to the grantee list.
HandleGrantees(ctx context.Context, ls file.LoadSaver, gls file.LoadSaver, granteeref swarm.Address, historyref swarm.Address, publisher *ecdsa.PublicKey, addList, removeList []*ecdsa.PublicKey) (swarm.Address, swarm.Address, swarm.Address, swarm.Address, error)
// GetGrantees returns the list of grantees for the given publisher.
UpdateHandler(ctx context.Context, ls file.LoadSaver, gls file.LoadSaver, granteeref swarm.Address, historyref swarm.Address, publisher *ecdsa.PublicKey, addList, removeList []*ecdsa.PublicKey) (swarm.Address, swarm.Address, swarm.Address, swarm.Address, error)
// Get returns the list of grantees for the given publisher.
// The list is accessible only by the publisher.
GetGrantees(ctx context.Context, ls file.LoadSaver, publisher *ecdsa.PublicKey, encryptedglref swarm.Address) ([]*ecdsa.PublicKey, error)
Get(ctx context.Context, ls file.LoadSaver, publisher *ecdsa.PublicKey, encryptedglref swarm.Address) ([]*ecdsa.PublicKey, error)
}

type Controller interface {
GranteeManager
Grantees
// DownloadHandler decrypts the encryptedRef using the lookupkey based on the history and timestamp.
DownloadHandler(ctx context.Context, ls file.LoadSaver, encryptedRef swarm.Address, publisher *ecdsa.PublicKey, historyRootHash swarm.Address, timestamp int64) (swarm.Address, error)
// UploadHandler encrypts the reference and stores it in the history as the latest update.
Expand Down Expand Up @@ -128,7 +128,7 @@ func NewController(accessLogic ActLogic) *ControllerStruct {
}
}

func (c *ControllerStruct) HandleGrantees(
func (c *ControllerStruct) UpdateHandler(
ctx context.Context,
ls file.LoadSaver,
gls file.LoadSaver,
Expand Down Expand Up @@ -186,7 +186,7 @@ func (c *ControllerStruct) HandleGrantees(
return swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, err
}

gl, err = NewGranteeListReference(gls, granteeref)
gl, err = NewGranteeListReference(ctx, gls, granteeref)
if err != nil {
return swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, err
}
Expand Down Expand Up @@ -262,12 +262,12 @@ func (c *ControllerStruct) HandleGrantees(
return glref, eglref, href, actref, nil
}

func (c *ControllerStruct) GetGrantees(_ context.Context, ls file.LoadSaver, publisher *ecdsa.PublicKey, encryptedglref swarm.Address) ([]*ecdsa.PublicKey, error) {
func (c *ControllerStruct) Get(ctx context.Context, ls file.LoadSaver, publisher *ecdsa.PublicKey, encryptedglref swarm.Address) ([]*ecdsa.PublicKey, error) {
granteeRef, err := c.decryptRefForPublisher(publisher, encryptedglref)
if err != nil {
return nil, err
}
gl, err := NewGranteeListReference(ls, granteeRef)
gl, err := NewGranteeListReference(ctx, ls, granteeRef)
if err != nil {
return nil, err
}
Expand Down
36 changes: 18 additions & 18 deletions pkg/dynamicaccess/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,27 @@ func TestController_HandleGrantees(t *testing.T) {

t.Run("add to new list", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey}
granteeRef, _, _, _, err := c.HandleGrantees(ctx, ls, ls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)
granteeRef, _, _, _, err := c.UpdateHandler(ctx, ls, ls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)
assert.NoError(t, err)

gl, err := dynamicaccess.NewGranteeListReference(ls, granteeRef)
gl, err := dynamicaccess.NewGranteeListReference(ctx, ls, granteeRef)

assert.NoError(t, err)
assert.Len(t, gl.Get(), 1)
})
t.Run("add to existing list", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey}
granteeRef, eglref, _, _, err := c.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
granteeRef, eglref, _, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
assert.NoError(t, err)

gl, err := dynamicaccess.NewGranteeListReference(ls, granteeRef)
gl, err := dynamicaccess.NewGranteeListReference(ctx, ls, granteeRef)

assert.NoError(t, err)
assert.Len(t, gl.Get(), 1)

addList = []*ecdsa.PublicKey{&getPrivKey(0).PublicKey}
granteeRef, _, _, _, _ = c.HandleGrantees(ctx, ls, ls, eglref, href, &publisher.PublicKey, addList, nil)
gl, err = dynamicaccess.NewGranteeListReference(ls, granteeRef)
granteeRef, _, _, _, _ = c.UpdateHandler(ctx, ls, ls, eglref, href, &publisher.PublicKey, addList, nil)
gl, err = dynamicaccess.NewGranteeListReference(ctx, ls, granteeRef)
assert.NoError(t, err)
assert.Len(t, gl.Get(), 2)
})
Expand All @@ -196,8 +196,8 @@ func TestController_HandleGrantees(t *testing.T) {
granteeRef, _ := gl.Save(ctx)
eglref, _ := refCipher.Encrypt(granteeRef.Bytes())

granteeRef, _, _, _, _ = c.HandleGrantees(ctx, ls, gls, swarm.NewAddress(eglref), href, &publisher.PublicKey, addList, revokeList)
gl, err := dynamicaccess.NewGranteeListReference(ls, granteeRef)
granteeRef, _, _, _, _ = c.UpdateHandler(ctx, ls, gls, swarm.NewAddress(eglref), href, &publisher.PublicKey, addList, revokeList)
gl, err := dynamicaccess.NewGranteeListReference(ctx, ls, granteeRef)

assert.NoError(t, err)
assert.Len(t, gl.Get(), 2)
Expand All @@ -206,17 +206,17 @@ func TestController_HandleGrantees(t *testing.T) {
t.Run("add twice", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey, &grantee.PublicKey}
//nolint:ineffassign,staticcheck,wastedassign
granteeRef, eglref, _, _, err := c.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
granteeRef, _, _, _, _ = c.HandleGrantees(ctx, ls, ls, eglref, href, &publisher.PublicKey, addList, nil)
gl, err := dynamicaccess.NewGranteeListReference(createLs(), granteeRef)
granteeRef, eglref, _, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
granteeRef, _, _, _, _ = c.UpdateHandler(ctx, ls, ls, eglref, href, &publisher.PublicKey, addList, nil)
gl, err := dynamicaccess.NewGranteeListReference(ctx, createLs(), granteeRef)

assert.NoError(t, err)
assert.Len(t, gl.Get(), 1)
})
t.Run("revoke non-existing", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey}
granteeRef, _, _, _, _ := c.HandleGrantees(ctx, ls, ls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
gl, err := dynamicaccess.NewGranteeListReference(createLs(), granteeRef)
granteeRef, _, _, _, _ := c.UpdateHandler(ctx, ls, ls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil)
gl, err := dynamicaccess.NewGranteeListReference(ctx, createLs(), granteeRef)

assert.NoError(t, err)
assert.Len(t, gl.Get(), 1)
Expand All @@ -239,19 +239,19 @@ func TestController_GetGrantees(t *testing.T) {

t.Run("get by publisher", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey}
granteeRef, eglRef, _, _, _ := c1.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)
granteeRef, eglRef, _, _, _ := c1.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)

grantees, err := c1.GetGrantees(ctx, ls, &publisher.PublicKey, eglRef)
grantees, err := c1.Get(ctx, ls, &publisher.PublicKey, eglRef)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(grantees, addList))

gl, _ := dynamicaccess.NewGranteeListReference(ls, granteeRef)
gl, _ := dynamicaccess.NewGranteeListReference(ctx, ls, granteeRef)
assert.True(t, reflect.DeepEqual(gl.Get(), addList))
})
t.Run("get by non-publisher", func(t *testing.T) {
addList := []*ecdsa.PublicKey{&grantee.PublicKey}
_, eglRef, _, _, _ := c1.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)
grantees, err := c2.GetGrantees(ctx, ls, &publisher.PublicKey, eglRef)
_, eglRef, _, _, _ := c1.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil)
grantees, err := c2.Get(ctx, ls, &publisher.PublicKey, eglRef)
assert.Error(t, err)
assert.Nil(t, grantees)
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/dynamicaccess/grantee.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func NewGranteeList(ls file.LoadSaver) (*GranteeListStruct, error) { // Why is t
}, nil
}

func NewGranteeListReference(ls file.LoadSaver, reference swarm.Address) (*GranteeListStruct, error) {
data, err := ls.Load(context.Background(), reference.Bytes())
func NewGranteeListReference(ctx context.Context, ls file.LoadSaver, reference swarm.Address) (*GranteeListStruct, error) {
data, err := ls.Load(ctx, reference.Bytes())
if err != nil {
return nil, fmt.Errorf("unable to load reference, %w", err)
}
Expand Down
Loading

0 comments on commit afccdbd

Please sign in to comment.