extracted block formatting options into a struct under 'block'

This commit is contained in:
Jarek Kowalski committed 2017-10-29 13:00:24 -07:00
1 parent 49a806154a
commit 7c41f45e9f
8 files changed
+38 -32

No files matched your search

+5 -7
View File
@@ -11,8 +11,6 @@ import (
"hash"
"sort"
"strings"
"github.com/kopia/kopia/internal/config"
)
// Formatter performs data block ID computation and encryption of a block of data when storing object in a repository.
@@ -112,17 +110,17 @@ func decodeHexSuffix(s string, length int) ([]byte, error) {
var SupportedFormats []string
// FormatterFactories maps known block formatters to their factory functions.
var FormatterFactories = map[string]func(f config.RepositoryObjectFormat) (Formatter, error){
"TESTONLY_MD5": func(f config.RepositoryObjectFormat) (Formatter, error) {
var FormatterFactories = map[string]func(f FormattingOptions) (Formatter, error){
"TESTONLY_MD5": func(f FormattingOptions) (Formatter, error) {
return &unencryptedFormat{computeHash(md5.New, md5.Size)}, nil
},
"UNENCRYPTED_HMAC_SHA256": func(f config.RepositoryObjectFormat) (Formatter, error) {
"UNENCRYPTED_HMAC_SHA256": func(f FormattingOptions) (Formatter, error) {
return &unencryptedFormat{computeHMAC(sha256.New, f.HMACSecret, sha256.Size)}, nil
},
"UNENCRYPTED_HMAC_SHA256_128": func(f config.RepositoryObjectFormat) (Formatter, error) {
"UNENCRYPTED_HMAC_SHA256_128": func(f FormattingOptions) (Formatter, error) {
return &unencryptedFormat{computeHMAC(sha256.New, f.HMACSecret, 16)}, nil
},
"ENCRYPTED_HMAC_SHA256_AES256_SIV": func(f config.RepositoryObjectFormat) (Formatter, error) {
"ENCRYPTED_HMAC_SHA256_AES256_SIV": func(f FormattingOptions) (Formatter, error) {
if len(f.MasterKey) < 32 {
return nil, fmt.Errorf("master key is not set")
}
+1 -3
View File
@@ -5,13 +5,11 @@ import (
"crypto/rand"
"crypto/sha1"
"testing"
"github.com/kopia/kopia/internal/config"
)
func TestFormatters(t *testing.T) {
secret := []byte("secret")
f := config.RepositoryObjectFormat{HMACSecret: secret, MasterKey: make([]byte, 32)}
f := FormattingOptions{HMACSecret: secret, MasterKey: make([]byte, 32)}
for k, v := range FormatterFactories {
data := make([]byte, 100)
+10
View File
@@ -0,0 +1,10 @@
package block
// FormattingOptions describes the rules for formatting blocks in repository.
type FormattingOptions struct {
Version int `json:"version,omitempty"` // version number, must be "1"
BlockFormat string `json:"objectFormat,omitempty"` // identifier of the block format
HMACSecret []byte `json:"secret,omitempty"` // HMAC secret used to generate encryption keys
MasterKey []byte `json:"masterKey,omitempty"` // master encryption key (SIV-mode encryption only)
MaxPackedContentLength int `json:"maxPackedContentLength,omitempty"` // maximum size of object to be considered for storage in a pack
}
+1 -2
View File
@@ -14,9 +14,8 @@ import (
"testing"
"time"
"github.com/kopia/kopia/storage"
"github.com/kopia/kopia/internal/storagetesting"
"github.com/kopia/kopia/storage"
)
const (
+6 -9
View File
@@ -5,6 +5,7 @@ import (
"io"
"os"
"github.com/kopia/kopia/block"
"github.com/kopia/kopia/storage"
)
@@ -16,16 +17,12 @@ type LocalConfig struct {
// RepositoryObjectFormat describes the format of objects in a repository.
type RepositoryObjectFormat struct {
Version int `json:"version,omitempty"` // version number, must be "1"
BlockFormat string `json:"objectFormat,omitempty"` // identifier of the block format
HMACSecret []byte `json:"secret,omitempty"` // HMAC secret used to generate encryption keys
MasterKey []byte `json:"masterKey,omitempty"` // master encryption key (SIV-mode encryption only)
Splitter string `json:"splitter,omitempty"` // splitter used to break objects into storage blocks
MaxPackedContentLength int `json:"maxPackedContentLength,omitempty"` // maximum size of object to be considered for storage in a pack
block.FormattingOptions
MinBlockSize int `json:"minBlockSize,omitempty"` // minimum block size used with dynamic splitter
AvgBlockSize int `json:"avgBlockSize,omitempty"` // approximate size of storage block (used with dynamic splitter)
MaxBlockSize int `json:"maxBlockSize,omitempty"` // maximum size of storage block
Splitter string `json:"splitter,omitempty"` // splitter used to break objects into storage blocks
MinBlockSize int `json:"minBlockSize,omitempty"` // minimum block size used with dynamic splitter
AvgBlockSize int `json:"avgBlockSize,omitempty"` // approximate size of storage block (used with dynamic splitter)
MaxBlockSize int `json:"maxBlockSize,omitempty"` // maximum size of storage block
}
// RepositoryConnectionInfo represents JSON-serializable configuration of the repository connection, including master key.
+3 -1
View File
@@ -70,7 +70,9 @@ func setupTest(t *testing.T) (map[string][]byte, *Manager) {
func setupTestWithData(t *testing.T, data map[string][]byte, opts ManagerOptions) (map[string][]byte, *Manager) {
r, err := NewObjectManager(&fakeBlockManager{data: data}, config.RepositoryObjectFormat{
Version: 1,
FormattingOptions: block.FormattingOptions{
Version: 1,
},
MaxBlockSize: 200,
Splitter: "FIXED",
}, opts)
+11 -9
View File
@@ -80,15 +80,17 @@ func formatBlockFromOptions(opt *NewRepositoryOptions) formatBlock {
func repositoryObjectFormatFromOptions(opt *NewRepositoryOptions) config.RepositoryObjectFormat {
f := config.RepositoryObjectFormat{
Version: 1,
Splitter: applyDefaultString(opt.Splitter, object.DefaultSplitter),
BlockFormat: applyDefaultString(opt.BlockFormat, block.DefaultFormat),
HMACSecret: applyDefaultRandomBytes(opt.ObjectHMACSecret, 32),
MasterKey: applyDefaultRandomBytes(opt.ObjectEncryptionKey, 32),
MaxBlockSize: applyDefaultInt(opt.MaxBlockSize, 20<<20), // 20MiB
MinBlockSize: applyDefaultInt(opt.MinBlockSize, 10<<20), // 10MiB
AvgBlockSize: applyDefaultInt(opt.AvgBlockSize, 16<<20), // 16MiB
MaxPackedContentLength: applyDefaultInt(opt.MaxPackedContentLength, 4<<20), // 3 MB
FormattingOptions: block.FormattingOptions{
Version: 1,
BlockFormat: applyDefaultString(opt.BlockFormat, block.DefaultFormat),
HMACSecret: applyDefaultRandomBytes(opt.ObjectHMACSecret, 32),
MasterKey: applyDefaultRandomBytes(opt.ObjectEncryptionKey, 32),
MaxPackedContentLength: applyDefaultInt(opt.MaxPackedContentLength, 4<<20), // 3 MB
},
Splitter: applyDefaultString(opt.Splitter, object.DefaultSplitter),
MaxBlockSize: applyDefaultInt(opt.MaxBlockSize, 20<<20), // 20MiB
MinBlockSize: applyDefaultInt(opt.MinBlockSize, 10<<20), // 10MiB
AvgBlockSize: applyDefaultInt(opt.AvgBlockSize, 16<<20), // 16MiB
}
if opt.noHMAC {
+1 -1
View File
@@ -110,7 +110,7 @@ func connect(ctx context.Context, st storage.Storage, creds auth.Credentials, op
return nil, fmt.Errorf("unsupported block format: %v", repoConfig.BlockFormat)
}
formatter, err := sf(repoConfig)
formatter, err := sf(repoConfig.FormattingOptions)
if err != nil {
return nil, err
}