|
6 | 6 | _ "embed"
|
7 | 7 | "encoding/json"
|
8 | 8 | "errors"
|
| 9 | + "fmt" |
9 | 10 | "strings"
|
10 | 11 | "sync"
|
11 | 12 | "time"
|
@@ -362,11 +363,11 @@ func (a RegoAuthorizer) Authorize(ctx context.Context, subject Subject, action p
|
362 | 363 | defer span.End()
|
363 | 364 |
|
364 | 365 | err := a.authorize(ctx, subject, action, object)
|
365 |
| - |
366 |
| - span.SetAttributes(attribute.Bool("authorized", err == nil)) |
| 366 | + authorized := err == nil |
| 367 | + span.SetAttributes(attribute.Bool("authorized", authorized)) |
367 | 368 |
|
368 | 369 | dur := time.Since(start)
|
369 |
| - if err != nil { |
| 370 | + if !authorized { |
370 | 371 | a.authorizeHist.WithLabelValues("false").Observe(dur.Seconds())
|
371 | 372 | return err
|
372 | 373 | }
|
@@ -741,3 +742,112 @@ func rbacTraceAttributes(actor Subject, action policy.Action, objectType string,
|
741 | 742 | attribute.String("object_type", objectType),
|
742 | 743 | )...)
|
743 | 744 | }
|
| 745 | + |
| 746 | +type authRecorder struct { |
| 747 | + authz Authorizer |
| 748 | +} |
| 749 | + |
| 750 | +// Recorder returns an Authorizer that records any authorization checks made |
| 751 | +// on the Context provided for the authorization check. |
| 752 | +// |
| 753 | +// Requires using the RecordAuthzChecks middleware. |
| 754 | +func Recorder(authz Authorizer) Authorizer { |
| 755 | + return &authRecorder{authz: authz} |
| 756 | +} |
| 757 | + |
| 758 | +func (c *authRecorder) Authorize(ctx context.Context, subject Subject, action policy.Action, object Object) error { |
| 759 | + err := c.authz.Authorize(ctx, subject, action, object) |
| 760 | + authorized := err == nil |
| 761 | + recordAuthzCheck(ctx, action, object, authorized) |
| 762 | + return err |
| 763 | +} |
| 764 | + |
| 765 | +func (c *authRecorder) Prepare(ctx context.Context, subject Subject, action policy.Action, objectType string) (PreparedAuthorized, error) { |
| 766 | + return c.authz.Prepare(ctx, subject, action, objectType) |
| 767 | +} |
| 768 | + |
| 769 | +type authzCheckRecorderKey struct{} |
| 770 | + |
| 771 | +type AuthzCheckRecorder struct { |
| 772 | + // lock guards checks |
| 773 | + lock sync.Mutex |
| 774 | + // checks is a list preformatted authz check IDs and their result |
| 775 | + checks []recordedCheck |
| 776 | +} |
| 777 | + |
| 778 | +type recordedCheck struct { |
| 779 | + name string |
| 780 | + // true => authorized, false => not authorized |
| 781 | + result bool |
| 782 | +} |
| 783 | + |
| 784 | +func WithAuthzCheckRecorder(ctx context.Context) context.Context { |
| 785 | + return context.WithValue(ctx, authzCheckRecorderKey{}, &AuthzCheckRecorder{}) |
| 786 | +} |
| 787 | + |
| 788 | +func recordAuthzCheck(ctx context.Context, action policy.Action, object Object, authorized bool) { |
| 789 | + r, ok := ctx.Value(authzCheckRecorderKey{}).(*AuthzCheckRecorder) |
| 790 | + if !ok { |
| 791 | + return |
| 792 | + } |
| 793 | + |
| 794 | + // We serialize the check using the following syntax |
| 795 | + var b strings.Builder |
| 796 | + if object.OrgID != "" { |
| 797 | + _, err := fmt.Fprintf(&b, "organization:%v::", object.OrgID) |
| 798 | + if err != nil { |
| 799 | + return |
| 800 | + } |
| 801 | + } |
| 802 | + if object.AnyOrgOwner { |
| 803 | + _, err := fmt.Fprint(&b, "organization:any::") |
| 804 | + if err != nil { |
| 805 | + return |
| 806 | + } |
| 807 | + } |
| 808 | + if object.Owner != "" { |
| 809 | + _, err := fmt.Fprintf(&b, "owner:%v::", object.Owner) |
| 810 | + if err != nil { |
| 811 | + return |
| 812 | + } |
| 813 | + } |
| 814 | + if object.ID != "" { |
| 815 | + _, err := fmt.Fprintf(&b, "id:%v::", object.ID) |
| 816 | + if err != nil { |
| 817 | + return |
| 818 | + } |
| 819 | + } |
| 820 | + _, err := fmt.Fprintf(&b, "%v.%v", object.RBACObject().Type, action) |
| 821 | + if err != nil { |
| 822 | + return |
| 823 | + } |
| 824 | + |
| 825 | + r.lock.Lock() |
| 826 | + defer r.lock.Unlock() |
| 827 | + r.checks = append(r.checks, recordedCheck{name: b.String(), result: authorized}) |
| 828 | +} |
| 829 | + |
| 830 | +func GetAuthzCheckRecorder(ctx context.Context) (*AuthzCheckRecorder, bool) { |
| 831 | + checks, ok := ctx.Value(authzCheckRecorderKey{}).(*AuthzCheckRecorder) |
| 832 | + if !ok { |
| 833 | + return nil, false |
| 834 | + } |
| 835 | + |
| 836 | + return checks, true |
| 837 | +} |
| 838 | + |
| 839 | +// String serializes all of the checks recorded, using the following syntax: |
| 840 | +func (r *AuthzCheckRecorder) String() string { |
| 841 | + r.lock.Lock() |
| 842 | + defer r.lock.Unlock() |
| 843 | + |
| 844 | + if len(r.checks) == 0 { |
| 845 | + return "nil" |
| 846 | + } |
| 847 | + |
| 848 | + checks := make([]string, 0, len(r.checks)) |
| 849 | + for _, check := range r.checks { |
| 850 | + checks = append(checks, fmt.Sprintf("%v=%v", check.name, check.result)) |
| 851 | + } |
| 852 | + return strings.Join(checks, "; ") |
| 853 | +} |
0 commit comments