github.com/cilium/cilium@v1.16.2/pkg/hubble/parser/seven/kafka_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package seven
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	flowpb "github.com/cilium/cilium/api/v1/flow"
    11  	"github.com/cilium/cilium/pkg/hubble/defaults"
    12  	"github.com/cilium/cilium/pkg/hubble/parser/options"
    13  	"github.com/cilium/cilium/pkg/proxy/accesslog"
    14  )
    15  
    16  func Test_decodeKafka(t *testing.T) {
    17  	type args struct {
    18  		flowType accesslog.FlowType
    19  		kafka    *accesslog.LogRecordKafka
    20  		opts     *options.Options
    21  	}
    22  	tests := []struct {
    23  		name string
    24  		args args
    25  		want *flowpb.Layer7_Kafka
    26  	}{
    27  		{
    28  			name: "request",
    29  			args: args{
    30  				flowType: accesslog.TypeRequest,
    31  				kafka: &accesslog.LogRecordKafka{
    32  					ErrorCode:     1,
    33  					APIVersion:    2,
    34  					APIKey:        "publish",
    35  					CorrelationID: 3,
    36  					Topic: accesslog.KafkaTopic{
    37  						Topic: "my-topic",
    38  					},
    39  				},
    40  				opts: &options.Options{
    41  					HubbleRedactSettings: options.HubbleRedactSettings{
    42  						Enabled:           false,
    43  						RedactKafkaAPIKey: false,
    44  					},
    45  				},
    46  			},
    47  			want: &flowpb.Layer7_Kafka{
    48  				Kafka: &flowpb.Kafka{
    49  					ApiVersion:    2,
    50  					ApiKey:        "publish",
    51  					CorrelationId: 3,
    52  					Topic:         "my-topic",
    53  				},
    54  			},
    55  		},
    56  		{
    57  			name: "response",
    58  			args: args{
    59  				flowType: accesslog.TypeResponse,
    60  				kafka: &accesslog.LogRecordKafka{
    61  					ErrorCode:     1,
    62  					APIVersion:    2,
    63  					APIKey:        "publish",
    64  					CorrelationID: 3,
    65  					Topic: accesslog.KafkaTopic{
    66  						Topic: "my-topic",
    67  					},
    68  				},
    69  				opts: &options.Options{
    70  					HubbleRedactSettings: options.HubbleRedactSettings{
    71  						Enabled:           false,
    72  						RedactKafkaAPIKey: false,
    73  					},
    74  				},
    75  			},
    76  			want: &flowpb.Layer7_Kafka{
    77  				Kafka: &flowpb.Kafka{
    78  					ErrorCode:     1,
    79  					ApiVersion:    2,
    80  					ApiKey:        "publish",
    81  					CorrelationId: 3,
    82  					Topic:         "my-topic",
    83  				},
    84  			},
    85  		},
    86  		{
    87  			name: "empty-topic",
    88  			args: args{
    89  				flowType: accesslog.TypeResponse,
    90  				kafka: &accesslog.LogRecordKafka{
    91  					ErrorCode:     1,
    92  					APIVersion:    2,
    93  					APIKey:        "publish",
    94  					CorrelationID: 3,
    95  				},
    96  				opts: &options.Options{
    97  					HubbleRedactSettings: options.HubbleRedactSettings{
    98  						Enabled:           false,
    99  						RedactKafkaAPIKey: false,
   100  					},
   101  				},
   102  			},
   103  			want: &flowpb.Layer7_Kafka{
   104  				Kafka: &flowpb.Kafka{
   105  					ErrorCode:     1,
   106  					ApiVersion:    2,
   107  					ApiKey:        "publish",
   108  					CorrelationId: 3,
   109  				},
   110  			},
   111  		},
   112  		{
   113  			name: "redact-api-key",
   114  			args: args{
   115  				flowType: accesslog.TypeResponse,
   116  				kafka: &accesslog.LogRecordKafka{
   117  					ErrorCode:     1,
   118  					APIVersion:    2,
   119  					APIKey:        "my-key",
   120  					CorrelationID: 3,
   121  					Topic: accesslog.KafkaTopic{
   122  						Topic: "my-topic",
   123  					},
   124  				},
   125  				opts: &options.Options{
   126  					HubbleRedactSettings: options.HubbleRedactSettings{
   127  						Enabled:           true,
   128  						RedactKafkaAPIKey: true,
   129  					},
   130  				},
   131  			},
   132  			want: &flowpb.Layer7_Kafka{
   133  				Kafka: &flowpb.Kafka{
   134  					ErrorCode:     1,
   135  					ApiVersion:    2,
   136  					ApiKey:        defaults.SensitiveValueRedacted,
   137  					CorrelationId: 3,
   138  					Topic:         "my-topic",
   139  				},
   140  			},
   141  		},
   142  	}
   143  	for _, tt := range tests {
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			got := decodeKafka(tt.args.flowType, tt.args.kafka, tt.args.opts)
   146  			if !reflect.DeepEqual(got, tt.want) {
   147  				t.Errorf("decodeKafka() = %v, want %v", got, tt.want)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func Test_kafkaSummary(t *testing.T) {
   154  	type args struct {
   155  		flow *flowpb.Flow
   156  	}
   157  	tests := []struct {
   158  		name string
   159  		args args
   160  		want string
   161  	}{
   162  		// TODO: Add test cases.
   163  		{
   164  			name: "request",
   165  			args: args{
   166  				flow: &flowpb.Flow{
   167  					L7: &flowpb.Layer7{
   168  						Type: flowpb.L7FlowType_REQUEST,
   169  						Record: &flowpb.Layer7_Kafka{
   170  							Kafka: &flowpb.Kafka{
   171  								ErrorCode:     1,
   172  								ApiVersion:    2,
   173  								ApiKey:        "publish",
   174  								CorrelationId: 3,
   175  								Topic:         "my-topic",
   176  							},
   177  						},
   178  					},
   179  				},
   180  			},
   181  			want: "Kafka request publish correlation id 3 topic 'my-topic'",
   182  		},
   183  		{
   184  			name: "response",
   185  			args: args{
   186  				flow: &flowpb.Flow{
   187  					L7: &flowpb.Layer7{
   188  						Type: flowpb.L7FlowType_RESPONSE,
   189  						Record: &flowpb.Layer7_Kafka{
   190  							Kafka: &flowpb.Kafka{
   191  								ErrorCode:     1,
   192  								ApiVersion:    2,
   193  								ApiKey:        "publish",
   194  								CorrelationId: 3,
   195  								Topic:         "my-topic",
   196  							},
   197  						},
   198  					},
   199  				},
   200  			},
   201  			want: "Kafka response publish correlation id 3 topic 'my-topic' return code 1",
   202  		},
   203  		{
   204  			name: "nil",
   205  			args: args{
   206  				flow: nil,
   207  			},
   208  			want: "",
   209  		},
   210  	}
   211  	for _, tt := range tests {
   212  		t.Run(tt.name, func(t *testing.T) {
   213  			if got := kafkaSummary(tt.args.flow); got != tt.want {
   214  				t.Errorf("kafkaSummary() = %v, want %v", got, tt.want)
   215  			}
   216  		})
   217  	}
   218  }