From 1635199827a4ce435b2fbde3eaba8cc73096f3a8 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Sat, 11 Jun 2016 19:52:11 -0700 Subject: [PATCH] RepositoryConfig removal --- cmd/kopia/command_create.go | 11 +---------- cmd/kopia/command_status.go | 12 ++++++------ cmd/kopia/objref.go | 1 - vault/format.go | 25 +----------------------- vault/vault.go | 39 +++++++++++++++++++++++++------------ 5 files changed, 35 insertions(+), 53 deletions(-) diff --git a/cmd/kopia/command_create.go b/cmd/kopia/command_create.go index 1952e718b..e98e2ae79 100644 --- a/cmd/kopia/command_create.go +++ b/cmd/kopia/command_create.go @@ -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) } diff --git a/cmd/kopia/command_status.go b/cmd/kopia/command_status.go index e6a315e6c..dde37437b 100644 --- a/cmd/kopia/command_status.go +++ b/cmd/kopia/command_status.go @@ -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 } diff --git a/cmd/kopia/objref.go b/cmd/kopia/objref.go index cd7ba101d..94c9ab49a 100644 --- a/cmd/kopia/objref.go +++ b/cmd/kopia/objref.go @@ -5,7 +5,6 @@ "strings" "github.com/kopia/kopia/fs" - "github.com/kopia/kopia/repo" "github.com/kopia/kopia/vault" ) diff --git a/vault/format.go b/vault/format.go index 9c9bfde78..1266cd8f2 100644 --- a/vault/format.go +++ b/vault/format.go @@ -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"` } diff --git a/vault/vault.go b/vault/vault.go index 88614af31..2c8d74cea 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -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)