Skip to content

Commit

Permalink
feat: forenklet java-config koden
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrremann committed Mar 1, 2022
1 parent 2cad9b4 commit d93d404
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 129 deletions.
24 changes: 0 additions & 24 deletions pkg/config/config.go

This file was deleted.

82 changes: 19 additions & 63 deletions pkg/config/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,36 @@ ssl.truststore.type=JKS
`
)

func NewJavaConfig(secret *v1.Secret, dest string) Config {
return &Java{
Props: fmt.Sprintf("# nais-cli %s\n", time.Now().Truncate(time.Minute)),
Secret: secret,
PrefixPath: dest,
RequiredFiles: map[string]RequiredFile{
consts.KafkaClientKeyStoreP12File: {consts.KafkaClientKeyStoreP12File, KeyStoreLocationProp, false},
consts.KafkaClientTruststoreJksFile: {consts.KafkaClientTruststoreJksFile, TrustStoreLocationProp, false},
},
func NewJavaConfig(secret *v1.Secret, destinationPath string) error {
properties := fmt.Sprintf("# nais-cli %s\n", time.Now().Truncate(time.Minute))
properties += fmt.Sprintf(FileHeader, secret.Namespace, secret.Data[consts.KafkaBrokersKey], filepath.Join(destinationPath, JavaConfigName))

envsToFile := map[string]string{
KeyPassProp: string(secret.Data[consts.KafkaCredStorePasswordKey]),
KeyStorePassProp: string(secret.Data[consts.KafkaCredStorePasswordKey]),
TrustStorePassProp: string(secret.Data[consts.KafkaCredStorePasswordKey]),
KeyStoreLocationProp: windowsify(filepath.Join(destinationPath, consts.KafkaClientKeyStoreP12File)),
TrustStoreLocationProp: windowsify(filepath.Join(destinationPath, consts.KafkaClientTruststoreJksFile)),
}
}

type Java struct {
Props string
Secret *v1.Secret
PrefixPath string
RequiredFiles map[string]RequiredFile
}

func (k *Java) WriteConfigToFile() error {
if err := k.write(); err != nil {
return fmt.Errorf("could not write %s to file: %s", JavaConfigName, err)
for key, value := range envsToFile {
properties += fmt.Sprintf("%s=%s\n", key, value)
}
return nil
}

func (k *Java) write() error {
if err := common.WriteToFile(k.PrefixPath, JavaConfigName, []byte(k.Props)); err != nil {
if err := common.WriteToFile(destinationPath, JavaConfigName, []byte(properties)); err != nil {
return fmt.Errorf("write envs to file: %s", err)
}
return nil
}

func (k *Java) Set(key string, value []byte) {
k.Props += fmt.Sprintf("%s=%s\n", key, string(value))
}

func (k *Java) SetPath(key, path string) {
k.Props += fmt.Sprintf("%s=%s\n", key, windowsify(path))
}

func (k *Java) Generate() (string, error) {
err := requiredSecretDataExists(k.RequiredFiles, k.Secret.Data, JavaConfigName)
if err != nil {
return "", err
secretsToFile := map[string][]byte{
consts.KafkaClientKeyStoreP12File: secret.Data[consts.KafkaClientKeyStoreP12File],
consts.KafkaClientTruststoreJksFile: secret.Data[consts.KafkaClientTruststoreJksFile],
}

k.Props += fmt.Sprintf(FileHeader, k.Secret.Namespace, k.Secret.Data[consts.KafkaBrokersKey], filepath.Join(k.PrefixPath, JavaConfigName))

for key, value := range k.Secret.Data {
if err := k.toFile(key, value); err != nil {
return "", fmt.Errorf("write to file for key: %s\n %s", key, err)
for fileName, value := range secretsToFile {
if err := common.WriteToFile(destinationPath, fileName, []byte(value)); err != nil {
return fmt.Errorf("write to file: %s", err)
}
k.toEnv(key, value)
}
return k.Props, nil
}

func (k *Java) toEnv(key string, value []byte) {
if key == consts.KafkaCredStorePasswordKey {
k.Set(KeyPassProp, value)
k.Set(KeyStorePassProp, value)
k.Set(TrustStorePassProp, value)
}
}

func (k *Java) toFile(key string, value []byte) error {
path := k.PrefixPath
if requiredFile, ok := k.RequiredFiles[key]; ok {
if err := common.WriteToFile(path, requiredFile.Filename, value); err != nil {
return err
}
k.SetPath(requiredFile.PathKey, filepath.Join(path, requiredFile.Filename))
}
return nil
}

Expand Down
38 changes: 11 additions & 27 deletions pkg/config/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"github.com/nais/cli/pkg/consts"
"github.com/nais/cli/pkg/test"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
Expand All @@ -19,36 +21,18 @@ func TestJavaConfigGenerated(t *testing.T) {
}

tmpDest := test.SetupDest(t)
javaConfig := NewJavaConfig(test.SetupSecret(envKeys), tmpDest)

result, err := javaConfig.Generate()
err := NewJavaConfig(test.SetupSecret(envKeys), tmpDest)
assert.NoError(t, err)

assert.True(t, strings.Contains(result, consts.KafkaClientTruststoreJksFile))
assert.True(t, strings.Contains(result, consts.KafkaClientKeyStoreP12File))
assert.True(t, strings.Contains(result, KeyPassProp))
assert.True(t, strings.Contains(result, KeyStorePassProp))
assert.True(t, strings.Contains(result, TrustStorePassProp))
assert.True(t, strings.Contains(result, KeyStoreLocationProp))

defer os.Remove(tmpDest)
}

func TestJavaSecretMissingRequiredData(t *testing.T) {

var envKeys = []string{
consts.KafkaCAKey,
consts.KafkaCertificateKey,
consts.KafkaPrivateKeyKey,
consts.KafkaClientKeyStoreP12File,
consts.KafkaCredStorePasswordKey,
consts.KafkaSchemaRegistryKey,
}
result, err := ioutil.ReadFile(filepath.Join(tmpDest, JavaConfigName))
assert.NoError(t, err)

tmpDest := test.SetupDest(t)
javaConfig := NewJavaConfig(test.SetupSecret(envKeys), tmpDest)
_, err := javaConfig.Generate()
assert.EqualError(t, err, "can not generate kafka.properties config, secret missing required key: client.truststore.jks")
assert.True(t, strings.Contains(string(result), consts.KafkaClientTruststoreJksFile))
assert.True(t, strings.Contains(string(result), consts.KafkaClientKeyStoreP12File))
assert.True(t, strings.Contains(string(result), KeyPassProp))
assert.True(t, strings.Contains(string(result), KeyStorePassProp))
assert.True(t, strings.Contains(string(result), TrustStorePassProp))
assert.True(t, strings.Contains(string(result), KeyStoreLocationProp))

defer os.Remove(tmpDest)
}
18 changes: 3 additions & 15 deletions pkg/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ func hasAnnotation(secret *v1.Secret, key string) bool {
}

func (s *Secret) CreateKafkaConfigs() error {
if err := s.CreateJavaConfig(); err != nil {
err := config.NewJavaConfig(s.Secret, s.DestinationPath)
if err != nil {
return err
}
err := config.WriteKCatConfigToFile(s.Secret, s.DestinationPath)
err = config.WriteKCatConfigToFile(s.Secret, s.DestinationPath)
if err != nil {
return err
}
Expand All @@ -114,16 +115,3 @@ func (s *Secret) Config() error {
}
return nil
}

func (s *Secret) CreateJavaConfig() error {
javaConfig := config.NewJavaConfig(s.Secret, s.DestinationPath)
_, err := javaConfig.Generate()
if err != nil {
return err
}

if err := javaConfig.WriteConfigToFile(); err != nil {
return err
}
return nil
}

0 comments on commit d93d404

Please sign in to comment.