Skip to content

Commit

Permalink
test(ticdc): add TestApplySASL
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Apr 7, 2022
1 parent e59bd89 commit 31757ac
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
67 changes: 67 additions & 0 deletions cdc/sink/producer/kafka/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,70 @@ func TestConfigurationCombinations(t *testing.T) {
_ = adminClient.Close()
}
}

func TestApplySASL(t *testing.T) {
t.Parallel()

tests := []struct {
name string
URI string
exceptErr string
}{
{
name: "no params",
URI: "kafka://127.0.0.1:9092/abc",
exceptErr: "",
},
{
name: "valid PLAIN SASL",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-username=user&sasl-password=password&sasl-mechanism=plain",
exceptErr: "",
},
{
name: "valid SCRAM SASL",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-username=user&sasl-password=password&sasl-mechanism=SCRAM-SHA-512",
exceptErr: "",
},
{
name: "valid GSSAPI user auth SASL",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-mechanism=GSSAPI&sasl-gssapi-auth-type=USER&sasl-gssapi-kerberos-config-path=/root/config" +
"&sasl-gssapi-service-name=a&sasl-gssapi-user=user&sasl-gssapi-password=pwd" +
"&sasl-gssapi-realm=realm&sasl-gssapi-disable-pafxfast=false",
exceptErr: "",
},
{
name: "valid GSSAPI keytab auth SASL",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-mechanism=GSSAPI&sasl-gssapi-auth-type=keytab&sasl-gssapi-kerberos-config-path=/root/config" +
"&sasl-gssapi-service-name=a&sasl-gssapi-user=user&sasl-gssapi-keytab-path=/root/keytab" +
"&sasl-gssapi-realm=realm&sasl-gssapi-disable-pafxfast=false",
exceptErr: "",
},
{
name: "invalid mechanism",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-mechanism=a",
exceptErr: "unknown a SASL mechanism",
},
{
name: "invalid GSSAPI auth type",
URI: "kafka://127.0.0.1:9092/abc?kafka-version=2.6.0&partition-num=0" +
"&sasl-mechanism=gssapi&sasl-gssapi-auth-type=keyta1b",
exceptErr: "unknown keyta1b auth type",
},
}

for _, test := range tests {
cfg := NewConfig()
sinkURI, err := url.Parse(test.URI)
require.Nil(t, err)
if test.exceptErr == "" {
require.Nil(t, cfg.applySASL(sinkURI.Query()))
} else {
require.Regexp(t, test.exceptErr, cfg.applySASL(sinkURI.Query()).Error())
}
}
}
5 changes: 2 additions & 3 deletions pkg/security/sasl_scram.go → pkg/security/sasl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package security

import (
"fmt"
"strings"

"github.com/Shopify/sarama"
Expand Down Expand Up @@ -50,7 +49,7 @@ func SASLMechanismFormString(s string) (SASLMechanism, error) {
case "gssapi":
return GSSAPIMechanism, nil
default:
return UnknownMechanism, errors.Errorf("unknown SASL mechanism %s", s)
return UnknownMechanism, errors.Errorf("unknown %s SASL mechanism", s)
}
}

Expand Down Expand Up @@ -82,7 +81,7 @@ func AuthTypeFromString(s string) (GSSAPIAuthType, error) {
case "keytab":
return KeyTabAuth, nil
default:
return UnknownAuth, errors.New(fmt.Sprintf("unknown %s auth type", s))
return UnknownAuth, errors.Errorf("unknown %s auth type", s)
}
}

Expand Down

0 comments on commit 31757ac

Please sign in to comment.