Skip to content

Commit

Permalink
Merge branch 'internal-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
byo committed Nov 19, 2023
2 parents f18e3c9 + 4fa3eb5 commit 2821712
Show file tree
Hide file tree
Showing 84 changed files with 5,054 additions and 1,245 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"blobtype",
"blobtypes",
"bmap",
"chacha",
"cinode",
"cinodefs",
"cipherfactory",
Expand All @@ -15,13 +16,16 @@
"dynamiclink",
"elink",
"fifos",
"fsys",
"goveralls",
"Hasher",
"homefile",
"jbenet",
"protobuf",
"securefifo",
"shogo",
"stretchr",
"subdir",
"testblobs",
"testvectors",
"validatingreader"
Expand Down
13 changes: 10 additions & 3 deletions cmd/static_datastore_builder/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2022 Bartłomiej Święcki (byo)
Copyright © 2023 Bartłomiej Święcki (byo)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,15 @@ limitations under the License.

package main

import "github.com/cinode/go/pkg/cmd/static_datastore"
import (
"context"
"log"

"github.com/cinode/go/pkg/cmd/static_datastore"
)

func main() {
static_datastore.Execute()
if err := static_datastore.Execute(context.Background()); err != nil {
log.Fatal(err.Error())
}
}
15 changes: 7 additions & 8 deletions pkg/blenc/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cinode/go/pkg/blobtypes"
"github.com/cinode/go/pkg/common"
"github.com/cinode/go/pkg/datastore"
"github.com/cinode/go/pkg/internal/utilities/cipherfactory"
"github.com/cinode/go/pkg/internal/utilities/securefifo"
)

Expand All @@ -51,7 +50,7 @@ type beDatastore struct {
newSecureFifo secureFifoGenerator
}

func (be *beDatastore) Open(ctx context.Context, name common.BlobName, key cipherfactory.Key) (io.ReadCloser, error) {
func (be *beDatastore) Open(ctx context.Context, name *common.BlobName, key *common.BlobKey) (io.ReadCloser, error) {
switch name.Type() {
case blobtypes.Static:
return be.openStatic(ctx, name, key)
Expand All @@ -66,9 +65,9 @@ func (be *beDatastore) Create(
blobType common.BlobType,
r io.Reader,
) (
common.BlobName,
cipherfactory.Key,
AuthInfo,
*common.BlobName,
*common.BlobKey,
*common.AuthInfo,
error,
) {
switch blobType {
Expand All @@ -80,7 +79,7 @@ func (be *beDatastore) Create(
return nil, nil, nil, blobtypes.ErrUnknownBlobType
}

func (be *beDatastore) Update(ctx context.Context, name common.BlobName, authInfo AuthInfo, key cipherfactory.Key, r io.Reader) error {
func (be *beDatastore) Update(ctx context.Context, name *common.BlobName, authInfo *common.AuthInfo, key *common.BlobKey, r io.Reader) error {
switch name.Type() {
case blobtypes.Static:
return be.updateStatic(ctx, name, authInfo, key, r)
Expand All @@ -90,10 +89,10 @@ func (be *beDatastore) Update(ctx context.Context, name common.BlobName, authInf
return blobtypes.ErrUnknownBlobType
}

func (be *beDatastore) Exists(ctx context.Context, name common.BlobName) (bool, error) {
func (be *beDatastore) Exists(ctx context.Context, name *common.BlobName) (bool, error) {
return be.ds.Exists(ctx, name)
}

func (be *beDatastore) Delete(ctx context.Context, name common.BlobName) error {
func (be *beDatastore) Delete(ctx context.Context, name *common.BlobName) error {
return be.ds.Delete(ctx, name)
}
24 changes: 11 additions & 13 deletions pkg/blenc/datastore_dynamic_link.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2022 Bartłomiej Święcki (byo)
Copyright © 2023 Bartłomiej Święcki (byo)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,13 @@ limitations under the License.
package blenc

import (
"bytes"
"context"
"errors"
"fmt"
"io"

"github.com/cinode/go/pkg/common"
"github.com/cinode/go/pkg/internal/blobtypes/dynamiclink"
"github.com/cinode/go/pkg/internal/utilities/cipherfactory"
)

var (
Expand All @@ -37,8 +35,8 @@ var (

func (be *beDatastore) openDynamicLink(
ctx context.Context,
name common.BlobName,
key cipherfactory.Key,
name *common.BlobName,
key *common.BlobKey,
) (
io.ReadCloser,
error,
Expand Down Expand Up @@ -77,9 +75,9 @@ func (be *beDatastore) createDynamicLink(
ctx context.Context,
r io.Reader,
) (
common.BlobName,
cipherfactory.Key,
AuthInfo,
*common.BlobName,
*common.BlobKey,
*common.AuthInfo,
error,
) {
version := be.generateVersion()
Expand Down Expand Up @@ -109,9 +107,9 @@ func (be *beDatastore) createDynamicLink(

func (be *beDatastore) updateDynamicLink(
ctx context.Context,
name common.BlobName,
authInfo AuthInfo,
key cipherfactory.Key,
name *common.BlobName,
authInfo *common.AuthInfo,
key *common.BlobKey,
r io.Reader,
) error {
newVersion := be.generateVersion()
Expand All @@ -127,10 +125,10 @@ func (be *beDatastore) updateDynamicLink(
}

// Sanity checks
if !bytes.Equal(encryptionKey, key) {
if !encryptionKey.Equal(key) {
return ErrDynamicLinkUpdateFailedWrongKey
}
if !bytes.Equal(name, dl.BlobName()) {
if !name.Equal(dl.BlobName()) {
return ErrDynamicLinkUpdateFailedWrongName
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/blenc/datastore_dynamic_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ import (

type dsWrapper struct {
datastore.DS
openFn func(ctx context.Context, name common.BlobName) (io.ReadCloser, error)
updateFn func(ctx context.Context, name common.BlobName, r io.Reader) error
openFn func(ctx context.Context, name *common.BlobName) (io.ReadCloser, error)
updateFn func(ctx context.Context, name *common.BlobName, r io.Reader) error
}

func (w *dsWrapper) Open(ctx context.Context, name common.BlobName) (io.ReadCloser, error) {
func (w *dsWrapper) Open(ctx context.Context, name *common.BlobName) (io.ReadCloser, error) {
if w.openFn != nil {
return w.openFn(ctx, name)
}
return w.DS.Open(ctx, name)
}

func (w *dsWrapper) Update(ctx context.Context, name common.BlobName, r io.Reader) error {
func (w *dsWrapper) Update(ctx context.Context, name *common.BlobName, r io.Reader) error {
if w.updateFn != nil {
return w.updateFn(ctx, name, r)
}
Expand All @@ -68,7 +68,7 @@ func TestDynamicLinkErrors(t *testing.T) {

t.Run("handle error while opening blob", func(t *testing.T) {
injectedErr := errors.New("test")
dsw.openFn = func(ctx context.Context, name common.BlobName) (io.ReadCloser, error) { return nil, injectedErr }
dsw.openFn = func(ctx context.Context, name *common.BlobName) (io.ReadCloser, error) { return nil, injectedErr }

rc, err := be.Open(context.Background(), bn, key)
require.ErrorIs(t, err, injectedErr)
Expand All @@ -84,7 +84,7 @@ func TestDynamicLinkErrors(t *testing.T) {

t.Run(fmt.Sprintf("error at byte %d", i), func(t *testing.T) {

dsw.openFn = func(ctx context.Context, name common.BlobName) (io.ReadCloser, error) {
dsw.openFn = func(ctx context.Context, name *common.BlobName) (io.ReadCloser, error) {
origRC, err := dsw.DS.Open(ctx, name)
require.NoError(t, err)

Expand Down Expand Up @@ -124,9 +124,9 @@ func TestDynamicLinkErrors(t *testing.T) {

bn, key, ai, err := be.Create(context.Background(), blobtypes.DynamicLink, bytes.NewReader(nil))
require.ErrorIs(t, err, injectedErr)
require.Nil(t, bn)
require.Nil(t, key)
require.Nil(t, ai)
require.Empty(t, bn)
require.Empty(t, key)
require.Empty(t, ai)

be.(*beDatastore).rand = rand.Reader

Expand All @@ -135,13 +135,13 @@ func TestDynamicLinkErrors(t *testing.T) {
t.Run("fail to store new dynamic link blob", func(t *testing.T) {
injectedErr := errors.New("test")

dsw.updateFn = func(ctx context.Context, name common.BlobName, r io.Reader) error { return injectedErr }
dsw.updateFn = func(ctx context.Context, name *common.BlobName, r io.Reader) error { return injectedErr }

bn, key, ai, err := be.Create(context.Background(), blobtypes.DynamicLink, bytes.NewReader(nil))
require.ErrorIs(t, err, injectedErr)
require.Nil(t, bn)
require.Nil(t, key)
require.Nil(t, ai)
require.Empty(t, bn)
require.Empty(t, key)
require.Empty(t, ai)

dsw.updateFn = nil
})
Expand All @@ -152,7 +152,7 @@ func TestDynamicLinkErrors(t *testing.T) {
bn, key, ai, err := be.Create(context.Background(), blobtypes.DynamicLink, bytes.NewReader(nil))
require.NoError(t, err)

dsw.updateFn = func(ctx context.Context, name common.BlobName, r io.Reader) error { return injectedErr }
dsw.updateFn = func(ctx context.Context, name *common.BlobName, r io.Reader) error { return injectedErr }

err = be.Update(context.Background(), bn, ai, key, bytes.NewReader(nil))
require.ErrorIs(t, err, injectedErr)
Expand Down
21 changes: 10 additions & 11 deletions pkg/blenc/datastore_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package blenc

import (
"bytes"
"context"
"crypto/sha256"
"errors"
Expand All @@ -33,14 +32,14 @@ var (
ErrCanNotUpdateStaticBlob = errors.New("blob update is not supported for static blobs")
)

func (be *beDatastore) openStatic(ctx context.Context, name common.BlobName, key cipherfactory.Key) (io.ReadCloser, error) {
func (be *beDatastore) openStatic(ctx context.Context, name *common.BlobName, key *common.BlobKey) (io.ReadCloser, error) {

rc, err := be.ds.Open(ctx, name)
if err != nil {
return nil, err
}

scr, err := cipherfactory.StreamCipherReader(key, key.DefaultIV(), rc)
scr, err := cipherfactory.StreamCipherReader(key, cipherfactory.DefaultIV(key), rc)
if err != nil {
return nil, err
}
Expand All @@ -54,7 +53,7 @@ func (be *beDatastore) openStatic(ctx context.Context, name common.BlobName, key
Reader: validatingreader.CheckOnEOF(
io.TeeReader(scr, keyGenerator),
func() error {
if !bytes.Equal(key, keyGenerator.Generate()) {
if !key.Equal(keyGenerator.Generate()) {
return blobtypes.ErrValidationFailed
}
return nil
Expand All @@ -68,9 +67,9 @@ func (be *beDatastore) createStatic(
ctx context.Context,
r io.Reader,
) (
common.BlobName,
cipherfactory.Key,
AuthInfo,
*common.BlobName,
*common.BlobKey,
*common.AuthInfo,
error,
) {
tempWriteBufferPlain, err := be.newSecureFifo()
Expand All @@ -92,7 +91,7 @@ func (be *beDatastore) createStatic(
}

key := keyGenerator.Generate()
iv := key.DefaultIV() // We can use this since each blob will have different key
iv := cipherfactory.DefaultIV(key) // We can use this since each blob will have different key

rClone, err := tempWriteBufferPlain.Done() // rClone will allow re-reading the source data
if err != nil {
Expand Down Expand Up @@ -141,9 +140,9 @@ func (be *beDatastore) createStatic(

func (be *beDatastore) updateStatic(
ctx context.Context,
name common.BlobName,
authInfo AuthInfo,
key cipherfactory.Key,
name *common.BlobName,
authInfo *common.AuthInfo,
key *common.BlobKey,
r io.Reader,
) error {
return ErrCanNotUpdateStaticBlob
Expand Down
Loading

0 comments on commit 2821712

Please sign in to comment.