Skip to content

Commit

Permalink
Merge pull request #1146 from hiddeco/revert-log-unify
Browse files Browse the repository at this point in the history
  • Loading branch information
onedr0p authored Jul 11, 2023
2 parents f495885 + d54c128 commit 1ea4a74
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
16 changes: 8 additions & 8 deletions age/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
if key.parsedRecipient == nil {
parsedRecipient, err := parseRecipient(key.Recipient)
if err != nil {
log.WithError(err).WithField("recipient", key.parsedRecipient).Error("Encryption failed")
log.WithField("recipient", key.parsedRecipient).Error("Encryption failed")
return err
}
key.parsedRecipient = parsedRecipient
Expand All @@ -134,19 +134,19 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
aw := armor.NewWriter(&buffer)
w, err := age.Encrypt(aw, key.parsedRecipient)
if err != nil {
log.WithError(err).WithField("recipient", key.parsedRecipient).Error("Encryption failed")
log.WithField("recipient", key.parsedRecipient).Error("Encryption failed")
return fmt.Errorf("failed to create writer for encrypting sops data key with age: %w", err)
}
if _, err := w.Write(dataKey); err != nil {
log.WithError(err).WithField("recipient", key.parsedRecipient).Error("Encryption failed")
log.WithField("recipient", key.parsedRecipient).Error("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key with age: %w", err)
}
if err := w.Close(); err != nil {
log.WithError(err).WithField("recipient", key.parsedRecipient).Error("Encryption failed")
log.WithField("recipient", key.parsedRecipient).Error("Encryption failed")
return fmt.Errorf("failed to close writer for encrypting sops data key with age: %w", err)
}
if err := aw.Close(); err != nil {
log.WithError(err).WithField("recipient", key.parsedRecipient).Error("Encryption failed")
log.WithField("recipient", key.parsedRecipient).Error("Encryption failed")
return fmt.Errorf("failed to close armored writer: %w", err)
}

Expand Down Expand Up @@ -180,7 +180,7 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
if len(key.parsedIdentities) == 0 {
ids, err := key.loadIdentities()
if err != nil {
log.WithError(err).Error("Decryption failed")
log.Error("Decryption failed")
return nil, fmt.Errorf("failed to load age identities: %w", err)
}
ids.ApplyToMasterKey(key)
Expand All @@ -190,13 +190,13 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
ar := armor.NewReader(src)
r, err := age.Decrypt(ar, key.parsedIdentities...)
if err != nil {
log.WithError(err).Error("Decryption failed")
log.Error("Decryption failed")
return nil, fmt.Errorf("failed to create reader for decrypting sops data key with age: %w", err)
}

var b bytes.Buffer
if _, err := io.Copy(&b, r); err != nil {
log.WithError(err).Error("Decryption failed")
log.Error("Decryption failed")
return nil, fmt.Errorf("failed to copy age decrypted data into bytes.Buffer: %w", err)
}

Expand Down
14 changes: 7 additions & 7 deletions gcpkms/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func (c CredentialJSON) ApplyToMasterKey(key *MasterKey) {
func (key *MasterKey) Encrypt(dataKey []byte) error {
service, err := key.newKMSClient()
if err != nil {
log.WithError(err).WithField("resourceID", key.ResourceID).Error("Encryption failed")
log.WithField("resourceID", key.ResourceID).Error("Encryption failed")
return fmt.Errorf("cannot create GCP KMS service: %w", err)
}
defer func() {
if err := service.Close(); err != nil {
log.WithError(err).Error("failed to close GCP KMS client connection")
log.Error("failed to close GCP KMS client connection")
}
}()

Expand All @@ -110,7 +110,7 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
ctx := context.Background()
resp, err := service.Encrypt(ctx, req)
if err != nil {
log.WithError(err).WithField("resourceID", key.ResourceID).Error("Encryption failed")
log.WithField("resourceID", key.ResourceID).Error("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key with GCP KMS key: %w", err)
}
// NB: base64 encoding is for compatibility with SOPS <=3.8.x.
Expand Down Expand Up @@ -145,20 +145,20 @@ func (key *MasterKey) EncryptIfNeeded(dataKey []byte) error {
func (key *MasterKey) Decrypt() ([]byte, error) {
service, err := key.newKMSClient()
if err != nil {
log.WithError(err).WithField("resourceID", key.ResourceID).Error("Decryption failed")
log.WithField("resourceID", key.ResourceID).Error("Decryption failed")
return nil, fmt.Errorf("cannot create GCP KMS service: %w", err)
}
defer func() {
if err := service.Close(); err != nil {
log.WithError(err).Error("failed to close GCP KMS client connection")
log.Error("failed to close GCP KMS client connection")
}
}()

// NB: this is for compatibility with SOPS <=3.8.x. The previous GCP KMS
// client used to work with base64 encoded strings.
decodedCipher, err := base64.StdEncoding.DecodeString(string(key.EncryptedDataKey()))
if err != nil {
log.WithError(err).WithField("resourceID", key.ResourceID).Error("Decryption failed")
log.WithField("resourceID", key.ResourceID).Error("Decryption failed")
return nil, err
}

Expand All @@ -169,7 +169,7 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
ctx := context.Background()
resp, err := service.Decrypt(ctx, req)
if err != nil {
log.WithError(err).WithField("resourceID", key.ResourceID).Error("Decryption failed")
log.WithField("resourceID", key.ResourceID).Error("Decryption failed")
return nil, fmt.Errorf("failed to decrypt sops data key with GCP KMS key: %w", err)
}

Expand Down
12 changes: 6 additions & 6 deletions hcvault/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {

client, err := vaultClient(key.VaultAddress, key.token)
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Encryption failed")
log.WithField("Path", fullPath).Error("Encryption failed")
return err
}

secret, err := client.Logical().Write(fullPath, encryptPayload(dataKey))
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Encryption failed")
log.WithField("Path", fullPath).Error("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key to Vault transit backend '%s': %w", fullPath, err)
}
encryptedKey, err := encryptedKeyFromSecret(secret)
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Encryption failed")
log.WithField("Path", fullPath).Error("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key to Vault transit backend '%s': %w", fullPath, err)
}

Expand Down Expand Up @@ -175,18 +175,18 @@ func (key *MasterKey) Decrypt() ([]byte, error) {

client, err := vaultClient(key.VaultAddress, key.token)
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Decryption failed")
log.WithField("Path", fullPath).Error("Decryption failed")
return nil, err
}

secret, err := client.Logical().Write(fullPath, decryptPayload(key.EncryptedKey))
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Decryption failed")
log.WithField("Path", fullPath).Error("Decryption failed")
return nil, fmt.Errorf("failed to decrypt sops data key from Vault transit backend '%s': %w", fullPath, err)
}
dataKey, err := dataKeyFromSecret(secret)
if err != nil {
log.WithError(err).WithField("Path", fullPath).Error("Decryption failed")
log.WithField("Path", fullPath).Error("Decryption failed")
return nil, fmt.Errorf("failed to decrypt sops data key from Vault transit backend '%s': %w", fullPath, err)
}

Expand Down
10 changes: 5 additions & 5 deletions kms/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (c CredentialsProvider) ApplyToMasterKey(key *MasterKey) {
func (key *MasterKey) Encrypt(dataKey []byte) error {
cfg, err := key.createKMSConfig()
if err != nil {
log.WithError(err).WithField("arn", key.Arn).Error("Encryption failed")
log.WithField("arn", key.Arn).Error("Encryption failed")
return err
}
client := kms.NewFromConfig(*cfg)
Expand All @@ -205,7 +205,7 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
}
out, err := client.Encrypt(context.TODO(), input)
if err != nil {
log.WithError(err).WithField("arn", key.Arn).Error("Encryption failed")
log.WithField("arn", key.Arn).Error("Encryption failed")
return fmt.Errorf("failed to encrypt sops data key with AWS KMS: %w", err)
}
key.EncryptedKey = base64.StdEncoding.EncodeToString(out.CiphertextBlob)
Expand Down Expand Up @@ -237,12 +237,12 @@ func (key *MasterKey) SetEncryptedDataKey(enc []byte) {
func (key *MasterKey) Decrypt() ([]byte, error) {
k, err := base64.StdEncoding.DecodeString(key.EncryptedKey)
if err != nil {
log.WithError(err).WithField("arn", key.Arn).Error("Decryption failed")
log.WithField("arn", key.Arn).Error("Decryption failed")
return nil, fmt.Errorf("error base64-decoding encrypted data key: %s", err)
}
cfg, err := key.createKMSConfig()
if err != nil {
log.WithError(err).WithField("arn", key.Arn).Error("Decryption failed")
log.WithField("arn", key.Arn).Error("Decryption failed")
return nil, err
}
client := kms.NewFromConfig(*cfg)
Expand All @@ -253,7 +253,7 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
}
decrypted, err := client.Decrypt(context.TODO(), input)
if err != nil {
log.WithError(err).WithField("arn", key.Arn).Error("Decryption failed")
log.WithField("arn", key.Arn).Error("Decryption failed")
return nil, fmt.Errorf("failed to decrypt sops data key with AWS KMS: %w", err)
}
log.WithField("arn", key.Arn).Info("Decryption succeeded")
Expand Down
4 changes: 2 additions & 2 deletions pgp/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (key *MasterKey) Encrypt(dataKey []byte) error {
}
errs = append(errs, fmt.Errorf("GnuPG binary error: %w", binaryErr))

log.WithError(errs).WithField("fingerprint", key.Fingerprint).Error("Encryption failed")
log.WithField("fingerprint", key.Fingerprint).Error("Encryption failed")
return fmt.Errorf("could not encrypt data key with PGP key: %w", errs)
}

Expand Down Expand Up @@ -379,7 +379,7 @@ func (key *MasterKey) Decrypt() ([]byte, error) {
}
errs = append(errs, fmt.Errorf("GnuPG binary error: %w", binaryErr))

log.WithError(errs).WithField("fingerprint", key.Fingerprint).Error("Decryption failed")
log.WithField("fingerprint", key.Fingerprint).Error("Decryption failed")
return nil, fmt.Errorf("could not decrypt data key with PGP key: %w", errs)
}

Expand Down

0 comments on commit 1ea4a74

Please sign in to comment.