Skip to content

Commit

Permalink
Add logging messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ma-hartma committed Sep 27, 2021
1 parent 2be76f8 commit 4cb67fe
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Repository struct {
// Const declarations.
const (
RepositoryVersion = 4
repositoryKeyLength = 32
repositoryKeyLength = 32 // A random key of 32 is considered safe right now and may be increased later
)

// Error declarations.
Expand All @@ -42,7 +42,7 @@ var (

// NewRepository returns a new repository.
func NewRepository(path, password string) (Repository, error) {
// A random key of 32 is considered safe right now and may be increased later
log.Info("Creating new repository...")
key, err := generateRandomKey(repositoryKeyLength)
if err != nil {
return Repository{}, ErrGenerateRandomKeyFailed
Expand All @@ -56,6 +56,7 @@ func NewRepository(path, password string) (Repository, error) {

backend, err := BackendFromURL(path)
if err != nil {
log.Fatal("Couldn't get backend from URL")
return repository, err
}
repository.backend.AddBackend(&backend)
Expand All @@ -66,28 +67,31 @@ func NewRepository(path, password string) (Repository, error) {

// generateRandomKey generates a random key with a specific length.
func generateRandomKey(length int) (string, error) {
log.Debug("Generating random key for repository encryption")
b := make([]byte, length)

_, err := rand.Read(b)
if err != nil {
return "", err
}

return base64.URLEncoding.EncodeToString(b), nil
}

// OpenRepository opens an existing repository and migrates it if possible.
func OpenRepository(path, password string) (Repository, error) {
log.Debug("Opening repository...")
repository := Repository{
password: password,
}

backend, err := BackendFromURL(path)
if err != nil {
log.Fatal("Couldn't get backend from URL")
return repository, err
}
b, err := backend.LoadRepository()
if err != nil {
log.Fatal("Couldn't load repository")
return repository, err
}

Expand All @@ -103,13 +107,15 @@ func OpenRepository(path, password string) (Repository, error) {
// migrate to current version
err = repository.Migrate()
if err != nil {
log.Fatalf("Couldn't migrate repository")
return repository, err
}
}

for _, url := range repository.Paths {
backend, err := BackendFromURL(url)
if err != nil {
log.Fatalf("Couldn't get backend from URL")
return repository, err
}
repository.backend.AddBackend(&backend)
Expand All @@ -120,12 +126,14 @@ func OpenRepository(path, password string) (Repository, error) {

// AddVolume adds a volume to a repository.
func (r *Repository) AddVolume(volume *Volume) error {
log.Infof("Adding volume %s to repository...", volume.ID)
r.Volumes = append(r.Volumes, volume)
return nil
}

// RemoveVolume removes a volume from a repository.
func (r *Repository) RemoveVolume(volume *Volume) error {
log.Infof("Removing volume %s from repository...", volume.ID)
for i, v := range r.Volumes {
if v == volume {
r.Volumes = append(r.Volumes[:i], r.Volumes[i+1:]...)
Expand All @@ -137,6 +145,7 @@ func (r *Repository) RemoveVolume(volume *Volume) error {

// FindVolume finds a volume within a repository.
func (r *Repository) FindVolume(id string) (*Volume, error) {
log.Debugf("Finding volume %s...", id)
if id == "latest" && len(r.Volumes) > 0 {
return r.Volumes[len(r.Volumes)-1], nil
}
Expand All @@ -152,6 +161,7 @@ func (r *Repository) FindVolume(id string) (*Volume, error) {

// FindSnapshot finds a snapshot within a repository.
func (r *Repository) FindSnapshot(id string) (*Volume, *Snapshot, error) {
log.Debugf("Finding snapshot %s...", id)
if id == "latest" {
latestVolume := &Volume{}
latestSnapshot := &Snapshot{}
Expand Down Expand Up @@ -204,6 +214,7 @@ func (r *Repository) BackendManager() *BackendManager {
func (r *Repository) init() error {
err := r.backend.InitRepository()
if err != nil {
log.Fatal("Couldn't initialize repository")
return err
}

Expand All @@ -212,6 +223,7 @@ func (r *Repository) init() error {

// Save writes a repository's metadata.
func (r *Repository) Save() error {
log.Infof("Saving repository...")
r.Paths = r.backend.Locations()

pipe, err := NewEncodingPipeline(CompressionNone, EncryptionAES, r.password)
Expand All @@ -222,22 +234,29 @@ func (r *Repository) Save() error {
if err != nil {
return err
}
return r.backend.SaveRepository(b)
err = r.backend.SaveRepository(b)
if err == nil {
log.Info("Saved repository successfully")
}
return err
}

// Changes password of repository.
func (r *Repository) ChangePassword(newPassword string) error {
log.Info("Changing password...")
r.password = newPassword

return r.Save()
}

// Migrates a repository to the current version, if possible.
func (r *Repository) Migrate() error {
log.Infof("Trying to migrate from repository version %s to version %s", r.Version, RepositoryVersion)
switch v := r.Version; {
case v < 3:
return ErrRepositoryIncompatible
case v == 3:
log.Infof("Migrating from repository version %s to version 4", v)
// since the introduction of the repo passwd command there are two keys:
// - Key is for encryption of the data and will be stored in encrypted repo file
// - password is for the encryption of the repository (which holds Key)
Expand All @@ -246,7 +265,11 @@ func (r *Repository) Migrate() error {
r.Key = r.password
r.Version = 4

return r.Save()
err := r.Save()
if err == nil {
log.Infof("Migrated to repository verion 4 successfully")
}
return err
}
}
return ErrRepositoryIncompatible
Expand Down

0 comments on commit 4cb67fe

Please sign in to comment.