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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package options
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // Option is used to configure parsers
    14  type Option func(*Options)
    15  
    16  // Options contains all parser options
    17  type Options struct {
    18  	CacheSize            int
    19  	HubbleRedactSettings HubbleRedactSettings
    20  }
    21  
    22  // HubbleRedactSettings contains all hubble redact related options
    23  type HubbleRedactSettings struct {
    24  	Enabled            bool
    25  	RedactHTTPQuery    bool
    26  	RedactHTTPUserInfo bool
    27  	RedactKafkaAPIKey  bool
    28  	RedactHttpHeaders  HttpHeadersList
    29  }
    30  
    31  // HttpHeadersList contains the allow/deny list of headers
    32  type HttpHeadersList struct {
    33  	Allow map[string]struct{}
    34  	Deny  map[string]struct{}
    35  }
    36  
    37  // CacheSize configures the amount of L7 requests cached for latency calculation
    38  func CacheSize(size int) Option {
    39  	return func(opt *Options) {
    40  		opt.CacheSize = size
    41  	}
    42  }
    43  
    44  // Redact configures which data Hubble will redact.
    45  func Redact(logger logrus.FieldLogger, httpQuery, httpUserInfo, kafkaApiKey bool, allowHeaders, denyHeaders []string) Option {
    46  	return func(opt *Options) {
    47  		opt.HubbleRedactSettings.Enabled = true
    48  		opt.HubbleRedactSettings.RedactHTTPQuery = httpQuery
    49  		opt.HubbleRedactSettings.RedactHTTPUserInfo = httpUserInfo
    50  		opt.HubbleRedactSettings.RedactKafkaAPIKey = kafkaApiKey
    51  		opt.HubbleRedactSettings.RedactHttpHeaders = HttpHeadersList{
    52  			Allow: headerSliceToMap(allowHeaders),
    53  			Deny:  headerSliceToMap(denyHeaders),
    54  		}
    55  		if logger != nil {
    56  			logger.WithField(
    57  				"options",
    58  				fmt.Sprintf("%+v", opt)).Info("configured Hubble with redact options")
    59  		}
    60  	}
    61  }
    62  
    63  func headerSliceToMap(headerList []string) map[string]struct{} {
    64  	headerMap := make(map[string]struct{}, len(headerList))
    65  	for _, header := range headerList {
    66  		headerMap[strings.ToLower(header)] = struct{}{}
    67  	}
    68  	return headerMap
    69  }