github.com/segmentio/kafka-go@v0.4.48-0.20240318174348-3f6244eb34fd/alteruserscramcredentials.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/segmentio/kafka-go/protocol/alteruserscramcredentials"
    10  )
    11  
    12  // AlterUserScramCredentialsRequest represents a request sent to a kafka broker to
    13  // alter user scram credentials.
    14  type AlterUserScramCredentialsRequest struct {
    15  	// Address of the kafka broker to send the request to.
    16  	Addr net.Addr
    17  
    18  	// List of credentials to delete.
    19  	Deletions []UserScramCredentialsDeletion
    20  
    21  	// List of credentials to upsert.
    22  	Upsertions []UserScramCredentialsUpsertion
    23  }
    24  
    25  type ScramMechanism int8
    26  
    27  const (
    28  	ScramMechanismUnknown ScramMechanism = iota // 0
    29  	ScramMechanismSha256                        // 1
    30  	ScramMechanismSha512                        // 2
    31  )
    32  
    33  type UserScramCredentialsDeletion struct {
    34  	Name      string
    35  	Mechanism ScramMechanism
    36  }
    37  
    38  type UserScramCredentialsUpsertion struct {
    39  	Name           string
    40  	Mechanism      ScramMechanism
    41  	Iterations     int
    42  	Salt           []byte
    43  	SaltedPassword []byte
    44  }
    45  
    46  // AlterUserScramCredentialsResponse represents a response from a kafka broker to an alter user
    47  // credentials request.
    48  type AlterUserScramCredentialsResponse struct {
    49  	// The amount of time that the broker throttled the request.
    50  	Throttle time.Duration
    51  
    52  	// List of altered user scram credentials.
    53  	Results []AlterUserScramCredentialsResponseUser
    54  }
    55  
    56  type AlterUserScramCredentialsResponseUser struct {
    57  	User  string
    58  	Error error
    59  }
    60  
    61  // AlterUserScramCredentials sends user scram credentials alteration request to a kafka broker and returns
    62  // the response.
    63  func (c *Client) AlterUserScramCredentials(ctx context.Context, req *AlterUserScramCredentialsRequest) (*AlterUserScramCredentialsResponse, error) {
    64  	deletions := make([]alteruserscramcredentials.RequestUserScramCredentialsDeletion, len(req.Deletions))
    65  	upsertions := make([]alteruserscramcredentials.RequestUserScramCredentialsUpsertion, len(req.Upsertions))
    66  
    67  	for deletionIdx, deletion := range req.Deletions {
    68  		deletions[deletionIdx] = alteruserscramcredentials.RequestUserScramCredentialsDeletion{
    69  			Name:      deletion.Name,
    70  			Mechanism: int8(deletion.Mechanism),
    71  		}
    72  	}
    73  
    74  	for upsertionIdx, upsertion := range req.Upsertions {
    75  		upsertions[upsertionIdx] = alteruserscramcredentials.RequestUserScramCredentialsUpsertion{
    76  			Name:           upsertion.Name,
    77  			Mechanism:      int8(upsertion.Mechanism),
    78  			Iterations:     int32(upsertion.Iterations),
    79  			Salt:           upsertion.Salt,
    80  			SaltedPassword: upsertion.SaltedPassword,
    81  		}
    82  	}
    83  
    84  	m, err := c.roundTrip(ctx, req.Addr, &alteruserscramcredentials.Request{
    85  		Deletions:  deletions,
    86  		Upsertions: upsertions,
    87  	})
    88  	if err != nil {
    89  		return nil, fmt.Errorf("kafka.(*Client).AlterUserScramCredentials: %w", err)
    90  	}
    91  
    92  	res := m.(*alteruserscramcredentials.Response)
    93  	responseEntries := make([]AlterUserScramCredentialsResponseUser, len(res.Results))
    94  
    95  	for responseIdx, responseResult := range res.Results {
    96  		responseEntries[responseIdx] = AlterUserScramCredentialsResponseUser{
    97  			User:  responseResult.User,
    98  			Error: makeError(responseResult.ErrorCode, responseResult.ErrorMessage),
    99  		}
   100  	}
   101  	ret := &AlterUserScramCredentialsResponse{
   102  		Throttle: makeDuration(res.ThrottleTimeMs),
   103  		Results:  responseEntries,
   104  	}
   105  
   106  	return ret, nil
   107  }