-
Notifications
You must be signed in to change notification settings - Fork 360
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
8224 report required permissions when authorization fails #8314
Changes from 8 commits
f1c6393
88175ae
2242218
b1f9e3a
11aa10a
ee2986a
d04f675
e883b3c
5da4f66
54dc6aa
743f818
4a054c6
803d4a3
758d910
7b304cc
7992c4b
060083f
2c50f4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"net/http" | ||
"slices" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -234,7 +235,7 @@ func TestCreateRepo_Unauthorized(t *testing.T) { | |
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp.JSON401) | ||
if resp.JSON401.Message != auth.ErrInsufficientPermissions.Error() { | ||
if !strings.Contains(resp.JSON401.Message, auth.ErrInsufficientPermissions.Error()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test here (and below) that we got an expected value? Obviously doing so makes sense only after we finalize the message contents. So it is fine to open an issue to improve these checks in this test file. |
||
t.Fatalf("expected error message %q, got %q", auth.ErrInsufficientPermissions.Error(), resp.JSON401.Message) | ||
} | ||
} | ||
|
@@ -255,15 +256,15 @@ func TestRepoMetadata_Unauthorized(t *testing.T) { | |
}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp.JSON401) | ||
if resp.JSON401.Message != auth.ErrInsufficientPermissions.Error() { | ||
if !strings.Contains(resp.JSON401.Message, auth.ErrInsufficientPermissions.Error()) { | ||
t.Errorf("expected error message %q, got %q", auth.ErrInsufficientPermissions.Error(), resp.JSON401.Message) | ||
} | ||
}) | ||
t.Run("delete", func(t *testing.T) { | ||
resp, err := clt.DeleteRepositoryMetadataWithResponse(ctx, repo, apigen.DeleteRepositoryMetadataJSONRequestBody{Keys: []string{"foo"}}) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp.JSON401) | ||
if resp.JSON401.Message != auth.ErrInsufficientPermissions.Error() { | ||
if !strings.Contains(resp.JSON401.Message, auth.ErrInsufficientPermissions.Error()) { | ||
t.Errorf("expected error message %q, got %q", auth.ErrInsufficientPermissions.Error(), resp.JSON401.Message) | ||
} | ||
}) | ||
|
@@ -272,7 +273,7 @@ func TestRepoMetadata_Unauthorized(t *testing.T) { | |
resp, err := clt.GetRepositoryMetadataWithResponse(ctx, repo) | ||
require.NoError(t, err) | ||
require.NotNil(t, resp.JSON401) | ||
if resp.JSON401.Message != auth.ErrInsufficientPermissions.Error() { | ||
if !strings.Contains(resp.JSON401.Message, auth.ErrInsufficientPermissions.Error()) { | ||
t.Errorf("expected error message %q, got %q", auth.ErrInsufficientPermissions.Error(), resp.JSON401.Message) | ||
} | ||
}) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -44,6 +44,11 @@ type AuthorizationResponse struct { | |||||||||||||
Error error | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
type NeededPermissions struct { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename, |
||||||||||||||
Denied []model.Statement | ||||||||||||||
Unauthorized []permissions.Node | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are a bit too informative IMO. Let's just keep the action name here? Otherwise I learn details of the policy including denied paths that I might not have known existed!
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// CheckResult - the final result for the authorization is accepted only if it's CheckAllow | ||||||||||||||
type CheckResult int | ||||||||||||||
|
||||||||||||||
|
@@ -918,13 +923,13 @@ func (a *APIAuthService) Authorize(ctx context.Context, req *AuthorizationReques | |||||||||||||
if err != nil { | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
allowed := CheckPermissions(ctx, req.RequiredPermissions, req.Username, policies) | ||||||||||||||
perm := &NeededPermissions{} | ||||||||||||||
allowed, permissions := CheckPermissions(ctx, req.RequiredPermissions, req.Username, policies, perm) | ||||||||||||||
|
||||||||||||||
if allowed != CheckAllow { | ||||||||||||||
return &AuthorizationResponse{ | ||||||||||||||
Allowed: false, | ||||||||||||||
Error: ErrInsufficientPermissions, | ||||||||||||||
Error: fmt.Errorf("%w\n%s", ErrInsufficientPermissions, permissions.String()), | ||||||||||||||
}, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -1147,8 +1152,27 @@ func NewAPIAuthServiceWithClient(client ClientWithResponsesInterface, externalPr | |||||||||||||
}, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func CheckPermissions(ctx context.Context, node permissions.Node, username string, policies []*model.Policy) CheckResult { | ||||||||||||||
func (n *NeededPermissions) String() string { | ||||||||||||||
if len(n.Denied) != 0 { | ||||||||||||||
deniedStr := "denied from:\n" | ||||||||||||||
for _, statement := range n.Denied { | ||||||||||||||
deniedStr += strings.Join(statement.Action, "\n") | ||||||||||||||
} | ||||||||||||||
return deniedStr | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||
} | ||||||||||||||
if len(n.Unauthorized) != 0 { | ||||||||||||||
permStr := "lacking permissions for:\n" | ||||||||||||||
for _, node := range n.Unauthorized { | ||||||||||||||
permStr += node.Permission.Action | ||||||||||||||
} | ||||||||||||||
return permStr | ||||||||||||||
} | ||||||||||||||
return "" | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func CheckPermissions(ctx context.Context, node permissions.Node, username string, policies []*model.Policy, perm *NeededPermissions) (CheckResult, *NeededPermissions) { | ||||||||||||||
allowed := CheckNeutral | ||||||||||||||
hasPermission := false | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This var is used only in |
||||||||||||||
switch node.Type { | ||||||||||||||
case permissions.NodeTypeNode: | ||||||||||||||
// check whether the permission is allowed, denied or natural (not allowed and not denied) | ||||||||||||||
|
@@ -1165,23 +1189,28 @@ func CheckPermissions(ctx context.Context, node permissions.Node, username strin | |||||||||||||
|
||||||||||||||
if stmt.Effect == model.StatementEffectDeny { | ||||||||||||||
// this is a "Deny" and it takes precedence | ||||||||||||||
return CheckDeny | ||||||||||||||
perm.Denied = append(perm.Denied, stmt) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is confusing: I take I think either make perm into an object, pass it by pointer, and keep state on it, or explicitly handle the lists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was thinking the same, agree with @arielshaqed 100% |
||||||||||||||
return CheckDeny, perm | ||||||||||||||
} else { | ||||||||||||||
hasPermission = true | ||||||||||||||
allowed = CheckAllow | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
is closer to the previous version. We prefer to use an "early return" style: errors return immediately - like l. 1188 does; successes keep going with no "else" and no need to indent. |
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
allowed = CheckAllow | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
if !hasPermission { | ||||||||||||||
perm.Unauthorized = append(perm.Unauthorized, node) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
case permissions.NodeTypeOr: | ||||||||||||||
// returns: | ||||||||||||||
// Allowed - at least one of the permissions is allowed and no one is denied | ||||||||||||||
// Denied - one of the permissions is Deny | ||||||||||||||
// Natural - otherwise | ||||||||||||||
for _, node := range node.Nodes { | ||||||||||||||
result := CheckPermissions(ctx, node, username, policies) | ||||||||||||||
result, perm := CheckPermissions(ctx, node, username, policies, perm) | ||||||||||||||
if result == CheckDeny { | ||||||||||||||
return CheckDeny | ||||||||||||||
return CheckDeny, perm | ||||||||||||||
} | ||||||||||||||
if allowed != CheckAllow { | ||||||||||||||
allowed = result | ||||||||||||||
|
@@ -1194,18 +1223,18 @@ func CheckPermissions(ctx context.Context, node permissions.Node, username strin | |||||||||||||
// Denied - one of the permissions is Deny | ||||||||||||||
// Natural - otherwise | ||||||||||||||
for _, node := range node.Nodes { | ||||||||||||||
result := CheckPermissions(ctx, node, username, policies) | ||||||||||||||
result, perm := CheckPermissions(ctx, node, username, policies, perm) | ||||||||||||||
if result == CheckNeutral || result == CheckDeny { | ||||||||||||||
return result | ||||||||||||||
return result, perm | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
return CheckAllow | ||||||||||||||
return CheckAllow, perm | ||||||||||||||
|
||||||||||||||
default: | ||||||||||||||
logging.FromContext(ctx).Error("unknown permission node type") | ||||||||||||||
return CheckDeny | ||||||||||||||
return CheckDeny, perm | ||||||||||||||
} | ||||||||||||||
return allowed | ||||||||||||||
return allowed, perm | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func interpolateUser(resource string, username string) string { | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, Im confused what are you doing with this variable?