pull/21908/head
Sarah Alsmiller 2024-11-05 14:27:58 -06:00 committed by sarahalsmiller
parent 05eeccee6d
commit 18e9efa154
3 changed files with 4 additions and 37 deletions

View File

@ -9,8 +9,6 @@ import (
"fmt"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl"
"regexp"
"strings"
)
@ -36,12 +34,11 @@ func (r *EnterprisePolicyRules) Validate(*Config) error {
func decodeRules(rules string, warnOnDuplicateKey bool, _ *Config, _ *EnterprisePolicyMeta) (*Policy, error) {
p := &Policy{}
err := hcl.Decode(p, rules)
err := hcl.DecodeErrorOnDuplicates(p, rules)
if errIsDuplicateKey(err) && warnOnDuplicateKey {
//because the snapshot saves the unparsed rules we have to assume some snapshots exist that shouldn't fail, but
// have duplicates
rules = cleanDuplicates(rules, err)
if err := hcl.Decode(p, rules); err != nil {
hclog.Default().Warn("Warning- Duplicate key in ACL Policy ignored", "errorMessage", err.Error())
return nil, fmt.Errorf("Failed to parse ACL rules: %v", err)
@ -59,35 +56,3 @@ func errIsDuplicateKey(err error) bool {
}
return strings.Contains(err.Error(), "was already set. Each argument can only be defined once")
}
// This replicates the behavior of the previous HCL parser in certain conditions, like when reading
// old ACL from the cache
func cleanDuplicates(rules string, err error) string {
p := &Policy{}
//hcl doesn't care about spaces or commas so we can remove them to make life easier
rules = strings.ReplaceAll(rules, " ", "")
rules = strings.ReplaceAll(rules, ",", "")
errRegexp, _ := regexp.Compile("The argument \"(.+)\"")
//we need to do this to maintain backwards compatabiliy, luckily the rules only contain string values
for errIsDuplicateKey(err) {
//capture current duplicate from error message. We already know there is a match from the above check
submatch := errRegexp.FindAllStringSubmatch(err.Error(), 1)
key := submatch[0][1]
keyRegexp, _ := regexp.Compile(key + `="[A-Za-z0-9]+"`)
keysubmatch := keyRegexp.FindAllStringSubmatch(rules, 1)
match := ""
if len(keysubmatch) > 0 {
//found a match
match = keysubmatch[0][0]
} else {
//no match found, return error up to caller
return rules
}
//replace the captured block 1 time to preserve the other instance of the key, then check the cleaned string
//for duplicate key errors
rules = strings.Replace(rules, match, "", 1)
err = hcl.Decode(p, rules)
}
return rules
}

2
go.mod
View File

@ -68,7 +68,7 @@ require (
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/hcdiag v0.5.1
github.com/hashicorp/hcl v1.0.1-vault-6
github.com/hashicorp/hcl v1.0.1-vault-6.0.20241105194533-fccb5644a3ed
github.com/hashicorp/hcl/v2 v2.14.1
github.com/hashicorp/hcp-scada-provider v0.2.4
github.com/hashicorp/hcp-sdk-go v0.80.0

2
go.sum
View File

@ -491,6 +491,8 @@ github.com/hashicorp/hcdiag v0.5.1/go.mod h1:RMC2KkffN9uJ+5mFSaL67ZFVj4CDeetPF2d
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl v1.0.1-vault-6 h1:qThxNRouu5cv9LCLZ7pY43TroykqN+Uc7fT3f7tyYh4=
github.com/hashicorp/hcl v1.0.1-vault-6/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM=
github.com/hashicorp/hcl v1.0.1-vault-6.0.20241105194533-fccb5644a3ed h1:lmBfoLJnumTa+P1VOjH/cEii2x+LAJMH+VLh3syJ5Sg=
github.com/hashicorp/hcl v1.0.1-vault-6.0.20241105194533-fccb5644a3ed/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM=
github.com/hashicorp/hcl/v2 v2.14.1 h1:x0BpjfZ+CYdbiz+8yZTQ+gdLO7IXvOut7Da+XJayx34=
github.com/hashicorp/hcl/v2 v2.14.1/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
github.com/hashicorp/hcp-scada-provider v0.2.4 h1:XvctVEd4VqWVlqN1VA4vIhJANstZrc4gd2oCfrFLWZc=