Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing env. key error #522

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

Unreleased changes are available as `avenga/couper:edge` container.

* **Fixed**
* Missing environment key error while using multiple configuration files ([#522](https://github.com/avenga/couper/pull/522))

---

## [1.9.0](https://github.com/avenga/couper/releases/tag/v1.9.0)
Expand Down
5 changes: 2 additions & 3 deletions config/configload/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type helper struct {
}

// newHelper creates a container with some methods to keep things simple here and there.
func newHelper(body hcl.Body, src []byte, filename string) (*helper, error) {
func newHelper(body hcl.Body, src [][]byte) (*helper, error) {
defaultsBlock := &config.DefaultsBlock{}
if diags := gohcl.DecodeBody(body, nil, defaultsBlock); diags.HasErrors() {
return nil, diags
Expand All @@ -28,10 +28,9 @@ func newHelper(body hcl.Body, src []byte, filename string) (*helper, error) {
defSettings := config.DefaultSettings

couperConfig := &config.Couper{
Context: eval.NewContext([][]byte{src}, defaultsBlock.Defaults),
Context: eval.NewContext(src, defaultsBlock.Defaults),
Definitions: &config.Definitions{},
Defaults: defaultsBlock.Defaults,
Filename: filename,
Settings: &defSettings,
}

Expand Down
16 changes: 6 additions & 10 deletions config/configload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func updateContext(body hcl.Body, srcBytes [][]byte) hcl.Diagnostics {
return diags
}

// We need the "envContext" to be able to resolve abs pathes in the config.
// We need the "envContext" to be able to resolve absolute paths in the config.
defaultsConfig = defaultsBlock.Defaults
evalContext = eval.NewContext(srcBytes, defaultsConfig)
envContext = evalContext.HCLContext()
Expand Down Expand Up @@ -154,7 +154,7 @@ func LoadFiles(filesList []string) (*config.Couper, error) {
}

for _, body := range parsedBodies {
if err := absolutizePaths(body); err != nil {
if err = absolutizePaths(body); err != nil {
return nil, err
}
}
Expand All @@ -180,7 +180,7 @@ func LoadFiles(filesList []string) (*config.Couper, error) {
Blocks: configBlocks,
}

conf, err := LoadConfig(configBody, srcBytes[0], filesList[0])
conf, err := LoadConfig(configBody, srcBytes)
if err != nil {
return nil, err
}
Expand All @@ -200,21 +200,17 @@ func LoadBytes(src []byte, filename string) (*config.Couper, error) {
return nil, diags
}

if diags = updateContext(hclBody, [][]byte{src}); diags.HasErrors() {
return nil, diags
}

return LoadConfig(hclBody, src, filename)
return LoadConfig(hclBody, [][]byte{src})
}

func LoadConfig(body hcl.Body, src []byte, filename string) (*config.Couper, error) {
func LoadConfig(body hcl.Body, src [][]byte) (*config.Couper, error) {
var err error

if diags := ValidateConfigSchema(body, &config.Couper{}); diags.HasErrors() {
return nil, diags
}

helper, err := newHelper(body, src, filename)
helper, err := newHelper(body, src)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion config/couper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const DefaultFilename = "couper.hcl"
// Couper represents the <Couper> config object.
type Couper struct {
Context context.Context
Filename string
Files file.Files
Definitions *Definitions `hcl:"definitions,block"`
Servers Servers `hcl:"server,block"`
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ type filesList struct {
}

func main() {
logrus.Exit(realmain(os.Args))
logrus.Exit(realmain(context.Background(), os.Args))
}

func realmain(arguments []string) int {
func realmain(ctx context.Context, arguments []string) int {
args := command.NewArgs(arguments)
ctx := context.Background()
filesList := filesList{}

type globalFlags struct {
Expand Down
8 changes: 7 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

logrustest "github.com/sirupsen/logrus/hooks/test"

Expand Down Expand Up @@ -44,6 +46,7 @@ func Test_realmain(t *testing.T) {
{"non-existent log level via env /w file", []string{"couper", "run", "-f", base + "/log_altered.hcl"}, []string{"COUPER_LOG_LEVEL=test"}, `level=error msg="configuration error: missing 'server' block" build=dev`, 1},
{"-f w/o file", []string{"couper", "run", "-f"}, nil, `level=error msg="flag needs an argument: -f" build=dev`, 1},
{"path from env", []string{"couper", "run", "-f", base + "/path_from_env.hcl"}, nil, `level=error msg="configuration error: token: jwt key: read error: open %s/public.pem: no such file or directory" build=dev`, 1},
{"path from env /w missing key", []string{"couper", "run", "-f", "public/couper.hcl", "-f", base + "/no_key_from_env.hcl"}, nil, "", 0},
{"undefined AC", []string{"couper", "run", "-f", base + "/04_couper.hcl"}, nil, `level=error msg="accessControl is not defined: undefined" build=dev`, 1},
{"empty string in allowed_methods in endpoint", []string{"couper", "run", "-f", base + "/13_couper.hcl"}, nil, `level=error msg="%s/13_couper.hcl:3,5-27: method contains invalid character(s); " build=dev`, 1},
{"invalid method in allowed_methods in endpoint", []string{"couper", "run", "-f", base + "/14_couper.hcl"}, nil, `level=error msg="%s/14_couper.hcl:3,5-35: method contains invalid character(s); " build=dev`, 1},
Expand All @@ -57,7 +60,10 @@ func Test_realmain(t *testing.T) {
})
}

if got := realmain(tt.args); got != tt.want {
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(time.Second, cancel)

if got := realmain(ctx, tt.args); got != tt.want {
subT.Errorf("realmain() = %v, want %v", got, tt.want)
}
env.OsEnviron = os.Environ
Expand Down
7 changes: 7 additions & 0 deletions server/testdata/settings/no_key_from_env.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
server "couper" {
endpoint "/down" {
proxy {
url = env.NO_ORIGIN
}
}
}
4 changes: 2 additions & 2 deletions server/testdata/settings/path_from_env.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ server {

definitions {
jwt "token" {
key_file = env.KEY
key_file = env.KEY_FILE
signature_algorithm = "HS256"
}
}

defaults {
environment_variables = {
KEY = "public.pem"
KEY_FILE = "public.pem"
}
}