RepositoryConfig removal

This commit is contained in:
Jarek Kowalski
2016-06-11 19:52:11 -07:00
parent d08bbc8130
commit 1635199827
5 changed files with 35 additions and 53 deletions

View File

@@ -2,7 +2,6 @@
import (
"crypto/rand"
"errors"
"fmt"
"io"
@@ -123,15 +122,7 @@ func runCreateCommand(context *kingpin.ParseContext) error {
return fmt.Errorf("unable to initialize repository: %v", err)
}
cip, ok := repositoryStorage.(blob.ConnectionInfoProvider)
if !ok {
return errors.New("repository does not support persisting configuration")
}
if err := vlt.SetRepository(vault.RepositoryConfig{
Connection: cip.ConnectionInfo(),
Format: repoFormat,
}); err != nil {
if err := vlt.SetRepository(repositoryStorage, repoFormat); err != nil {
return fmt.Errorf("unable to save repository configuration in vault: %v", err)
}

View File

@@ -20,17 +20,17 @@ func runRepositoryInfoCommand(context *kingpin.ParseContext) error {
return err
}
rc, err := v.RepositoryConfig()
f, err := v.RepositoryFormat()
if err != nil {
return err
}
fmt.Println("Repository:")
fmt.Println(" Version: ", rc.Format.Version)
fmt.Println(" Secret: ", len(rc.Format.Secret), "bytes")
fmt.Println(" ID Format: ", rc.Format.ObjectFormat)
fmt.Println(" Blob Size: ", rc.Format.MaxBlobSize)
fmt.Println(" Inline Blob Size:", rc.Format.MaxInlineBlobSize)
fmt.Println(" Version: ", f.Version)
fmt.Println(" Secret: ", len(f.Secret), "bytes")
fmt.Println(" ID Format: ", f.ObjectFormat)
fmt.Println(" Blob Size: ", f.MaxBlobSize)
fmt.Println(" Inline Blob Size:", f.MaxInlineBlobSize)
return nil
}

View File

@@ -5,7 +5,6 @@
"strings"
"github.com/kopia/kopia/fs"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/vault"
)

View File

@@ -1,18 +1,10 @@
package vault
import (
"crypto/rand"
"fmt"
"io"
"github.com/kopia/kopia/blob"
"github.com/kopia/kopia/repo"
)
const (
minUniqueIDLength = 32
)
// Format describes the format of a Vault.
// Contents of this structure are serialized in plain text in the Vault storage.
type Format struct {
@@ -22,22 +14,7 @@ type Format struct {
Checksum string `json:"checksum"`
}
func (f *Format) ensureUniqueID() error {
if f.UniqueID == nil {
f.UniqueID = make([]byte, minUniqueIDLength)
if _, err := io.ReadFull(rand.Reader, f.UniqueID); err != nil {
return err
}
}
if len(f.UniqueID) < minUniqueIDLength {
return fmt.Errorf("UniqueID too short, must be at least %v bytes", minUniqueIDLength)
}
return nil
}
type RepositoryConfig struct {
type repositoryConfig struct {
Connection blob.ConnectionInfo `json:"connection"`
Format *repo.Format `json:"format"`
}

View File

@@ -157,7 +157,17 @@ func (v *Vault) deriveKey(purpose []byte, key []byte) error {
return err
}
func (v *Vault) SetRepository(rc RepositoryConfig) error {
func (v *Vault) SetRepository(st blob.Storage, f *repo.Format) error {
cip, ok := st.(blob.ConnectionInfoProvider)
if !ok {
return errors.New("repository does not support persisting configuration")
}
rc := repositoryConfig{
Connection: cip.ConnectionInfo(),
Format: f,
}
b, err := json.Marshal(&rc)
if err != nil {
return err
@@ -166,8 +176,8 @@ func (v *Vault) SetRepository(rc RepositoryConfig) error {
return v.writeEncryptedBlock(repositoryConfigBlock, b)
}
func (v *Vault) RepositoryConfig() (*RepositoryConfig, error) {
var rc RepositoryConfig
func (v *Vault) repositoryConfig() (*repositoryConfig, error) {
var rc repositoryConfig
b, err := v.readEncryptedBlock(repositoryConfigBlock)
if err != nil {
@@ -182,8 +192,17 @@ func (v *Vault) RepositoryConfig() (*RepositoryConfig, error) {
return &rc, nil
}
func (v *Vault) RepositoryFormat() (*repo.Format, error) {
rc, err := v.repositoryConfig()
if err != nil {
return nil, err
}
return rc.Format, nil
}
func (v *Vault) OpenRepository() (repo.Repository, error) {
rc, err := v.RepositoryConfig()
rc, err := v.repositoryConfig()
if err != nil {
return nil, err
}
@@ -312,19 +331,15 @@ func (v *Vault) Remove(id string) error {
// Create creates a Vault in the specified storage.
func Create(storage blob.Storage, format *Format, creds Credentials) (*Vault, error) {
if err := format.ensureUniqueID(); err != nil {
return nil, err
}
v := Vault{
storage: storage,
format: *format,
}
v.format.Version = "1"
if err := v.format.ensureUniqueID(); err != nil {
v.format.UniqueID = make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, v.format.UniqueID); err != nil {
return nil, err
}
v.masterKey = creds.getMasterKey(v.format.UniqueID)
formatBytes, err := json.Marshal(&v.format)
@@ -376,13 +391,13 @@ func Open(storage blob.Storage, creds Credentials) (*Vault, error) {
func OpenWithToken(token string) (*Vault, error) {
b, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return nil, fmt.Errorf("invalid vault token")
return nil, fmt.Errorf("invalid vault base64 token: %v", err)
}
var vc vaultConfig
err = json.Unmarshal(b, &vc)
if err != nil {
return nil, fmt.Errorf("invalid vault token")
return nil, fmt.Errorf("invalid vault json token: %v", err)
}
st, err := blob.NewStorage(vc.ConnectionInfo)