2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2015-03-18 01:53:29 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
2017-02-27 22:42:00 +03:00
|
|
|
"errors"
|
2015-03-18 01:53:29 +03:00
|
|
|
"fmt"
|
2015-07-06 00:58:38 +03:00
|
|
|
"strings"
|
2017-02-27 22:42:00 +03:00
|
|
|
"time"
|
2015-03-18 01:53:29 +03:00
|
|
|
|
2019-01-09 03:48:57 +03:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2021-07-16 03:17:31 +03:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
2015-03-18 01:53:29 +03:00
|
|
|
"github.com/hashicorp/hcl"
|
2016-03-10 21:36:54 +03:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2018-08-15 21:42:56 +03:00
|
|
|
"github.com/hashicorp/vault/helper/identity"
|
2018-09-18 06:03:00 +03:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2019-04-13 10:44:06 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/hclutil"
|
2020-01-06 21:16:52 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/identitytpl"
|
2022-05-17 02:23:08 +03:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-06-07 20:49:51 +03:00
|
|
|
"github.com/mitchellh/copystructure"
|
2015-03-18 01:53:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-01-13 01:08:10 +03:00
|
|
|
DenyCapability = "deny"
|
2016-01-07 23:10:05 +03:00
|
|
|
CreateCapability = "create"
|
|
|
|
ReadCapability = "read"
|
|
|
|
UpdateCapability = "update"
|
|
|
|
DeleteCapability = "delete"
|
|
|
|
ListCapability = "list"
|
|
|
|
SudoCapability = "sudo"
|
2016-03-04 21:21:07 +03:00
|
|
|
RootCapability = "root"
|
2021-10-13 22:24:31 +03:00
|
|
|
PatchCapability = "patch"
|
2016-01-07 23:10:05 +03:00
|
|
|
|
|
|
|
// Backwards compatibility
|
|
|
|
OldDenyPathPolicy = "deny"
|
|
|
|
OldReadPathPolicy = "read"
|
|
|
|
OldWritePathPolicy = "write"
|
|
|
|
OldSudoPathPolicy = "sudo"
|
2015-03-18 01:53:29 +03:00
|
|
|
)
|
|
|
|
|
2016-01-13 01:08:10 +03:00
|
|
|
const (
|
|
|
|
DenyCapabilityInt uint32 = 1 << iota
|
|
|
|
CreateCapabilityInt
|
|
|
|
ReadCapabilityInt
|
|
|
|
UpdateCapabilityInt
|
|
|
|
DeleteCapabilityInt
|
|
|
|
ListCapabilityInt
|
|
|
|
SudoCapabilityInt
|
2021-10-13 22:24:31 +03:00
|
|
|
PatchCapabilityInt
|
2016-01-13 01:08:10 +03:00
|
|
|
)
|
|
|
|
|
2021-06-07 19:15:35 +03:00
|
|
|
// Error constants for testing
|
|
|
|
const (
|
|
|
|
// ControlledCapabilityPolicySubsetError is thrown when a control group's controlled capabilities
|
|
|
|
// are not a subset of the policy's capabilities.
|
|
|
|
ControlledCapabilityPolicySubsetError = "control group factor capabilities must be a subset of the policy's capabilities"
|
|
|
|
)
|
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
type PolicyType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
PolicyTypeACL PolicyType = iota
|
|
|
|
PolicyTypeRGP
|
|
|
|
PolicyTypeEGP
|
|
|
|
|
|
|
|
// Triggers a lookup in the map to figure out if ACL or RGP
|
|
|
|
PolicyTypeToken
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p PolicyType) String() string {
|
|
|
|
switch p {
|
|
|
|
case PolicyTypeACL:
|
|
|
|
return "acl"
|
|
|
|
case PolicyTypeRGP:
|
|
|
|
return "rgp"
|
|
|
|
case PolicyTypeEGP:
|
|
|
|
return "egp"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-04-08 19:43:39 +03:00
|
|
|
var cap2Int = map[string]uint32{
|
|
|
|
DenyCapability: DenyCapabilityInt,
|
|
|
|
CreateCapability: CreateCapabilityInt,
|
|
|
|
ReadCapability: ReadCapabilityInt,
|
|
|
|
UpdateCapability: UpdateCapabilityInt,
|
|
|
|
DeleteCapability: DeleteCapabilityInt,
|
|
|
|
ListCapability: ListCapabilityInt,
|
|
|
|
SudoCapability: SudoCapabilityInt,
|
2021-10-13 22:24:31 +03:00
|
|
|
PatchCapability: PatchCapabilityInt,
|
2021-04-08 19:43:39 +03:00
|
|
|
}
|
2016-01-13 01:08:10 +03:00
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
type egpPath struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
Glob bool `json:"glob"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy is used to represent the policy specified by an ACL configuration.
|
2015-03-18 01:53:29 +03:00
|
|
|
type Policy struct {
|
2018-09-18 06:03:00 +03:00
|
|
|
sentinelPolicy
|
2018-08-15 21:42:56 +03:00
|
|
|
Name string `hcl:"name"`
|
|
|
|
Paths []*PathRules `hcl:"-"`
|
|
|
|
Raw string
|
|
|
|
Type PolicyType
|
|
|
|
Templated bool
|
2018-09-18 06:03:00 +03:00
|
|
|
namespace *namespace.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShallowClone returns a shallow clone of the policy. This should not be used
|
|
|
|
// if any of the reference-typed fields are going to be modified
|
|
|
|
func (p *Policy) ShallowClone() *Policy {
|
|
|
|
return &Policy{
|
|
|
|
sentinelPolicy: p.sentinelPolicy,
|
|
|
|
Name: p.Name,
|
|
|
|
Paths: p.Paths,
|
|
|
|
Raw: p.Raw,
|
|
|
|
Type: p.Type,
|
2019-01-23 21:20:04 +03:00
|
|
|
Templated: p.Templated,
|
2018-09-18 06:03:00 +03:00
|
|
|
namespace: p.namespace,
|
|
|
|
}
|
2015-03-18 01:53:29 +03:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
// PathRules represents a policy for a path in the namespace.
|
|
|
|
type PathRules struct {
|
2019-02-15 05:31:43 +03:00
|
|
|
Path string
|
|
|
|
Policy string
|
|
|
|
Permissions *ACLPermissions
|
|
|
|
IsPrefix bool
|
|
|
|
HasSegmentWildcards bool
|
|
|
|
Capabilities []string
|
2017-02-16 05:12:26 +03:00
|
|
|
|
2017-02-27 22:42:00 +03:00
|
|
|
// These keys are used at the top level to make the HCL nicer; we store in
|
2017-10-23 21:59:37 +03:00
|
|
|
// the ACLPermissions object though
|
2017-11-02 14:18:49 +03:00
|
|
|
MinWrappingTTLHCL interface{} `hcl:"min_wrapping_ttl"`
|
|
|
|
MaxWrappingTTLHCL interface{} `hcl:"max_wrapping_ttl"`
|
|
|
|
AllowedParametersHCL map[string][]interface{} `hcl:"allowed_parameters"`
|
|
|
|
DeniedParametersHCL map[string][]interface{} `hcl:"denied_parameters"`
|
|
|
|
RequiredParametersHCL []string `hcl:"required_parameters"`
|
2018-09-18 06:03:00 +03:00
|
|
|
MFAMethodsHCL []string `hcl:"mfa_methods"`
|
|
|
|
ControlGroupHCL *ControlGroupHCL `hcl:"control_group"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ControlGroupHCL struct {
|
|
|
|
TTL interface{} `hcl:"ttl"`
|
|
|
|
Factors map[string]*ControlGroupFactor `hcl:"factor"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ControlGroup struct {
|
|
|
|
TTL time.Duration
|
|
|
|
Factors []*ControlGroupFactor
|
|
|
|
}
|
|
|
|
|
2022-10-15 02:15:15 +03:00
|
|
|
func (c *ControlGroup) Clone() (*ControlGroup, error) {
|
|
|
|
clonedControlGroup, err := copystructure.Copy(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cg := clonedControlGroup.(*ControlGroup)
|
|
|
|
|
|
|
|
return cg, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
type ControlGroupFactor struct {
|
2021-06-07 19:15:35 +03:00
|
|
|
Name string
|
|
|
|
Identity *IdentityFactor `hcl:"identity"`
|
|
|
|
ControlledCapabilities []string `hcl:"controlled_capabilities"`
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type IdentityFactor struct {
|
|
|
|
GroupIDs []string `hcl:"group_ids"`
|
|
|
|
GroupNames []string `hcl:"group_names"`
|
|
|
|
ApprovalsRequired int `hcl:"approvals"`
|
2016-10-14 20:22:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
type ACLPermissions struct {
|
2022-05-17 02:23:08 +03:00
|
|
|
CapabilitiesBitmap uint32
|
|
|
|
MinWrappingTTL time.Duration
|
|
|
|
MaxWrappingTTL time.Duration
|
|
|
|
AllowedParameters map[string][]interface{}
|
|
|
|
DeniedParameters map[string][]interface{}
|
|
|
|
RequiredParameters []string
|
|
|
|
MFAMethods []string
|
|
|
|
ControlGroup *ControlGroup
|
|
|
|
GrantingPoliciesMap map[uint32][]logical.PolicyInfo
|
2015-07-06 02:30:19 +03:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
func (p *ACLPermissions) Clone() (*ACLPermissions, error) {
|
|
|
|
ret := &ACLPermissions{
|
2017-06-07 20:49:51 +03:00
|
|
|
CapabilitiesBitmap: p.CapabilitiesBitmap,
|
|
|
|
MinWrappingTTL: p.MinWrappingTTL,
|
|
|
|
MaxWrappingTTL: p.MaxWrappingTTL,
|
2017-11-02 14:18:49 +03:00
|
|
|
RequiredParameters: p.RequiredParameters[:],
|
2017-06-07 20:49:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.AllowedParameters == nil:
|
|
|
|
case len(p.AllowedParameters) == 0:
|
|
|
|
ret.AllowedParameters = make(map[string][]interface{})
|
|
|
|
default:
|
|
|
|
clonedAllowed, err := copystructure.Copy(p.AllowedParameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.AllowedParameters = clonedAllowed.(map[string][]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.DeniedParameters == nil:
|
|
|
|
case len(p.DeniedParameters) == 0:
|
|
|
|
ret.DeniedParameters = make(map[string][]interface{})
|
|
|
|
default:
|
|
|
|
clonedDenied, err := copystructure.Copy(p.DeniedParameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.DeniedParameters = clonedDenied.(map[string][]interface{})
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
switch {
|
|
|
|
case p.MFAMethods == nil:
|
|
|
|
case len(p.MFAMethods) == 0:
|
|
|
|
ret.MFAMethods = []string{}
|
|
|
|
default:
|
|
|
|
clonedMFAMethods, err := copystructure.Copy(p.MFAMethods)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.MFAMethods = clonedMFAMethods.([]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case p.ControlGroup == nil:
|
|
|
|
default:
|
|
|
|
clonedControlGroup, err := copystructure.Copy(p.ControlGroup)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.ControlGroup = clonedControlGroup.(*ControlGroup)
|
|
|
|
}
|
|
|
|
|
2022-05-17 02:23:08 +03:00
|
|
|
switch {
|
|
|
|
case p.GrantingPoliciesMap == nil:
|
|
|
|
case len(p.GrantingPoliciesMap) == 0:
|
|
|
|
ret.GrantingPoliciesMap = make(map[uint32][]logical.PolicyInfo)
|
|
|
|
default:
|
|
|
|
clonedGrantingPoliciesMap, err := copystructure.Copy(p.GrantingPoliciesMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.GrantingPoliciesMap = clonedGrantingPoliciesMap.(map[uint32][]logical.PolicyInfo)
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:49:51 +03:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 02:23:08 +03:00
|
|
|
func addGrantingPoliciesToMap(m map[uint32][]logical.PolicyInfo, policy *Policy, capabilitiesBitmap uint32) map[uint32][]logical.PolicyInfo {
|
|
|
|
if m == nil {
|
|
|
|
m = make(map[uint32][]logical.PolicyInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all possible policies, check if the provided capabilities include
|
|
|
|
// them
|
|
|
|
for _, capability := range cap2Int {
|
|
|
|
if capabilitiesBitmap&capability == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m[capability] = append(m[capability], logical.PolicyInfo{
|
2023-05-05 01:08:22 +03:00
|
|
|
Name: policy.Name,
|
|
|
|
NamespaceId: policy.namespace.ID,
|
|
|
|
NamespacePath: policy.namespace.Path,
|
|
|
|
Type: "acl",
|
2022-05-17 02:23:08 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-08-31 01:45:11 +03:00
|
|
|
// ParseACLPolicy is used to parse the specified ACL rules into an
|
2015-03-18 01:53:29 +03:00
|
|
|
// intermediary set of policies, before being compiled into
|
|
|
|
// the ACL
|
2018-09-18 06:03:00 +03:00
|
|
|
func ParseACLPolicy(ns *namespace.Namespace, rules string) (*Policy, error) {
|
|
|
|
return parseACLPolicyWithTemplating(ns, rules, false, nil, nil)
|
2018-08-23 19:15:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseACLPolicyWithTemplating performs the actual work and checks whether we
|
|
|
|
// should perform substitutions. If performTemplating is true we know that it
|
|
|
|
// is templated so we don't check again, otherwise we check to see if it's a
|
|
|
|
// templated policy.
|
2018-09-18 06:03:00 +03:00
|
|
|
func parseACLPolicyWithTemplating(ns *namespace.Namespace, rules string, performTemplating bool, entity *identity.Entity, groups []*identity.Group) (*Policy, error) {
|
2016-03-10 21:36:54 +03:00
|
|
|
// Parse the rules
|
|
|
|
root, err := hcl.Parse(rules)
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to parse policy: %w", err)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level item should be the object list
|
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
2018-04-05 18:49:21 +03:00
|
|
|
return nil, fmt.Errorf("failed to parse policy: does not contain a root object")
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid top-level keys
|
|
|
|
valid := []string{
|
|
|
|
"name",
|
|
|
|
"path",
|
|
|
|
}
|
2018-06-12 19:38:08 +03:00
|
|
|
if err := hclutil.CheckHCLKeys(list, valid); err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to parse policy: %w", err)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the initial policy and store the raw text of the rules
|
2018-09-18 06:03:00 +03:00
|
|
|
p := Policy{
|
|
|
|
Raw: rules,
|
|
|
|
Type: PolicyTypeACL,
|
|
|
|
namespace: ns,
|
|
|
|
}
|
2016-03-10 21:36:54 +03:00
|
|
|
if err := hcl.DecodeObject(&p, list); err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to parse policy: %w", err)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if o := list.Filter("path"); len(o.Items) > 0 {
|
2018-08-23 19:15:02 +03:00
|
|
|
if err := parsePaths(&p, o, performTemplating, entity, groups); err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return nil, fmt.Errorf("failed to parse policy: %w", err)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
2015-03-18 01:53:29 +03:00
|
|
|
}
|
|
|
|
|
2016-03-10 23:55:47 +03:00
|
|
|
return &p, nil
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:15:02 +03:00
|
|
|
func parsePaths(result *Policy, list *ast.ObjectList, performTemplating bool, entity *identity.Entity, groups []*identity.Group) error {
|
2017-10-23 21:59:37 +03:00
|
|
|
paths := make([]*PathRules, 0, len(list.Items))
|
2016-03-10 21:36:54 +03:00
|
|
|
for _, item := range list.Items {
|
|
|
|
key := "path"
|
|
|
|
if len(item.Keys) > 0 {
|
2017-01-20 22:32:58 +03:00
|
|
|
key = item.Keys[0].Token.Value().(string)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
2018-08-23 19:15:02 +03:00
|
|
|
|
|
|
|
// Check the path
|
|
|
|
if performTemplating {
|
2020-01-06 21:16:52 +03:00
|
|
|
_, templated, err := identitytpl.PopulateString(identitytpl.PopulateStringInput{
|
|
|
|
Mode: identitytpl.ACLTemplating,
|
|
|
|
String: key,
|
|
|
|
Entity: identity.ToSDKEntity(entity),
|
|
|
|
Groups: identity.ToSDKGroups(groups),
|
|
|
|
NamespaceID: result.namespace.ID,
|
2018-08-23 19:15:02 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key = templated
|
2018-08-31 01:45:11 +03:00
|
|
|
} else {
|
2020-01-06 21:16:52 +03:00
|
|
|
hasTemplating, _, err := identitytpl.PopulateString(identitytpl.PopulateStringInput{
|
|
|
|
Mode: identitytpl.ACLTemplating,
|
2018-08-31 01:45:11 +03:00
|
|
|
ValidityCheckOnly: true,
|
|
|
|
String: key,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return fmt.Errorf("failed to validate policy templating: %w", err)
|
2018-08-31 01:45:11 +03:00
|
|
|
}
|
|
|
|
if hasTemplating {
|
|
|
|
result.Templated = true
|
|
|
|
}
|
2018-08-23 19:15:02 +03:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:36:54 +03:00
|
|
|
valid := []string{
|
2018-08-24 16:42:47 +03:00
|
|
|
"comment",
|
2016-03-10 21:36:54 +03:00
|
|
|
"policy",
|
|
|
|
"capabilities",
|
2017-02-16 05:12:26 +03:00
|
|
|
"allowed_parameters",
|
|
|
|
"denied_parameters",
|
2017-11-02 14:18:49 +03:00
|
|
|
"required_parameters",
|
2017-02-27 22:42:00 +03:00
|
|
|
"min_wrapping_ttl",
|
|
|
|
"max_wrapping_ttl",
|
2018-09-18 06:03:00 +03:00
|
|
|
"mfa_methods",
|
|
|
|
"control_group",
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
2018-06-12 19:38:08 +03:00
|
|
|
if err := hclutil.CheckHCLKeys(item.Val, valid); err != nil {
|
2016-03-10 21:36:54 +03:00
|
|
|
return multierror.Prefix(err, fmt.Sprintf("path %q:", key))
|
|
|
|
}
|
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
var pc PathRules
|
2016-10-16 02:43:55 +03:00
|
|
|
|
2017-10-23 21:59:37 +03:00
|
|
|
// allocate memory so that DecodeObject can initialize the ACLPermissions struct
|
|
|
|
pc.Permissions = new(ACLPermissions)
|
2016-10-16 02:43:55 +03:00
|
|
|
|
2019-01-23 23:04:49 +03:00
|
|
|
pc.Path = key
|
2018-08-23 19:15:02 +03:00
|
|
|
|
2016-03-10 21:36:54 +03:00
|
|
|
if err := hcl.DecodeObject(&pc, item.Val); err != nil {
|
|
|
|
return multierror.Prefix(err, fmt.Sprintf("path %q:", key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip a leading '/' as paths in Vault start after the / in the API path
|
2019-01-23 23:04:49 +03:00
|
|
|
if len(pc.Path) > 0 && pc.Path[0] == '/' {
|
|
|
|
pc.Path = pc.Path[1:]
|
2016-03-03 19:32:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
// Ensure we are using the full request path internally
|
2019-01-23 23:04:49 +03:00
|
|
|
pc.Path = result.namespace.Path + pc.Path
|
2018-09-18 06:03:00 +03:00
|
|
|
|
2019-04-11 00:46:17 +03:00
|
|
|
if strings.Contains(pc.Path, "+*") {
|
|
|
|
return fmt.Errorf("path %q: invalid use of wildcards ('+*' is forbidden)", pc.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pc.Path == "+" || strings.Count(pc.Path, "/+") > 0 || strings.HasPrefix(pc.Path, "+/") {
|
2019-02-15 05:31:43 +03:00
|
|
|
pc.HasSegmentWildcards = true
|
|
|
|
}
|
|
|
|
|
2019-01-23 23:04:49 +03:00
|
|
|
if strings.HasSuffix(pc.Path, "*") {
|
2019-02-15 05:31:43 +03:00
|
|
|
// If there are segment wildcards, don't actually strip the
|
|
|
|
// trailing asterisk, but don't want to hit the default case
|
|
|
|
if !pc.HasSegmentWildcards {
|
|
|
|
// Strip the glob character if found
|
|
|
|
pc.Path = strings.TrimSuffix(pc.Path, "*")
|
|
|
|
pc.IsPrefix = true
|
|
|
|
}
|
2015-07-06 00:58:38 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 23:10:05 +03:00
|
|
|
// Map old-style policies into capabilities
|
2016-03-10 21:36:54 +03:00
|
|
|
if len(pc.Policy) > 0 {
|
|
|
|
switch pc.Policy {
|
|
|
|
case OldDenyPathPolicy:
|
|
|
|
pc.Capabilities = []string{DenyCapability}
|
|
|
|
case OldReadPathPolicy:
|
|
|
|
pc.Capabilities = append(pc.Capabilities, []string{ReadCapability, ListCapability}...)
|
|
|
|
case OldWritePathPolicy:
|
|
|
|
pc.Capabilities = append(pc.Capabilities, []string{CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability}...)
|
|
|
|
case OldSudoPathPolicy:
|
|
|
|
pc.Capabilities = append(pc.Capabilities, []string{CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability}...)
|
|
|
|
default:
|
2018-04-05 18:49:21 +03:00
|
|
|
return fmt.Errorf("path %q: invalid policy %q", key, pc.Policy)
|
2016-03-10 21:36:54 +03:00
|
|
|
}
|
2015-03-18 01:53:29 +03:00
|
|
|
}
|
2016-01-07 23:10:05 +03:00
|
|
|
|
|
|
|
// Initialize the map
|
2016-10-10 01:39:58 +03:00
|
|
|
pc.Permissions.CapabilitiesBitmap = 0
|
2016-01-07 23:10:05 +03:00
|
|
|
for _, cap := range pc.Capabilities {
|
|
|
|
switch cap {
|
|
|
|
// If it's deny, don't include any other capability
|
|
|
|
case DenyCapability:
|
|
|
|
pc.Capabilities = []string{DenyCapability}
|
2016-10-10 01:39:58 +03:00
|
|
|
pc.Permissions.CapabilitiesBitmap = DenyCapabilityInt
|
2016-01-07 23:10:05 +03:00
|
|
|
goto PathFinished
|
2021-10-13 22:24:31 +03:00
|
|
|
case CreateCapability, ReadCapability, UpdateCapability, DeleteCapability, ListCapability, SudoCapability, PatchCapability:
|
2016-10-10 01:39:58 +03:00
|
|
|
pc.Permissions.CapabilitiesBitmap |= cap2Int[cap]
|
2016-01-07 23:10:05 +03:00
|
|
|
default:
|
2018-04-05 18:49:21 +03:00
|
|
|
return fmt.Errorf("path %q: invalid capability %q", key, cap)
|
2016-01-07 23:10:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:50:10 +03:00
|
|
|
if pc.AllowedParametersHCL != nil {
|
|
|
|
pc.Permissions.AllowedParameters = make(map[string][]interface{}, len(pc.AllowedParametersHCL))
|
2022-07-22 21:13:14 +03:00
|
|
|
for k, v := range pc.AllowedParametersHCL {
|
|
|
|
pc.Permissions.AllowedParameters[strings.ToLower(k)] = v
|
2017-02-17 04:50:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if pc.DeniedParametersHCL != nil {
|
|
|
|
pc.Permissions.DeniedParameters = make(map[string][]interface{}, len(pc.DeniedParametersHCL))
|
|
|
|
|
2022-07-22 21:13:14 +03:00
|
|
|
for k, v := range pc.DeniedParametersHCL {
|
|
|
|
pc.Permissions.DeniedParameters[strings.ToLower(k)] = v
|
2017-02-17 04:50:10 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-27 22:42:00 +03:00
|
|
|
if pc.MinWrappingTTLHCL != nil {
|
2017-03-07 19:21:22 +03:00
|
|
|
dur, err := parseutil.ParseDurationSecond(pc.MinWrappingTTLHCL)
|
2017-02-27 22:42:00 +03:00
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return fmt.Errorf("error parsing min_wrapping_ttl: %w", err)
|
2017-02-27 22:42:00 +03:00
|
|
|
}
|
|
|
|
pc.Permissions.MinWrappingTTL = dur
|
|
|
|
}
|
|
|
|
if pc.MaxWrappingTTLHCL != nil {
|
2017-03-07 19:21:22 +03:00
|
|
|
dur, err := parseutil.ParseDurationSecond(pc.MaxWrappingTTLHCL)
|
2017-02-27 22:42:00 +03:00
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return fmt.Errorf("error parsing max_wrapping_ttl: %w", err)
|
2017-02-27 22:42:00 +03:00
|
|
|
}
|
|
|
|
pc.Permissions.MaxWrappingTTL = dur
|
|
|
|
}
|
2018-09-18 06:03:00 +03:00
|
|
|
if pc.MFAMethodsHCL != nil {
|
|
|
|
pc.Permissions.MFAMethods = make([]string, len(pc.MFAMethodsHCL))
|
2022-07-22 21:13:14 +03:00
|
|
|
copy(pc.Permissions.MFAMethods, pc.MFAMethodsHCL)
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
if pc.ControlGroupHCL != nil {
|
|
|
|
pc.Permissions.ControlGroup = new(ControlGroup)
|
|
|
|
if pc.ControlGroupHCL.TTL != nil {
|
|
|
|
dur, err := parseutil.ParseDurationSecond(pc.ControlGroupHCL.TTL)
|
|
|
|
if err != nil {
|
2021-05-11 20:12:54 +03:00
|
|
|
return fmt.Errorf("error parsing control group max ttl: %w", err)
|
2018-09-18 06:03:00 +03:00
|
|
|
}
|
|
|
|
pc.Permissions.ControlGroup.TTL = dur
|
|
|
|
}
|
|
|
|
var factors []*ControlGroupFactor
|
|
|
|
if pc.ControlGroupHCL.Factors != nil {
|
|
|
|
for key, factor := range pc.ControlGroupHCL.Factors {
|
|
|
|
// Although we only have one factor here, we need to check to make sure there is at least
|
|
|
|
// one factor defined in this factor block.
|
|
|
|
if factor.Identity == nil {
|
|
|
|
return errors.New("no control_group factor provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if factor.Identity.ApprovalsRequired <= 0 ||
|
|
|
|
(len(factor.Identity.GroupIDs) == 0 && len(factor.Identity.GroupNames) == 0) {
|
|
|
|
return errors.New("must provide more than one identity group and approvals > 0")
|
|
|
|
}
|
|
|
|
|
2021-06-07 19:15:35 +03:00
|
|
|
// Ensure that configured ControlledCapabilities for factor are a subset of the
|
|
|
|
// Capabilities of the policy.
|
|
|
|
if len(factor.ControlledCapabilities) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, controlledCapability := range factor.ControlledCapabilities {
|
|
|
|
found = false
|
|
|
|
for _, policyCap := range pc.Capabilities {
|
|
|
|
if controlledCapability == policyCap {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New(ControlledCapabilityPolicySubsetError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 06:03:00 +03:00
|
|
|
factors = append(factors, &ControlGroupFactor{
|
2021-06-07 19:15:35 +03:00
|
|
|
Name: key,
|
|
|
|
Identity: factor.Identity,
|
|
|
|
ControlledCapabilities: factor.ControlledCapabilities,
|
2018-09-18 06:03:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(factors) == 0 {
|
|
|
|
return errors.New("no control group factors provided")
|
|
|
|
}
|
|
|
|
pc.Permissions.ControlGroup.Factors = factors
|
|
|
|
}
|
2017-02-27 22:42:00 +03:00
|
|
|
if pc.Permissions.MinWrappingTTL != 0 &&
|
|
|
|
pc.Permissions.MaxWrappingTTL != 0 &&
|
|
|
|
pc.Permissions.MaxWrappingTTL < pc.Permissions.MinWrappingTTL {
|
|
|
|
return errors.New("max_wrapping_ttl cannot be less than min_wrapping_ttl")
|
|
|
|
}
|
2017-11-02 14:18:49 +03:00
|
|
|
if len(pc.RequiredParametersHCL) > 0 {
|
|
|
|
pc.Permissions.RequiredParameters = pc.RequiredParametersHCL[:]
|
|
|
|
}
|
2016-03-10 21:36:54 +03:00
|
|
|
|
2017-02-16 05:12:26 +03:00
|
|
|
PathFinished:
|
2016-03-10 21:36:54 +03:00
|
|
|
paths = append(paths, &pc)
|
2015-03-18 01:53:29 +03:00
|
|
|
}
|
2016-03-10 21:36:54 +03:00
|
|
|
|
|
|
|
result.Paths = paths
|
|
|
|
return nil
|
|
|
|
}
|