Skip to content

Commit

Permalink
refactor: Remove function
Browse files Browse the repository at this point in the history
  • Loading branch information
jachym-tousek-keboola committed Dec 4, 2024
1 parent 16bfb39 commit e90d3cb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 31 deletions.
7 changes: 4 additions & 3 deletions internal/pkg/service/stream/dependencies/mocked.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dependencies

import (
"crypto/rand"
"fmt"
"net/url"
"strings"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/keboola/keboola-as-code/internal/pkg/service/common/configmap"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/dependencies"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/config"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/encryption"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/test/dummy"
"github.com/keboola/keboola-as-code/internal/pkg/utils/netutils"
)
Expand Down Expand Up @@ -64,9 +64,10 @@ func testConfig(tb testing.TB, d dependencies.Mocked) config.Config {
cfg.API.Task.CleanupEnabled = false

// Use native encryption for tests
key, err := encryption.RandomSecretKey()
secretKey := make([]byte, 32)
_, err := rand.Read(secretKey)
assert.NoError(tb, err)

Check failure on line 69 in internal/pkg/service/stream/dependencies/mocked.go

View workflow job for this annotation

GitHub Actions / Lint / lint

require-error: for error assertions use require (testifylint)
cfg.Encryption.Native.SecretKey = key
cfg.Encryption.Native.SecretKey = string(secretKey)

// Validate configuration
require.NoError(tb, configmap.ValidateAndNormalize(&cfg))
Expand Down
19 changes: 0 additions & 19 deletions internal/pkg/service/stream/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package encryption

import (
"context"
"crypto/rand"
"math/big"

"github.com/keboola/go-cloud-encrypt/pkg/cloudencrypt"
)
Expand Down Expand Up @@ -53,20 +51,3 @@ func NewEncryptor(ctx context.Context, config Config) (cloudencrypt.Encryptor, e

return encryptor, nil
}

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func RandomSecretKey() (string, error) {
result := make([]byte, 32)
charsetLength := big.NewInt(int64(len(charset)))

for i := range result {
randIndex, err := rand.Int(rand.Reader, charsetLength)
if err != nil {
return "", err
}
result[i] = charset[randIndex.Int64()]
}

return string(result), nil
}
7 changes: 4 additions & 3 deletions test/stream/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api

import (
"context"
"crypto/rand"
"fmt"
"os"
"path/filepath"
Expand All @@ -14,7 +15,6 @@ import (

"github.com/keboola/keboola-as-code/internal/pkg/env"
"github.com/keboola/keboola-as-code/internal/pkg/filesystem"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/encryption"
volume "github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/local/volume/model"
"github.com/keboola/keboola-as-code/internal/pkg/utils/etcdhelper"
"github.com/keboola/keboola-as-code/internal/pkg/utils/netutils"
Expand Down Expand Up @@ -60,7 +60,8 @@ func TestStreamApiE2E(t *testing.T) {
require.NoError(t, os.WriteFile(filepath.Join(volumePath, volume.IDFile), []byte("my-volume"), 0o600))

// Use native encryption for tests
secretKey, err := encryption.RandomSecretKey()
secretKey := make([]byte, 32)
_, err = rand.Read(secretKey)
require.NoError(t, err)

addEnvs := env.FromMap(map[string]string{
Expand All @@ -76,7 +77,7 @@ func TestStreamApiE2E(t *testing.T) {
"STREAM_ETCD_ENDPOINT": etcdCfg.Endpoint,
"STREAM_ETCD_USERNAME": etcdCfg.Username,
"STREAM_ETCD_PASSWORD": etcdCfg.Password,
"STREAM_ENCRYPTION_NATIVE_SECRET_KEY": secretKey,
"STREAM_ENCRYPTION_NATIVE_SECRET_KEY": string(secretKey),
})

// Run the test
Expand Down
7 changes: 4 additions & 3 deletions test/stream/bridge/keboola/guest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keboola_test

import (
"context"
"crypto/rand"
"net/http"
"net/url"
"strconv"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/api"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/config"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/dependencies"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/encryption"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
"github.com/keboola/keboola-as-code/internal/pkg/utils/netutils"
)
Expand All @@ -28,11 +28,12 @@ func TestGuestUserWorkflow(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
defer cancel()

secretKey, err := encryption.RandomSecretKey()
secretKey := make([]byte, 32)
_, err := rand.Read(secretKey)
require.NoError(t, err)

modifyConfig := func(cfg *config.Config) {
cfg.Encryption.Native.SecretKey = secretKey
cfg.Encryption.Native.SecretKey = string(secretKey)
apiPort := netutils.FreePortForTest(t)
cfg.API.Listen = "0.0.0.0:" + strconv.FormatInt(int64(apiPort), 10)
u, err := url.Parse("http://localhost:" + strconv.FormatInt(int64(apiPort), 10))
Expand Down
7 changes: 4 additions & 3 deletions test/stream/bridge/keboola/keboola_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keboola_test
import (
"compress/gzip"
"context"
"crypto/rand"
"fmt"
"io"
"net/http"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/keboola/keboola-as-code/internal/pkg/encoding/json"
"github.com/keboola/keboola-as-code/internal/pkg/service/common/duration"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/config"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/encryption"
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/local/diskwriter/network"
stagingConfig "github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/staging/config"
targetConfig "github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/target/config"
Expand All @@ -33,13 +33,14 @@ func TestKeboolaBridgeWorkflow(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second)
defer cancel()

secretKey, err := encryption.RandomSecretKey()
secretKey := make([]byte, 32)
_, err := rand.Read(secretKey)
require.NoError(t, err)

// Update configuration to make the cluster testable
configFn := func(cfg *config.Config) {
// Setup encryption
cfg.Encryption.Native.SecretKey = secretKey
cfg.Encryption.Native.SecretKey = string(secretKey)
// Enable metadata cleanup for removing storage jobs
cfg.Storage.MetadataCleanup.Enabled = true
// Disable unrelated workers
Expand Down

0 comments on commit e90d3cb

Please sign in to comment.