github.com/Jeffail/benthos/v3@v3.65.0/internal/impl/pulsar/auth/config.go (about)

     1  package auth
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // Config contains configuration params for Pulsar authentication.
     8  type Config struct {
     9  	OAuth2 OAuth2Config `json:"oauth2" yaml:"oauth2"`
    10  	Token  TokenConfig  `json:"token" yaml:"token"`
    11  }
    12  
    13  // OAuth2Config contains configuration params for Pulsar OAuth2 authentication.
    14  type OAuth2Config struct {
    15  	Enabled        bool   `json:"enabled" yaml:"enabled"`
    16  	Audience       string `json:"audience" yaml:"audience"`
    17  	IssuerURL      string `json:"issuer_url" yaml:"issuer_url"`
    18  	PrivateKeyFile string `json:"private_key_file" yaml:"private_key_file"`
    19  }
    20  
    21  // TokenConfig contains configuration params for Pulsar Token authentication.
    22  type TokenConfig struct {
    23  	Enabled bool   `json:"enabled" yaml:"enabled"`
    24  	Token   string `json:"token" yaml:"token"`
    25  }
    26  
    27  // New creates a new Config instance.
    28  func New() Config {
    29  	return Config{
    30  		OAuth2: NewOAuth(),
    31  		Token:  NewToken(),
    32  	}
    33  }
    34  
    35  // NewOAuth creates a new OAuth2Config instance.
    36  func NewOAuth() OAuth2Config {
    37  	return OAuth2Config{
    38  		Enabled:        false,
    39  		PrivateKeyFile: "",
    40  		Audience:       "",
    41  		IssuerURL:      "",
    42  	}
    43  }
    44  
    45  // NewToken creates a new TokenConfig instance.
    46  func NewToken() TokenConfig {
    47  	return TokenConfig{
    48  		Enabled: false,
    49  		Token:   "",
    50  	}
    51  }
    52  
    53  // Validate checks whether Config is valid.
    54  func (c *Config) Validate() error {
    55  	if c.OAuth2.Enabled && c.Token.Enabled {
    56  		return errors.New("only one auth method can be enabled at once")
    57  	}
    58  	if c.OAuth2.Enabled {
    59  		return c.OAuth2.Validate()
    60  	}
    61  	if c.Token.Enabled {
    62  		return c.Token.Validate()
    63  	}
    64  	return nil
    65  }
    66  
    67  // Validate checks whether OAuth2Config is valid.
    68  func (c *OAuth2Config) Validate() error {
    69  	if c.Audience == "" {
    70  		return errors.New("oauth2 audience is empty")
    71  	}
    72  	if c.IssuerURL == "" {
    73  		return errors.New("oauth2 issuer URL is empty")
    74  	}
    75  	if c.PrivateKeyFile == "" {
    76  		return errors.New("oauth2 private key file is empty")
    77  	}
    78  	return nil
    79  }
    80  
    81  // ToMap returns OAuth2Config as a map representing OAuth2 client credentails.
    82  func (c *OAuth2Config) ToMap() map[string]string {
    83  	// Pulsar docs: https://pulsar.apache.org/docs/en/2.8.0/security-oauth2/#go-client
    84  	return map[string]string{
    85  		"type":       "client_credentials",
    86  		"issuerUrl":  c.IssuerURL,
    87  		"audience":   c.Audience,
    88  		"privateKey": c.PrivateKeyFile,
    89  	}
    90  }
    91  
    92  // Validate checks whether TokenConfig is valid.
    93  func (c *TokenConfig) Validate() error {
    94  	if c.Token == "" {
    95  		return errors.New("token is empty")
    96  	}
    97  	return nil
    98  }