[go: nahoru, domu]

Skip to content

Commit

Permalink
refactor(repository): extract parts repo/content into packages (kopia…
Browse files Browse the repository at this point in the history
…#2651)

- repolog package
- blobcrypto package
- indexblob package

Minor cleanups:

- removed dead code
- introduced New*() methods for object construction
  • Loading branch information
jkowalski committed Dec 17, 2022
1 parent 4c0fe39 commit f8be8f6
Show file tree
Hide file tree
Showing 33 changed files with 818 additions and 700 deletions.
7 changes: 4 additions & 3 deletions cli/command_blob_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"strings"

"github.com/kopia/kopia/internal/epoch"
"github.com/kopia/kopia/internal/repolog"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/indexblob"
)

type commandBlobList struct {
Expand Down Expand Up @@ -57,15 +58,15 @@ func (c *commandBlobList) run(ctx context.Context, rep repo.DirectRepository) er

func (c *commandBlobList) shouldInclude(b blob.Metadata) bool {
if c.dataOnly {
if strings.HasPrefix(string(b.BlobID), content.LegacyIndexBlobPrefix) {
if strings.HasPrefix(string(b.BlobID), indexblob.V0IndexBlobPrefix) {
return false
}

if strings.HasPrefix(string(b.BlobID), epoch.EpochManagerIndexUberPrefix) {
return false
}

if strings.HasPrefix(string(b.BlobID), content.TextLogBlobPrefix) {
if strings.HasPrefix(string(b.BlobID), repolog.BlobPrefix) {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions cli/command_blob_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"github.com/pkg/errors"

"github.com/kopia/kopia/internal/blobcrypto"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/internal/iocopy"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
)

type commandBlobShow struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *commandBlobShow) maybeDecryptBlob(ctx context.Context, w io.Writer, rep
var tmp gather.WriteBuffer
defer tmp.Close()

if err := content.DecryptBLOB(rep.ContentReader().ContentFormat(), b, blobID, &tmp); err != nil {
if err := blobcrypto.Decrypt(rep.ContentReader().ContentFormat(), b, blobID, &tmp); err != nil {
return errors.Wrap(err, "error decrypting blob")
}

Expand Down
3 changes: 2 additions & 1 deletion cli/command_index_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/indexblob"
)

type commandIndexInspect struct {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (c *commandIndexInspect) inspectAllBlobs(ctx context.Context, rep repo.Dire
return errors.Wrap(err, "error listing index blobs")
}

indexesCh := make(chan content.IndexBlobInfo, len(indexes))
indexesCh := make(chan indexblob.Metadata, len(indexes))
for _, bm := range indexes {
indexesCh <- bm
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command_index_optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/indexblob"
)

type commandIndexOptimize struct {
Expand Down Expand Up @@ -36,7 +36,7 @@ func (c *commandIndexOptimize) runOptimizeCommand(ctx context.Context, rep repo.
return err
}

opt := content.CompactOptions{
opt := indexblob.CompactOptions{
MaxSmallBlobs: c.optimizeMaxSmallBlobs,
AllIndexes: c.optimizeAllIndexes,
DropContents: contentIDs,
Expand Down
3 changes: 2 additions & 1 deletion cli/command_index_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/indexblob"
)

type commandIndexRecover struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func (c *commandIndexRecover) run(ctx context.Context, rep repo.DirectRepository
}()

if c.deleteIndexes {
if err := rep.BlobReader().ListBlobs(ctx, content.LegacyIndexBlobPrefix, func(bm blob.Metadata) error {
if err := rep.BlobReader().ListBlobs(ctx, indexblob.V0IndexBlobPrefix, func(bm blob.Metadata) error {
if c.commit {
log(ctx).Infof("deleting old index blob: %v", bm.BlobID)
return errors.Wrap(rep.BlobStorage().DeleteBlob(ctx, bm.BlobID), "error deleting index blob")
Expand Down
4 changes: 2 additions & 2 deletions cli/command_logs_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/pkg/errors"

"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/internal/repolog"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
)

type logSessionInfo struct {
Expand Down Expand Up @@ -70,7 +70,7 @@ func getLogSessions(ctx context.Context, st blob.Reader) ([]*logSessionInfo, err

var allSessions []*logSessionInfo

if err := st.ListBlobs(ctx, content.TextLogBlobPrefix, func(bm blob.Metadata) error {
if err := st.ListBlobs(ctx, repolog.BlobPrefix, func(bm blob.Metadata) error {
parts := strings.Split(string(bm.BlobID), "_")

//nolint:gomnd
Expand Down
4 changes: 2 additions & 2 deletions cli/command_logs_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/pkg/errors"

"github.com/kopia/kopia/internal/blobcrypto"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)

type commandLogsShow struct {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *commandLogsShow) run(ctx context.Context, rep repo.DirectRepository) er
return errors.Wrap(err, "error getting log")
}

if err := content.DecryptBLOB(rep.ContentReader().ContentFormat(), data.Bytes(), bm.BlobID, &decrypted); err != nil {
if err := blobcrypto.Decrypt(rep.ContentReader().ContentFormat(), data.Bytes(), bm.BlobID, &decrypted); err != nil {
return errors.Wrap(err, "error decrypting log")
}

Expand Down
3 changes: 2 additions & 1 deletion cli/command_repository_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/index"
"github.com/kopia/kopia/repo/content/indexblob"
"github.com/kopia/kopia/repo/format"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func assign(iif content.Info, i int, m map[content.ID][2]index.Info) {
}

// loadIndexBlobs load index blobs into indexEntries map. indexEntries map will allow comparison betweel two indexes (index at which == 0 and index at which == 1).
func loadIndexBlobs(ctx context.Context, indexEntries map[content.ID][2]index.Info, sm *content.SharedManager, which int, indexBlobInfos []content.IndexBlobInfo) error {
func loadIndexBlobs(ctx context.Context, indexEntries map[content.ID][2]index.Info, sm *content.SharedManager, which int, indexBlobInfos []indexblob.Metadata) error {
d := gather.WriteBuffer{}

for _, indexBlobInfo := range indexBlobInfos {
Expand Down
19 changes: 10 additions & 9 deletions repo/content/blob_crypto.go → internal/blobcrypto/blob_crypto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package content
// Package blobcrypto performs whole-blob crypto operations.
package blobcrypto

import (
"crypto/aes"
Expand All @@ -13,8 +14,8 @@ import (
"github.com/kopia/kopia/repo/hashing"
)

// crypter ecapsulates hashing and encryption.
type crypter interface {
// Crypter ecapsulates hashing and encryption.
type Crypter interface {
HashFunc() hashing.HashFunc
Encryptor() encryption.Encryptor
}
Expand All @@ -38,16 +39,16 @@ func getIndexBlobIV(s blob.ID) ([]byte, error) {
return v, nil
}

// EncryptBLOB encrypts the given data using crypter-defined key and returns a name that should
// Encrypt encrypts the given data using crypter-defined key and returns a name that should
// be used to save the blob in thre repository.
func EncryptBLOB(c crypter, payload gather.Bytes, prefix blob.ID, sessionID SessionID, output *gather.WriteBuffer) (blob.ID, error) {
func Encrypt(c Crypter, payload gather.Bytes, prefix, suffix blob.ID, output *gather.WriteBuffer) (blob.ID, error) {
var hashOutput [hashing.MaxHashSize]byte

hash := c.HashFunc()(hashOutput[:0], payload)
blobID := prefix + blob.ID(hex.EncodeToString(hash))

if sessionID != "" {
blobID += blob.ID("-" + sessionID)
if suffix != "" {
blobID += "-" + suffix
}

iv, err := getIndexBlobIV(blobID)
Expand All @@ -64,8 +65,8 @@ func EncryptBLOB(c crypter, payload gather.Bytes, prefix blob.ID, sessionID Sess
return blobID, nil
}

// DecryptBLOB decrypts the provided data using provided blobID to derive initialization vector.
func DecryptBLOB(c crypter, payload gather.Bytes, blobID blob.ID, output *gather.WriteBuffer) error {
// Decrypt decrypts the provided data using provided blobID to derive initialization vector.
func Decrypt(c Crypter, payload gather.Bytes, blobID blob.ID, output *gather.WriteBuffer) error {
iv, err := getIndexBlobIV(blobID)
if err != nil {
return errors.Wrap(err, "unable to get index blob IV")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package content
package blobcrypto

import (
"strings"
Expand Down Expand Up @@ -30,31 +30,31 @@ func TestBlobCrypto(t *testing.T) {
defer tmp2.Close()
defer tmp3.Close()

id, err := EncryptBLOB(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
id, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
require.NoError(t, err)

id2, err := EncryptBLOB(cr, gather.FromSlice([]byte{1, 2, 4}), "n", "mysessionid", &tmp2)
id2, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 4}), "n", "mysessionid", &tmp2)
require.NoError(t, err)

require.NotEqual(t, id, id2)

require.NoError(t, DecryptBLOB(cr, tmp.Bytes(), id, &tmp3))
require.NoError(t, Decrypt(cr, tmp.Bytes(), id, &tmp3))
require.Equal(t, []byte{1, 2, 3}, tmp3.ToByteSlice())
require.NoError(t, DecryptBLOB(cr, tmp2.Bytes(), id2, &tmp3))
require.NoError(t, Decrypt(cr, tmp2.Bytes(), id2, &tmp3))
require.Equal(t, []byte{1, 2, 4}, tmp3.ToByteSlice())

// decrypting using invalid ID fails
require.Error(t, DecryptBLOB(cr, tmp.Bytes(), id2, &tmp3))
require.Error(t, DecryptBLOB(cr, tmp2.Bytes(), id, &tmp3))
require.Error(t, Decrypt(cr, tmp.Bytes(), id2, &tmp3))
require.Error(t, Decrypt(cr, tmp2.Bytes(), id, &tmp3))

require.True(t, strings.HasPrefix(string(id), "n"))
require.True(t, strings.HasSuffix(string(id), "-mysessionid"), id)

// negative cases
require.Error(t, DecryptBLOB(cr, tmp.Bytes(), "invalid-blob-id", &tmp3))
require.Error(t, DecryptBLOB(cr, tmp.Bytes(), "zzz0123456789abcdef0123456789abcde-suffix", &tmp3))
require.Error(t, DecryptBLOB(cr, tmp.Bytes(), id2, &tmp3))
require.Error(t, DecryptBLOB(cr, gather.FromSlice([]byte{2, 3, 4}), id, &tmp2))
require.Error(t, Decrypt(cr, tmp.Bytes(), "invalid-blob-id", &tmp3))
require.Error(t, Decrypt(cr, tmp.Bytes(), "zzz0123456789abcdef0123456789abcde-suffix", &tmp3))
require.Error(t, Decrypt(cr, tmp.Bytes(), id2, &tmp3))
require.Error(t, Decrypt(cr, gather.FromSlice([]byte{2, 3, 4}), id, &tmp2))
}

type badEncryptor struct{}
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestBlobCrypto_Invalid(t *testing.T) {
defer tmp2.Close()
defer tmp3.Close()

_, err := EncryptBLOB(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
_, err := Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
require.Error(t, err)

f := &format.ContentFormat{
Expand All @@ -97,6 +97,19 @@ func TestBlobCrypto_Invalid(t *testing.T) {

cr.h = hf

_, err = EncryptBLOB(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
_, err = Encrypt(cr, gather.FromSlice([]byte{1, 2, 3}), "n", "mysessionid", &tmp)
require.Error(t, err)
}

type staticCrypter struct {
h hashing.HashFunc
e encryption.Encryptor
}

func (p staticCrypter) Encryptor() encryption.Encryptor {
return p.e
}

func (p staticCrypter) HashFunc() hashing.HashFunc {
return p.h
}
Loading

0 comments on commit f8be8f6

Please sign in to comment.