github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/security/certmanager/option.go (about)

     1  // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package certmanagersec
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/sirupsen/logrus"
    13  
    14  	"github.com/choria-io/go-choria/config"
    15  )
    16  
    17  type Option func(*CertManagerSecurity) error
    18  
    19  func WithChoriaConfig(c *config.Config) Option {
    20  	return func(p *CertManagerSecurity) error {
    21  		cfg := Config{
    22  			apiVersion:      c.Choria.CertManagerAPIVersion,
    23  			sslDir:          c.Choria.SSLDir,
    24  			privilegedUsers: c.Choria.PrivilegedUsers,
    25  			namespace:       c.Choria.CertManagerSecurityNamespace,
    26  			issuer:          c.Choria.CertManagerSecurityIssuer,
    27  			replace:         c.Choria.CertManagerSecurityReplaceCSR,
    28  			altnames:        c.Choria.CertManagerSecurityAltNames,
    29  			identity:        c.Identity,
    30  			legacyCerts:     c.Choria.SecurityAllowLegacyCerts,
    31  		}
    32  
    33  		if c.OverrideCertname == "" {
    34  			if cn, ok := os.LookupEnv("MCOLLECTIVE_CERTNAME"); ok {
    35  				c.OverrideCertname = cn
    36  			}
    37  		}
    38  
    39  		if c.OverrideCertname != "" {
    40  			cfg.identity = c.OverrideCertname
    41  		}
    42  
    43  		if cfg.sslDir == "" {
    44  			return fmt.Errorf("plugin.choria.ssldir is required")
    45  		}
    46  
    47  		if cfg.identity == "" {
    48  			return fmt.Errorf("identity could not be established")
    49  		}
    50  
    51  		if cfg.apiVersion == "" {
    52  			cfg.apiVersion = "v1"
    53  		}
    54  
    55  		p.conf = &cfg
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  func WithLog(l *logrus.Entry) Option {
    62  	return func(p *CertManagerSecurity) error {
    63  		p.log = l.WithFields(logrus.Fields{"ssl": "certmanager"})
    64  
    65  		return nil
    66  	}
    67  }
    68  
    69  func WithContext(ctx context.Context) Option {
    70  	return func(p *CertManagerSecurity) error {
    71  		p.ctx = ctx
    72  
    73  		return nil
    74  	}
    75  }