diff --git a/Jenkinsfile b/Jenkinsfile index 6ea5a1b391001c078f77b15b1d459b812c742db0..901dccc3ccd8cc2b5c2c8370680f982cd02ae9d3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -81,6 +81,15 @@ tools { go 'go-1.22.0' } } } } + stage('Test fachstellen-proxy') { + steps { + script { + FAILED_STAGE=env.STAGE_NAME + sh "go test ./internal/*" + } + } + } + stage ('OWASP Dependency-Check Vulnerabilities') { steps { diff --git a/README.md b/README.md index 96c81e1bfc4fb8ef849e90497aece7f47d3673b1..d2dcd94de950213cac36ca40b20ca99bef9e2121 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,20 @@ buf generate ### Anwendung starten ``` -go run cmd/fachstellen-proxy/main.go +ACTIVE_PROFILE=local go run cmd/fachstellen-proxy/main.go ``` ## Config +config.yml is the default config for deployment [Config-Datei](./config/config.yml) +config-local.yml is the config file for local development +setting ACTIVE_PROFILE=local will load the config-local.yml + +[Config-Datei](./config/config-local.yml) + + ``` http: server: diff --git a/config/config-local.yml b/config/config-local.yml new file mode 100755 index 0000000000000000000000000000000000000000..f785d25ac4f5e72c23b00a3b76c330ccd40604ca --- /dev/null +++ b/config/config-local.yml @@ -0,0 +1,12 @@ +http: + server: + port: 8082 +grpc: + mock: false + collaboration: + router: + port: 50051 + server: + port: 9090 +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index b46e14a135880eb52874f7a20cc7dc4075873e3b..556a664e9bdfbede3df3b383a2942bc720b4e3c8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -32,28 +32,30 @@ import ( "os" "regexp" "testing" + "fmt" ) const ( - defaultFilePath = "config/config.yml" - testFilePath = "testdata/test_config.yml" grpcUrlRegex = `^[A-Za-z\d-]+(\.[A-Za-z\d-]+){0,512}:\d{1,5}$` + defaultConfig = "config.yml" + testFilePath = "testdata/" + activeProfileParam = "ACTIVE_PROFILE" ) type Config struct { Http struct { Server struct { - Port int `yaml:"port" envconfig:"HTTP_SERVER_PORT"` + Port int `yaml:"port"` } `yaml:"server"` } `yaml:"http"` Grpc struct { - Mock bool `yaml:"mock" envconfig:"GRPC_MOCK"` + Mock bool `yaml:"mock"` Collaboration struct { Server struct { - Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_SERVER_PORT"` + Port int `yaml:"port"` } `yaml:"server"` Router struct { - Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_ROUTER_PORT"` + Port int `yaml:"port"` } `yaml:"router"` } `yaml:"collaboration"` Registration struct { @@ -63,18 +65,26 @@ type Config struct { } `yaml:"registration"` } `yaml:"grpc"` Logging struct { - Level string `yaml:"level" envconfig:"LOGGING_LEVEL"` + Level string `yaml:"level"` } `yaml:"logging"` } func LoadConfig(configFilePath ...string) Config { var config Config - fp := defaultFilePath + configFile := defaultConfig + + env := os.Getenv(activeProfileParam) + if env != "" { + configFile = fmt.Sprintf("config-%s.yml", env) + } + + fp := fmt.Sprintf("config/%s", configFile) + if len(configFilePath) > 0 { fp = configFilePath[0] } else if testing.Testing() { - fp = testFilePath + fp = testFilePath + configFile } file, err := os.ReadFile(fp) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9f615b6bc639bf66da9fb3d5070123c63b70fc83..603bff3958512f156c15e48c203f250a50cd7eb4 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -33,7 +33,7 @@ import ( ) func TestLoadConfig(t *testing.T) { - t.Run("should load config from file", func(t *testing.T) { + t.Run("should load default config from file", func(t *testing.T) { config := LoadConfig() expectedConfig := Config{} @@ -42,64 +42,43 @@ func TestLoadConfig(t *testing.T) { expectedConfig.Grpc.Collaboration.Router.Port = 50051 expectedConfig.Grpc.Registration.Server.Url = "localhost:50052" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) - t.Run("should load config from env", func(t *testing.T) { - envVars := map[string]string{ - "HTTP_SERVER_PORT": "9090", - "GRPC_COLLABORATION_SERVER_PORT": "50052", - "GRPC_COLLABORATION_ROUTER_PORT": "50051", - "GRPC_REGISTRATION_SERVER_URL": "localhost:99999", - "LOGGING_LEVEL": "ERROR", - } - - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } + t.Run("should load local config from env", func(t *testing.T) { + assert.NoError(t, os.Setenv(activeProfileParam, "local"), "Setenv "+activeProfileParam+" should not return an error") - config := LoadConfig("testdata/empty_test_config.yml") + config := LoadConfig() - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } + assert.NoError(t, os.Unsetenv(activeProfileParam), "Unsetenv "+activeProfileParam+" should not return an error") expectedConfig := Config{} - expectedConfig.Http.Server.Port = 9090 + expectedConfig.Http.Server.Port = 8080 expectedConfig.Grpc.Collaboration.Server.Port = 50052 expectedConfig.Grpc.Collaboration.Router.Port = 50051 - expectedConfig.Grpc.Registration.Server.Url = "localhost:99999" + expectedConfig.Grpc.Registration.Server.Url = "localhost:50052" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "ERROR" + expectedConfig.Logging.Level = "DEBUG" assert.Equal(t, expectedConfig, config) }) t.Run("should overwrite config with env", func(t *testing.T) { - envVars := map[string]string{ - "HTTP_SERVER_PORT": "9090", - "GRPC_REGISTRATION_SERVER_URL": "localhost:99999", - } - - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } + assert.NoError(t, os.Setenv("GRPC_REGISTRATION_SERVER_URL", "localhost:99999"), "Setenv GRPC_REGISTRATION_SERVER_URL should not return an error") config := LoadConfig() - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } + assert.NoError(t, os.Unsetenv("GRPC_REGISTRATION_SERVER_URL"), "Unsetenv GRPC_REGISTRATION_SERVER_URL should not return an error") expectedConfig := Config{} - expectedConfig.Http.Server.Port = 9090 + expectedConfig.Http.Server.Port = 8080 expectedConfig.Grpc.Collaboration.Server.Port = 50052 expectedConfig.Grpc.Collaboration.Router.Port = 50051 expectedConfig.Grpc.Registration.Server.Url = "localhost:99999" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) diff --git a/internal/config/testdata/test_config.yml b/internal/config/testdata/config-local.yml similarity index 100% rename from internal/config/testdata/test_config.yml rename to internal/config/testdata/config-local.yml diff --git a/internal/logging/testdata/test_config.yml b/internal/config/testdata/config.yml similarity index 100% rename from internal/logging/testdata/test_config.yml rename to internal/config/testdata/config.yml diff --git a/internal/config/testdata/empty_test_config.yml b/internal/config/testdata/empty_test_config.yml deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/internal/logging/testdata/config.yml b/internal/logging/testdata/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..64e799f15a52e1afec93177b366e66a3c8c87c88 --- /dev/null +++ b/internal/logging/testdata/config.yml @@ -0,0 +1,14 @@ +http: + server: + port: 8080 +grpc: + collaboration: + server: + port: 50052 + router: + port: 50051 + registration: + server: + url: "localhost:50052" +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/mock/testdata/test_config.yml b/internal/mock/testdata/config.yml similarity index 100% rename from internal/mock/testdata/test_config.yml rename to internal/mock/testdata/config.yml diff --git a/internal/server/testdata/test_config.yml b/internal/server/testdata/config.yml similarity index 100% rename from internal/server/testdata/test_config.yml rename to internal/server/testdata/config.yml diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml index cb1dc50237f22fdfcce25f2b96c09c124e28c9d5..ad8c912c00f1288061a4542a7e17bc1495120bbf 100644 --- a/src/main/helm/templates/deployment.yaml +++ b/src/main/helm/templates/deployment.yaml @@ -58,14 +58,12 @@ spec: app.kubernetes.io/name: {{ .Release.Name }} containers: - env: - - name: HTTP_SERVER_PORT - value: "8082" - - name: GRPC_MOCK - value: "{{ (.Values.grpc).mock | default false }}" - name: GRPC_REGISTRATION_SERVER_URL value: "{{ required ".Values.grpc.registration.server.url must be set" (((.Values.grpc).registration).server).url }}" - - name: GRPC_COLLABORATION_SERVER_PORT - value: "9090" + {{- if (.Values.env).overrideGoProfiles }} + - name: ACTIVE_PROFILE + value: {{ (.Values.env).overrideGoProfiles }} + {{- end }} {{- with include "app.getCustomList" . }} {{ . | indent 10 }} {{- end }} diff --git a/src/test/helm-linter-values.yaml b/src/test/helm-linter-values.yaml index 2e0237dfb94f4f00c52bfa9693470b69367f4eb9..1a63e1ae5245c4dc1b36cb9c4a4afce21adb5dad 100644 --- a/src/test/helm-linter-values.yaml +++ b/src/test/helm-linter-values.yaml @@ -29,9 +29,7 @@ zufiManager: namespace: zufi imagePullSecret: ozgcloud-image-pull-secret -http: - server: - port: 8080 + grpc: registration: server: diff --git a/src/test/helm/deployment_env_test.yaml b/src/test/helm/deployment_env_test.yaml index 19ea5ae90e4e905fa3162534a29291c4663cc484..918065c65ba2ac2710a0ccfed74c56c90834f36f 100644 --- a/src/test/helm/deployment_env_test.yaml +++ b/src/test/helm/deployment_env_test.yaml @@ -78,28 +78,43 @@ tests: server: url: "zufi.de:9090" asserts: - - contains: - path: spec.template.spec.containers[0].env - content: - name: GRPC_COLLABORATION_SERVER_PORT - value: "9090" - contains: path: spec.template.spec.containers[0].env content: name: GRPC_REGISTRATION_SERVER_URL value: "zufi.de:9090" - - contains: - path: spec.template.spec.containers[0].env - content: - name: GRPC_MOCK - value: "false" - - contains: - path: spec.template.spec.containers[0].env - content: - name: HTTP_SERVER_PORT - value: "8082" + - it: should fail template if grpc.registration.server.url not set set: asserts: - failedTemplate: errormessage: ".Values.grpc.registration.server.url must be set" + + + + - it: should by default not have ACTIVE_PROFILE env + set: + grpc: + registration: + server: + url: "zufi.de:9090" + asserts: + - notContains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + any: true + + - it: should have env ACTIVE_PROFILE with correct value when set + set: + env.overrideGoProfiles: oc,test,ea + grpc: + registration: + server: + url: "zufi.de:9090" + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + value: oc,test,ea \ No newline at end of file