|
| 1 | +package data_masks |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "regexp" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/akitasoftware/akita-ir/go/api_spec" |
| 9 | + "github.com/akitasoftware/akita-libs/akid" |
| 10 | + kgxapi "github.com/akitasoftware/akita-libs/api_schema" |
| 11 | + "github.com/akitasoftware/akita-libs/test" |
| 12 | + "github.com/akitasoftware/go-utils/optionals" |
| 13 | + "github.com/golang/mock/gomock" |
| 14 | + "github.com/golang/protobuf/proto" |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 17 | + mockrest "github.com/postmanlabs/postman-insights-agent/rest/mock" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | +) |
| 20 | + |
| 21 | +var cmpOptions = []cmp.Option{ |
| 22 | + cmp.Comparer(proto.Equal), |
| 23 | + cmpopts.SortSlices(sortMethod), |
| 24 | + cmpopts.SortSlices(sortWitness), |
| 25 | +} |
| 26 | + |
| 27 | +func sortMethod(m1, m2 *api_spec.Method) bool { |
| 28 | + return proto.MarshalTextString(m1) < proto.MarshalTextString(m2) |
| 29 | +} |
| 30 | + |
| 31 | +func sortWitness(m1, m2 *api_spec.Witness) bool { |
| 32 | + return proto.MarshalTextString(m1) < proto.MarshalTextString(m2) |
| 33 | +} |
| 34 | + |
| 35 | +func TestRedaction(t *testing.T) { |
| 36 | + testCases := map[string]struct { |
| 37 | + agentConfig optionals.Optional[*kgxapi.FieldRedactionConfig] |
| 38 | + inputFile string |
| 39 | + expectedFile string |
| 40 | + }{ |
| 41 | + // Expect values with 16-character identifiers to remain unchanged. |
| 42 | + "16-character identifier": { |
| 43 | + inputFile: "002-witness.pb.txt", |
| 44 | + expectedFile: "002-witness.pb.txt", |
| 45 | + }, |
| 46 | + |
| 47 | + "default redaction rules": { |
| 48 | + inputFile: "003-witness.pb.txt", |
| 49 | + expectedFile: "003-expected-default-redaction.pb.txt", |
| 50 | + }, |
| 51 | + |
| 52 | + "agent config: redact by name": { |
| 53 | + agentConfig: optionals.Some(&kgxapi.FieldRedactionConfig{ |
| 54 | + FieldNames: []string{"by-name"}, |
| 55 | + }), |
| 56 | + inputFile: "003-witness.pb.txt", |
| 57 | + expectedFile: "003-expected-redact-by-name.pb.txt", |
| 58 | + }, |
| 59 | + |
| 60 | + "agent config: redact by name regexp": { |
| 61 | + agentConfig: optionals.Some(&kgxapi.FieldRedactionConfig{ |
| 62 | + FieldNameRegexps: []*regexp.Regexp{regexp.MustCompile("nam.*xp$")}, |
| 63 | + }), |
| 64 | + inputFile: "003-witness.pb.txt", |
| 65 | + expectedFile: "003-expected-redact-by-name-regexp.pb.txt", |
| 66 | + }, |
| 67 | + |
| 68 | + "agent config: redact by name and by name regexp": { |
| 69 | + agentConfig: optionals.Some(&kgxapi.FieldRedactionConfig{ |
| 70 | + FieldNames: []string{"by-name"}, |
| 71 | + FieldNameRegexps: []*regexp.Regexp{regexp.MustCompile("nam.*xp$")}, |
| 72 | + }), |
| 73 | + inputFile: "003-witness.pb.txt", |
| 74 | + expectedFile: "003-expected-redact-by-name-and-by-name-regexp.pb.txt", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for testName, testCase := range testCases { |
| 79 | + func() { |
| 80 | + ctrl := gomock.NewController(t) |
| 81 | + mockClient := mockrest.NewMockLearnClient(ctrl) |
| 82 | + defer ctrl.Finish() |
| 83 | + |
| 84 | + agentConfig := kgxapi.NewServiceAgentConfig() |
| 85 | + if fieldsToRedact, exists := testCase.agentConfig.Get(); exists { |
| 86 | + agentConfig.FieldsToRedact = fieldsToRedact |
| 87 | + } |
| 88 | + |
| 89 | + mockClient. |
| 90 | + EXPECT(). |
| 91 | + GetDynamicAgentConfigForService(gomock.Any(), gomock.Any()). |
| 92 | + AnyTimes(). |
| 93 | + Return(agentConfig, nil) |
| 94 | + |
| 95 | + o, err := NewRedactor(akid.GenerateServiceID(), mockClient) |
| 96 | + assert.NoError(t, err) |
| 97 | + |
| 98 | + testWitness := test.LoadWitnessFromFileOrDie(filepath.Join("testdata", testCase.inputFile)) |
| 99 | + expectedWitness := test.LoadWitnessFromFileOrDie(filepath.Join("testdata", testCase.expectedFile)) |
| 100 | + |
| 101 | + o.RedactSensitiveData(testWitness.Method) |
| 102 | + |
| 103 | + if diff := cmp.Diff(expectedWitness, testWitness, cmpOptions...); diff != "" { |
| 104 | + t.Errorf("found unexpected diff in test case %q:\n%s", testName, diff) |
| 105 | + } |
| 106 | + }() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func BenchmarkRedaction(b *testing.B) { |
| 111 | + ctrl := gomock.NewController(b) |
| 112 | + mockClient := mockrest.NewMockLearnClient(ctrl) |
| 113 | + defer ctrl.Finish() |
| 114 | + |
| 115 | + mockClient. |
| 116 | + EXPECT(). |
| 117 | + GetDynamicAgentConfigForService(gomock.Any(), gomock.Any()). |
| 118 | + AnyTimes(). |
| 119 | + Return(kgxapi.NewServiceAgentConfig(), nil) |
| 120 | + |
| 121 | + o, err := NewRedactor(akid.GenerateServiceID(), mockClient) |
| 122 | + assert.NoError(b, err) |
| 123 | + |
| 124 | + testWitness := test.LoadWitnessFromFileOrDie(filepath.Join("testdata", "001-witness.pb.txt")) |
| 125 | + |
| 126 | + b.ResetTimer() |
| 127 | + |
| 128 | + for i := 0; i < b.N; i++ { |
| 129 | + o.RedactSensitiveData(testWitness.Method) |
| 130 | + } |
| 131 | +} |
0 commit comments