|
| 1 | +package aws_msk_iam_v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "runtime" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 16 | + signer "github.com/aws/aws-sdk-go-v2/aws/signer/v4" |
| 17 | + "github.com/segmentio/kafka-go/sasl" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + // These constants come from https://github.com/aws/aws-msk-iam-auth#details and |
| 22 | + // https://github.com/aws/aws-msk-iam-auth/blob/main/src/main/java/software/amazon/msk/auth/iam/internals/AWS4SignedPayloadGenerator.java. |
| 23 | + signAction = "kafka-cluster:Connect" |
| 24 | + signPayload = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" // the hex encoded SHA-256 of an empty string |
| 25 | + signService = "kafka-cluster" |
| 26 | + signVersion = "2020_10_22" |
| 27 | + signActionKey = "action" |
| 28 | + signHostKey = "host" |
| 29 | + signUserAgentKey = "user-agent" |
| 30 | + signVersionKey = "version" |
| 31 | + queryActionKey = "Action" |
| 32 | + queryExpiryKey = "X-Amz-Expires" |
| 33 | +) |
| 34 | + |
| 35 | +var signUserAgent = fmt.Sprintf("kafka-go/sasl/aws_msk_iam/%s", runtime.Version()) |
| 36 | + |
| 37 | +// Mechanism implements sasl.Mechanism for the AWS_MSK_IAM mechanism, based on the official java implementation: |
| 38 | +// https://github.com/aws/aws-msk-iam-auth |
| 39 | +type Mechanism struct { |
| 40 | + // The sigv4.Signer of aws-sdk-go-v2 to use when signing the request. Required. |
| 41 | + Signer *signer.Signer |
| 42 | + // The aws.Credentials of aws-sdk-go-v2. Required. |
| 43 | + Credentials aws.Credentials |
| 44 | + // The region where the msk cluster is hosted, e.g. "us-east-1". Required. |
| 45 | + Region string |
| 46 | + // The time the request is planned for. Optional, defaults to time.Now() at time of authentication. |
| 47 | + SignTime time.Time |
| 48 | + // The duration for which the presigned request is active. Optional, defaults to 5 minutes. |
| 49 | + Expiry time.Duration |
| 50 | +} |
| 51 | + |
| 52 | +func (m *Mechanism) Name() string { |
| 53 | + return "AWS_MSK_IAM" |
| 54 | +} |
| 55 | + |
| 56 | +func (m *Mechanism) Next(ctx context.Context, challenge []byte) (bool, []byte, error) { |
| 57 | + // After the initial step, the authentication is complete |
| 58 | + // kafka will return error if it rejected the credentials, so we'll only |
| 59 | + // arrive here on success. |
| 60 | + return true, nil, nil |
| 61 | +} |
| 62 | + |
| 63 | +// Start produces the authentication values required for AWS_MSK_IAM. It produces the following json as a byte array, |
| 64 | +// making use of the aws-sdk to produce the signed output. |
| 65 | +// { |
| 66 | +// "version" : "2020_10_22", |
| 67 | +// "host" : "<broker host>", |
| 68 | +// "user-agent": "<user agent string from the client>", |
| 69 | +// "action": "kafka-cluster:Connect", |
| 70 | +// "x-amz-algorithm" : "<algorithm>", |
| 71 | +// "x-amz-credential" : "<clientAWSAccessKeyID>/<date in yyyyMMdd format>/<region>/kafka-cluster/aws4_request", |
| 72 | +// "x-amz-date" : "<timestamp in yyyyMMdd'T'HHmmss'Z' format>", |
| 73 | +// "x-amz-security-token" : "<clientAWSSessionToken if any>", |
| 74 | +// "x-amz-signedheaders" : "host", |
| 75 | +// "x-amz-expires" : "<expiration in seconds>", |
| 76 | +// "x-amz-signature" : "<AWS SigV4 signature computed by the client>" |
| 77 | +// } |
| 78 | +func (m *Mechanism) Start(ctx context.Context) (sess sasl.StateMachine, ir []byte, err error) { |
| 79 | + signedMap, err := m.preSign(ctx) |
| 80 | + if err != nil { |
| 81 | + return nil, nil, err |
| 82 | + } |
| 83 | + |
| 84 | + signedJson, err := json.Marshal(signedMap) |
| 85 | + return m, signedJson, err |
| 86 | +} |
| 87 | + |
| 88 | +// preSign produces the authentication values required for AWS_MSK_IAM. |
| 89 | +func (m *Mechanism) preSign(ctx context.Context) (map[string]string, error) { |
| 90 | + req, err := buildReq(ctx, defaultExpiry(m.Expiry)) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + signedUrl, header, err := m.Signer.PresignHTTP(ctx, m.Credentials, req, signPayload, signService, m.Region, defaultSignTime(m.SignTime)) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + u, err := url.Parse(signedUrl) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + return buildSignedMap(u, header), nil |
| 105 | +} |
| 106 | + |
| 107 | +// buildReq builds http.Request for aws PreSign. |
| 108 | +func buildReq(ctx context.Context, expiry time.Duration) (*http.Request, error) { |
| 109 | + query := url.Values{ |
| 110 | + queryActionKey: {signAction}, |
| 111 | + queryExpiryKey: {strconv.FormatInt(int64(expiry/time.Second), 10)}, |
| 112 | + } |
| 113 | + saslMeta := sasl.MetadataFromContext(ctx) |
| 114 | + if saslMeta == nil { |
| 115 | + return nil, errors.New("missing sasl metadata") |
| 116 | + } |
| 117 | + |
| 118 | + signUrl := url.URL{ |
| 119 | + Scheme: "kafka", |
| 120 | + Host: saslMeta.Host, |
| 121 | + Path: "/", |
| 122 | + RawQuery: query.Encode(), |
| 123 | + } |
| 124 | + |
| 125 | + req, err := http.NewRequest(http.MethodGet, signUrl.String(), nil) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + return req, nil |
| 131 | +} |
| 132 | + |
| 133 | +// buildSignedMap builds signed string map which will be used to authenticate with MSK. |
| 134 | +func buildSignedMap(u *url.URL, header http.Header) map[string]string { |
| 135 | + signedMap := map[string]string{ |
| 136 | + signVersionKey: signVersion, |
| 137 | + signHostKey: u.Host, |
| 138 | + signUserAgentKey: signUserAgent, |
| 139 | + signActionKey: signAction, |
| 140 | + } |
| 141 | + // The protocol requires lowercase keys. |
| 142 | + for key, vals := range header { |
| 143 | + signedMap[strings.ToLower(key)] = vals[0] |
| 144 | + } |
| 145 | + for key, vals := range u.Query() { |
| 146 | + signedMap[strings.ToLower(key)] = vals[0] |
| 147 | + } |
| 148 | + |
| 149 | + return signedMap |
| 150 | +} |
| 151 | + |
| 152 | +// defaultExpiry set default expiration time if user doesn't define Mechanism.Expiry. |
| 153 | +func defaultExpiry(v time.Duration) time.Duration { |
| 154 | + if v == 0 { |
| 155 | + return 5 * time.Minute |
| 156 | + } |
| 157 | + return v |
| 158 | +} |
| 159 | + |
| 160 | +// defaultSignTime set default sign time if user doesn't define Mechanism.SignTime. |
| 161 | +func defaultSignTime(v time.Time) time.Time { |
| 162 | + if v.IsZero() { |
| 163 | + return time.Now() |
| 164 | + } |
| 165 | + return v |
| 166 | +} |
0 commit comments