-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Deprecation structure * Do not add wildcard label * Fix panics * Do not ignore errors * Add deprecated items * Add logger * Remove unnecessary assignment to the blank identifier * Cleanup * Update config/configload/deprecated_test.go Co-authored-by: Johannes Koch <[email protected]> * store granted permissions and required permission in request context under beta_ prefixed key, too * Lowercase error message --------- Co-authored-by: Johannes Koch <[email protected]> Co-authored-by: Johannes Koch <[email protected]>
- Loading branch information
1 parent
4856fd6
commit b76e7d2
Showing
10 changed files
with
245 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package configload | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type deprecated struct { | ||
newName string | ||
version string | ||
} | ||
|
||
type ( | ||
attributesList map[string]deprecated | ||
blocksList map[string]deprecated | ||
labelsList map[string]deprecated | ||
) | ||
|
||
var ( | ||
deprecatedAttributes attributesList | ||
deprecatedBlocks blocksList | ||
deprecatedLabels labelsList | ||
) | ||
|
||
func init() { | ||
deprecatedAttributes = make(map[string]deprecated) | ||
deprecatedBlocks = make(map[string]deprecated) | ||
deprecatedLabels = make(map[string]deprecated) | ||
|
||
// Deprecated attributes: | ||
// deprecatedAttributes["..."] = deprecated{"...", "..."} | ||
deprecatedAttributes["beta_permissions_claim"] = deprecated{"permissions_claim", "1.13"} | ||
deprecatedAttributes["beta_permissions_map"] = deprecated{"permissions_map", "1.13"} | ||
deprecatedAttributes["beta_permissions_map_file"] = deprecated{"permissions_map_file", "1.13"} | ||
deprecatedAttributes["beta_required_permission"] = deprecated{"required_permission", "1.13"} | ||
deprecatedAttributes["beta_roles_claim"] = deprecated{"roles_claim", "1.13"} | ||
deprecatedAttributes["beta_roles_map"] = deprecated{"roles_map", "1.13"} | ||
deprecatedAttributes["beta_roles_file"] = deprecated{"roles_map_file", "1.13"} | ||
|
||
// Deprecated blocks: | ||
// deprecatedBlocks["..."] = deprecated{"...", "..."} | ||
|
||
// Deprecated labels: | ||
// deprecatedLabels["..."] = deprecated{"...", "..."} | ||
deprecatedLabels["beta_insufficient_permissions"] = deprecated{"insufficient_permissions", "1.13"} | ||
|
||
// TODO with 1.13, also remove | ||
// ctxAcMap["beta_"+grantedPermissions] = seetie.GoToValue(gp) | ||
// ctxAcMap["beta_"+requiredPermission] = seetie.GoToValue(rp) | ||
// in eval/context.go newVariable() | ||
} | ||
|
||
func deprecate(bodies []*hclsyntax.Body, logger *logrus.Entry) { | ||
for _, body := range bodies { | ||
deprecateBody(body, logger) | ||
} | ||
} | ||
|
||
func deprecateBody(body *hclsyntax.Body, logger *logrus.Entry) { | ||
if body == nil { | ||
return | ||
} | ||
|
||
body.Attributes = deprecateAttributes(body.Attributes, logger) | ||
|
||
deprecateBlocks(body.Blocks, logger) | ||
} | ||
|
||
func deprecateAttributes(attributes hclsyntax.Attributes, logger *logrus.Entry) hclsyntax.Attributes { | ||
attrs := make(hclsyntax.Attributes) | ||
|
||
for _, attr := range attributes { | ||
name := attr.Name | ||
|
||
if rename, exists := deprecatedAttributes[name]; exists { | ||
rename.log("attribute", name, logger) | ||
|
||
name = rename.newName | ||
} | ||
|
||
attrs[name] = attr | ||
} | ||
|
||
return attrs | ||
} | ||
|
||
func deprecateBlocks(blocks hclsyntax.Blocks, logger *logrus.Entry) { | ||
for _, block := range blocks { | ||
block.Labels = deprecateLabels(block, logger) | ||
|
||
if rename, exists := deprecatedBlocks[block.Type]; exists { | ||
rename.log("block", block.Type, logger) | ||
|
||
block.Type = rename.newName | ||
} | ||
|
||
deprecateBody(block.Body, logger) | ||
} | ||
} | ||
|
||
func deprecateLabels(block *hclsyntax.Block, logger *logrus.Entry) []string { | ||
var ( | ||
err error | ||
labels []string = block.Labels | ||
renamed []string | ||
) | ||
|
||
if block.Type == errorHandler { | ||
labels, err = newKindsFromLabels(block, false) | ||
|
||
if err != nil { | ||
return block.Labels | ||
} | ||
} | ||
|
||
for _, label := range labels { | ||
name := label | ||
|
||
if rename, exists := deprecatedLabels[label]; exists { | ||
rename.log("label", label, logger) | ||
|
||
name = rename.newName | ||
} | ||
|
||
renamed = append(renamed, name) | ||
} | ||
|
||
return renamed | ||
} | ||
|
||
// In some test cases the logger is <nil>, but not in production code. | ||
func (d deprecated) log(name, old string, logger *logrus.Entry) { | ||
if logger != nil { | ||
logger.Warnf( | ||
"replacing %s %q with %q; as of Couper version %s, the old value is no longer supported", | ||
name, old, d.newName, d.version, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package configload | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
|
||
"github.com/avenga/couper/config/parser" | ||
"github.com/avenga/couper/internal/test" | ||
) | ||
|
||
func Test_deprecated(t *testing.T) { | ||
// Insert test data: | ||
deprecatedAttributes["couper_test_attribute"] = deprecated{"couper_new_attribute", "1.23"} | ||
deprecatedBlocks["couper_test_block"] = deprecated{"couper_new_block", "1.23"} | ||
deprecatedLabels["couper_test_label"] = deprecated{"couper_new_label", "1.23"} | ||
|
||
src := []byte(` | ||
error_handler "x" "couper_test_label" "abc couper_test_label def" "y" { | ||
couper_test_attribute = true | ||
couper_test_block { | ||
} | ||
} | ||
`) | ||
|
||
body, err := parser.Load(src, "test.go") | ||
if err != nil { | ||
t.Fatalf("%s", err) | ||
} | ||
|
||
logger, hook := test.NewLogger() | ||
hook.Reset() | ||
|
||
deprecate([]*hclsyntax.Body{body}, logger.WithContext(context.TODO())) | ||
|
||
if len(body.Blocks) != 1 { | ||
t.Fatal("Unexpected number of blocks given") | ||
} | ||
|
||
if len(body.Blocks[0].Body.Attributes) != 1 { | ||
t.Fatal("Unexpected number of attributes given") | ||
} | ||
|
||
if attr, exists := body.Blocks[0].Body.Attributes["couper_new_attribute"]; !exists { | ||
t.Error("Missing 'couper_new_attribute' attribute") | ||
} else if attr.Name != "couper_test_attribute" { | ||
t.Errorf("Unexpected attribute name given: '%s'", attr.Name) | ||
} | ||
|
||
if body.Blocks[0].Body.Blocks[0].Type != "couper_new_block" { | ||
t.Errorf("Expected 'couper_new_block' block name, got '%s'", body.Blocks[0].Type) | ||
} | ||
|
||
expLabels := []string{"x", "couper_new_label", "abc", "couper_new_label", "def", "y"} | ||
if !cmp.Equal(expLabels, body.Blocks[0].Labels) { | ||
t.Errorf("Expected\n%#v, got:\n%#v", expLabels, body.Blocks[0].Labels) | ||
} | ||
|
||
entries := hook.AllEntries() | ||
if len(entries) != 4 { | ||
t.Fatal("Unexpected number of log entries given") | ||
} | ||
|
||
exp := `replacing label "couper_test_label" with "couper_new_label"; as of Couper version 1.23, the old value is no longer supported` | ||
if entries[0].Message != exp { | ||
t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message) | ||
} | ||
if entries[1].Message != exp { | ||
t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message) | ||
} | ||
|
||
exp = `replacing attribute "couper_test_attribute" with "couper_new_attribute"; as of Couper version 1.23, the old value is no longer supported` | ||
if entries[2].Message != exp { | ||
t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message) | ||
} | ||
|
||
exp = `replacing block "couper_test_block" with "couper_new_block"; as of Couper version 1.23, the old value is no longer supported` | ||
if entries[3].Message != exp { | ||
t.Errorf("Expected\n%#v, got:\n%#v", exp, entries[0].Message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.