github.com/Axway/agent-sdk@v1.1.101/pkg/traceability/config.go (about)

     1  package traceability
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	"github.com/Axway/agent-sdk/pkg/agent"
     8  	"github.com/Axway/agent-sdk/pkg/traceability/redaction"
     9  	"github.com/Axway/agent-sdk/pkg/traceability/sampling"
    10  	"github.com/Axway/agent-sdk/pkg/util/log"
    11  
    12  	"github.com/elastic/beats/v7/libbeat/beat"
    13  	"github.com/elastic/beats/v7/libbeat/common"
    14  	"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
    15  	"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
    16  )
    17  
    18  // Config -
    19  type HostConfig struct {
    20  	Protocol string   `config:"protocol"`
    21  	Hosts    []string `config:"hosts"`
    22  }
    23  
    24  // Config -
    25  type Config struct {
    26  	Index             string            `config:"index"`
    27  	LoadBalance       bool              `config:"loadbalance"`
    28  	BulkMaxSize       int               `config:"bulk_max_size"`
    29  	SlowStart         bool              `config:"slow_start"`
    30  	Timeout           time.Duration     `config:"client_timeout"    validate:"min=0"`
    31  	TTL               time.Duration     `config:"ttl"               validate:"min=0"`
    32  	Pipelining        int               `config:"pipelining"        validate:"min=0"`
    33  	CompressionLevel  int               `config:"compression_level" validate:"min=0, max=9"`
    34  	MaxRetries        int               `config:"max_retries"       validate:"min=-1"`
    35  	TLS               *tlscommon.Config `config:"ssl"`
    36  	Proxy             ProxyConfig       `config:",inline"`
    37  	Backoff           Backoff           `config:"backoff"`
    38  	EscapeHTML        bool              `config:"escape_html"`
    39  	Protocol          string            `config:"protocol"`
    40  	Hosts             []string          `config:"hosts"`
    41  	Redaction         redaction.Config  `config:"redaction" yaml:"redaction"`
    42  	Sampling          sampling.Sampling `config:"sampling" yaml:"sampling"`
    43  	APIExceptionsList []string          `config:"apiExceptionsList"`
    44  }
    45  
    46  // ProxyConfig holds the configuration information required to proxy
    47  // connections through a SOCKS5 proxy server.
    48  type ProxyConfig struct {
    49  	// URL of the SOCKS proxy. Scheme must be socks5. Username and password can be
    50  	// embedded in the URL.
    51  	URL string `config:"proxy_url"`
    52  
    53  	// Resolve names locally instead of on the SOCKS server.
    54  	LocalResolve bool `config:"proxy_use_local_resolver"`
    55  }
    56  
    57  // Backoff -
    58  type Backoff struct {
    59  	Init time.Duration
    60  	Max  time.Duration
    61  }
    62  
    63  var outputConfig *Config
    64  
    65  // DefaultConfig -
    66  func DefaultConfig() *Config {
    67  	return &Config{
    68  		LoadBalance:      false,
    69  		Pipelining:       0,
    70  		BulkMaxSize:      512,
    71  		SlowStart:        false,
    72  		CompressionLevel: 3,
    73  		Timeout:          60 * time.Second,
    74  		MaxRetries:       3,
    75  		TTL:              0 * time.Second,
    76  		Backoff: Backoff{
    77  			Init: 1 * time.Second,
    78  			Max:  60 * time.Second,
    79  		},
    80  		EscapeHTML: false,
    81  		Protocol:   "tcp",
    82  		Redaction:  redaction.DefaultConfig(),
    83  		Sampling:   sampling.DefaultConfig(),
    84  	}
    85  }
    86  
    87  func readConfig(cfg *common.Config, info beat.Info) (*Config, error) {
    88  	outputConfig = DefaultConfig()
    89  
    90  	err := cfgwarn.CheckRemoved6xSettings(cfg, "port")
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	if err := cfg.Unpack(outputConfig); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	if agent.GetCentralConfig().GetTraceabilityHost() != "" {
   100  		outputConfig.Protocol = "tcp"
   101  		outputConfig.Hosts = []string{agent.GetCentralConfig().GetTraceabilityHost()}
   102  	}
   103  
   104  	if outputConfig.Index == "" {
   105  		outputConfig.Index = info.IndexPrefix
   106  	}
   107  
   108  	// Setup the redaction regular expressions
   109  	redaction.SetupGlobalRedaction(outputConfig.Redaction)
   110  
   111  	// Setup the sampling config, if central config can not be found assume online mode
   112  	if agent.GetCentralConfig() != nil && agent.GetCentralConfig().GetUsageReportingConfig() != nil {
   113  		err = sampling.SetupSampling(outputConfig.Sampling, agent.GetCentralConfig().GetUsageReportingConfig().IsOfflineMode())
   114  	} else {
   115  		err = sampling.SetupSampling(outputConfig.Sampling, false)
   116  	}
   117  
   118  	if err != nil {
   119  		log.Warn(err.Error())
   120  	}
   121  
   122  	// Force piplining to 0
   123  	if outputConfig.Pipelining > 0 {
   124  		log.Warn("Pipelining is not supported by Amplify Visibility yet, forcing to synchronous")
   125  		outputConfig.Pipelining = 0
   126  	}
   127  
   128  	// if set, check for valid proxyURL
   129  	if outputConfig.Proxy.URL != "" {
   130  		if _, err := url.ParseRequestURI(outputConfig.Proxy.URL); err != nil {
   131  			return nil, ErrInvalidConfig.FormatError("traceability.proxyURL")
   132  		}
   133  	}
   134  
   135  	// set up the api exceptions list for logging events
   136  	exception, err := setUpAPIExceptionList(outputConfig.APIExceptionsList)
   137  	if err != nil {
   138  		err = ErrInvalidRegex.FormatError("apiExceptionValue", exception, err)
   139  		log.Error(err)
   140  	}
   141  
   142  	return outputConfig, nil
   143  }
   144  
   145  // IsHTTPTransport - Returns true if the protocol is set to http/https
   146  func IsHTTPTransport() bool {
   147  	if outputConfig == nil {
   148  		return false
   149  	}
   150  	return (outputConfig.Protocol == "https" || outputConfig.Protocol == "http")
   151  }
   152  
   153  // GetMaxRetries - Returns the max retries configured for transport
   154  func GetMaxRetries() int {
   155  	if outputConfig == nil {
   156  		return 3
   157  	}
   158  	return outputConfig.MaxRetries
   159  }