-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #85
- Loading branch information
Showing
6 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ladon | ||
|
||
// AuditLogger tracks denied and granted authorizations. | ||
type AuditLogger interface { | ||
LogRejectedAccessRequest(request *Request, pool Policies, deciders Policies) | ||
LogGrantedAccessRequest(request *Request, pool Policies, deciders Policies) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ladon | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// AuditLoggerInfo outputs information about granting or rejecting policies. | ||
type AuditLoggerInfo struct { | ||
Logger *log.Logger | ||
} | ||
|
||
func (a *AuditLoggerInfo) logger() *log.Logger { | ||
if a.Logger == nil { | ||
a.Logger = log.New(os.Stderr, "", log.LstdFlags) | ||
} | ||
return a.Logger | ||
} | ||
|
||
func (a *AuditLoggerInfo) LogRejectedAccessRequest(r *Request, p Policies, d Policies) { | ||
if len(d) > 1 { | ||
allowed := joinPoliciesNames(d[0 : len(d)-1]) | ||
denied := d[len(d)-1].GetID() | ||
a.Logger.Printf("policies %s allow access, but policy %s forcefully denied it", allowed, denied) | ||
} else if len(d) == 1 { | ||
denied := d[len(d)-1].GetID() | ||
a.Logger.Printf("policy %s forcefully denied the access", denied) | ||
} else { | ||
a.Logger.Printf("no policy allowed access") | ||
} | ||
} | ||
|
||
func (a *AuditLoggerInfo) LogGrantedAccessRequest(r *Request, p Policies, d Policies) { | ||
a.Logger.Printf("policies %s allow access", joinPoliciesNames(d)) | ||
} | ||
|
||
func joinPoliciesNames(policies Policies) string { | ||
names := []string{} | ||
for _, policy := range policies { | ||
names = append(names, policy.GetID()) | ||
} | ||
return strings.Join(names, ", ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ladon | ||
|
||
// AuditLoggerNoOp is the default AuditLogger, that tracks nothing. | ||
type AuditLoggerNoOp struct{} | ||
|
||
func (*AuditLoggerNoOp) LogRejectedAccessRequest(r *Request, p Policies, d Policies) {} | ||
func (*AuditLoggerNoOp) LogGrantedAccessRequest(r *Request, p Policies, d Policies) {} | ||
|
||
var DefaultAuditLogger = &AuditLoggerNoOp{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ladon_test | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"testing" | ||
|
||
. "github.com/ory/ladon" | ||
. "github.com/ory/ladon/manager/memory" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAuditLogger(t *testing.T) { | ||
var output bytes.Buffer | ||
|
||
warden := &Ladon{ | ||
Manager: NewMemoryManager(), | ||
AuditLogger: &AuditLoggerInfo{ | ||
Logger: log.New(&output, "", 0), | ||
}, | ||
} | ||
|
||
warden.Manager.Create(&DefaultPolicy{ | ||
ID: "no-updates", | ||
Subjects: []string{"<.*>"}, | ||
Actions: []string{"update"}, | ||
Resources: []string{"<.*>"}, | ||
Effect: DenyAccess, | ||
}) | ||
warden.Manager.Create(&DefaultPolicy{ | ||
ID: "yes-deletes", | ||
Subjects: []string{"<.*>"}, | ||
Actions: []string{"delete"}, | ||
Resources: []string{"<.*>"}, | ||
Effect: AllowAccess, | ||
}) | ||
warden.Manager.Create(&DefaultPolicy{ | ||
ID: "no-bob", | ||
Subjects: []string{"bob"}, | ||
Actions: []string{"delete"}, | ||
Resources: []string{"<.*>"}, | ||
Effect: DenyAccess, | ||
}) | ||
|
||
r := &Request{} | ||
assert.NotNil(t, warden.IsAllowed(r)) | ||
assert.Equal(t, "no policy allowed access\n", output.String()) | ||
|
||
output.Reset() | ||
|
||
r = &Request{ | ||
Action: "update", | ||
} | ||
assert.NotNil(t, warden.IsAllowed(r)) | ||
assert.Equal(t, "policy no-updates forcefully denied the access\n", output.String()) | ||
|
||
output.Reset() | ||
|
||
r = &Request{ | ||
Subject: "bob", | ||
Action: "delete", | ||
} | ||
assert.NotNil(t, warden.IsAllowed(r)) | ||
assert.Equal(t, "policies yes-deletes allow access, but policy no-bob forcefully denied it\n", output.String()) | ||
|
||
output.Reset() | ||
|
||
r = &Request{ | ||
Subject: "alice", | ||
Action: "delete", | ||
} | ||
assert.Nil(t, warden.IsAllowed(r)) | ||
assert.Equal(t, "policies yes-deletes allow access\n", output.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters