|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package client |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/go-logr/zapr" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "go.uber.org/zap" |
| 29 | + "go.uber.org/zap/zapcore" |
| 30 | + |
| 31 | + adctypes "github.com/apache/apisix-ingress-controller/api/adc" |
| 32 | + "github.com/apache/apisix-ingress-controller/internal/types" |
| 33 | +) |
| 34 | + |
| 35 | +// bufferLogger builds a logger identical to the production one (zapr + zap |
| 36 | +// console encoder) but writing into buf, so we can assert on real log output. |
| 37 | +func bufferLogger(buf *bytes.Buffer) logr.Logger { |
| 38 | + core := zapcore.NewCore( |
| 39 | + zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), |
| 40 | + zapcore.AddSync(buf), |
| 41 | + zapcore.DebugLevel, |
| 42 | + ) |
| 43 | + return zapr.NewLogger(zap.New(core)) |
| 44 | +} |
| 45 | + |
| 46 | +const ( |
| 47 | + secretTLSKey = "-----BEGIN-PRIVATE-KEY-SUPER-SECRET-----" |
| 48 | + secretCredKey = "SUPER-SECRET-KEYAUTH-KEY" |
| 49 | + secretAdminKey = "SUPER-SECRET-ADMINKEY" |
| 50 | +) |
| 51 | + |
| 52 | +func secretResources() *adctypes.Resources { |
| 53 | + return &adctypes.Resources{ |
| 54 | + SSLs: []*adctypes.SSL{{ |
| 55 | + Metadata: adctypes.Metadata{Name: "ssl-1"}, |
| 56 | + Certificates: []adctypes.Certificate{{Certificate: "cert", Key: secretTLSKey}}, |
| 57 | + }}, |
| 58 | + Consumers: []*adctypes.Consumer{{ |
| 59 | + Username: "alice", |
| 60 | + Plugins: adctypes.Plugins{"key-auth": map[string]any{"key": secretCredKey}}, |
| 61 | + }}, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// FINDING-016/037: logging a Task must not leak TLS private keys or consumer |
| 66 | +// credentials, while still emitting identity for debugging. |
| 67 | +func TestTaskMarshalLogRedactsSecrets(t *testing.T) { |
| 68 | + var buf bytes.Buffer |
| 69 | + log := bufferLogger(&buf) |
| 70 | + |
| 71 | + task := Task{ |
| 72 | + Key: types.NamespacedNameKind{Namespace: "ns", Name: "route-1", Kind: "ApisixRoute"}, |
| 73 | + Name: "ns/route-1", |
| 74 | + Configs: map[types.NamespacedNameKind]adctypes.Config{ |
| 75 | + {}: {Name: "gw", Token: secretAdminKey, ServerAddrs: []string{"http://x"}}, |
| 76 | + }, |
| 77 | + ResourceTypes: []string{"ssl", "consumer"}, |
| 78 | + Resources: secretResources(), |
| 79 | + } |
| 80 | + log.Error(assert.AnError, "store insert failed", "args", task) |
| 81 | + |
| 82 | + out := buf.String() |
| 83 | + assert.NotContains(t, out, secretTLSKey, "TLS private key leaked") |
| 84 | + assert.NotContains(t, out, secretCredKey, "consumer credential leaked") |
| 85 | + assert.NotContains(t, out, secretAdminKey, "admin key leaked") |
| 86 | + assert.Contains(t, out, "route-1", "identity should still be logged") |
| 87 | +} |
| 88 | + |
| 89 | +// FINDING-023: logging the ADC request body must redact the AdminKey token and |
| 90 | +// the secret-bearing config, without altering what is sent to the server. |
| 91 | +func TestADCServerRequestMarshalLogRedactsToken(t *testing.T) { |
| 92 | + var buf bytes.Buffer |
| 93 | + log := bufferLogger(&buf) |
| 94 | + |
| 95 | + reqBody := ADCServerRequest{ |
| 96 | + Task: ADCServerTask{ |
| 97 | + Opts: ADCServerOpts{ |
| 98 | + Backend: "apisix", |
| 99 | + Server: []string{"http://x"}, |
| 100 | + Token: secretAdminKey, |
| 101 | + CacheKey: "gw", |
| 102 | + }, |
| 103 | + Config: *secretResources(), |
| 104 | + }, |
| 105 | + } |
| 106 | + log.V(1).Info("prepared request body", "body", reqBody) |
| 107 | + |
| 108 | + out := buf.String() |
| 109 | + assert.NotContains(t, out, secretAdminKey, "admin key leaked") |
| 110 | + assert.NotContains(t, out, secretTLSKey, "TLS private key leaked") |
| 111 | + assert.NotContains(t, out, secretCredKey, "consumer credential leaked") |
| 112 | + assert.Contains(t, out, "[REDACTED]", "token should be redacted") |
| 113 | + |
| 114 | + // The real wire payload must be untouched. |
| 115 | + require.Equal(t, secretAdminKey, reqBody.Task.Opts.Token) |
| 116 | +} |
0 commit comments