From 9f7e16fc60e5115f1e4fa7194663c7fd34db3abf Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Tue, 30 Mar 2021 17:44:08 +0200 Subject: [PATCH] Fix loading optional labels of same type Replaced chicken-and-egg retry method with bodyToContent --- config/configload/load.go | 72 +++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/config/configload/load.go b/config/configload/load.go index 0be8a808e..7afdce21c 100644 --- a/config/configload/load.go +++ b/config/configload/load.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "path/filepath" "regexp" - "strings" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" @@ -221,17 +220,11 @@ func mergeBackendBodies(definedBackends Backends, inline config.Inline) (hcl.Bod // getBackendReference tries to fetch a backend from `definitions` // block by a reference name, e.g. `backend = "name"`. func getBackendReference(definedBackends Backends, body hcl.Body) (hcl.Body, error) { - content, _, diags := body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: backend}, - }}) - if diags.HasErrors() { - return nil, diags - } + content := bodyToContent(body) // read out possible attribute reference var name string - if attr, ok := content.Attributes["backend"]; ok { + if attr, ok := content.Attributes[backend]; ok { val, valDiags := attr.Expr.Value(envContext) if valDiags.HasErrors() { return nil, valDiags @@ -263,22 +256,7 @@ func getBackendReference(definedBackends Backends, body hcl.Body) (hcl.Body, err func refineEndpoints(definedBackends Backends, endpoints config.Endpoints) error { for _, endpoint := range endpoints { - // try to obtain proxy and request block with a chicken-and-egg situation: - // hcl labels are required if set, to make them optional we must know the content - // which could not unwrapped without label errors. We will handle this by block type - // and may have to throw an error which hints the user to configure the file properly. - endpointContent := &hcl.BodyContent{Attributes: make(hcl.Attributes)} - for _, t := range []string{proxy, request} { - c, err := contentByType(t, endpoint.Remain) - if err != nil { - return err - } - endpointContent.MissingItemRange = c.MissingItemRange - endpointContent.Blocks = append(endpointContent.Blocks, c.Blocks...) - for n, attr := range c.Attributes { // possible same key and content override, it's ok. - endpointContent.Attributes[n] = attr - } - } + endpointContent := bodyToContent(endpoint.Remain) proxies := endpointContent.Blocks.OfType(proxy) requests := endpointContent.Blocks.OfType(request) @@ -422,6 +400,41 @@ func uniqueLabelName(unique map[string]struct{}, name string, hr *hcl.Range) err return nil } +func bodyToContent(body hcl.Body) *hcl.BodyContent { + content := &hcl.BodyContent{ + MissingItemRange: body.MissingItemRange(), + } + b, ok := body.(*hclsyntax.Body) + if !ok { + return content + } + + if len(b.Attributes) > 0 { + content.Attributes = make(hcl.Attributes) + } + for name, attr := range b.Attributes { + content.Attributes[name] = &hcl.Attribute{ + Name: attr.Name, + Expr: attr.Expr, + Range: attr.Range(), + NameRange: attr.NameRange, + } + } + + for _, block := range b.Blocks { + content.Blocks = append(content.Blocks, &hcl.Block{ + Body: block.Body, + DefRange: block.DefRange(), + LabelRanges: block.LabelRanges, + Labels: block.Labels, + Type: block.Type, + TypeRange: block.TypeRange, + }) + } + + return content +} + func contentByType(blockType string, body hcl.Body) (*hcl.BodyContent, error) { headerSchema := &hcl.BodySchema{ Blocks: []hcl.BlockHeaderSchema{ @@ -501,18 +514,11 @@ func newOAuthBackend(definedBackends Backends, parent hcl.Body) (hcl.Body, error return nil, err } - b, err := newBackend(definedBackends, &config.OAuth2{Remain: hclbody.New(&hcl.BodyContent{ + return newBackend(definedBackends, &config.OAuth2{Remain: hclbody.New(&hcl.BodyContent{ Blocks: []*hcl.Block{ {Type: backend, Body: oauthBackend}, }, })}) - if err != nil { - diags := err.(hcl.Diagnostics) - if strings.HasPrefix(diags[0].Summary, "The host of 'url'") { - diags[0].Summary = strings.Replace(diags[0].Summary, "The host of 'url'", "The host of 'token_endpoint'", 1) - } - } - return b, err } func renameAttribute(content *hcl.BodyContent, old, new string) {