Skip to content

Commit

Permalink
Merge branch 'main' into AT-1194-document-jobqueue-library
Browse files Browse the repository at this point in the history
  • Loading branch information
tmstff authored Dec 11, 2023
2 parents 0f4ba9f + 5dd3312 commit ad01a79
Show file tree
Hide file tree
Showing 23 changed files with 300 additions and 1,121 deletions.
68 changes: 68 additions & 0 deletions pkg/configReader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Config Reader

The `configReader` package provides a function `ReadEnvVarsIntoStruct` that can be used to read environment variables or config files into a struct.

## Usage

To use the `configReader` package, first create a struct that you want to read environment variables into:

```go
type Config struct {
Port int `viperEnv:"port"`
Host string `viperEnV:"host" default:"localhost"`
LogLevel string `viperEnv:"LOG_LEVEL" default:"info"`
}
```

Then, in your code, you can use the `ReadEnvVarsIntoStruct` function to read environment variables into the struct:

```go
package main

import (
"github.com/greenbone/opensight-golang-libraries/configReader"
)

func main() {
var config Config
err := configReader.ReadEnvVarsIntoStruct(&config)
if err != nil {
panic(err)
}
// use the config
}
```

Any environment variables that match the struct fields will be read into the struct. The following environment variables will be read into the `Config` struct:

```
PORT=8080
HOST=localhost
LOG_LEVEL=debug
```

If a field in the struct has a `viperEnv` tag, the environment variable will be matched to that tag. Otherwise, the field name will be used as the tag.

If a field in the struct has a `default` tag, a default value will be used if the environment variable is not set.

The `configReader` package uses [Viper](https://github.com/spf13/viper) for reading environment variables, and [Zerolog](https://github.com/rs/zerolog) for logging.

## Maintainer

This project is maintained by [Greenbone AG][Greenbone AG]

## Contributing

Your contributions are highly appreciated. Please
[create a pull request](https://github.com/greenbone/opensight-golang-libraries/pulls)
on GitHub. Bigger changes need to be discussed with the development team via the
[issues section at GitHub](https://github.com/greenbone/opensight-golang-libraries/issues)
first.

## License

Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]

Licensed under the [GNU General Public License v3.0 or later](LICENSE).

[Greenbone AG]: https://www.greenbone.net/
File renamed without changes.
63 changes: 1 addition & 62 deletions pkg/postgres/dbcrypt/dbcryptutil.go → pkg/dbcrypt/dbcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/greenbone/opensight-golang-libraries/pkg/dbcrypt/config"
"io"
"reflect"

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

type DBCrypt[T any] struct {
Expand Down Expand Up @@ -54,12 +53,6 @@ func (d *DBCrypt[T]) DecryptStruct(data *T) error {
field := value.Field(i)
fieldType := valueType.Field(i)
if encrypt, ok := fieldType.Tag.Lookup("encrypt"); ok && encrypt == "true" {
/*
ciphertext, err := hex.DecodeString(field.String()[4:])
if err != nil {
return err
}*/

plaintext, err := d.decrypt(field.String())
if err != nil {
return err
Expand Down Expand Up @@ -132,57 +125,3 @@ func (d *DBCrypt[T]) decrypt(encrypted string) (string, error) {

return string(plaintext), nil
}

/*
// This function encrypts a value using AES encryption with the derived key
func (d *DBCrypt[T]) encryptValue(value string) ([]byte, error) {
key, genKeyErr := d.deriveEncryptionKey()
if genKeyErr != nil {
return nil, genKeyErr
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, []byte(value), nil)
return ciphertext, nil
}
// This function decrypts a value using AES decryption with the derived key
func (d *DBCrypt[T]) decryptValue(ciphertext []byte) (string, error) {
key, genKeyErr := d.deriveEncryptionKey()
if genKeyErr != nil {
return "", genKeyErr
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return "", errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}
*/

// TODO test without DB
// TODO Test DB beforeUpdate ...
File renamed without changes.
Loading

0 comments on commit ad01a79

Please sign in to comment.