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

Feat/add elastic search api key authentication #379

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charts/policy-reporter/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ elasticsearch:
skipTLS: {{ .Values.target.elasticsearch.skipTLS }}
username: {{ .Values.target.elasticsearch.username | quote }}
password: {{ .Values.target.elasticsearch.password | quote }}
apiKey: {{ .Values.target.elasticsearch.password | quote }}
secretRef: {{ .Values.target.elasticsearch.secretRef | quote }}
mountedSecret: {{ .Values.target.elasticsearch.mountedSecret | quote }}
index: {{ .Values.target.elasticsearch.index | default "policy-reporter" | quote }}
Expand Down
4 changes: 3 additions & 1 deletion charts/policy-reporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ target:
username: ""
# elasticsearch password für HTTP Basic Auth
password: ""
# receive the host, username and/or password from an existing secret instead
# elasticsearch apiKey für apiKey authentication
apiKey: ""
# receive the host, username and/or password,apiKey from an existing secret instead
secretRef: ""
# Mounted secret path by Secrets Controller, secret should be in json format
mountedSecret: ""
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Elasticsearch struct {
Rotation string `mapstructure:"rotation"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
ApiKey string `mapstructure:"apiKey"`
Channels []*Elasticsearch `mapstructure:"channels"`
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func (f *TargetFactory) createElasticsearchClient(config, parent *Elasticsearch)
setBool(&config.SkipTLS, parent.SkipTLS)
setFallback(&config.Username, parent.Username)
setFallback(&config.Password, parent.Password)
setFallback(&config.ApiKey, parent.ApiKey)
setFallback(&config.Index, parent.Index, "policy-reporter")
setFallback(&config.Rotation, parent.Rotation, elasticsearch.Daily)

Expand All @@ -420,6 +421,7 @@ func (f *TargetFactory) createElasticsearchClient(config, parent *Elasticsearch)
Host: config.Host,
Username: config.Username,
Password: config.Password,
ApiKey: config.ApiKey,
Rotation: config.Rotation,
Index: config.Index,
CustomFields: config.CustomFields,
Expand Down Expand Up @@ -822,6 +824,9 @@ func (f *TargetFactory) mapSecretValues(config any, ref, mountedSecret string) {
if values.Password != "" {
c.Password = values.Password
}
if values.ApiKey != "" {
c.ApiKey = values.ApiKey
}

case *S3:
if values.AccessKeyID != "" {
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/target_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newFakeClient() v1.SecretInterface {
"host": []byte("http://localhost:9200"),
"username": []byte("username"),
"password": []byte("password"),
"apiKey": []byte("apiKey"),
"webhook": []byte("http://localhost:9200/webhook"),
"accessKeyID": []byte("accessKeyID"),
"secretAccessKey": []byte("secretAccessKey"),
Expand All @@ -49,6 +50,7 @@ func mountSecret() {
Webhook: "http://localhost:9200/webhook",
Username: "username",
Password: "password",
ApiKey: "apiKey",
AccessKeyID: "accessKeyId",
SecretAccessKey: "secretAccessKey",
KmsKeyID: "kmsKeyId",
Expand Down Expand Up @@ -332,6 +334,11 @@ func Test_GetValuesFromSecret(t *testing.T) {
if password != "password" {
t.Errorf("Expected password from secret, got %s", password)
}

apiKey := client.FieldByName("apiKey").String()
if apiKey != "apiKey" {
t.Errorf("Expected apiKey from secret, got %s", apiKey)
}
})

t.Run("Get Discord values from Secret", func(t *testing.T) {
Expand Down Expand Up @@ -639,6 +646,11 @@ func Test_GetValuesFromMountedSecret(t *testing.T) {
if password != "password" {
t.Errorf("Expected password from mounted secret, got %s", password)
}

apiKey := client.FieldByName("apiKey").String()
if apiKey != "apiKey" {
t.Errorf("Expected apiKey from secret, got %s", apiKey)
}
})

t.Run("Get Discord values from MountedSecret", func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubernetes/secrets/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Values struct {
Channel string `json:"channel,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
ApiKey string `json:"apiKey,omitempty"`
AccessKeyID string `json:"accessKeyID,omitempty"`
SecretAccessKey string `json:"secretAccessKey,omitempty"`
AccountID string `json:"accountID,omitempty"`
Expand Down Expand Up @@ -87,6 +88,10 @@ func (c *k8sClient) Get(ctx context.Context, name string) (Values, error) {
values.Password = string(password)
}

if apiKey, ok := secret.Data["apiKey"]; ok {
values.ApiKey = string(apiKey)
}

if database, ok := secret.Data["database"]; ok {
values.Database = string(database)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/kubernetes/secrets/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func newFakeClient() v1.SecretInterface {
"host": []byte("http://localhost:9200"),
"username": []byte("username"),
"password": []byte("password"),
"apiKey": []byte("apiKey"),
"webhook": []byte("http://localhost:9200/webhook"),
"accessKeyID": []byte("accessKeyID"),
"secretAccessKey": []byte("secretAccessKey"),
Expand Down Expand Up @@ -62,6 +63,10 @@ func Test_Client(t *testing.T) {
t.Errorf("Unexpected Password: %s", values.Password)
}

if values.ApiKey != "apiKey" {
t.Errorf("Unexpected ApiKey: %s", values.ApiKey)
}

if values.AccessKeyID != "accessKeyID" {
t.Errorf("Unexpected AccessKeyID: %s", values.AccessKeyID)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/target/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Options struct {
Host string
Username string
Password string
ApiKey string
Index string
Rotation string
CustomFields map[string]string
Expand All @@ -37,6 +38,7 @@ type client struct {
index string
username string
password string
apiKey string
rotation Rotation
customFields map[string]string
client http.Client
Expand Down Expand Up @@ -76,6 +78,8 @@ func (e *client) Send(result v1alpha2.PolicyReportResult) {

if e.username != "" {
req.SetBasicAuth(e.username, e.password)
} else if e.apiKey != "" {
req.Header.Add("Authorization", "ApiKey "+e.apiKey)
}

resp, err := e.client.Do(req)
Expand All @@ -90,6 +94,7 @@ func NewClient(options Options) target.Client {
options.Index,
options.Username,
options.Password,
options.ApiKey,
options.Rotation,
options.CustomFields,
options.HTTPClient,
Expand Down
1 change: 1 addition & 0 deletions pkg/target/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Test_ElasticsearchTarget(t *testing.T) {
Host: "http://localhost:9200",
Username: "username",
Password: "password",
ApiKey: "ApiKey",
Index: "policy-reporter",
Rotation: elasticsearch.Annually,
HTTPClient: testClient{callback, 200},
Expand Down
Loading