-
Notifications
You must be signed in to change notification settings - Fork 798
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
Deleteacls support #1174
Merged
Merged
Deleteacls support #1174
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9885c6
support deleteacls
petedannemann fba392a
Merge branch 'main' into deleteacls
petedannemann 724fe5f
add deleteacls_test
petedannemann d6c061a
add protocol test
petedannemann 0ee4aae
test that acl was deleted
petedannemann afd9abd
trigger build
petedannemann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/segmentio/kafka-go/protocol/deleteacls" | ||
) | ||
|
||
// DeleteACLsRequest represents a request sent to a kafka broker to delete | ||
// ACLs. | ||
type DeleteACLsRequest struct { | ||
// Address of the kafka broker to send the request to. | ||
Addr net.Addr | ||
|
||
// List of ACL filters to use for deletion. | ||
Filters []DeleteACLsFilter | ||
} | ||
|
||
type DeleteACLsFilter struct { | ||
ResourceTypeFilter ResourceType | ||
ResourceNameFilter string | ||
ResourcePatternTypeFilter PatternType | ||
PrincipalFilter string | ||
HostFilter string | ||
Operation ACLOperationType | ||
PermissionType ACLPermissionType | ||
} | ||
|
||
// DeleteACLsResponse represents a response from a kafka broker to an ACL | ||
// deletion request. | ||
type DeleteACLsResponse struct { | ||
// The amount of time that the broker throttled the request. | ||
Throttle time.Duration | ||
|
||
// List of the results from the deletion request. | ||
Results []DeleteACLsResult | ||
} | ||
|
||
type DeleteACLsResult struct { | ||
Error error | ||
MatchingACLs []DeleteACLsMatchingACLs | ||
} | ||
|
||
type DeleteACLsMatchingACLs struct { | ||
Error error | ||
ResourceType ResourceType | ||
ResourceName string | ||
ResourcePatternType PatternType | ||
Principal string | ||
Host string | ||
Operation ACLOperationType | ||
PermissionType ACLPermissionType | ||
} | ||
|
||
// DeleteACLs sends ACLs deletion request to a kafka broker and returns the | ||
// response. | ||
func (c *Client) DeleteACLs(ctx context.Context, req *DeleteACLsRequest) (*DeleteACLsResponse, error) { | ||
filters := make([]deleteacls.RequestFilter, 0, len(req.Filters)) | ||
|
||
for _, filter := range req.Filters { | ||
filters = append(filters, deleteacls.RequestFilter{ | ||
ResourceTypeFilter: int8(filter.ResourceTypeFilter), | ||
ResourceNameFilter: filter.ResourceNameFilter, | ||
ResourcePatternTypeFilter: int8(filter.ResourcePatternTypeFilter), | ||
PrincipalFilter: filter.PrincipalFilter, | ||
HostFilter: filter.HostFilter, | ||
Operation: int8(filter.Operation), | ||
PermissionType: int8(filter.PermissionType), | ||
}) | ||
} | ||
|
||
m, err := c.roundTrip(ctx, req.Addr, &deleteacls.Request{ | ||
Filters: filters, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("kafka.(*Client).DeleteACLs: %w", err) | ||
} | ||
|
||
res := m.(*deleteacls.Response) | ||
|
||
results := make([]DeleteACLsResult, 0, len(res.FilterResults)) | ||
|
||
for _, result := range res.FilterResults { | ||
matchingACLs := make([]DeleteACLsMatchingACLs, 0, len(result.MatchingACLs)) | ||
|
||
for _, matchingACL := range result.MatchingACLs { | ||
matchingACLs = append(matchingACLs, DeleteACLsMatchingACLs{ | ||
Error: makeError(matchingACL.ErrorCode, matchingACL.ErrorMessage), | ||
ResourceType: ResourceType(matchingACL.ResourceType), | ||
ResourceName: matchingACL.ResourceName, | ||
ResourcePatternType: PatternType(matchingACL.ResourcePatternType), | ||
Principal: matchingACL.Principal, | ||
Host: matchingACL.Host, | ||
Operation: ACLOperationType(matchingACL.Operation), | ||
PermissionType: ACLPermissionType(matchingACL.PermissionType), | ||
}) | ||
} | ||
|
||
results = append(results, DeleteACLsResult{ | ||
Error: makeError(result.ErrorCode, result.ErrorMessage), | ||
MatchingACLs: matchingACLs, | ||
}) | ||
} | ||
|
||
ret := &DeleteACLsResponse{ | ||
Throttle: makeDuration(res.ThrottleTimeMs), | ||
Results: results, | ||
} | ||
|
||
return ret, nil | ||
} |
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,112 @@ | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
ktesting "github.com/segmentio/kafka-go/testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClientDeleteACLs(t *testing.T) { | ||
if !ktesting.KafkaIsAtLeast("2.0.1") { | ||
return | ||
} | ||
|
||
client, shutdown := newLocalClient() | ||
defer shutdown() | ||
|
||
topic := makeTopic() | ||
group := makeGroupID() | ||
|
||
createRes, err := client.CreateACLs(context.Background(), &CreateACLsRequest{ | ||
ACLs: []ACLEntry{ | ||
{ | ||
Principal: "User:alice", | ||
PermissionType: ACLPermissionTypeAllow, | ||
Operation: ACLOperationTypeRead, | ||
ResourceType: ResourceTypeTopic, | ||
ResourcePatternType: PatternTypeLiteral, | ||
ResourceName: topic, | ||
Host: "*", | ||
}, | ||
{ | ||
Principal: "User:bob", | ||
PermissionType: ACLPermissionTypeAllow, | ||
Operation: ACLOperationTypeRead, | ||
ResourceType: ResourceTypeGroup, | ||
ResourcePatternType: PatternTypeLiteral, | ||
ResourceName: group, | ||
Host: "*", | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for _, err := range createRes.Errors { | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
deleteResp, err := client.DeleteACLs(context.Background(), &DeleteACLsRequest{ | ||
Filters: []DeleteACLsFilter{ | ||
{ | ||
ResourceTypeFilter: ResourceTypeTopic, | ||
ResourceNameFilter: topic, | ||
ResourcePatternTypeFilter: PatternTypeLiteral, | ||
Operation: ACLOperationTypeRead, | ||
PermissionType: ACLPermissionTypeAllow, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedDeleteResp := DeleteACLsResponse{ | ||
Throttle: 0, | ||
Results: []DeleteACLsResult{ | ||
{ | ||
Error: makeError(0, ""), | ||
MatchingACLs: []DeleteACLsMatchingACLs{ | ||
{ | ||
Error: makeError(0, ""), | ||
ResourceType: ResourceTypeTopic, | ||
ResourceName: topic, | ||
ResourcePatternType: PatternTypeLiteral, | ||
Principal: "User:alice", | ||
Host: "*", | ||
Operation: ACLOperationTypeRead, | ||
PermissionType: ACLPermissionTypeAllow, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, expectedDeleteResp, *deleteResp) | ||
|
||
describeResp, err := client.DescribeACLs(context.Background(), &DescribeACLsRequest{ | ||
Filter: ACLFilter{ | ||
ResourceTypeFilter: ResourceTypeTopic, | ||
ResourceNameFilter: topic, | ||
ResourcePatternTypeFilter: PatternTypeLiteral, | ||
Operation: ACLOperationTypeRead, | ||
PermissionType: ACLPermissionTypeAllow, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedDescribeResp := DescribeACLsResponse{ | ||
Throttle: 0, | ||
Error: makeError(0, ""), | ||
Resources: []ACLResource{}, | ||
} | ||
|
||
assert.Equal(t, expectedDescribeResp, *describeResp) | ||
} |
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 deleteacls | ||
|
||
import "github.com/segmentio/kafka-go/protocol" | ||
|
||
func init() { | ||
protocol.Register(&Request{}, &Response{}) | ||
} | ||
|
||
type Request struct { | ||
// We need at least one tagged field to indicate that v2+ uses "flexible" | ||
// messages. | ||
_ struct{} `kafka:"min=v2,max=v3,tag"` | ||
|
||
Filters []RequestFilter `kafka:"min=v0,max=v3"` | ||
} | ||
|
||
func (r *Request) ApiKey() protocol.ApiKey { return protocol.DeleteAcls } | ||
|
||
func (r *Request) Broker(cluster protocol.Cluster) (protocol.Broker, error) { | ||
return cluster.Brokers[cluster.Controller], nil | ||
} | ||
|
||
type RequestFilter struct { | ||
// We need at least one tagged field to indicate that v2+ uses "flexible" | ||
// messages. | ||
_ struct{} `kafka:"min=v2,max=v3,tag"` | ||
|
||
ResourceTypeFilter int8 `kafka:"min=v0,max=v3"` | ||
ResourceNameFilter string `kafka:"min=v0,max=v1,nullable|min=v2,max=v3,nullable,compact"` | ||
ResourcePatternTypeFilter int8 `kafka:"min=v1,max=v3"` | ||
PrincipalFilter string `kafka:"min=v0,max=v1,nullable|min=v2,max=v3,nullable,compact"` | ||
HostFilter string `kafka:"min=v0,max=v1,nullable|min=v2,max=v3,nullable,compact"` | ||
Operation int8 `kafka:"min=v0,max=v3"` | ||
PermissionType int8 `kafka:"min=v0,max=v3"` | ||
} | ||
|
||
type Response struct { | ||
// We need at least one tagged field to indicate that v2+ uses "flexible" | ||
// messages. | ||
_ struct{} `kafka:"min=v2,max=v3,tag"` | ||
|
||
ThrottleTimeMs int32 `kafka:"min=v0,max=v3"` | ||
FilterResults []FilterResult `kafka:"min=v0,max=v3"` | ||
} | ||
|
||
func (r *Response) ApiKey() protocol.ApiKey { return protocol.DeleteAcls } | ||
|
||
type FilterResult struct { | ||
// We need at least one tagged field to indicate that v2+ uses "flexible" | ||
// messages. | ||
_ struct{} `kafka:"min=v2,max=v3,tag"` | ||
|
||
ErrorCode int16 `kafka:"min=v0,max=v3"` | ||
ErrorMessage string `kafka:"min=v0,max=v1,nullable|min=v2,max=v3,nullable,compact"` | ||
MatchingACLs []MatchingACL `kafka:"min=v0,max=v3"` | ||
} | ||
|
||
type MatchingACL struct { | ||
// We need at least one tagged field to indicate that v2+ uses "flexible" | ||
// messages. | ||
_ struct{} `kafka:"min=v2,max=v3,tag"` | ||
|
||
ErrorCode int16 `kafka:"min=v0,max=v3"` | ||
ErrorMessage string `kafka:"min=v0,max=v1,nullable|min=v2,max=v3,nullable,compact"` | ||
ResourceType int8 `kafka:"min=v0,max=v3"` | ||
ResourceName string `kafka:"min=v0,max=v1|min=v2,max=v3,compact"` | ||
ResourcePatternType int8 `kafka:"min=v1,max=v3"` | ||
Principal string `kafka:"min=v0,max=v1|min=v2,max=v3,compact"` | ||
Host string `kafka:"min=v0,max=v1|min=v2,max=v3,compact"` | ||
Operation int8 `kafka:"min=v0,max=v3"` | ||
PermissionType int8 `kafka:"min=v0,max=v3"` | ||
} | ||
|
||
var _ protocol.BrokerMessage = (*Request)(nil) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there any value in querying for a deleted ACL and expecting an empty/"not found" response in addition to checking for non-error in deleteResp?
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.
Good idea, I just added this