Skip to content

Commit

Permalink
feat: add env check in config package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebianchi committed Jun 14, 2023
1 parent 0e5b287 commit 576900d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
28 changes: 16 additions & 12 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
)

const (
APIPermissionsFilePathEnvKey = "API_PERMISSIONS_FILE_PATH"
TargetServiceOASPathEnvKey = "TARGET_SERVICE_OAS_PATH"
StandaloneEnvKey = "STANDALONE"
TargetServiceHostEnvKey = "TARGET_SERVICE_HOST"
BindingsCrudServiceURL = "BINDINGS_CRUD_SERVICE_URL"
apiPermissionsFilePathEnvKey = "API_PERMISSIONS_FILE_PATH"
targetServiceOASPathEnvKey = "TARGET_SERVICE_OAS_PATH"
standaloneEnvKey = "STANDALONE"
targetServiceHostEnvKey = "TARGET_SERVICE_HOST"
bindingsCrudServiceURL = "BINDINGS_CRUD_SERVICE_URL"

traceLogLevel = "trace"
)
Expand Down Expand Up @@ -76,11 +76,11 @@ var EnvVariablesConfig = []configlib.EnvConfig{
DefaultValue: "latest",
},
{
Key: TargetServiceHostEnvKey,
Key: targetServiceHostEnvKey,
Variable: "TargetServiceHost",
},
{
Key: TargetServiceOASPathEnvKey,
Key: targetServiceOASPathEnvKey,
Variable: "TargetServiceOASPath",
},
{
Expand All @@ -89,7 +89,7 @@ var EnvVariablesConfig = []configlib.EnvConfig{
Required: true,
},
{
Key: APIPermissionsFilePathEnvKey,
Key: apiPermissionsFilePathEnvKey,
Variable: "APIPermissionsFilePath",
},
{
Expand Down Expand Up @@ -130,7 +130,7 @@ var EnvVariablesConfig = []configlib.EnvConfig{
Variable: "RolesCollectionName",
},
{
Key: StandaloneEnvKey,
Key: standaloneEnvKey,
Variable: "Standalone",
},
{
Expand All @@ -139,7 +139,7 @@ var EnvVariablesConfig = []configlib.EnvConfig{
DefaultValue: "/eval",
},
{
Key: BindingsCrudServiceURL,
Key: bindingsCrudServiceURL,
Variable: "BindingsCrudServiceURL",
},
{
Expand Down Expand Up @@ -184,11 +184,15 @@ func GetEnvOrDie() EnvironmentVariables {
}

if env.TargetServiceHost == "" && !env.Standalone {
panic(fmt.Errorf("missing environment variables, one of %s or %s set to true is required", TargetServiceHostEnvKey, StandaloneEnvKey))
panic(fmt.Errorf("missing environment variables, one of %s or %s set to true is required", targetServiceHostEnvKey, standaloneEnvKey))
}

if env.Standalone && env.BindingsCrudServiceURL == "" {
panic(fmt.Errorf("missing environment variables, %s must be set if mode is standalone", BindingsCrudServiceURL))
panic(fmt.Errorf("missing environment variables, %s must be set if mode is standalone", bindingsCrudServiceURL))
}

if env.APIPermissionsFilePath == "" && env.TargetServiceOASPath == "" {
panic(fmt.Errorf("missing environment variables, one of %s or %s is required", apiPermissionsFilePathEnvKey, targetServiceOASPathEnvKey))
}

return env
Expand Down
45 changes: 40 additions & 5 deletions internal/config/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ func TestGetEnvOrDie(t *testing.T) {
ServiceVersion: "latest",

OPAModulesDirectory: "/modules",
APIPermissionsFilePath: "/oas",
AdditionalHeadersToProxy: "miauserid",
ExposeMetrics: true,
}

t.Run(`returns correctly - with TargetServiceHost`, func(t *testing.T) {
otherEnvs := []env{
{name: "TARGET_SERVICE_HOST", value: "http://localhost:3000"},
{name: apiPermissionsFilePathEnvKey, value: "/oas"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)
Expand All @@ -101,6 +103,7 @@ func TestGetEnvOrDie(t *testing.T) {
otherEnvs := []env{
{name: "STANDALONE", value: "true"},
{name: "BINDINGS_CRUD_SERVICE_URL", value: "http://crud-client"},
{name: apiPermissionsFilePathEnvKey, value: "/oas"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)
Expand All @@ -113,17 +116,17 @@ func TestGetEnvOrDie(t *testing.T) {
require.Equal(t, expectedEnvs, actualEnvs, "Unexpected envs variables.")
})

t.Run(`returns error - with Standalone and not BindingsCrudServiceURL`, func(t *testing.T) {
t.Run(`throws - with Standalone and not BindingsCrudServiceURL`, func(t *testing.T) {
otherEnvs := []env{
{name: "STANDALONE", value: "true"},
{name: apiPermissionsFilePathEnvKey, value: "/oas"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)

defer func() {
r := recover()
t.Logf("expected panic %+v", r)

}()

GetEnvOrDie()
Expand All @@ -133,24 +136,56 @@ func TestGetEnvOrDie(t *testing.T) {
t.Run(`throws - with Standalone to false`, func(t *testing.T) {
otherEnvs := []env{
{name: "STANDALONE", value: "false"},
{name: apiPermissionsFilePathEnvKey, value: "/oas"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)

require.PanicsWithError(t, fmt.Sprintf("missing environment variables, one of %s or %s set to true is required", TargetServiceHostEnvKey, StandaloneEnvKey), func() {
require.PanicsWithError(t, fmt.Sprintf("missing environment variables, one of %s or %s set to true is required", targetServiceHostEnvKey, standaloneEnvKey), func() {
GetEnvOrDie()
}, "Unexpected envs variables.")
})

t.Run(`throws - no Standalone or TargetServiceHost`, func(t *testing.T) {
otherEnvs := []env{}
otherEnvs := []env{
{name: apiPermissionsFilePathEnvKey, value: "/oas"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)

require.PanicsWithError(t, fmt.Sprintf("missing environment variables, one of %s or %s set to true is required", targetServiceHostEnvKey, standaloneEnvKey), func() {
GetEnvOrDie()
}, "Unexpected envs variables.")
})

t.Run(`throws - no APIPermissionsFilePath or TargetServiceOASPath`, func(t *testing.T) {
otherEnvs := []env{
{name: "TARGET_SERVICE_HOST", value: "http://localhost:3000"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)

require.PanicsWithError(t, fmt.Sprintf("missing environment variables, one of %s or %s set to true is required", TargetServiceHostEnvKey, StandaloneEnvKey), func() {
require.PanicsWithError(t, fmt.Sprintf("missing environment variables, one of %s or %s is required", apiPermissionsFilePathEnvKey, targetServiceOASPathEnvKey), func() {
GetEnvOrDie()
}, "Unexpected envs variables.")
})

t.Run(`returns correctly - TargetServiceOASPath set`, func(t *testing.T) {
otherEnvs := []env{
{name: "TARGET_SERVICE_HOST", value: "http://localhost:3000"},
{name: "TARGET_SERVICE_OAS_PATH", value: "/path"},
}
envs := append(requiredEnvs, otherEnvs...)
setEnvs(t, envs)

actualEnvs := GetEnvOrDie()
expectedEnvs := defaultAndRequiredEnvironmentVariables
expectedEnvs.TargetServiceHost = "http://localhost:3000"
expectedEnvs.APIPermissionsFilePath = ""
expectedEnvs.TargetServiceOASPath = "/path"

require.Equal(t, expectedEnvs, actualEnvs, "Unexpected envs variables.")
})
}

type env struct {
Expand Down
3 changes: 1 addition & 2 deletions openapi/openapi_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"testing"

"github.com/rond-authz/rond/internal/config"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
Expand Down Expand Up @@ -270,7 +269,7 @@ func TestLoadOAS(t *testing.T) {
_, err := LoadOASFromFileOrNetwork(log, options)

t.Logf("Expected error occurred: %s", err.Error())
require.True(t, err != nil, fmt.Errorf("missing environment variables one of %s or %s is required", config.TargetServiceOASPathEnvKey, config.APIPermissionsFilePathEnvKey))
require.ErrorContains(t, err, "missing openapi config: one of TargetServiceOASPath or APIPermissionsFilePath is required")
})
}

Expand Down

0 comments on commit 576900d

Please sign in to comment.