github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/describeacls.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol/describeacls"
    10  )
    11  
    12  // DescribeACLsRequest represents a request sent to a kafka broker to describe
    13  // existing ACLs.
    14  type DescribeACLsRequest struct {
    15  	// Address of the kafka broker to send the request to.
    16  	Addr net.Addr
    17  
    18  	// Filter to filter ACLs on.
    19  	Filter ACLFilter
    20  }
    21  
    22  type ACLFilter struct {
    23  	ResourceTypeFilter ResourceType
    24  	ResourceNameFilter string
    25  	// ResourcePatternTypeFilter was added in v1 and is not available prior to that.
    26  	ResourcePatternTypeFilter PatternType
    27  	PrincipalFilter           string
    28  	HostFilter                string
    29  	Operation                 ACLOperationType
    30  	PermissionType            ACLPermissionType
    31  }
    32  
    33  // DescribeACLsResponse represents a response from a kafka broker to an ACL
    34  // describe request.
    35  type DescribeACLsResponse struct {
    36  	// The amount of time that the broker throttled the request.
    37  	Throttle time.Duration
    38  
    39  	// Error that occurred while attempting to describe
    40  	// the ACLs.
    41  	Error error
    42  
    43  	// ACL resources returned from the describe request.
    44  	Resources []ACLResource
    45  }
    46  
    47  type ACLResource struct {
    48  	ResourceType ResourceType
    49  	ResourceName string
    50  	PatternType  PatternType
    51  	ACLs         []ACLDescription
    52  }
    53  
    54  type ACLDescription struct {
    55  	Principal      string
    56  	Host           string
    57  	Operation      ACLOperationType
    58  	PermissionType ACLPermissionType
    59  }
    60  
    61  func (c *Client) DescribeACLs(ctx context.Context, req *DescribeACLsRequest) (*DescribeACLsResponse, error) {
    62  	m, err := c.roundTrip(ctx, req.Addr, &describeacls.Request{
    63  		Filter: describeacls.ACLFilter{
    64  			ResourceTypeFilter:        int8(req.Filter.ResourceTypeFilter),
    65  			ResourceNameFilter:        req.Filter.ResourceNameFilter,
    66  			ResourcePatternTypeFilter: int8(req.Filter.ResourcePatternTypeFilter),
    67  			PrincipalFilter:           req.Filter.PrincipalFilter,
    68  			HostFilter:                req.Filter.HostFilter,
    69  			Operation:                 int8(req.Filter.Operation),
    70  			PermissionType:            int8(req.Filter.PermissionType),
    71  		},
    72  	})
    73  	if err != nil {
    74  		return nil, fmt.Errorf("kafka.(*Client).DescribeACLs: %w", err)
    75  	}
    76  
    77  	res := m.(*describeacls.Response)
    78  	resources := make([]ACLResource, len(res.Resources))
    79  
    80  	for resourceIdx, respResource := range res.Resources {
    81  		descriptions := make([]ACLDescription, len(respResource.ACLs))
    82  
    83  		for descriptionIdx, respDescription := range respResource.ACLs {
    84  			descriptions[descriptionIdx] = ACLDescription{
    85  				Principal:      respDescription.Principal,
    86  				Host:           respDescription.Host,
    87  				Operation:      ACLOperationType(respDescription.Operation),
    88  				PermissionType: ACLPermissionType(respDescription.PermissionType),
    89  			}
    90  		}
    91  
    92  		resources[resourceIdx] = ACLResource{
    93  			ResourceType: ResourceType(respResource.ResourceType),
    94  			ResourceName: respResource.ResourceName,
    95  			PatternType:  PatternType(respResource.PatternType),
    96  			ACLs:         descriptions,
    97  		}
    98  	}
    99  
   100  	ret := &DescribeACLsResponse{
   101  		Throttle:  makeDuration(res.ThrottleTimeMs),
   102  		Error:     makeError(res.ErrorCode, res.ErrorMessage),
   103  		Resources: resources,
   104  	}
   105  
   106  	return ret, nil
   107  }