Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Adding scram authentication for kafka #2110

Closed
wants to merge 5 commits into from

Conversation

TheDavidKruse
Copy link

Co-authored by Zenobius Selvadhason [email protected]

Signed-off-by: david kruse [email protected]

Which problem is this PR solving?

  • Resolves #2046 SCRAM authentication between jaeger and kafka

Short description of the changes

  • Added CLI flags to pass in algorithm (SHA), tls config, username, password, etc
  • Added package xdg-go/scram package to manage handshake
  • Added scram.go to the kafka auth directory

This is a near drop-in from the sarama scram configuration they listed in their examples.

@TheDavidKruse TheDavidKruse requested a review from a team as a code owner March 2, 2020 20:37
Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make sure to add unit tests

pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/options.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
@TheDavidKruse TheDavidKruse changed the title Adding scram authentication for kafka WIP - Adding scram authentication for kafka Apr 9, 2020
saramaConfig.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &SCRAMClient{HashGeneratorFcn: SHA256} }
saramaConfig.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256)
} else {
errors.Errorf("invalid SHA algorithm '%s': can be either 'sha256' or 'sha512'", config.algorithm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing return? and consequently, missing unit test for it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a unit test with instantiating the scramConfig itself but adding initFromViper test soon if I can get it working.

@codecov
Copy link

codecov bot commented Apr 10, 2020

Codecov Report

Merging #2110 (8b2ef89) into master (2f8ffa9) will increase coverage by 0.40%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2110      +/-   ##
==========================================
+ Coverage   95.73%   96.14%   +0.40%     
==========================================
  Files         216      219       +3     
  Lines        9599    10585     +986     
==========================================
+ Hits         9190    10177     +987     
- Misses        336      351      +15     
+ Partials       73       57      -16     
Impacted Files Coverage Δ
cmd/collector/app/server/http.go 0.00% <0.00%> (-88.00%) ⬇️
cmd/collector/app/server/zipkin.go 0.00% <0.00%> (-76.93%) ⬇️
pkg/netutils/port.go 50.00% <0.00%> (-50.00%) ⬇️
cmd/query/app/static_handler.go 86.84% <0.00%> (-7.95%) ⬇️
cmd/collector/app/collector.go 69.11% <0.00%> (-5.54%) ⬇️
plugin/storage/es/dependencystore/storage.go 85.71% <0.00%> (-5.40%) ⬇️
plugin/storage/grpc/shared/grpc_server.go 76.47% <0.00%> (-4.98%) ⬇️
cmd/flags/flags.go 43.47% <0.00%> (-3.90%) ⬇️
plugin/storage/grpc/shared/grpc_client.go 85.96% <0.00%> (-0.65%) ⬇️
plugin/storage/kafka/options.go 93.68% <0.00%> (-0.07%) ⬇️
... and 224 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc091c5...48a9ead. Read the comment docs.

pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved

"github.com/Shopify/sarama"
"github.com/pkg/errors"
SCRAM "github.com/xdg/scram"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Package name should be in lower-case. There's no need to rename it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"scram" is being used in the config file as a string constant for the AuthenticationConfig, I would need to change the constant name or change the name of the import of the package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but it's in a different file, why is it a problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe because the constant is package scoped and so it conflicts with the standard import name of "scram". Would something like "scrampkg" be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe scrumclient

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that scrampkg may be better to define that it's a package, or something like xdgscram as it's the name of the import of the package github.com/xdg/scram.

I feel as though scrumclient or scramclient would be confusing as there's a struct named scramClient and scrum just seems mispelled. Those are my thoughts about those.

pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/options.go Outdated Show resolved Hide resolved
pkg/kafka/auth/options.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
type SCRAMClient struct {
*SCRAM.Client
*SCRAM.ClientConversation
SCRAM.HashGeneratorFcn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any need for embedding these? If not, it's better to give them private field names, because otherwise all their methods are leaking into the scramClient API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaik they're based in the interface for the sarama config to begin using Scram and need to be embedded for the Begin, Step and Done. Let me check and I'll reply to this comment.

Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
pkg/kafka/auth/scram.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
Signed-off-by: david kruse <[email protected]>
go.mod Outdated
@@ -52,7 +52,7 @@ require (
github.com/opentracing/opentracing-go v1.1.0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still have places where errors pkg is introduced, that's why it's moving to direct dependency. Please use fmt.Errorf

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed!

Signed-off-by: david kruse <[email protected]>
@yurishkuro
Copy link
Member

Please make sure to run make test, several tests are currently failing with your change.

@jpkrohling
Copy link
Contributor

What's the status of this PR, other than the conflicts?

Copy link
Contributor

@jpkrohling jpkrohling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the similar PR from OpenTelemetry Collector? open-telemetry/opentelemetry-collector#2322

@@ -49,6 +49,14 @@ const (

defaultPlainTextUserName = ""
defaultPlainTextPassword = ""

// Scram configuration options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of the convention, but Go rules state that acronyms should be all in caps, so, SCRAM everywhere in the next few lines.

flagSet.String(
configPrefix+scramPrefix+suffixScramUserName,
"",
"Scram username used to authenticate with the client")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well

Algorithm string `mapstructure:"algorithm"`
}

// SetSCRAMConfiguration ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... ? :-)


var mechanism sarama.SASLMechanism

switch config.Algorithm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matter of taste I guess, but I think the version from this PR more readable: open-telemetry/opentelemetry-collector#2322

return nil
}

type scramClient struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment I had in the similar PR from OpenTelemetry: you should probably do a type assert here, ensuring that this is a sarama.SCRAMClient

}, authCfg.SCRAM)
}

// testing Begin, Step, and Done require a network connection to test,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it hard to mock this?

@Neustradamus
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kafka connection using SCRAM authentication
4 participants