2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 20:12:41 +00:00
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build !consulent
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
package acl
|
|
|
|
|
2019-10-24 18:38:09 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-14 15:57:08 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-24 18:38:09 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2024-11-14 15:57:08 +00:00
|
|
|
"strings"
|
2019-10-24 18:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnterprisePolicyMeta stub
|
|
|
|
type EnterprisePolicyMeta struct{}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
// EnterpriseRule stub
|
|
|
|
type EnterpriseRule struct{}
|
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func (r *EnterpriseRule) Validate(string, *Config) error {
|
2019-10-15 20:58:50 +00:00
|
|
|
// nothing to validate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnterprisePolicyRules stub
|
|
|
|
type EnterprisePolicyRules struct{}
|
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func (r *EnterprisePolicyRules) Validate(*Config) error {
|
2019-10-15 20:58:50 +00:00
|
|
|
// nothing to validate
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-24 18:38:09 +00:00
|
|
|
|
2024-11-14 15:57:08 +00:00
|
|
|
func decodeRules(rules string, warnOnDuplicateKey bool, _ *Config, _ *EnterprisePolicyMeta) (*Policy, error) {
|
2019-10-24 18:38:09 +00:00
|
|
|
p := &Policy{}
|
|
|
|
|
2024-11-14 15:57:08 +00:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2019-10-24 18:38:09 +00:00
|
|
|
return nil, fmt.Errorf("Failed to parse ACL rules: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
2024-11-14 15:57:08 +00:00
|
|
|
|
|
|
|
func errIsDuplicateKey(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.Contains(err.Error(), "was already set. Each argument can only be defined once")
|
|
|
|
}
|