-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): add authentication and authorization support
- Loading branch information
Showing
3 changed files
with
150 additions
and
76 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
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,67 @@ | ||
package kafka | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/IBM/sarama" | ||
Check failure on line 8 in plugins/extractors/kafka/kubernetes_token_provider.go GitHub Actions / test
|
||
"github.com/rs/zerolog/log" | ||
Check failure on line 9 in plugins/extractors/kafka/kubernetes_token_provider.go GitHub Actions / test
Check failure on line 9 in plugins/extractors/kafka/kubernetes_token_provider.go GitHub Actions / plugins-test (extractors/kafka)
|
||
) | ||
|
||
const ( | ||
kubernetesServiceAccountTokenPath = "/var/run/secrets/kafka/serviceaccount/token" | ||
) | ||
|
||
// NewKubernetesTokenProvider creates a new TokenProvider that reads the token from kubernetes pod service account | ||
// token file. By default, the token file path for kafka is stored in `/var/run/secrets/kafka/serviceaccount/token`. | ||
// User need to make sure there a valid projected service account token on that path. | ||
func NewKubernetesTokenProvider(opts ...TokenProviderOption) *KubernetesTokenProvider { | ||
options := &TokenProviderOptions{ | ||
FilePath: kubernetesServiceAccountTokenPath, | ||
} | ||
for _, o := range opts { | ||
o(options) | ||
} | ||
log.Info().Str("token_file_path", options.FilePath).Msg("token provider options") | ||
return &KubernetesTokenProvider{ | ||
serviceAccountFilePath: options.FilePath, | ||
} | ||
} | ||
|
||
type KubernetesTokenProvider struct { | ||
serviceAccountFilePath string | ||
} | ||
|
||
// Token returns the token from the service account token file. | ||
func (tp *KubernetesTokenProvider) Token() (*sarama.AccessToken, error) { | ||
token, err := tp.readFile() | ||
if err != nil { | ||
log.Error().Err(err).Msg("failed to read token from service account token file") | ||
return nil, err | ||
} | ||
return &sarama.AccessToken{ | ||
Token: token, | ||
}, nil | ||
} | ||
func (tp *KubernetesTokenProvider) readFile() (string, error) { | ||
token, err := os.ReadFile(tp.serviceAccountFilePath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read files: %w", err) | ||
} | ||
tkn := strings.TrimSpace(string(token)) | ||
return tkn, nil | ||
} | ||
|
||
type TokenProviderOptions struct { | ||
// FilePath is the path to the file containing the token. | ||
FilePath string | ||
} | ||
type TokenProviderOption func(*TokenProviderOptions) | ||
|
||
// WithTokenFilePath sets the file path to the token. | ||
func WithTokenFilePath(path string) TokenProviderOption { | ||
return func(o *TokenProviderOptions) { | ||
o.FilePath = path | ||
} | ||
} |