Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-7 Don't panic in case of path to encryption file doesn't exist. #3307

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion managed/services/encryption/encryption_rotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func createOriginEncryptionKey() error {
if err != nil {
return err
}
encryption.DefaultEncryption = encryption.New()
encryption.DefaultEncryption, _ = encryption.New()
return nil
}

Expand Down
21 changes: 10 additions & 11 deletions managed/utils/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/google/tink/go/tink"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tink-crypto/tink-go/aead"
"github.com/tink-crypto/tink-go/insecurecleartextkeyset"
"github.com/tink-crypto/tink-go/keyset"
Expand All @@ -40,7 +39,7 @@ var (
// ErrEncryptionNotInitialized is error in case of encryption is not initialized.
ErrEncryptionNotInitialized = errors.New("encryption is not initialized")
// DefaultEncryption is the default implementation of encryption.
DefaultEncryption = New()
DefaultEncryption, _ = New()
defaultEncryptionMtx sync.Mutex
)

Expand Down Expand Up @@ -72,7 +71,7 @@ type QueryValues struct {
}

// New creates an encryption; if key on path doesn't exist, it will be generated.
func New() *Encryption {
func New() (*Encryption, error) {
e := &Encryption{}
e.Path = encryptionKeyPath()

Expand All @@ -81,21 +80,22 @@ func New() *Encryption {
case os.IsNotExist(err):
err = e.generateKey()
if err != nil {
logrus.Panicf("Encryption: %v", err)
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it make sense to continue if PMM is not able to generate encryption key?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't run tests locally because of this panic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Ok

}

case err != nil:
logrus.Panicf("Encryption: %v", err)
return nil, err
default:
e.Key = string(bytes)
}

primitive, err := e.getPrimitive()
if err != nil {
logrus.Panicf("Encryption: %v", err)
return nil, err
}
e.Primitive = primitive

return e
return e, nil
}

// RotateEncryptionKey is a wrapper around DefaultEncryption.RotateEncryptionKey.
Expand All @@ -106,10 +106,9 @@ func RotateEncryptionKey() error {
}

defaultEncryptionMtx.Lock()
DefaultEncryption = New()
defaultEncryptionMtx.Unlock()

return nil
defer defaultEncryptionMtx.Unlock()
DefaultEncryption, err = New()
return err
}

// RestoreOldEncryptionKey is a wrapper around DefaultEncryption.RestoreOldEncryptionKey.
Expand Down
Loading