/
initialize.go
173 lines (145 loc) · 4.8 KB
/
initialize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package repo
import (
"context"
"crypto/rand"
"io"
"os"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/ecc"
"github.com/kopia/kopia/repo/encryption"
"github.com/kopia/kopia/repo/format"
"github.com/kopia/kopia/repo/hashing"
"github.com/kopia/kopia/repo/splitter"
)
// BuildInfo is the build information of Kopia.
//
//nolint:gochecknoglobals
var (
BuildInfo = "unknown"
BuildVersion = "v0-unofficial"
BuildGitHubRepo = ""
)
const (
hmacSecretLength = 32
masterKeyLength = 32
)
// NewRepositoryOptions specifies options that apply to newly created repositories.
// All fields are optional, when not provided, reasonable defaults will be used.
type NewRepositoryOptions struct {
UniqueID []byte `json:"uniqueID"` // force the use of particular unique ID
BlockFormat format.ContentFormat `json:"blockFormat"`
DisableHMAC bool `json:"disableHMAC"`
ObjectFormat format.ObjectFormat `json:"objectFormat"` // object format
RetentionMode blob.RetentionMode `json:"retentionMode,omitempty"`
RetentionPeriod time.Duration `json:"retentionPeriod,omitempty"`
}
// Initialize creates initial repository data structures in the specified storage with given credentials.
func Initialize(ctx context.Context, st blob.Storage, opt *NewRepositoryOptions, password string) error {
if opt == nil {
opt = &NewRepositoryOptions{}
}
formatBlob := formatBlobFromOptions(opt)
blobcfg := blobCfgBlobFromOptions(opt)
repoConfig, err := repositoryObjectFormatFromOptions(opt)
if err != nil {
return errors.Wrap(err, "invalid parameters")
}
//nolint:wrapcheck
return format.Initialize(ctx, st, formatBlob, repoConfig, blobcfg, password)
}
func formatBlobFromOptions(opt *NewRepositoryOptions) *format.KopiaRepositoryJSON {
return &format.KopiaRepositoryJSON{
Tool: "https://github.com/kopia/kopia",
BuildInfo: BuildInfo,
BuildVersion: BuildVersion,
KeyDerivationAlgorithm: format.DefaultKeyDerivationAlgorithm,
UniqueID: applyDefaultRandomBytes(opt.UniqueID, format.UniqueIDLengthBytes),
EncryptionAlgorithm: format.DefaultFormatEncryption,
}
}
func blobCfgBlobFromOptions(opt *NewRepositoryOptions) format.BlobStorageConfiguration {
return format.BlobStorageConfiguration{
RetentionMode: opt.RetentionMode,
RetentionPeriod: opt.RetentionPeriod,
}
}
func repositoryObjectFormatFromOptions(opt *NewRepositoryOptions) (*format.RepositoryConfig, error) {
fv := opt.BlockFormat.Version
if fv == 0 {
switch os.Getenv("KOPIA_REPOSITORY_FORMAT_VERSION") {
case "1":
fv = format.FormatVersion1
case "2":
fv = format.FormatVersion2
case "3":
fv = format.FormatVersion3
default:
fv = format.FormatVersion3
}
}
f := &format.RepositoryConfig{
ContentFormat: format.ContentFormat{
Hash: applyDefaultString(opt.BlockFormat.Hash, hashing.DefaultAlgorithm),
Encryption: applyDefaultString(opt.BlockFormat.Encryption, encryption.DefaultAlgorithm),
ECC: applyDefaultString(opt.BlockFormat.ECC, ecc.DefaultAlgorithm),
ECCOverheadPercent: applyDefaultIntRange(opt.BlockFormat.ECCOverheadPercent, 0, 100), //nolint:gomnd
HMACSecret: applyDefaultRandomBytes(opt.BlockFormat.HMACSecret, hmacSecretLength),
MasterKey: applyDefaultRandomBytes(opt.BlockFormat.MasterKey, masterKeyLength),
MutableParameters: format.MutableParameters{
Version: fv,
MaxPackSize: applyDefaultInt(opt.BlockFormat.MaxPackSize, 20<<20), //nolint:gomnd
IndexVersion: applyDefaultInt(opt.BlockFormat.IndexVersion, content.DefaultIndexVersion),
EpochParameters: opt.BlockFormat.EpochParameters,
},
EnablePasswordChange: opt.BlockFormat.EnablePasswordChange,
},
ObjectFormat: format.ObjectFormat{
Splitter: applyDefaultString(opt.ObjectFormat.Splitter, splitter.DefaultAlgorithm),
},
}
if opt.DisableHMAC {
f.HMACSecret = nil
}
if fv == format.FormatVersion1 || f.ContentFormat.ECCOverheadPercent == 0 {
f.ContentFormat.ECC = ""
f.ContentFormat.ECCOverheadPercent = 0
}
if err := f.ContentFormat.ResolveFormatVersion(); err != nil {
return nil, errors.Wrap(err, "error resolving format version")
}
return f, nil
}
func applyDefaultInt(v, def int) int {
if v == 0 {
return def
}
return v
}
func applyDefaultIntRange(v, min, max int) int {
if v < min {
return min
} else if v > max {
return max
}
return v
}
func applyDefaultString(v, def string) string {
if v == "" {
return def
}
return v
}
func randomBytes(n int) []byte {
b := make([]byte, n)
io.ReadFull(rand.Reader, b) //nolint:errcheck
return b
}
func applyDefaultRandomBytes(b []byte, n int) []byte {
if b == nil {
return randomBytes(n)
}
return b
}