Skip to content

Commit e90f51d

Browse files
committed
Merge branch 'main' into 3318-RavenDB-state-store-new
2 parents eae9409 + eb8fefd commit e90f51d

File tree

10 files changed

+206
-326
lines changed

10 files changed

+206
-326
lines changed

common/authentication/aws/aws.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ type EnvironmentSettings struct {
2626
Metadata map[string]string
2727
}
2828

29-
// TODO: Delete in Dapr 1.17 so we can move all IAM fields to use the defaults of:
30-
// accessKey and secretKey and region as noted in the docs, and Options struct above.
31-
type DeprecatedKafkaIAM struct {
32-
Region string `json:"awsRegion" mapstructure:"awsRegion"`
33-
AccessKey string `json:"awsAccessKey" mapstructure:"awsAccessKey"`
34-
SecretKey string `json:"awsSecretKey" mapstructure:"awsSecretKey"`
35-
SessionToken string `json:"awsSessionToken" mapstructure:"awsSessionToken"`
36-
IamRoleArn string `json:"awsIamRoleArn" mapstructure:"awsIamRoleArn"`
37-
StsSessionName string `json:"awsStsSessionName" mapstructure:"awsStsSessionName"`
38-
}
39-
4029
type Options struct {
4130
Logger logger.Logger
4231
Properties map[string]string
@@ -89,7 +78,6 @@ type Provider interface {
8978
ParameterStore() *ParameterStoreClients
9079
Kinesis() *KinesisClients
9180
Ses() *SesClients
92-
Kafka(KafkaOptions) (*KafkaClients, error)
9381

9482
// Postgres is an outlier to the others in the sense that we can update only it's config,
9583
// as we use a max connection time of 8 minutes.
@@ -115,14 +103,3 @@ func NewEnvironmentSettings(md map[string]string) (EnvironmentSettings, error) {
115103

116104
return es, nil
117105
}
118-
119-
// Coalesce is a helper function to return the first non-empty string from the inputs
120-
// This helps us to migrate away from the deprecated duplicate aws auth profile metadata fields in Dapr 1.17.
121-
func Coalesce(values ...string) string {
122-
for _, v := range values {
123-
if v != "" {
124-
return v
125-
}
126-
}
127-
return ""
128-
}

common/authentication/aws/client.go

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ package aws
1616
import (
1717
"context"
1818
"errors"
19-
"fmt"
2019
"sync"
21-
"time"
2220

23-
"github.com/IBM/sarama"
24-
"github.com/aws/aws-msk-iam-sasl-signer-go/signer"
25-
aws2 "github.com/aws/aws-sdk-go-v2/aws"
2621
"github.com/aws/aws-sdk-go/aws"
2722
"github.com/aws/aws-sdk-go/aws/credentials"
2823
"github.com/aws/aws-sdk-go/aws/session"
@@ -56,7 +51,6 @@ type Clients struct {
5651
ParameterStore *ParameterStoreClients
5752
kinesis *KinesisClients
5853
ses *SesClients
59-
kafka *KafkaClients
6054
}
6155

6256
func newClients() *Clients {
@@ -85,14 +79,6 @@ func (c *Clients) refresh(session *session.Session) error {
8579
c.kinesis.New(session)
8680
case c.ses != nil:
8781
c.ses.New(session)
88-
case c.kafka != nil:
89-
// Note: we pass in nil for token provider
90-
// as there are no special fields for x509 auth for it.
91-
// Only static auth passes it in.
92-
err := c.kafka.New(session, nil)
93-
if err != nil {
94-
return fmt.Errorf("failed to refresh Kafka AWS IAM Config: %w", err)
95-
}
9682
}
9783
return nil
9884
}
@@ -139,16 +125,6 @@ type SesClients struct {
139125
Ses *ses.SES
140126
}
141127

142-
type KafkaClients struct {
143-
config *sarama.Config
144-
consumerGroup *string
145-
brokers *[]string
146-
maxMessageBytes *int
147-
148-
ConsumerGroup sarama.ConsumerGroup
149-
Producer sarama.SyncProducer
150-
}
151-
152128
func (c *S3Clients) New(session *session.Session) {
153129
refreshedS3 := s3.New(session, session.Config)
154130
c.S3 = refreshedS3
@@ -232,120 +208,3 @@ func (c *KinesisClients) WorkerCfg(ctx context.Context, stream, consumer, mode s
232208
func (c *SesClients) New(session *session.Session) {
233209
c.Ses = ses.New(session, session.Config)
234210
}
235-
236-
type KafkaOptions struct {
237-
Config *sarama.Config
238-
ConsumerGroup string
239-
Brokers []string
240-
MaxMessageBytes int
241-
}
242-
243-
func initKafkaClients(opts KafkaOptions) *KafkaClients {
244-
return &KafkaClients{
245-
config: opts.Config,
246-
consumerGroup: &opts.ConsumerGroup,
247-
brokers: &opts.Brokers,
248-
maxMessageBytes: &opts.MaxMessageBytes,
249-
}
250-
}
251-
252-
func (c *KafkaClients) New(session *session.Session, tokenProvider *mskTokenProvider) error {
253-
const timeout = 10 * time.Second
254-
creds, err := session.Config.Credentials.Get()
255-
if err != nil {
256-
return fmt.Errorf("failed to get credentials from session: %w", err)
257-
}
258-
259-
// fill in token provider common fields across x509 and static auth
260-
if tokenProvider == nil {
261-
tokenProvider = &mskTokenProvider{}
262-
}
263-
tokenProvider.generateTokenTimeout = timeout
264-
tokenProvider.region = *session.Config.Region
265-
tokenProvider.accessKey = creds.AccessKeyID
266-
tokenProvider.secretKey = creds.SecretAccessKey
267-
tokenProvider.sessionToken = creds.SessionToken
268-
269-
c.config.Net.SASL.Enable = true
270-
c.config.Net.SASL.Mechanism = sarama.SASLTypeOAuth
271-
c.config.Net.SASL.TokenProvider = tokenProvider
272-
273-
_, err = c.config.Net.SASL.TokenProvider.Token()
274-
if err != nil {
275-
return fmt.Errorf("error validating iam credentials %v", err)
276-
}
277-
278-
consumerGroup, err := sarama.NewConsumerGroup(*c.brokers, *c.consumerGroup, c.config)
279-
if err != nil {
280-
return err
281-
}
282-
c.ConsumerGroup = consumerGroup
283-
284-
producer, err := c.getSyncProducer()
285-
if err != nil {
286-
return err
287-
}
288-
c.Producer = producer
289-
290-
return nil
291-
}
292-
293-
// Kafka specific
294-
type mskTokenProvider struct {
295-
generateTokenTimeout time.Duration
296-
accessKey string
297-
secretKey string
298-
sessionToken string
299-
awsIamRoleArn string
300-
awsStsSessionName string
301-
region string
302-
}
303-
304-
func (m *mskTokenProvider) Token() (*sarama.AccessToken, error) {
305-
// this function can't use the context passed on Init because that context would be cancelled right after Init
306-
ctx, cancel := context.WithTimeout(context.Background(), m.generateTokenTimeout)
307-
defer cancel()
308-
309-
switch {
310-
// we must first check if we are using the assume role auth profile
311-
case m.awsIamRoleArn != "" && m.awsStsSessionName != "":
312-
token, _, err := signer.GenerateAuthTokenFromRole(ctx, m.region, m.awsIamRoleArn, m.awsStsSessionName)
313-
return &sarama.AccessToken{Token: token}, err
314-
case m.accessKey != "" && m.secretKey != "":
315-
token, _, err := signer.GenerateAuthTokenFromCredentialsProvider(ctx, m.region, aws2.CredentialsProviderFunc(func(ctx context.Context) (aws2.Credentials, error) {
316-
return aws2.Credentials{
317-
AccessKeyID: m.accessKey,
318-
SecretAccessKey: m.secretKey,
319-
SessionToken: m.sessionToken,
320-
}, nil
321-
}))
322-
return &sarama.AccessToken{Token: token}, err
323-
324-
default: // load default aws creds
325-
token, _, err := signer.GenerateAuthToken(ctx, m.region)
326-
return &sarama.AccessToken{Token: token}, err
327-
}
328-
}
329-
330-
func (c *KafkaClients) getSyncProducer() (sarama.SyncProducer, error) {
331-
// Add SyncProducer specific properties to copy of base config
332-
c.config.Producer.RequiredAcks = sarama.WaitForAll
333-
c.config.Producer.Retry.Max = 5
334-
c.config.Producer.Return.Successes = true
335-
336-
if *c.maxMessageBytes > 0 {
337-
c.config.Producer.MaxMessageBytes = *c.maxMessageBytes
338-
}
339-
340-
saramaClient, err := sarama.NewClient(*c.brokers, c.config)
341-
if err != nil {
342-
return nil, err
343-
}
344-
345-
producer, err := sarama.NewSyncProducerFromClient(saramaClient)
346-
if err != nil {
347-
return nil, err
348-
}
349-
350-
return producer, nil
351-
}

common/authentication/aws/static.go

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package aws
1515

1616
import (
1717
"context"
18-
"errors"
1918
"fmt"
2019
"strconv"
2120
"sync"
@@ -319,36 +318,6 @@ func (a *StaticAuth) getDatabaseToken(ctx context.Context, poolConfig *pgxpool.C
319318
return authenticationToken, nil
320319
}
321320

322-
func (a *StaticAuth) Kafka(opts KafkaOptions) (*KafkaClients, error) {
323-
a.mu.Lock()
324-
defer a.mu.Unlock()
325-
326-
// This means we've already set the config in our New function
327-
// to use the SASL token provider.
328-
if a.clients.kafka != nil {
329-
return a.clients.kafka, nil
330-
}
331-
332-
a.clients.kafka = initKafkaClients(opts)
333-
// static auth has additional fields we need added,
334-
// so we add those static auth specific fields here,
335-
// and the rest of the token provider fields are added in New()
336-
tokenProvider := mskTokenProvider{}
337-
if a.assumeRoleARN != nil {
338-
tokenProvider.awsIamRoleArn = *a.assumeRoleARN
339-
}
340-
if a.sessionName != "" {
341-
tokenProvider.awsStsSessionName = a.sessionName
342-
}
343-
344-
err := a.clients.kafka.New(a.session, &tokenProvider)
345-
if err != nil {
346-
return nil, fmt.Errorf("failed to create AWS IAM Kafka config: %w", err)
347-
}
348-
349-
return a.clients.kafka, nil
350-
}
351-
352321
func (a *StaticAuth) createSession() (*session.Session, error) {
353322
var awsConfig *aws.Config
354323
if a.cfg == nil {
@@ -390,21 +359,7 @@ func (a *StaticAuth) createSession() (*session.Session, error) {
390359
}
391360

392361
func (a *StaticAuth) Close() error {
393-
a.mu.Lock()
394-
defer a.mu.Unlock()
395-
396-
errs := make([]error, 2)
397-
if a.clients.kafka != nil {
398-
if a.clients.kafka.Producer != nil {
399-
errs[0] = a.clients.kafka.Producer.Close()
400-
a.clients.kafka.Producer = nil
401-
}
402-
if a.clients.kafka.ConsumerGroup != nil {
403-
errs[1] = a.clients.kafka.ConsumerGroup.Close()
404-
a.clients.kafka.ConsumerGroup = nil
405-
}
406-
}
407-
return errors.Join(errs...)
362+
return nil
408363
}
409364

410365
func GetConfigV2(accessKey string, secretKey string, sessionToken string, region string, endpoint string) (awsv2.Config, error) {

common/authentication/aws/x509.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,7 @@ func newX509(ctx context.Context, opts Options, cfg *aws.Config) (*x509, error)
138138
}
139139

140140
func (a *x509) Close() error {
141-
a.mu.Lock()
142-
defer a.mu.Unlock()
143-
close(a.closeCh)
144-
a.wg.Wait()
145-
146-
errs := make([]error, 2)
147-
if a.clients.kafka != nil {
148-
if a.clients.kafka.Producer != nil {
149-
errs[0] = a.clients.kafka.Producer.Close()
150-
a.clients.kafka.Producer = nil
151-
}
152-
if a.clients.kafka.ConsumerGroup != nil {
153-
errs[1] = a.clients.kafka.ConsumerGroup.Close()
154-
a.clients.kafka.ConsumerGroup = nil
155-
}
156-
}
157-
return errors.Join(errs...)
141+
return nil
158142
}
159143

160144
func (a *x509) getCertPEM(ctx context.Context) error {
@@ -409,26 +393,6 @@ func (a *x509) UpdatePostgres(ctx context.Context, poolConfig *pgxpool.Config) {
409393
}
410394
}
411395

412-
func (a *x509) Kafka(opts KafkaOptions) (*KafkaClients, error) {
413-
a.mu.Lock()
414-
defer a.mu.Unlock()
415-
416-
// This means we've already set the config in our New function
417-
// to use the SASL token provider.
418-
if a.clients.kafka != nil {
419-
return a.clients.kafka, nil
420-
}
421-
422-
a.clients.kafka = initKafkaClients(opts)
423-
// Note: we pass in nil for token provider,
424-
// as there are no special fields for x509 auth for it.
425-
err := a.clients.kafka.New(a.session, nil)
426-
if err != nil {
427-
return nil, fmt.Errorf("failed to create AWS IAM Kafka config: %w", err)
428-
}
429-
return a.clients.kafka, nil
430-
}
431-
432396
func (a *x509) initializeTrustAnchors() error {
433397
var (
434398
trustAnchor arn.ARN

common/aws/auth/auth.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ type Options struct {
2222
Logger logger.Logger
2323
Properties map[string]string
2424

25-
Region string `json:"region" mapstructure:"region" mapstructurealiases:"awsRegion"`
26-
AccessKey string `json:"accessKey" mapstructure:"accessKey"`
27-
SecretKey string `json:"secretKey" mapstructure:"secretKey"`
28-
SessionToken string `json:"sessionToken" mapstructure:"sessionToken"`
29-
AssumeRoleArn string `json:"assumeRoleArn" mapstructure:"assumeRoleArn"`
30-
TrustAnchorArn string `json:"trustAnchorArn" mapstructure:"trustAnchorArn"`
31-
TrustProfileArn string `json:"trustProfileArn" mapstructure:"trustProfileArn"`
25+
Region string `json:"region" mapstructure:"region" mapstructurealiases:"awsRegion"`
26+
AccessKey string `json:"accessKey" mapstructure:"accessKey"`
27+
SecretKey string `json:"secretKey" mapstructure:"secretKey"`
28+
SessionToken string `json:"sessionToken" mapstructure:"sessionToken"`
29+
AssumeRoleArn string `json:"assumeRoleArn" mapstructure:"assumeRoleArn"`
30+
AssumeRoleSessionName string `json:"assumeRoleSessionName" mapstructure:"assumeRoleSessionName"`
31+
TrustAnchorArn string `json:"trustAnchorArn" mapstructure:"trustAnchorArn"`
32+
TrustProfileArn string `json:"trustProfileArn" mapstructure:"trustProfileArn"`
3233

3334
Endpoint string `json:"endpoint" mapstructure:"endpoint"`
3435
}
@@ -48,3 +49,14 @@ func NewCredentialProvider(ctx context.Context, opts Options, configOpts []func(
4849
}
4950
return newAuthStatic(ctx, opts, configOpts)
5051
}
52+
53+
// Coalesce is a helper function to return the first non-empty string from the inputs
54+
// This helps us to migrate away from the deprecated duplicate aws auth profile metadata fields in Dapr 1.17.
55+
func Coalesce(values ...string) string {
56+
for _, v := range values {
57+
if v != "" {
58+
return v
59+
}
60+
}
61+
return ""
62+
}

0 commit comments

Comments
 (0)