Skip to content

Commit 06917ba

Browse files
Clee2691openshift-merge-bot[bot]
authored andcommitted
LOG-5991: Setting authentication to token for ES output causes invalid configuration error
1 parent dbeb897 commit 06917ba

File tree

10 files changed

+515
-102
lines changed

10 files changed

+515
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package elasticsearch
2+
3+
import (
4+
obs "github.com/openshift/cluster-logging-operator/api/observability/v1"
5+
"github.com/openshift/cluster-logging-operator/internal/api/observability"
6+
"github.com/openshift/cluster-logging-operator/internal/constants"
7+
"github.com/openshift/cluster-logging-operator/internal/generator/framework"
8+
"github.com/openshift/cluster-logging-operator/internal/generator/vector/helpers"
9+
"github.com/openshift/cluster-logging-operator/internal/utils"
10+
)
11+
12+
type BearerToken struct {
13+
ID string
14+
Token string
15+
}
16+
17+
func (bt BearerToken) Name() string {
18+
return "esBearerTokenTemplate"
19+
}
20+
21+
func (bt BearerToken) Template() string {
22+
return `{{define "` + bt.Name() + `" -}}
23+
[sinks.{{.ID}}.request.headers]
24+
Authorization = "Bearer {{.Token}}"
25+
{{end}}
26+
`
27+
}
28+
29+
func NewBearerToken(id string, spec *obs.HTTPAuthentication, secrets observability.Secrets, op framework.Options) BearerToken {
30+
bt := BearerToken{}
31+
if spec != nil {
32+
key := spec.Token
33+
bt.ID = id
34+
switch key.From {
35+
case obs.BearerTokenFromSecret:
36+
if key.Secret != nil {
37+
bt.Token = helpers.SecretFrom(&obs.SecretReference{
38+
SecretName: key.Secret.Name,
39+
Key: key.Secret.Key,
40+
})
41+
}
42+
case obs.BearerTokenFromServiceAccount:
43+
if name, found := utils.GetOption(op, framework.OptionServiceAccountTokenSecretName, ""); found {
44+
bt.Token = helpers.SecretFrom(&obs.SecretReference{
45+
Key: constants.TokenKey,
46+
SecretName: name,
47+
})
48+
}
49+
}
50+
}
51+
return bt
52+
}

internal/generator/vector/output/elasticsearch/elasticsearch.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ if exists(.kubernetes.event.metadata.uid) {
8383
common.NewBuffer(id, strategy),
8484
common.NewRequest(id, strategy),
8585
tls.New(id, o.TLS, secrets, op, Option{Name: URL, Value: o.Elasticsearch.URL}),
86-
auth.HTTPAuth(id, o.Elasticsearch.Authentication, secrets, op),
8786
)
8887

88+
if o.Elasticsearch.Authentication != nil && o.Elasticsearch.Authentication.Token != nil {
89+
outputs = append(outputs, NewBearerToken(id, o.Elasticsearch.Authentication, secrets, op))
90+
} else if o.Elasticsearch.Authentication != nil && o.Elasticsearch.Authentication.Username != nil && o.Elasticsearch.Authentication.Password != nil {
91+
outputs = append(outputs, auth.NewBasic(id, o.Elasticsearch.Authentication, secrets))
92+
}
93+
8994
return outputs
9095
}
9196

internal/generator/vector/output/elasticsearch/elasticsearch_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var _ = Describe("Generate Vector config", func() {
2222
secretName = "es-1"
2323
aUserName = "testuser"
2424
aPassword = "testpass"
25+
aToken = "my-token"
2526
)
2627
var (
2728
adapter fake.Output
@@ -71,6 +72,7 @@ var _ = Describe("Generate Vector config", func() {
7172
Data: map[string][]byte{
7273
constants.ClientUsername: []byte(aUserName),
7374
constants.ClientPassword: []byte(aPassword),
75+
constants.TokenKey: []byte(aToken),
7476
},
7577
},
7678
}
@@ -98,6 +100,26 @@ var _ = Describe("Generate Vector config", func() {
98100
Expect(string(exp)).To(EqualConfigFrom(conf))
99101
},
100102
Entry("with username,password", nil, false, framework.NoOptions, "es_with_auth_username_password.toml"),
103+
Entry("with custom bearer token", func(spec *obs.OutputSpec) {
104+
spec.Elasticsearch.Authentication = &obs.HTTPAuthentication{
105+
Token: &obs.BearerToken{
106+
From: obs.BearerTokenFromSecret,
107+
Secret: &obs.BearerTokenSecretKey{
108+
Name: secretName,
109+
Key: constants.TokenKey,
110+
},
111+
},
112+
}
113+
}, false, framework.NoOptions, "es_with_auth_custom_bearer_token.toml"),
114+
Entry("with serviceaccount token", func(spec *obs.OutputSpec) {
115+
spec.Elasticsearch.Authentication = &obs.HTTPAuthentication{
116+
Token: &obs.BearerToken{
117+
From: obs.BearerTokenFromServiceAccount,
118+
},
119+
}
120+
}, false, framework.Options{
121+
framework.OptionServiceAccountTokenSecretName: "my-service-account-token",
122+
}, "es_with_auth_serviceaccount_token.toml"),
101123
Entry("with tls key,cert,ca-bundle", func(spec *obs.OutputSpec) {
102124
spec.Elasticsearch.Authentication = nil
103125
spec.TLS = tlsSpec
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Elasticsearch Index
2+
[transforms.es_1_index]
3+
type = "remap"
4+
inputs = ["application"]
5+
source = '''
6+
._internal.es_1_index = to_string!(._internal.log_type||"none")
7+
'''
8+
9+
[sinks.es_1]
10+
type = "elasticsearch"
11+
inputs = ["es_1_index"]
12+
endpoints = ["https://es.svc.infra.cluster:9200"]
13+
bulk.index = "{{ _internal.es_1_index }}"
14+
bulk.action = "create"
15+
api_version = "v8"
16+
17+
[sinks.es_1.encoding]
18+
except_fields = ["_internal"]
19+
20+
[sinks.es_1.request.headers]
21+
Authorization = "Bearer SECRET[kubernetes_secret.es-1/token]"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Elasticsearch Index
2+
[transforms.es_1_index]
3+
type = "remap"
4+
inputs = ["application"]
5+
source = '''
6+
._internal.es_1_index = to_string!(._internal.log_type||"none")
7+
'''
8+
9+
[sinks.es_1]
10+
type = "elasticsearch"
11+
inputs = ["es_1_index"]
12+
endpoints = ["https://es.svc.infra.cluster:9200"]
13+
bulk.index = "{{ _internal.es_1_index }}"
14+
bulk.action = "create"
15+
api_version = "v8"
16+
17+
[sinks.es_1.encoding]
18+
except_fields = ["_internal"]
19+
20+
[sinks.es_1.request.headers]
21+
Authorization = "Bearer SECRET[kubernetes_secret.my-service-account-token/token]"

test/framework/functional/elasticsearch.go

-87
This file was deleted.

test/framework/functional/framework.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package functional
33
import (
44
"context"
55
"fmt"
6-
"github.com/openshift/cluster-logging-operator/internal/api/initialize"
76
"net"
87
"os"
98
"strconv"
109
"strings"
1110
"time"
1211

12+
"github.com/openshift/cluster-logging-operator/internal/api/initialize"
13+
1314
obs "github.com/openshift/cluster-logging-operator/api/observability/v1"
1415

1516
internalobs "github.com/openshift/cluster-logging-operator/internal/api/observability"
@@ -156,6 +157,13 @@ func (f *CollectorFunctionalFramework) RunCommand(container string, cmd ...strin
156157
return out, err
157158
}
158159

160+
func (f *CollectorFunctionalFramework) RunCommandInPod(pod *corev1.Pod, container string, cmd ...string) (string, error) {
161+
log.V(2).Info("Running", "container", container, "cmd", cmd)
162+
out, err := testruntime.ExecOc(pod, strings.ToLower(container), cmd[0], cmd[1:]...)
163+
log.V(2).Info("Exec'd", "out", out, "err", err)
164+
return out, err
165+
}
166+
159167
func (f *CollectorFunctionalFramework) AddOutputContainersVisitors() []runtime.PodBuilderVisitor {
160168
visitors := []runtime.PodBuilderVisitor{
161169
func(b *runtime.PodBuilder) error {
@@ -274,8 +282,10 @@ func (f *CollectorFunctionalFramework) DeployWithVisitors(visitors []runtime.Pod
274282
WithImagePullPolicy(corev1.PullAlways).ResourceRequirements(resources), FunctionalNodeName).
275283
End()
276284
for _, visit := range visitors {
277-
if err = visit(b); err != nil {
278-
return err
285+
if visit != nil {
286+
if err = visit(b); err != nil {
287+
return err
288+
}
279289
}
280290
}
281291

@@ -417,7 +427,7 @@ func (f *CollectorFunctionalFramework) addOutputContainers(b *runtime.PodBuilder
417427
return err
418428
}
419429
case obs.OutputTypeElasticsearch:
420-
if err := f.AddES7Output(b, output); err != nil {
430+
if err := f.AddESOutput(ElasticsearchVersion(output.Elasticsearch.Version), b, output, nil); err != nil {
421431
return err
422432
}
423433
case obs.OutputTypeHTTP:

0 commit comments

Comments
 (0)