Skip to content

Commit

Permalink
send service accounts auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Jul 25, 2022
1 parent c0cb1c0 commit 65b70e4
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 40 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/flowcollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ type FlowCollectorLoki struct {
// it will be ignored if instanceSpec is specified
TenantID string `json:"tenantID,omitempty"`

//+kubebuilder:default:=false
// SendAuthToken is a flag to enable or disable Authorization header from service account secret
// It allows authentication to loki operator gateway
SendAuthToken bool `json:"sendAuthToken,omitempty"`

//+kubebuilder:default:="1s"
// BatchWait is max time to wait before sending a batch
BatchWait metav1.Duration `json:"batchWait,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/flows.netobserv.io_flowcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,12 @@ spec:
If empty, the URL value will be used (assuming that the Loki
ingester and querier are int he same host).
type: string
sendAuthToken:
default: false
description: SendAuthToken is a flag to enable or disable Authorization
header from service account secret It allows authentication
to loki operator gateway
type: boolean
staticLabels:
additionalProperties:
type: string
Expand Down
93 changes: 53 additions & 40 deletions controllers/consoleplugin/consoleplugin_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const configMapName = "console-plugin-config"
const configFile = "config.yaml"
const configVolume = "config-volume"
const configPath = "/opt/app-root/"
const tokensPath = "/var/run/secrets/tokens/"

// PodConfigurationDigest is an annotation name to facilitate pod restart after
// any external configuration change
Expand Down Expand Up @@ -97,21 +98,69 @@ func (b *builder) deployment(cmDigest string) *appsv1.Deployment {
}
}

func tokenPath(desiredLoki *flowsv1alpha1.FlowCollectorLoki) string {
if desiredLoki.SendAuthToken {
return tokensPath + constants.PluginName
}
return ""
}

func buildArgs(desired *flowsv1alpha1.FlowCollectorConsolePlugin, desiredLoki *flowsv1alpha1.FlowCollectorLoki) []string {
return []string{
args := []string{
"-cert", "/var/serving-cert/tls.crt",
"-key", "/var/serving-cert/tls.key",
"-loki", querierURL(desiredLoki),
"-loki-labels", strings.Join(constants.LokiIndexFields, ","),
"-loki-tenant-id", desiredLoki.TenantID,
"-loki-token-path", tokenPath(desiredLoki),
//TODO: add loki tls config https://issues.redhat.com/browse/NETOBSERV-309
"-loki-skip-tls", "true",
"-loglevel", desired.LogLevel,
"-frontend-config", configPath + configFile,
}

return args
}

func (b *builder) podTemplate(cmDigest string) *corev1.PodTemplateSpec {
volumes := []corev1.Volume{
{
Name: secretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretName,
},
},
},
{
Name: configVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapName,
},
},
},
},
}

volumeMounts := []corev1.VolumeMount{
{
Name: secretName,
MountPath: "/var/serving-cert",
ReadOnly: true,
},
{
Name: configVolume,
MountPath: configPath,
ReadOnly: true,
},
}

if b.desiredLoki.SendAuthToken {
volumes, volumeMounts = helper.AppendTokenVolume(volumes, volumeMounts, constants.PluginName, constants.PluginName)
}

return &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.labels,
Expand All @@ -125,46 +174,10 @@ func (b *builder) podTemplate(cmDigest string) *corev1.PodTemplateSpec {
Image: b.desired.Image,
ImagePullPolicy: corev1.PullPolicy(b.desired.ImagePullPolicy),
Resources: *b.desired.Resources.DeepCopy(),
VolumeMounts: []corev1.VolumeMount{{
Name: secretName,
MountPath: "/var/serving-cert",
ReadOnly: true,
},
{
Name: configVolume,
MountPath: configPath,
ReadOnly: true,
}},
Args: []string{
"-cert", "/var/serving-cert/tls.crt",
"-key", "/var/serving-cert/tls.key",
"-loki", querierURL(b.desiredLoki),
"-loki-labels", strings.Join(constants.LokiIndexFields, ","),
"-loki-tenant-id", b.desiredLoki.TenantID,
//TODO: add loki tls config https://issues.redhat.com/browse/NETOBSERV-309
"-loki-skip-tls", "true",
"-loglevel", b.desired.LogLevel,
"-frontend-config", configPath + configFile,
},
VolumeMounts: volumeMounts,
Args: buildArgs(b.desired, b.desiredLoki),
}},
Volumes: []corev1.Volume{{
Name: secretName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretName,
},
},
}, {
Name: configVolume,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapName,
},
},
},
},
},
Volumes: volumes,
ServiceAccountName: constants.PluginName,
},
}
Expand Down
1 change: 1 addition & 0 deletions controllers/consoleplugin/consoleplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var testArgs = []string{
"-loki", "http://loki:3100/",
"-loki-labels", "SrcK8S_Namespace,SrcK8S_OwnerName,DstK8S_Namespace,DstK8S_OwnerName,FlowDirection",
"-loki-tenant-id", "netobserv",
"-loki-token-path", "",
"-loki-skip-tls", "true",
"-loglevel", "info",
"-frontend-config", "/opt/app-root/config.yaml",
Expand Down
12 changes: 12 additions & 0 deletions controllers/flowlogspipeline/flp_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (b *builder) podTemplate(hostNetwork bool, configDigest string) corev1.PodT
volumes, volumeMounts = helper.AppendCertVolumes(volumes, volumeMounts, &b.desiredKafka.TLS, kafkaCerts)
}

if b.desiredLoki.SendAuthToken {
volumes, volumeMounts = helper.AppendTokenVolume(volumes, volumeMounts, constants.FLPName+b.confKindSuffix, constants.FLPName)
}

container := corev1.Container{
Name: constants.FLPName + b.confKindSuffix,
Image: b.desired.Image,
Expand Down Expand Up @@ -255,8 +259,16 @@ func (b *builder) addTransformStages(lastStage *config.PipelineBuilderStage) {
lokiWrite.TimestampLabel = "TimeFlowEndMs"
lokiWrite.TimestampScale = "1ms"
lokiWrite.TenantID = b.desiredLoki.TenantID
var authorization *promConfig.Authorization
if b.desiredLoki.SendAuthToken {
authorization = &promConfig.Authorization{
Type: "Bearer",
CredentialsFile: helper.TokensPath + constants.FLPName,
}
}
//TODO: set proper tls config https://issues.redhat.com/browse/NETOBSERV-309
lokiWrite.ClientConfig = &promConfig.HTTPClientConfig{
Authorization: authorization,
TLSConfig: promConfig.TLSConfig{
InsecureSkipVerify: true,
},
Expand Down
9 changes: 9 additions & 0 deletions docs/FlowCollector.md
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,15 @@ Settings related to the Loki client, used as a flow store.
QuerierURL specifies the address of the Loki querier service, in case it is different from the Loki ingester URL. If empty, the URL value will be used (assuming that the Loki ingester and querier are int he same host).<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>sendAuthToken</b></td>
<td>boolean</td>
<td>
SendAuthToken is a flag to enable or disable Authorization header from service account secret It allows authentication to loki operator gateway<br/>
<br/>
<i>Default</i>: false<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>staticLabels</b></td>
<td>map[string]string</td>
Expand Down
33 changes: 33 additions & 0 deletions pkg/helper/tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package helper

import (
corev1 "k8s.io/api/core/v1"
)

const TokensPath = "/var/run/secrets/tokens/"

// AppendTokenVolume will add a volume + volume mount for a service account token if defined
func AppendTokenVolume(volumes []corev1.Volume, volumeMounts []corev1.VolumeMount, name string, fileName string) ([]corev1.Volume, []corev1.VolumeMount) {
volOut := append(volumes,
corev1.Volume{
Name: name,
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{
{
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
Path: fileName,
},
},
},
},
},
})
vmOut := append(volumeMounts,
corev1.VolumeMount{
MountPath: TokensPath,
Name: name,
},
)
return volOut, vmOut
}

0 comments on commit 65b70e4

Please sign in to comment.