Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list of granting policies audit logs #15457

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions audit/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ func (f *AuditFormatter) FormatRequest(ctx context.Context, w io.Writer, config
reqEntry.Auth.TokenIssueTime = auth.IssueTime.Format(time.RFC3339)
}

if auth.PolicyResults != nil {
reqEntry.Auth.PolicyResults = &AuditPolicyResults{
Allowed: auth.PolicyResults.Allowed,
}

for _, p := range auth.PolicyResults.GrantingPolicies {
reqEntry.Auth.PolicyResults.GrantingPolicies = append(reqEntry.Auth.PolicyResults.GrantingPolicies, PolicyInfo{
Name: p.Name,
NamespaceId: p.NamespaceId,
Type: p.Type,
})
}
}

if req.WrapInfo != nil {
reqEntry.Request.WrapTTL = int(req.WrapInfo.TTL / time.Second)
}
Expand Down Expand Up @@ -277,6 +291,7 @@ func (f *AuditFormatter) FormatResponse(ctx context.Context, w io.Writer, config
ID: req.ID,
ClientToken: req.ClientToken,
ClientTokenAccessor: req.ClientTokenAccessor,
ClientID: req.ClientID,
Operation: req.Operation,
MountType: req.MountType,
MountAccessor: req.MountAccessor,
Expand Down Expand Up @@ -307,6 +322,20 @@ func (f *AuditFormatter) FormatResponse(ctx context.Context, w io.Writer, config
},
}

if auth.PolicyResults != nil {
respEntry.Auth.PolicyResults = &AuditPolicyResults{
Allowed: auth.PolicyResults.Allowed,
}

for _, p := range auth.PolicyResults.GrantingPolicies {
respEntry.Auth.PolicyResults.GrantingPolicies = append(respEntry.Auth.PolicyResults.GrantingPolicies, PolicyInfo{
Name: p.Name,
NamespaceId: p.NamespaceId,
Type: p.Type,
})
}
}

if !auth.IssueTime.IsZero() {
respEntry.Auth.TokenIssueTime = auth.IssueTime.Format(time.RFC3339)
}
Expand Down Expand Up @@ -381,6 +410,7 @@ type AuditAuth struct {
IdentityPolicies []string `json:"identity_policies,omitempty"`
ExternalNamespacePolicies map[string][]string `json:"external_namespace_policies,omitempty"`
NoDefaultPolicy bool `json:"no_default_policy,omitempty"`
PolicyResults *AuditPolicyResults `json:"policy_results,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
NumUses int `json:"num_uses,omitempty"`
RemainingUses int `json:"remaining_uses,omitempty"`
Expand All @@ -390,6 +420,17 @@ type AuditAuth struct {
TokenIssueTime string `json:"token_issue_time,omitempty"`
}

type AuditPolicyResults struct {
Allowed bool `json:"allowed"`
GrantingPolicies []PolicyInfo `json:"granting_policies,omitempty"`
}

type PolicyInfo struct {
Name string `json:"name,omitempty"`
NamespaceId string `json:"namespace_id,omitempty"`
Type string `json:"type"`
}

type AuditSecret struct {
LeaseID string `json:"lease_id,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions changelog/15457.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:improvement
audit: Add a policy_results block into the audit log that contains the set of
policies that granted this request access.
```
18 changes: 17 additions & 1 deletion sdk/logical/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

// Auth is the resulting authentication information that is part of
// Response for credential backends.
// Response for credential backends. It's also attached to Request objects and
// defines the authentication used for the request. This value is audit logged.
type Auth struct {
LeaseOptions

Expand Down Expand Up @@ -101,10 +102,25 @@ type Auth struct {
// Orphan is set if the token does not have a parent
Orphan bool `json:"orphan"`

// PolicyResults is the set of policies that grant the token access to the
// requesting path.
PolicyResults *PolicyResults `json:"policy_results"`

// MFARequirement
MFARequirement *MFARequirement `json:"mfa_requirement"`
}

func (a *Auth) GoString() string {
return fmt.Sprintf("*%#v", *a)
}

type PolicyResults struct {
Allowed bool `json:"allowed"`
GrantingPolicies []PolicyInfo `json:"granting_policies"`
}

type PolicyInfo struct {
Name string `json:"name"`
NamespaceId string `json:"namespace_id"`
Type string `json:"type"`
}
36 changes: 31 additions & 5 deletions vault/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ type PolicyCheckOpts struct {
}

type AuthResults struct {
ACLResults *ACLResults
Allowed bool
RootPrivs bool
DeniedError bool
Error *multierror.Error
ACLResults *ACLResults
SentinelResults *SentinelResults
Allowed bool
RootPrivs bool
DeniedError bool
Error *multierror.Error
}

type ACLResults struct {
Expand All @@ -54,6 +55,11 @@ type ACLResults struct {
MFAMethods []string
ControlGroup *ControlGroup
CapabilitiesBitmap uint32
GrantingPolicies []logical.PolicyInfo
}

type SentinelResults struct {
GrantingPolicies []logical.PolicyInfo
}

// NewACL is used to construct a policy based ACL from a set of policies.
Expand Down Expand Up @@ -126,6 +132,10 @@ func NewACL(ctx context.Context, policies []*Policy) (*ACL, error) {
if err != nil {
return nil, fmt.Errorf("error cloning ACL permissions: %w", err)
}

// Store this policy name as the policy that permits these
// capabilities
clonedPerms.GrantingPoliciesMap = addGrantingPoliciesToMap(nil, policy, clonedPerms.CapabilitiesBitmap)
switch {
case pc.HasSegmentWildcards:
a.segmentWildcardPaths[pc.Path] = clonedPerms
Expand Down Expand Up @@ -155,6 +165,7 @@ func NewACL(ctx context.Context, policies []*Policy) (*ACL, error) {
// Insert the capabilities in this new policy into the existing
// value
existingPerms.CapabilitiesBitmap = existingPerms.CapabilitiesBitmap | pc.Permissions.CapabilitiesBitmap
existingPerms.GrantingPoliciesMap = addGrantingPoliciesToMap(existingPerms.GrantingPoliciesMap, policy, pc.Permissions.CapabilitiesBitmap)
}

// Note: In these stanzas, we're preferring minimum lifetimes. So
Expand Down Expand Up @@ -326,6 +337,11 @@ func (a *ACL) AllowOperation(ctx context.Context, req *logical.Request, capCheck
ret.Allowed = true
ret.RootPrivs = true
ret.IsRoot = true
ret.GrantingPolicies = []logical.PolicyInfo{{
Name: "root",
NamespaceId: "root",
Type: "acl",
}}
return
}
op := req.Operation
Expand Down Expand Up @@ -397,25 +413,33 @@ CHECK:
ret.MFAMethods = permissions.MFAMethods
ret.ControlGroup = permissions.ControlGroup

var grantingPolicies []logical.PolicyInfo
operationAllowed := false
switch op {
case logical.ReadOperation:
operationAllowed = capabilities&ReadCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[ReadCapabilityInt]
case logical.ListOperation:
operationAllowed = capabilities&ListCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[ListCapabilityInt]
case logical.UpdateOperation:
operationAllowed = capabilities&UpdateCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[UpdateCapabilityInt]
case logical.DeleteOperation:
operationAllowed = capabilities&DeleteCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[DeleteCapabilityInt]
case logical.CreateOperation:
operationAllowed = capabilities&CreateCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[CreateCapabilityInt]
case logical.PatchOperation:
operationAllowed = capabilities&PatchCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[PatchCapabilityInt]

// These three re-use UpdateCapabilityInt since that's the most appropriate
// capability/operation mapping
case logical.RevokeOperation, logical.RenewOperation, logical.RollbackOperation:
operationAllowed = capabilities&UpdateCapabilityInt > 0
grantingPolicies = permissions.GrantingPoliciesMap[UpdateCapabilityInt]

default:
return
Expand All @@ -425,6 +449,8 @@ CHECK:
return
}

ret.GrantingPolicies = grantingPolicies

if permissions.MaxWrappingTTL > 0 {
if req.WrapInfo == nil || req.WrapInfo.TTL > permissions.MaxWrappingTTL {
return
Expand Down
121 changes: 121 additions & 0 deletions vault/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,127 @@ func TestACL_CreationRace(t *testing.T) {
wg.Wait()
}

func TestACLGrantingPolicies(t *testing.T) {
ns := namespace.RootNamespace
policy, err := ParseACLPolicy(ns, grantingTestPolicy)
if err != nil {
t.Fatalf("err: %v", err)
}
merged, err := ParseACLPolicy(ns, grantingTestPolicyMerged)
if err != nil {
t.Fatalf("err: %v", err)
}
ctx := namespace.ContextWithNamespace(context.Background(), ns)

type tcase struct {
path string
op logical.Operation
policies []*Policy
expected []logical.PolicyInfo
allowed bool
}

policyInfo := logical.PolicyInfo{
Name: "granting_policy",
NamespaceId: "root",
Type: "acl",
}
mergedInfo := logical.PolicyInfo{
Name: "granting_policy_merged",
NamespaceId: "root",
Type: "acl",
}

tcases := []tcase{
{"kv/foo", logical.ReadOperation, []*Policy{policy}, []logical.PolicyInfo{policyInfo}, true},
{"kv/foo", logical.UpdateOperation, []*Policy{policy}, []logical.PolicyInfo{policyInfo}, true},
{"kv/bad", logical.ReadOperation, []*Policy{policy}, nil, false},
{"kv/deny", logical.ReadOperation, []*Policy{policy}, nil, false},
{"kv/path/foo", logical.ReadOperation, []*Policy{policy}, []logical.PolicyInfo{policyInfo}, true},
{"kv/path/longer", logical.ReadOperation, []*Policy{policy}, []logical.PolicyInfo{policyInfo}, true},
{"kv/foo", logical.ReadOperation, []*Policy{policy, merged}, []logical.PolicyInfo{policyInfo, mergedInfo}, true},
{"kv/path/longer3", logical.ReadOperation, []*Policy{policy, merged}, []logical.PolicyInfo{mergedInfo}, true},
{"kv/bar", logical.ReadOperation, []*Policy{policy, merged}, []logical.PolicyInfo{mergedInfo}, true},
{"kv/deny", logical.ReadOperation, []*Policy{policy, merged}, nil, false},
{"kv/path/longer", logical.UpdateOperation, []*Policy{policy, merged}, []logical.PolicyInfo{policyInfo}, true},
{"kv/path/foo", logical.ReadOperation, []*Policy{policy, merged}, []logical.PolicyInfo{policyInfo, mergedInfo}, true},
}

for _, tc := range tcases {
request := &logical.Request{
Path: tc.path,
Operation: tc.op,
}

acl, err := NewACL(ctx, tc.policies)
if err != nil {
t.Fatalf("err: %v", err)
}

authResults := acl.AllowOperation(ctx, request, false)
if authResults.Allowed != tc.allowed {
t.Fatalf("bad: case %#v: %v", tc, authResults.Allowed)
}
if !reflect.DeepEqual(authResults.GrantingPolicies, tc.expected) {
t.Fatalf("bad: case %#v: got\n%#v\nexpected\n%#v\n", tc, authResults.GrantingPolicies, tc.expected)
}
}
}

var grantingTestPolicy = `
name = "granting_policy"
path "kv/foo" {
capabilities = ["update", "read"]
}

path "kv/path/*" {
capabilities = ["read"]
}

path "kv/path/longer" {
capabilities = ["update", "read"]
}

path "kv/path/longer2" {
capabilities = ["update"]
}

path "kv/deny" {
capabilities = ["deny"]
}

path "ns1/kv/foo" {
capabilities = ["update", "read"]
}
`

var grantingTestPolicyMerged = `
name = "granting_policy_merged"
path "kv/foo" {
capabilities = ["update", "read"]
}

path "kv/bar" {
capabilities = ["update", "read"]
}

path "kv/path/*" {
capabilities = ["read"]
}

path "kv/path/longer" {
capabilities = ["read"]
}

path "kv/path/longer3" {
capabilities = ["read"]
}

path "kv/deny" {
capabilities = ["update"]
}
`

var tokenCreationPolicy = `
name = "tokenCreation"
path "auth/token/create*" {
Expand Down
Loading