Skip to content

Add tokenpath parameter to collector #1077

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

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ func processOtlpReceivers(tlsConfig *OtlpTLSConfig) error {
func resolveExtensions() Extensions {
var health *Health
var headersSetter *HeadersSetter
var tokenValue string

if isHealthExtensionSet() {
health = &Health{
Expand Down Expand Up @@ -899,6 +900,19 @@ func resolveExtensions() Extensions {
}
}

if headersSetter != nil {
tokenValue = headersSetter.Headers[0].Value
}

_, err := os.Stat(tokenValue)
if err == nil {
token, fileErr := file.ReadFromFile(tokenValue)
if fileErr != nil {
slog.Error("error reading from file", "error", fileErr)
}
headersSetter.Headers[0].Value = token
}

return Extensions{
Health: health,
HeadersSetter: headersSetter,
Expand Down
57 changes: 57 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package config

import (
_ "embed"
"errors"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -631,6 +633,61 @@ func TestValidateYamlFile(t *testing.T) {
}
}

//go:embed testdata/nginx-agent-with-token.conf
var agentConfigWithToken string

func getAgentConfigWithToken(token string) string {
return fmt.Sprintf(agentConfigWithToken, token)
}

func TestResolveExtensions(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Test 1: Header value is a valid token",
input: "super-secret-token",
expected: "super-secret-token",
},
{
name: "Test 2: Header value is a valid token path",
input: "testdata/nginx-token.crt",
expected: "super-secret-token",
},
{
name: "Test 3: Header value is empty",
input: "",
expected: "",
},
}

viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
tempDir := t.TempDir()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx-agent.conf")
defer helpers.RemoveFileWithErrorCheck(t, tempFile.Name())

confContent := []byte(getAgentConfigWithToken(tt.input))
_, writeErr := tempFile.Write(confContent)
require.NoError(t, writeErr)

err := loadPropertiesFromFile(tempFile.Name())
require.NoError(t, err)

extension := resolveExtensions()
require.NotNil(t, extension)
assert.Equal(t, tt.expected, extension.HeadersSetter.Headers[0].Value)

err = tempFile.Close()
require.NoError(t, err)
})
}
}

func getAgentConfig() *Config {
return &Config{
UUID: "",
Expand Down
10 changes: 10 additions & 0 deletions internal/config/testdata/nginx-agent-with-token.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
log:
level: info

collector:
extensions:
headers_setter:
headers:
- action: insert
key: "authorization"
value: %s
1 change: 1 addition & 0 deletions internal/config/testdata/nginx-token.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
super-secret-token
1 change: 0 additions & 1 deletion nginx-agent.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ allowed_directories:
# token: ""
# tls:
# skip_verify: false