-
Notifications
You must be signed in to change notification settings - Fork 4
fix: redact credentials and AdminKey from controller logs #435
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
shreemaan-abhishek
wants to merge
1
commit into
master
Choose a base branch
from
sec/b05-credential-logging
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package client | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/go-logr/zapr" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
|
|
||
| adctypes "github.com/apache/apisix-ingress-controller/api/adc" | ||
| "github.com/apache/apisix-ingress-controller/internal/types" | ||
| ) | ||
|
|
||
| // bufferLogger builds a logger identical to the production one (zapr + zap | ||
| // console encoder) but writing into buf, so we can assert on real log output. | ||
| func bufferLogger(buf *bytes.Buffer) logr.Logger { | ||
| core := zapcore.NewCore( | ||
| zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), | ||
| zapcore.AddSync(buf), | ||
| zapcore.DebugLevel, | ||
| ) | ||
| return zapr.NewLogger(zap.New(core)) | ||
| } | ||
|
|
||
| const ( | ||
| secretTLSKey = "-----BEGIN-PRIVATE-KEY-SUPER-SECRET-----" | ||
| secretCredKey = "SUPER-SECRET-KEYAUTH-KEY" | ||
| secretAdminKey = "SUPER-SECRET-ADMINKEY" | ||
| ) | ||
|
|
||
| func secretResources() *adctypes.Resources { | ||
| return &adctypes.Resources{ | ||
| SSLs: []*adctypes.SSL{{ | ||
| Metadata: adctypes.Metadata{Name: "ssl-1"}, | ||
| Certificates: []adctypes.Certificate{{Certificate: "cert", Key: secretTLSKey}}, | ||
| }}, | ||
| Consumers: []*adctypes.Consumer{{ | ||
| Username: "alice", | ||
| Plugins: adctypes.Plugins{"key-auth": map[string]any{"key": secretCredKey}}, | ||
| }}, | ||
| } | ||
| } | ||
|
|
||
| // FINDING-016/037: logging a Task must not leak TLS private keys or consumer | ||
| // credentials, while still emitting identity for debugging. | ||
| func TestTaskMarshalLogRedactsSecrets(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| log := bufferLogger(&buf) | ||
|
|
||
| task := Task{ | ||
| Key: types.NamespacedNameKind{Namespace: "ns", Name: "route-1", Kind: "ApisixRoute"}, | ||
| Name: "ns/route-1", | ||
| Configs: map[types.NamespacedNameKind]adctypes.Config{ | ||
| {}: {Name: "gw", Token: secretAdminKey, ServerAddrs: []string{"http://x"}}, | ||
| }, | ||
| ResourceTypes: []string{"ssl", "consumer"}, | ||
| Resources: secretResources(), | ||
| } | ||
| log.Error(assert.AnError, "store insert failed", "args", task) | ||
|
|
||
| out := buf.String() | ||
| assert.NotContains(t, out, secretTLSKey, "TLS private key leaked") | ||
| assert.NotContains(t, out, secretCredKey, "consumer credential leaked") | ||
| assert.NotContains(t, out, secretAdminKey, "admin key leaked") | ||
| assert.Contains(t, out, "route-1", "identity should still be logged") | ||
| } | ||
|
|
||
| // FINDING-023: logging the ADC request body must redact the AdminKey token and | ||
| // the secret-bearing config, without altering what is sent to the server. | ||
| func TestADCServerRequestMarshalLogRedactsToken(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| log := bufferLogger(&buf) | ||
|
|
||
| reqBody := ADCServerRequest{ | ||
| Task: ADCServerTask{ | ||
| Opts: ADCServerOpts{ | ||
| Backend: "apisix", | ||
| Server: []string{"http://x"}, | ||
| Token: secretAdminKey, | ||
| CacheKey: "gw", | ||
| }, | ||
| Config: *secretResources(), | ||
| }, | ||
| } | ||
| log.V(1).Info("prepared request body", "body", reqBody) | ||
|
|
||
| out := buf.String() | ||
| assert.NotContains(t, out, secretAdminKey, "admin key leaked") | ||
| assert.NotContains(t, out, secretTLSKey, "TLS private key leaked") | ||
| assert.NotContains(t, out, secretCredKey, "consumer credential leaked") | ||
| assert.Contains(t, out, "[REDACTED]", "token should be redacted") | ||
|
|
||
| // The real wire payload must be untouched. | ||
| require.Equal(t, secretAdminKey, reqBody.Task.Opts.Token) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diffed this against the OSS half (apache/apisix-ingress-controller#2808) — identical apart from hunk offsets, so the sync side is fine.
Mechanism verified: I built the production logger (zapr + zap console encoder) into a buffer and confirmed
MarshalLogis honoured at all three sites, the token comes out[REDACTED], and the wire payload is untouched. The remaining"config", config/"configs", configssites inclient.goare already safe too —Config.MarshalJSONgets picked up by zap's reflect encoder, so the AdminKey doesn't survive there.One sharp edge on this receiver: it's on
*Resources, so aResourcesvalue doesn't satisfylogr.Marshalerand silently falls back to reflection, which dumps everything:No current call site does that (
Task.Resourcesis a pointer,ADCServerRequest.MarshalLogcalls it explicitly), so nothing broken — but a value receiver would keep it safe under refactoring, since the pointer method set includes it either way.Out of scope for this PR, but same class of leak:
translator/httproute.gostill logs the decoded plugin map from a PluginConfig extensionRef at V(1), andtranslator/gateway.godoes the same for GatewayProxy plugins plus the raw plugin_metadata blob on unmarshal error. Worth a follow-up?