mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
Added param for CA certs
This commit is contained in:
parent
a7d78e8240
commit
cda5e71584
4 changed files with 69 additions and 5 deletions
|
|
@ -17,6 +17,7 @@ import (
|
||||||
var (
|
var (
|
||||||
tokenFetchRetries int
|
tokenFetchRetries int
|
||||||
pluginPaths []string
|
pluginPaths []string
|
||||||
|
cacertPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
var generateCmd = &cobra.Command{
|
var generateCmd = &cobra.Command{
|
||||||
|
|
@ -118,6 +119,7 @@ func init() {
|
||||||
generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the target configs to make")
|
generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the target configs to make")
|
||||||
generateCmd.Flags().StringSliceVar(&pluginPaths, "plugins", []string{}, "set the generator plugins directory path")
|
generateCmd.Flags().StringSliceVar(&pluginPaths, "plugins", []string{}, "set the generator plugins directory path")
|
||||||
generateCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets")
|
generateCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets")
|
||||||
|
generateCmd.Flags().StringVar(&cacertPath, "ca-cert", "", "path to CA cert. (defaults to system CAs)")
|
||||||
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
||||||
|
|
||||||
rootCmd.AddCommand(generateCmd)
|
rootCmd.AddCommand(generateCmd)
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,15 @@ package configurator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/configurator/internal/util"
|
"github.com/OpenCHAMI/configurator/internal/util"
|
||||||
)
|
)
|
||||||
|
|
@ -19,6 +24,61 @@ type SmdClient struct {
|
||||||
|
|
||||||
type Params = map[string]any
|
type Params = map[string]any
|
||||||
type Option func(Params)
|
type Option func(Params)
|
||||||
|
type ClientOption func(*SmdClient)
|
||||||
|
|
||||||
|
func NewSmdClient(opts ...ClientOption) SmdClient {
|
||||||
|
client := SmdClient{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&client)
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithHost(host string) ClientOption {
|
||||||
|
return func(c *SmdClient) {
|
||||||
|
c.Host = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithPort(port int) ClientOption {
|
||||||
|
return func(c *SmdClient) {
|
||||||
|
c.Port = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithAccessToken(token string) ClientOption {
|
||||||
|
return func(c *SmdClient) {
|
||||||
|
c.AccessToken = token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithCertPool(certPool *x509.CertPool) ClientOption {
|
||||||
|
return func(c *SmdClient) {
|
||||||
|
c.Client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
RootCAs: certPool,
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
DisableKeepAlives: true,
|
||||||
|
Dial: (&net.Dialer{
|
||||||
|
Timeout: 120 * time.Second,
|
||||||
|
KeepAlive: 120 * time.Second,
|
||||||
|
}).Dial,
|
||||||
|
TLSHandshakeTimeout: 120 * time.Second,
|
||||||
|
ResponseHeaderTimeout: 120 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSecureTLS(certPath string) ClientOption {
|
||||||
|
if certPath == "" {
|
||||||
|
return func(sc *SmdClient) {}
|
||||||
|
}
|
||||||
|
cacert, _ := os.ReadFile(certPath)
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
certPool.AppendCertsFromPEM(cacert)
|
||||||
|
return WithCertPool(certPool)
|
||||||
|
}
|
||||||
|
|
||||||
func WithVerbosity() Option {
|
func WithVerbosity() Option {
|
||||||
return func(p util.Params) {
|
return func(p util.Params) {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ type Config struct {
|
||||||
AccessToken string `yaml:"access-token"`
|
AccessToken string `yaml:"access-token"`
|
||||||
Targets map[string]Target `yaml:"targets"`
|
Targets map[string]Target `yaml:"targets"`
|
||||||
PluginDirs []string `yaml:"plugins"`
|
PluginDirs []string `yaml:"plugins"`
|
||||||
|
CertPath string `yaml:"ca-cert"`
|
||||||
Options Options `yaml:"options"`
|
Options Options `yaml:"options"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -186,11 +186,12 @@ func Generate(config *configurator.Config, params Params) (Files, error) {
|
||||||
// load generator plugins to generate configs or to print
|
// load generator plugins to generate configs or to print
|
||||||
var (
|
var (
|
||||||
generators = make(map[string]Generator)
|
generators = make(map[string]Generator)
|
||||||
client = configurator.SmdClient{
|
client = configurator.NewSmdClient(
|
||||||
Host: config.SmdClient.Host,
|
configurator.WithHost(config.SmdClient.Host),
|
||||||
Port: config.SmdClient.Port,
|
configurator.WithPort(config.SmdClient.Port),
|
||||||
AccessToken: config.AccessToken,
|
configurator.WithAccessToken(config.AccessToken),
|
||||||
}
|
configurator.WithSecureTLS(config.CertPath),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// load all plugins from params
|
// load all plugins from params
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue