|
| 1 | +package kafka |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go/aws/session" |
| 11 | + "github.com/aws/aws-sdk-go/service/sts" |
| 12 | + "github.com/twmb/franz-go/pkg/kgo" |
| 13 | + awssasl "github.com/twmb/franz-go/pkg/sasl/aws" |
| 14 | + "github.com/twmb/franz-go/pkg/sasl/plain" |
| 15 | + |
| 16 | + sdklogger "github.com/scribd/go-sdk/pkg/logger" |
| 17 | + "github.com/scribd/go-sdk/pkg/pubsub" |
| 18 | +) |
| 19 | + |
| 20 | +// Config provides a common configuration for Kafka PubSub clients. |
| 21 | +type Config struct { |
| 22 | + // Application name that will be used in a serviceName provided to tracer spans |
| 23 | + ApplicationName string |
| 24 | + // Kafka configuration provided by go-sdk |
| 25 | + KafkaConfig pubsub.Kafka |
| 26 | + // AWS session reference, it will be used in case AWS MSK IAM authentication mechanism is used |
| 27 | + AwsSession *session.Session |
| 28 | + // MsgHandler is a function that will be called when a message is received |
| 29 | + MsgHandler MsgHandler |
| 30 | + Logger sdklogger.Logger |
| 31 | +} |
| 32 | + |
| 33 | +const tlsConnectionTimeout = 10 * time.Second |
| 34 | + |
| 35 | +func newConfig(c Config, opts ...kgo.Opt) ([]kgo.Opt, error) { |
| 36 | + options := []kgo.Opt{ |
| 37 | + kgo.SeedBrokers(c.KafkaConfig.BrokerUrls...), |
| 38 | + kgo.ClientID(c.KafkaConfig.ClientId), |
| 39 | + } |
| 40 | + |
| 41 | + if c.KafkaConfig.SASL.Enabled { |
| 42 | + switch c.KafkaConfig.SASLMechanism() { |
| 43 | + case pubsub.Plain: |
| 44 | + options = append(options, getPlainSaslOption(c.KafkaConfig.SASL)) |
| 45 | + case pubsub.AWSMskIam: |
| 46 | + options = append(options, getAwsMskIamSaslOption(c.KafkaConfig.SASL.AWSMskIam, c.AwsSession)) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if c.KafkaConfig.TLS.Enabled || c.KafkaConfig.SecurityProtocol == "ssl" { |
| 51 | + var caCertPool *x509.CertPool |
| 52 | + |
| 53 | + if c.KafkaConfig.TLS.Ca != "" { |
| 54 | + caCertPool = x509.NewCertPool() |
| 55 | + caCertPool.AppendCertsFromPEM([]byte(c.KafkaConfig.TLS.Ca)) |
| 56 | + } |
| 57 | + |
| 58 | + var certificates []tls.Certificate |
| 59 | + if c.KafkaConfig.TLS.Cert != "" && c.KafkaConfig.TLS.CertKey != "" { |
| 60 | + cert, err := tls.X509KeyPair([]byte(c.KafkaConfig.TLS.Cert), []byte(c.KafkaConfig.TLS.CertKey)) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + certificates = []tls.Certificate{cert} |
| 65 | + } |
| 66 | + |
| 67 | + if c.KafkaConfig.Cert != "" && c.KafkaConfig.CertKey != "" { |
| 68 | + cert, err := tls.X509KeyPair([]byte(c.KafkaConfig.Cert), []byte(c.KafkaConfig.CertKey)) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + certificates = []tls.Certificate{cert} |
| 73 | + } |
| 74 | + |
| 75 | + var skipTLSVerify bool |
| 76 | + if c.KafkaConfig.TLS.InsecureSkipVerify || !c.KafkaConfig.SSLVerificationEnabled { |
| 77 | + skipTLSVerify = true |
| 78 | + } |
| 79 | + |
| 80 | + tlsDialer := &tls.Dialer{ |
| 81 | + NetDialer: &net.Dialer{Timeout: tlsConnectionTimeout}, |
| 82 | + Config: &tls.Config{ |
| 83 | + InsecureSkipVerify: skipTLSVerify, |
| 84 | + Certificates: certificates, |
| 85 | + RootCAs: caCertPool, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + options = append(options, kgo.Dialer(tlsDialer.DialContext)) |
| 90 | + } |
| 91 | + |
| 92 | + options = append(options, opts...) |
| 93 | + |
| 94 | + return options, nil |
| 95 | +} |
| 96 | + |
| 97 | +func getPlainSaslOption(saslConf pubsub.SASL) kgo.Opt { |
| 98 | + return kgo.SASL(plain.Auth{ |
| 99 | + User: saslConf.Username, |
| 100 | + Pass: saslConf.Password, |
| 101 | + }.AsMechanism()) |
| 102 | +} |
| 103 | + |
| 104 | +func getAwsMskIamSaslOption(iamConf pubsub.SASLAwsMskIam, s *session.Session) kgo.Opt { |
| 105 | + var opt kgo.Opt |
| 106 | + |
| 107 | + // no AWS session provided |
| 108 | + if s == nil { |
| 109 | + opt = kgo.SASL(awssasl.Auth{ |
| 110 | + AccessKey: iamConf.AccessKey, |
| 111 | + SecretKey: iamConf.SecretKey, |
| 112 | + SessionToken: iamConf.SessionToken, |
| 113 | + UserAgent: iamConf.UserAgent, |
| 114 | + }.AsManagedStreamingIAMMechanism()) |
| 115 | + } else { |
| 116 | + opt = kgo.SASL( |
| 117 | + awssasl.ManagedStreamingIAM(func(ctx context.Context) (awssasl.Auth, error) { |
| 118 | + // If assumable role is not provided, we try to get credentials from the provided AWS session |
| 119 | + if iamConf.AssumableRole == "" { |
| 120 | + val, err := s.Config.Credentials.Get() |
| 121 | + if err != nil { |
| 122 | + return awssasl.Auth{}, err |
| 123 | + } |
| 124 | + |
| 125 | + return awssasl.Auth{ |
| 126 | + AccessKey: val.AccessKeyID, |
| 127 | + SecretKey: val.SecretAccessKey, |
| 128 | + SessionToken: val.SessionToken, |
| 129 | + UserAgent: iamConf.UserAgent, |
| 130 | + }, nil |
| 131 | + } |
| 132 | + |
| 133 | + svc := sts.New(s) |
| 134 | + |
| 135 | + res, stsErr := svc.AssumeRole(&sts.AssumeRoleInput{ |
| 136 | + RoleArn: &iamConf.AssumableRole, |
| 137 | + RoleSessionName: &iamConf.SessionName, |
| 138 | + }) |
| 139 | + if stsErr != nil { |
| 140 | + return awssasl.Auth{}, stsErr |
| 141 | + } |
| 142 | + |
| 143 | + return awssasl.Auth{ |
| 144 | + AccessKey: *res.Credentials.AccessKeyId, |
| 145 | + SecretKey: *res.Credentials.SecretAccessKey, |
| 146 | + SessionToken: *res.Credentials.SessionToken, |
| 147 | + UserAgent: iamConf.UserAgent, |
| 148 | + }, nil |
| 149 | + }), |
| 150 | + ) |
| 151 | + } |
| 152 | + |
| 153 | + return opt |
| 154 | +} |
0 commit comments