github.com/prebid/prebid-server/v2@v2.18.0/config/experiment.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/url"
     7  )
     8  
     9  var (
    10  	ErrSignerModeIncorrect              = errors.New("signer mode is not specified, specify 'off', 'inprocess' or 'remote'")
    11  	ErrInProcessSignerInvalidPrivateKey = errors.New("private key for inprocess signer cannot be empty")
    12  
    13  	ErrMsgInProcessSignerInvalidURL                = "invalid url for inprocess signer"
    14  	ErrMsgInProcessSignerInvalidDNSRenewalInterval = "invalid dns renewal interval for inprocess signer"
    15  	ErrMsgInProcessSignerInvalidDNSCheckInterval   = "invalid dns check interval for inprocess signer"
    16  	ErrMsgInvalidRemoteSignerURL                   = "invalid url for remote signer"
    17  	ErrMsgInvalidRemoteSignerSigningTimeout        = "invalid signing timeout for remote signer"
    18  )
    19  
    20  const (
    21  	AdCertsSignerModeOff       = "off"
    22  	AdCertsSignerModeInprocess = "inprocess"
    23  	AdCertsSignerModeRemote    = "remote"
    24  )
    25  
    26  // Experiment defines if experimental features are available
    27  type Experiment struct {
    28  	AdCerts ExperimentAdsCert `mapstructure:"adscert"`
    29  }
    30  
    31  // ExperimentAdsCert configures and enables functionality to generate and send Ads Cert Auth header to bidders
    32  type ExperimentAdsCert struct {
    33  	Mode      string           `mapstructure:"mode"`
    34  	InProcess AdsCertInProcess `mapstructure:"inprocess"`
    35  	Remote    AdsCertRemote    `mapstructure:"remote"`
    36  }
    37  
    38  // AdsCertInProcess configures data to sign requests using ads certs library in core PBS logic
    39  type AdsCertInProcess struct {
    40  	// Origin is ads.cert hostname for the originating party
    41  	Origin string `mapstructure:"origin"`
    42  	// PrivateKey is a base-64 encoded private key.
    43  	PrivateKey string `mapstructure:"key"`
    44  	// DNSCheckIntervalInSeconds specifies frequency to check origin _delivery._adscert and _adscert subdomains, used for indexing data, default: 30
    45  	DNSCheckIntervalInSeconds int `mapstructure:"domain_check_interval_seconds"`
    46  	// DNSRenewalIntervalInSeconds specifies frequency to renew origin _delivery._adscert and _adscert subdomains, used for indexing data, default: 30
    47  	DNSRenewalIntervalInSeconds int `mapstructure:"domain_renewal_interval_seconds"`
    48  }
    49  
    50  // AdsCertRemote configures data to sign requests using remote signatory service
    51  type AdsCertRemote struct {
    52  	// Url is the address of gRPC server that will create a call signature
    53  	Url string `mapstructure:"url"`
    54  	// SigningTimeoutMs specifies how long this client will wait for signing to finish before abandoning
    55  	SigningTimeoutMs int `mapstructure:"signing_timeout_ms"`
    56  }
    57  
    58  func (cfg *Experiment) validate(errs []error) []error {
    59  	if len(cfg.AdCerts.Mode) == 0 {
    60  		return errs
    61  	}
    62  	if !(cfg.AdCerts.Mode == AdCertsSignerModeOff ||
    63  		cfg.AdCerts.Mode == AdCertsSignerModeInprocess ||
    64  		cfg.AdCerts.Mode == AdCertsSignerModeRemote) {
    65  		return append(errs, ErrSignerModeIncorrect)
    66  	}
    67  	if cfg.AdCerts.Mode == AdCertsSignerModeInprocess {
    68  		_, err := url.ParseRequestURI(cfg.AdCerts.InProcess.Origin)
    69  		if err != nil {
    70  			errs = append(errs, fmt.Errorf("%s: %s", ErrMsgInProcessSignerInvalidURL, cfg.AdCerts.InProcess.Origin))
    71  		}
    72  		if len(cfg.AdCerts.InProcess.PrivateKey) == 0 {
    73  			errs = append(errs, ErrInProcessSignerInvalidPrivateKey)
    74  		}
    75  		if cfg.AdCerts.InProcess.DNSRenewalIntervalInSeconds <= 0 {
    76  			errs = append(errs, fmt.Errorf("%s: %d", ErrMsgInProcessSignerInvalidDNSRenewalInterval, cfg.AdCerts.InProcess.DNSRenewalIntervalInSeconds))
    77  		}
    78  		if cfg.AdCerts.InProcess.DNSCheckIntervalInSeconds <= 0 {
    79  			errs = append(errs, fmt.Errorf("%s: %d", ErrMsgInProcessSignerInvalidDNSCheckInterval, cfg.AdCerts.InProcess.DNSCheckIntervalInSeconds))
    80  		}
    81  	} else if cfg.AdCerts.Mode == AdCertsSignerModeRemote {
    82  		_, err := url.ParseRequestURI(cfg.AdCerts.Remote.Url)
    83  		if err != nil {
    84  			errs = append(errs, fmt.Errorf("%s: %s", ErrMsgInvalidRemoteSignerURL, cfg.AdCerts.Remote.Url))
    85  		}
    86  		if cfg.AdCerts.Remote.SigningTimeoutMs <= 0 {
    87  			errs = append(errs, fmt.Errorf("%s: %d", ErrMsgInvalidRemoteSignerSigningTimeout, cfg.AdCerts.Remote.SigningTimeoutMs))
    88  		}
    89  	}
    90  	return errs
    91  }