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