|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package kafkaauth |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/IBM/sarama" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/changefeedbase" |
| 15 | + "github.com/cockroachdb/errors" |
| 16 | + "github.com/twmb/franz-go/pkg/kgo" |
| 17 | +) |
| 18 | + |
| 19 | +type saslMechanismBuilder interface { |
| 20 | + name() string |
| 21 | + validateParams(u *changefeedbase.SinkURL) error |
| 22 | + build(u *changefeedbase.SinkURL) (SASLMechanism, error) |
| 23 | +} |
| 24 | + |
| 25 | +// SASLMechanism is an interface for SASL mechanism instances, built from URLs, |
| 26 | +// to be applied to sarama and kgo configurations. |
| 27 | +type SASLMechanism interface { |
| 28 | + // ApplySarama applies the SASL mechanism to the given sarama configuration. |
| 29 | + ApplySarama(ctx context.Context, cfg *sarama.Config) error |
| 30 | + // KgoOpts returns kgo options that implement the SASL mechanism. |
| 31 | + KgoOpts(ctx context.Context) ([]kgo.Opt, error) |
| 32 | +} |
| 33 | + |
| 34 | +type saslMechanismRegistry map[string]saslMechanismBuilder |
| 35 | + |
| 36 | +// registry is the global registry of SASL Mechanisms. |
| 37 | +var registry saslMechanismRegistry = make(map[string]saslMechanismBuilder) |
| 38 | + |
| 39 | +// register registers a SASL Mechanism to the global registry. It must only be |
| 40 | +// called during init(). |
| 41 | +func (r saslMechanismRegistry) register(b saslMechanismBuilder) { |
| 42 | + n := b.name() |
| 43 | + if _, ok := r[n]; ok { |
| 44 | + panic("duplicate sasl mechanism registered: " + n) |
| 45 | + } |
| 46 | + r[n] = b |
| 47 | +} |
| 48 | + |
| 49 | +// Pick wraps registry.pick() which returns a saslMechanism for the given sink |
| 50 | +// URL, or ok=false if none is specified. It consumes all relevant query |
| 51 | +// parameters from `u`. |
| 52 | +func Pick(u *changefeedbase.SinkURL) (_ SASLMechanism, ok bool, _ error) { |
| 53 | + return registry.pick(u) |
| 54 | +} |
| 55 | + |
| 56 | +// pick returns a saslMechanism for the given sink URL, or ok=false if none is specified. |
| 57 | +func (r saslMechanismRegistry) pick(u *changefeedbase.SinkURL) (_ SASLMechanism, ok bool, _ error) { |
| 58 | + if u == nil { |
| 59 | + return nil, false, errors.AssertionFailedf("sink url is nil") |
| 60 | + } |
| 61 | + |
| 62 | + var enabled bool |
| 63 | + if _, err := u.ConsumeBool(changefeedbase.SinkParamSASLEnabled, &enabled); err != nil { |
| 64 | + return nil, false, err |
| 65 | + } |
| 66 | + if !enabled { |
| 67 | + return nil, false, maybeHelpfulErrorMessage(enabled, u) |
| 68 | + } |
| 69 | + |
| 70 | + mechanism := u.ConsumeParam(changefeedbase.SinkParamSASLMechanism) |
| 71 | + if mechanism == "" { |
| 72 | + mechanism = sarama.SASLTypePlaintext |
| 73 | + } |
| 74 | + b, ok := r[mechanism] |
| 75 | + if !ok { |
| 76 | + return nil, false, errors.Newf("param sasl_mechanism must be one of %s", r.allMechanismNames()) |
| 77 | + } |
| 78 | + |
| 79 | + // Return slightly nicer errors for this common case. |
| 80 | + if b.name() != sarama.SASLTypeOAuth { |
| 81 | + if err := validateNoOAuthOnlyParams(u); err != nil { |
| 82 | + return nil, false, err |
| 83 | + } |
| 84 | + } |
| 85 | + if err := b.validateParams(u); err != nil { |
| 86 | + return nil, false, err |
| 87 | + } |
| 88 | + mech, err := b.build(u) |
| 89 | + if err != nil { |
| 90 | + return nil, false, err |
| 91 | + } |
| 92 | + return mech, true, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (r saslMechanismRegistry) allMechanismNames() string { |
| 96 | + allMechanisms := make([]string, 0, len(r)) |
| 97 | + for k := range r { |
| 98 | + allMechanisms = append(allMechanisms, k) |
| 99 | + } |
| 100 | + sort.Strings(allMechanisms) |
| 101 | + return strings.Join(allMechanisms[:len(allMechanisms)-1], ", ") + |
| 102 | + ", or " + allMechanisms[len(allMechanisms)-1] |
| 103 | +} |
| 104 | + |
| 105 | +func newRequiredParamError(mechName string, param string) error { |
| 106 | + return errors.Newf("%s must be provided when SASL is enabled using mechanism %s", param, mechName) |
| 107 | +} |
| 108 | + |
| 109 | +func peekAndRequireParams( |
| 110 | + mechName string, u *changefeedbase.SinkURL, requiredParams []string, |
| 111 | +) error { |
| 112 | + var errs []error |
| 113 | + for _, param := range requiredParams { |
| 114 | + if u.PeekParam(param) == "" { |
| 115 | + errs = append(errs, newRequiredParamError(mechName, param)) |
| 116 | + } |
| 117 | + } |
| 118 | + return errors.Join(errs...) |
| 119 | +} |
| 120 | + |
| 121 | +// consumeHandshake consumes the handshake parameter from the sink URL. |
| 122 | +// handshake defaults to true (if sasl is enabled), unlike other options. |
| 123 | +func consumeHandshake(u *changefeedbase.SinkURL) (bool, error) { |
| 124 | + var handshake bool |
| 125 | + set, err := u.ConsumeBool(changefeedbase.SinkParamSASLHandshake, &handshake) |
| 126 | + if err != nil { |
| 127 | + return false, err |
| 128 | + } |
| 129 | + if !set { |
| 130 | + handshake = true |
| 131 | + } |
| 132 | + return handshake, nil |
| 133 | +} |
| 134 | + |
| 135 | +// maybeHelpfulErrorMessage returns an error if the user has provided SASL parameters without enabling SASL. |
| 136 | +func maybeHelpfulErrorMessage(saslEnabled bool, u *changefeedbase.SinkURL) error { |
| 137 | + if !saslEnabled { |
| 138 | + // Handle special error messages. |
| 139 | + if u.PeekParam(changefeedbase.SinkParamSASLHandshake) != "" { |
| 140 | + return errors.New("sasl_enabled must be enabled to configure SASL handshake behavior") |
| 141 | + } |
| 142 | + if u.PeekParam(changefeedbase.SinkParamSASLMechanism) != "" { |
| 143 | + return errors.New("sasl_enabled must be enabled to configure SASL mechanism") |
| 144 | + } |
| 145 | + |
| 146 | + saslOnlyParams := []string{ |
| 147 | + changefeedbase.SinkParamSASLUser, |
| 148 | + changefeedbase.SinkParamSASLPassword, |
| 149 | + changefeedbase.SinkParamSASLEnabled, |
| 150 | + changefeedbase.SinkParamSASLClientID, |
| 151 | + changefeedbase.SinkParamSASLClientSecret, |
| 152 | + changefeedbase.SinkParamSASLTokenURL, |
| 153 | + changefeedbase.SinkParamSASLGrantType, |
| 154 | + changefeedbase.SinkParamSASLScopes, |
| 155 | + changefeedbase.SinkParamSASLAwsIAMRoleArn, |
| 156 | + changefeedbase.SinkParamSASLAwsRegion, |
| 157 | + changefeedbase.SinkParamSASLAwsIAMSessionName, |
| 158 | + } |
| 159 | + for _, p := range saslOnlyParams { |
| 160 | + if u.PeekParam(p) != "" { |
| 161 | + return errors.Newf("sasl_enabled must be enabled if %s is provided", p) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// validateNoOAuthOnlyParams returns an error if the user has provided |
| 169 | +// OAUTHBEARER parameters without setting sasl_mechanism=OAUTHBEARER, for the |
| 170 | +// sake of slightly nicer errors. |
| 171 | +func validateNoOAuthOnlyParams(u *changefeedbase.SinkURL) error { |
| 172 | + oauthOnlyParams := []string{ |
| 173 | + changefeedbase.SinkParamSASLClientID, |
| 174 | + changefeedbase.SinkParamSASLClientSecret, |
| 175 | + changefeedbase.SinkParamSASLTokenURL, |
| 176 | + changefeedbase.SinkParamSASLGrantType, |
| 177 | + changefeedbase.SinkParamSASLScopes, |
| 178 | + } |
| 179 | + |
| 180 | + for _, p := range oauthOnlyParams { |
| 181 | + if u.PeekParam(p) != "" { |
| 182 | + return errors.Newf("%s is only a valid parameter for sasl_mechanism=OAUTHBEARER", p) |
| 183 | + } |
| 184 | + } |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +func applySaramaCommon(cfg *sarama.Config, mechName sarama.SASLMechanism, handshake bool) { |
| 189 | + cfg.Net.SASL.Enable = true |
| 190 | + cfg.Net.SASL.Mechanism = mechName |
| 191 | + cfg.Net.SASL.Handshake = handshake |
| 192 | +} |
0 commit comments