Skip to content

Commit 972387f

Browse files
authored
feat: add context.Context to interfaces (#163)
Signed-off-by: Tabias Pittman <[email protected]>
1 parent 4efda1f commit 972387f

27 files changed

+292
-198
lines changed

audit_logger.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
package ladon
2222

23+
import "context"
24+
2325
// AuditLogger tracks denied and granted authorizations.
2426
type AuditLogger interface {
25-
LogRejectedAccessRequest(request *Request, pool Policies, deciders Policies)
26-
LogGrantedAccessRequest(request *Request, pool Policies, deciders Policies)
27+
LogRejectedAccessRequest(ctx context.Context, request *Request, pool Policies, deciders Policies)
28+
LogGrantedAccessRequest(ctx context.Context, request *Request, pool Policies, deciders Policies)
2729
}

audit_logger_info.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ladon
2222

2323
import (
24+
"context"
2425
"log"
2526
"os"
2627
"strings"
@@ -38,7 +39,7 @@ func (a *AuditLoggerInfo) logger() *log.Logger {
3839
return a.Logger
3940
}
4041

41-
func (a *AuditLoggerInfo) LogRejectedAccessRequest(r *Request, p Policies, d Policies) {
42+
func (a *AuditLoggerInfo) LogRejectedAccessRequest(ctx context.Context, r *Request, p Policies, d Policies) {
4243
if len(d) > 1 {
4344
allowed := joinPoliciesNames(d[0 : len(d)-1])
4445
denied := d[len(d)-1].GetID()
@@ -51,7 +52,7 @@ func (a *AuditLoggerInfo) LogRejectedAccessRequest(r *Request, p Policies, d Pol
5152
}
5253
}
5354

54-
func (a *AuditLoggerInfo) LogGrantedAccessRequest(r *Request, p Policies, d Policies) {
55+
func (a *AuditLoggerInfo) LogGrantedAccessRequest(ctx context.Context, r *Request, p Policies, d Policies) {
5556
a.logger().Printf("policies %s allow access", joinPoliciesNames(d))
5657
}
5758

audit_logger_noop.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020

2121
package ladon
2222

23+
import "context"
24+
2325
// AuditLoggerNoOp is the default AuditLogger, that tracks nothing.
2426
type AuditLoggerNoOp struct{}
2527

26-
func (*AuditLoggerNoOp) LogRejectedAccessRequest(r *Request, p Policies, d Policies) {}
27-
func (*AuditLoggerNoOp) LogGrantedAccessRequest(r *Request, p Policies, d Policies) {}
28+
func (*AuditLoggerNoOp) LogRejectedAccessRequest(ctx context.Context, r *Request, p Policies, d Policies) {
29+
}
30+
func (*AuditLoggerNoOp) LogGrantedAccessRequest(ctx context.Context, r *Request, p Policies, d Policies) {
31+
}
2832

2933
var DefaultAuditLogger = &AuditLoggerNoOp{}

audit_logger_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package ladon_test
2222

2323
import (
2424
"bytes"
25+
"context"
2526
"log"
2627
"testing"
2728

@@ -41,21 +42,23 @@ func TestAuditLogger(t *testing.T) {
4142
},
4243
}
4344

44-
warden.Manager.Create(&DefaultPolicy{
45+
ctx := context.Background()
46+
47+
warden.Manager.Create(ctx, &DefaultPolicy{
4548
ID: "no-updates",
4649
Subjects: []string{"<.*>"},
4750
Actions: []string{"update"},
4851
Resources: []string{"<.*>"},
4952
Effect: DenyAccess,
5053
})
51-
warden.Manager.Create(&DefaultPolicy{
54+
warden.Manager.Create(ctx, &DefaultPolicy{
5255
ID: "yes-deletes",
5356
Subjects: []string{"<.*>"},
5457
Actions: []string{"delete"},
5558
Resources: []string{"<.*>"},
5659
Effect: AllowAccess,
5760
})
58-
warden.Manager.Create(&DefaultPolicy{
61+
warden.Manager.Create(ctx, &DefaultPolicy{
5962
ID: "no-bob",
6063
Subjects: []string{"bob"},
6164
Actions: []string{"delete"},
@@ -64,15 +67,15 @@ func TestAuditLogger(t *testing.T) {
6467
})
6568

6669
r := &Request{}
67-
assert.NotNil(t, warden.IsAllowed(r))
70+
assert.NotNil(t, warden.IsAllowed(ctx, r))
6871
assert.Equal(t, "no policy allowed access\n", output.String())
6972

7073
output.Reset()
7174

7275
r = &Request{
7376
Action: "update",
7477
}
75-
assert.NotNil(t, warden.IsAllowed(r))
78+
assert.NotNil(t, warden.IsAllowed(ctx, r))
7679
assert.Equal(t, "policy no-updates forcefully denied the access\n", output.String())
7780

7881
output.Reset()
@@ -81,7 +84,7 @@ func TestAuditLogger(t *testing.T) {
8184
Subject: "bob",
8285
Action: "delete",
8386
}
84-
assert.NotNil(t, warden.IsAllowed(r))
87+
assert.NotNil(t, warden.IsAllowed(ctx, r))
8588
assert.Equal(t, "policies yes-deletes allow access, but policy no-bob forcefully denied it\n", output.String())
8689

8790
output.Reset()
@@ -90,6 +93,6 @@ func TestAuditLogger(t *testing.T) {
9093
Subject: "alice",
9194
Action: "delete",
9295
}
93-
assert.Nil(t, warden.IsAllowed(r))
96+
assert.Nil(t, warden.IsAllowed(ctx, r))
9497
assert.Equal(t, "policies yes-deletes allow access\n", output.String())
9598
}

benchmark_warden_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ladon_test
2222

2323
import (
24+
"context"
2425
"fmt"
2526
"strconv"
2627
"testing"
@@ -50,16 +51,18 @@ func benchmarkLadon(i int, b *testing.B, warden *ladon.Ladon) {
5051
// sem <- true
5152
//}
5253

54+
ctx := context.Background()
55+
5356
for _, pol := range generatePolicies(i) {
54-
if err := warden.Manager.Create(pol); err != nil {
57+
if err := warden.Manager.Create(ctx, pol); err != nil {
5558
b.Logf("Got error from warden.Manager.Create: %s", err)
5659
}
5760
}
5861

5962
b.ResetTimer()
6063
var err error
6164
for n := 0; n < b.N; n++ {
62-
if err = warden.IsAllowed(&ladon.Request{
65+
if err = warden.IsAllowed(ctx, &ladon.Request{
6366
Subject: "5",
6467
Action: "bar",
6568
Resource: "baz",

condition.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ladon
2222

2323
import (
24+
"context"
2425
"encoding/json"
2526

2627
"github.com/pkg/errors"
@@ -32,7 +33,7 @@ type Condition interface {
3233
GetName() string
3334

3435
// Fulfills returns true if the request is fulfilled by the condition.
35-
Fulfills(interface{}, *Request) bool
36+
Fulfills(context.Context, interface{}, *Request) bool
3637
}
3738

3839
// Conditions is a collection of conditions.
@@ -129,6 +130,6 @@ var ConditionFactories = map[string]func() Condition{
129130
return new(ResourceContainsCondition)
130131
},
131132
new(BooleanCondition).GetName(): func() Condition {
132-
return new (BooleanCondition)
133+
return new(BooleanCondition)
133134
},
134135
}

condition_boolean.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ladon
22

3+
import "context"
4+
35
/*
46
BooleanCondition is used to determine if a boolean context matches an expected
57
boolean condition.
@@ -18,7 +20,7 @@ func (c *BooleanCondition) GetName() string {
1820

1921
// Fulfills determines if the BooleanCondition is fulfilled.
2022
// The BooleanCondition is fulfilled if the provided boolean value matches the conditions boolean value.
21-
func (c *BooleanCondition) Fulfills(value interface{}, _ *Request) bool {
23+
func (c *BooleanCondition) Fulfills(ctx context.Context, value interface{}, _ *Request) bool {
2224
val, ok := value.(bool)
2325

2426
return ok && val == c.BooleanValue

condition_cidr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ladon
2222

2323
import (
24+
"context"
2425
"net"
2526
)
2627

@@ -30,7 +31,7 @@ type CIDRCondition struct {
3031
}
3132

3233
// Fulfills returns true if the the request is fulfilled by the condition.
33-
func (c *CIDRCondition) Fulfills(value interface{}, _ *Request) bool {
34+
func (c *CIDRCondition) Fulfills(ctx context.Context, value interface{}, _ *Request) bool {
3435
ips, ok := value.(string)
3536
if !ok {
3637
return false

condition_cidr_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package ladon
2222

2323
import (
24+
"context"
2425
"testing"
2526

2627
"github.com/stretchr/testify/assert"
@@ -42,6 +43,6 @@ func TestCIDRMatch(t *testing.T) {
4243
CIDR: c.cidr,
4344
}
4445

45-
assert.Equal(t, c.pass, condition.Fulfills(c.ip, new(Request)), "%s; %s", c.ip, c.cidr)
46+
assert.Equal(t, c.pass, condition.Fulfills(context.Background(), c.ip, new(Request)), "%s; %s", c.ip, c.cidr)
4647
}
4748
}

condition_resource_contains.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020

2121
package ladon
2222

23-
import "strings"
23+
import (
24+
"context"
25+
"strings"
26+
)
2427

2528
// ResourceContainsCondition is fulfilled if the context matches a substring within the resource name
2629
type ResourceContainsCondition struct{}
2730

2831
// Fulfills returns true if the request's resouce contains the given value string
29-
func (c *ResourceContainsCondition) Fulfills(value interface{}, r *Request) bool {
32+
func (c *ResourceContainsCondition) Fulfills(ctx context.Context, value interface{}, r *Request) bool {
3033

3134
filter, ok := value.(map[string]interface{})
3235
if !ok {

0 commit comments

Comments
 (0)