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

Redact static tokens and custom http headers #4182

Merged
merged 6 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
35 changes: 35 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package config
import (
"context"
"errors"
"strings"
"sync"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -168,6 +169,29 @@ func redactOutput(cfg *Config) Output {
redacted.Elasticsearch.TLS = &newTLS
}

if redacted.Elasticsearch.Headers != nil {
headers := make(map[string]string)
for k, v := range redacted.Elasticsearch.Headers {
headers[k] = v
lk := strings.ToLower(k)
if strings.Contains(lk, "auth") || strings.Contains(lk, "token") || strings.Contains(lk, "key") || strings.Contains(lk, "bearer") { // best-effort scan to redact sensitive headers
headers[k] = kRedacted
}
}
redacted.Elasticsearch.Headers = headers
}

if redacted.Elasticsearch.ProxyHeaders != nil {
proxyHeaders := make(map[string]string)
for k, v := range redacted.Elasticsearch.ProxyHeaders {
proxyHeaders[k] = v
lk := strings.ToLower(k)
if strings.Contains(lk, "auth") || strings.Contains(lk, "token") || strings.Contains(lk, "key") || strings.Contains(lk, "bearer") { // best-effort scan to redact sensitive headers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a function just to prevent people from forgetting to update both places.

We could also one day split out a lot of our best effort redaction logic into a reusable package.

proxyHeaders[k] = kRedacted
}
}
redacted.Elasticsearch.ProxyHeaders = proxyHeaders
}
return redacted
}

Expand Down Expand Up @@ -195,6 +219,17 @@ func redactServer(cfg *Config) Server {
redacted.Instrumentation.SecretToken = kRedacted
}

if redacted.StaticPolicyTokens.PolicyTokens != nil {
policyTokens := make([]PolicyToken, len(redacted.StaticPolicyTokens.PolicyTokens))
for i := range redacted.StaticPolicyTokens.PolicyTokens {
policyTokens[i] = PolicyToken{
TokenKey: kRedacted,
PolicyID: redacted.StaticPolicyTokens.PolicyTokens[i].PolicyID,
}
}
redacted.StaticPolicyTokens.PolicyTokens = policyTokens
}

return redacted
}

Expand Down
79 changes: 79 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,85 @@ func TestConfigRedact(t *testing.T) {
},
},
},
{
name: "Redact custom output headers",
inputCfg: &Config{
Inputs: []Input{{}},
Output: Output{
Elasticsearch: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
Headers: map[string]string{"X-Authorization": "secretValue", "X-Custom": "value", "X-App-Token": "customToken", "X-App-Key": "secretKey", "X-Custom-Bearer": "secretBearer"},
ServiceTokenPath: "path/to/file",
},
},
},
redactedCfg: &Config{
Inputs: []Input{{}},
Output: Output{
Elasticsearch: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
Headers: map[string]string{"X-Authorization": kRedacted, "X-Custom": "value", "X-App-Token": kRedacted, "X-App-Key": kRedacted, "X-Custom-Bearer": kRedacted},
ServiceTokenPath: "path/to/file",
},
},
},
},
{
name: "Redact proxy authorization output header",
inputCfg: &Config{
Inputs: []Input{{}},
Output: Output{
Elasticsearch: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
ProxyHeaders: map[string]string{"X-Proxy-Authorization": "secretValue"},
ServiceTokenPath: "path/to/file",
},
},
},
redactedCfg: &Config{
Inputs: []Input{{}},
Output: Output{
Elasticsearch: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
ProxyHeaders: map[string]string{"X-Proxy-Authorization": kRedacted},
ServiceTokenPath: "path/to/file",
},
},
},
},
{
name: "redact static tokens",
inputCfg: &Config{
Inputs: []Input{{
Server: Server{
StaticPolicyTokens: StaticPolicyTokens{
Enabled: true,
PolicyTokens: []PolicyToken{{
TokenKey: "secretValue",
PolicyID: "testPolicy",
}},
},
},
}},
},
redactedCfg: &Config{
Inputs: []Input{{
Server: Server{
StaticPolicyTokens: StaticPolicyTokens{
Enabled: true,
PolicyTokens: []PolicyToken{{
TokenKey: kRedacted,
PolicyID: "testPolicy",
}},
},
},
}},
},
},
}

for _, tt := range testcases {
Expand Down
Loading