-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kafkaexporter: Add support for AWS_MSK_IAM SASL Auth (#5763)
This allows for developers to use this the existing kafka exporter and use the newly minted AWS_MSK_IAM SASL auth. **Link to tracking Issue:** #5009 In a very loose definition of related ticket. **Testing:** I have some rather basic testing locally to see what I can get done with this. I had followed https://github.com/aws/aws-msk-iam-auth#details as close as I could with this. **Documentation:** I haven't added any new documentation for this since I hadn't have the chance to actually validate this in a production like setting so I am hoping to leave it as a dark feature for the time being.
- Loading branch information
1 parent
f812bfd
commit 67e165e
Showing
11 changed files
with
378 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package msk implements the required IAM auth used by AWS' managed Kafka platform | ||
// to be used with the Surama kafka producer. | ||
// | ||
// Further details on how the SASL connector works can be viewed here: | ||
// https://github.com/aws/aws-msk-iam-auth#details | ||
package awsmsk |
192 changes: 192 additions & 0 deletions
192
exporter/kafkaexporter/internal/awsmsk/iam_scram_client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package awsmsk | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
sign "github.com/aws/aws-sdk-go/aws/signer/v4" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
const ( | ||
Mechanism = "AWS_MSK_IAM" | ||
|
||
service = "kafka-cluster" | ||
supportedVersion = "2020_10_22" | ||
scopeFormat = `%s/%s/%s/kafka-cluster/aws4_request` | ||
) | ||
|
||
const ( | ||
_ int32 = iota // Ignoring the zero value to ensure we start up correctly | ||
initMessage | ||
serverResponse | ||
complete | ||
failed | ||
) | ||
|
||
var ( | ||
ErrFailedServerChallenge = errors.New("failed server challenge") | ||
ErrBadChallenge = errors.New("invalid challenge data provided") | ||
ErrInvalidStateReached = errors.New("invalid state reached") | ||
) | ||
|
||
type IAMSASLClient struct { | ||
MSKHostname string | ||
Region string | ||
UserAgent string | ||
|
||
signer *sign.StreamSigner | ||
|
||
state int32 | ||
accessKey string | ||
secretKey string | ||
} | ||
|
||
type payload struct { | ||
Version string `json:"version"` | ||
BrokerHost string `json:"host"` | ||
UserAgent string `json:"user-agent"` | ||
Action string `json:"action"` | ||
Algorithm string `json:"x-amz-algorithm"` | ||
Credentials string `json:"x-amz-credential"` | ||
Date string `json:"x-amz-date"` | ||
Expires string `json:"x-amz-expires"` | ||
SignedHeaders string `json:"x-amz-signedheaders"` | ||
Signature string `json:"x-amz-signature"` | ||
} | ||
|
||
type response struct { | ||
Version string `json:"version"` | ||
RequestID string `json:"request-id"` | ||
} | ||
|
||
var _ sarama.SCRAMClient = (*IAMSASLClient)(nil) | ||
|
||
func NewIAMSASLClient(MSKHostname, region, useragent string) sarama.SCRAMClient { | ||
return &IAMSASLClient{ | ||
MSKHostname: MSKHostname, | ||
Region: region, | ||
UserAgent: useragent, | ||
} | ||
} | ||
|
||
func (sc *IAMSASLClient) Begin(username, password, _ string) error { | ||
if sc.MSKHostname == "" { | ||
return errors.New("missing required MSK Broker hostname") | ||
} | ||
|
||
if sc.Region == "" { | ||
return errors.New("missing MSK cluster region") | ||
} | ||
|
||
if sc.UserAgent == "" { | ||
return errors.New("missing value for MSK user agent") | ||
} | ||
|
||
sc.signer = sign.NewStreamSigner( | ||
sc.Region, | ||
service, | ||
nil, | ||
credentials.NewChainCredentials([]credentials.Provider{ | ||
&credentials.EnvProvider{}, | ||
&credentials.StaticProvider{ | ||
Value: credentials.Value{ | ||
AccessKeyID: username, | ||
SecretAccessKey: password, | ||
}, | ||
}, | ||
}), | ||
) | ||
sc.accessKey = username | ||
sc.secretKey = password | ||
sc.state = initMessage | ||
return nil | ||
} | ||
|
||
func (sc *IAMSASLClient) Step(challenge string) (string, error) { | ||
var resp string | ||
|
||
switch sc.state { | ||
case initMessage: | ||
if challenge != "" { | ||
sc.state = failed | ||
return "", fmt.Errorf("challenge must be empty for initial request: %w", ErrBadChallenge) | ||
} | ||
payload, err := sc.getAuthPayload() | ||
if err != nil { | ||
sc.state = failed | ||
return "", err | ||
} | ||
resp = string(payload) | ||
sc.state = serverResponse | ||
case serverResponse: | ||
if challenge == "" { | ||
sc.state = failed | ||
return "", fmt.Errorf("challenge must not be empty for server resposne: %w", ErrBadChallenge) | ||
} | ||
|
||
var resp response | ||
if err := json.NewDecoder(strings.NewReader(challenge)).Decode(&resp); err != nil { | ||
sc.state = failed | ||
return "", fmt.Errorf("unable to process msk challenge response: %w", multierr.Combine(err, ErrFailedServerChallenge)) | ||
} | ||
|
||
if resp.Version != supportedVersion { | ||
sc.state = failed | ||
return "", fmt.Errorf("unknown version found in response: %w", ErrFailedServerChallenge) | ||
} | ||
|
||
sc.state = complete | ||
default: | ||
return "", fmt.Errorf("invalid invocation: %w", ErrInvalidStateReached) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (sc *IAMSASLClient) Done() bool { return sc.state == complete } | ||
|
||
func (sc *IAMSASLClient) getAuthPayload() ([]byte, error) { | ||
ts := time.Now().UTC() | ||
|
||
headers := []byte("host:" + sc.MSKHostname) | ||
|
||
sig, err := sc.signer.GetSignature(headers, nil, ts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Creating a timestamp in the form of: yyyyMMdd'T'HHmmss'Z' | ||
date := ts.Format("20060102T150405Z") | ||
|
||
return json.Marshal(&payload{ | ||
Version: supportedVersion, | ||
BrokerHost: sc.MSKHostname, | ||
UserAgent: sc.UserAgent, | ||
Action: "kafka-cluster:Connect", | ||
Algorithm: "AWS4-HMAC-SHA256", | ||
Credentials: fmt.Sprintf(scopeFormat, sc.accessKey, date[:8], sc.Region), | ||
Date: date, | ||
SignedHeaders: "host", | ||
Expires: "300", // Seconds => 5 Minutes | ||
Signature: string(sig), | ||
}) | ||
} |
Oops, something went wrong.