Skip to content
Closed
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
23 changes: 17 additions & 6 deletions pkg/dbcrypt/dbcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"fmt"
"io"
"reflect"

"github.com/greenbone/opensight-golang-libraries/pkg/dbcrypt/config"
"sync/atomic"

"github.com/rs/zerolog/log"

"github.com/greenbone/opensight-golang-libraries/pkg/dbcrypt/config"
)

const (
Expand All @@ -24,18 +25,28 @@ const (
)

type DBCrypt[T any] struct {
config config.CryptoConfig
config atomic.Pointer[config.CryptoConfig]
}

// New creates a new instance of DBCrypt that will perform cryptographic operations based on the provided CryptoConfig.
func New[T any](config config.CryptoConfig) *DBCrypt[T] {
d := &DBCrypt[T]{}
d.config.Store(&config)
return d
}

func (d *DBCrypt[T]) loadKey() []byte {
if d.config == (config.CryptoConfig{}) {
configPtr := d.config.Load()
if configPtr == nil {
conf, err := config.Read()
if err != nil {
log.Fatal().Err(err).Msg("crypto config is invalid")
}
d.config = conf
d.config.CompareAndSwap(nil, &conf)
configPtr = d.config.Load()
}
key := []byte(d.config.ReportEncryptionV1Password + d.config.ReportEncryptionV1Salt)[:32] // Truncate key to 32 bytes
// TODO: Use proper KDF instead of truncating the password (to maintain backward compatibility use encrypted values prefixes to determine the method)
key := []byte(configPtr.ReportEncryptionV1Password + configPtr.ReportEncryptionV1Salt)[:32] // Truncate key to 32 bytes
return key
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/dbcrypt/dbcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"

"github.com/greenbone/opensight-golang-libraries/pkg/dbcrypt/config"
)

type MyTable struct {
Expand Down Expand Up @@ -111,3 +113,34 @@ func TestApplianceEncryption(t *testing.T) {
myDB.First(&resultData, tblData.ID)
assert.EqualValues(t, "thePasswordWhichCanBeEncrypted", resultData.PwdField)
}

func TestNew(t *testing.T) {
t.Setenv("TASK_REPORT_CRYPTO_V1_PASSWORD", "from-env-password")
t.Setenv("TASK_REPORT_CRYPTO_V1_SALT", "from-env-salt-012345678901234567")

configCustom := config.CryptoConfig{
ReportEncryptionV1Password: "custom-password",
ReportEncryptionV1Salt: "custom-salt-01234567890123456789",
}
dataClear := MyTable{
Field1: "OtherField",
PwdField: "SecretPassword",
}
cryptDefault := DBCrypt[MyTable]{}
cryptCustom := New[MyTable](configCustom)

dataEncrypted := dataClear
err := cryptCustom.EncryptStruct(&dataEncrypted)
require.NoError(t, err)
require.NotEqual(t, dataClear, dataEncrypted, "password was not encrypted")

dataDefaultDecrypted := dataEncrypted
err = cryptDefault.DecryptStruct(&dataDefaultDecrypted)
require.Error(t, err)
assert.Equal(t, dataEncrypted, dataDefaultDecrypted)

dataCustomDecrypted := dataEncrypted
err = cryptCustom.DecryptStruct(&dataCustomDecrypted)
require.NoError(t, err)
assert.Equal(t, dataClear, dataCustomDecrypted)
Comment on lines +132 to +145
Copy link
Copy Markdown
Contributor

@llugin llugin Nov 18, 2025

Choose a reason for hiding this comment

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

I would at least add comments about each test case intention, I think it would make it easier to comprehend. For me it would be also more logical to read if it would be in order:

cryptCustom.EncryptStruct()
...
cryptCustom.DecryptStruct()
...
cryptDefault.DecryptStruct()

so dividing test cases per DBCrypt instance, which could be even separate t.Run() testcases.

}
Loading