From 5ce17ec4d65a24750fcc531ded38bf19dd3d19bf Mon Sep 17 00:00:00 2001 From: pormirza Date: Wed, 18 Nov 2026 10:11:58 +0100 Subject: [PATCH 1/6] wip: add api-ca flag, env and context field --- cmd/config/config.go | 12 ++++++++++++ cmd/config/context.go | 2 ++ cmd/context.go | 6 ++++++ cmd/login.go | 1 + cmd/root.go | 2 ++ 5 files changed, 23 insertions(+) diff --git a/cmd/config/config.go b/cmd/config/config.go index a83c3b5..777c5b0 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -119,6 +119,18 @@ func (c *Config) GetApiURL() string { return viper.GetString("api-url") } +func (c *Config) GetApiCA() string { + if viper.GetString("api-ca") != "" { + return viper.GetString("api-ca") + } + if c.Context.ApiCA != nil { + return *c.Context.ApiCA + } + + // fallback to the default specified by viper + return viper.GetString("api-ca") +} + func (c *Config) GetProvider() string { if viper.GetString("provider") != "" { return viper.GetString("provider") diff --git a/cmd/config/context.go b/cmd/config/context.go index 2ba9110..fe90de1 100644 --- a/cmd/config/context.go +++ b/cmd/config/context.go @@ -26,6 +26,7 @@ type Contexts struct { type Context struct { Name string `json:"name"` ApiURL *string `json:"api-url,omitempty"` + ApiCA *string `json:"api-ca,omitempty"` Token string `json:"api-token"` DefaultProject string `json:"default-project"` Timeout *time.Duration `json:"timeout,omitempty"` @@ -133,6 +134,7 @@ func (c *Config) MustDefaultContext() Context { func defaultCtx() Context { return Context{ ApiURL: pointer.PointerOrNil(viper.GetString("api-url")), + ApiCA: pointer.PointerOrNil(viper.GetString("api-ca")), Token: viper.GetString("api-token"), } } diff --git a/cmd/context.go b/cmd/context.go index 228e901..acb178d 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -87,6 +87,7 @@ func newContextCmd(c *config.Config) *cobra.Command { }, } contextAddCmd.Flags().String("api-url", "", "sets the api-url for this context") + contextAddCmd.Flags().String("api-ca", "", "sets the api-ca for this context") contextAddCmd.Flags().String("api-token", "", "sets the api-token for this context") contextAddCmd.Flags().String("default-project", "", "sets a default project to act on") contextAddCmd.Flags().Duration("timeout", 0, "sets a default request timeout") @@ -104,6 +105,7 @@ func newContextCmd(c *config.Config) *cobra.Command { ValidArgsFunction: c.ContextListCompletion, } contextUpdateCmd.Flags().String("api-url", "", "sets the api-url for this context") + contextUpdateCmd.Flags().String("api-ca", "", "sets the api-ca for this context") contextUpdateCmd.Flags().String("api-token", "", "sets the api-token for this context") contextUpdateCmd.Flags().String("default-project", "", "sets a default project to act on") contextUpdateCmd.Flags().Duration("timeout", 0, "sets a default request timeout") @@ -169,6 +171,7 @@ func (c *ctx) add(args []string) error { ctx := &config.Context{ Name: name, ApiURL: pointer.PointerOrNil(viper.GetString("api-url")), + ApiCA: pointer.PointerOrNil(viper.GetString("api-ca")), Token: viper.GetString("api-token"), DefaultProject: viper.GetString("default-project"), Timeout: pointer.PointerOrNil(viper.GetDuration("timeout")), @@ -211,6 +214,9 @@ func (c *ctx) update(args []string) error { if viper.IsSet("api-url") { ctx.ApiURL = pointer.PointerOrNil(viper.GetString("api-url")) } + if viper.IsSet("api-ca") { + ctx.ApiCA = pointer.PointerOrNil(viper.GetString("api-ca")) + } if viper.IsSet("api-token") { ctx.Token = viper.GetString("api-token") } diff --git a/cmd/login.go b/cmd/login.go index 9466f39..c9ecd66 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -69,6 +69,7 @@ func (l *login) login() error { newCtx.Name = viper.GetString("context") } newCtx.ApiURL = new(l.c.GetApiURL()) + newCtx.ApiCA = new(l.c.GetApiCA()) ctxs.Contexts = append(ctxs.Contexts, &newCtx) ctx = &newCtx } diff --git a/cmd/root.go b/cmd/root.go index 7f37abc..d223eee 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -72,6 +72,7 @@ func NewRootCmd(c *config.Config) *cobra.Command { rootCmd.PersistentFlags().Duration("timeout", 0, "request timeout used for api requests") rootCmd.PersistentFlags().String("api-url", "", "the url to the metal-stack.io api") + rootCmd.PersistentFlags().String("api-ca", "", "the path to the certificate authority to trust for the api endpoint") rootCmd.PersistentFlags().String("api-token", "", "the token used for api requests") genericcli.Must(viper.BindPFlags(rootCmd.PersistentFlags())) @@ -81,6 +82,7 @@ func NewRootCmd(c *config.Config) *cobra.Command { viper.MustBindEnv("debug") viper.MustBindEnv("timeout") viper.MustBindEnv("api-url") + viper.MustBindEnv("api-ca") viper.MustBindEnv("api-token") markdownCmd := &cobra.Command{ From 17ce423fac8ab0efcc66018b1cf7cfdcbde7173b Mon Sep 17 00:00:00 2001 From: pormirza Date: Wed, 18 Nov 2026 15:07:29 +0100 Subject: [PATCH 2/6] build tls config in newApiClient --- cmd/login.go | 4 ++-- cmd/root.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index c9ecd66..0fa4623 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -123,7 +123,7 @@ func (l *login) login() error { } if viper.IsSet("admin-role") { - mc, err := newApiClient(l.c.GetApiURL(), token) + mc, err := newApiClient(l.c.GetApiURL(), token, l.c.GetApiCA()) if err != nil { return err } @@ -143,7 +143,7 @@ func (l *login) login() error { ctx.Token = token if ctx.DefaultProject == "" { - mc, err := newApiClient(l.c.GetApiURL(), token) + mc, err := newApiClient(l.c.GetApiURL(), token, l.c.GetApiCA()) if err != nil { return err } diff --git a/cmd/root.go b/cmd/root.go index d223eee..691c6d4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,9 @@ package cmd import ( + "crypto/tls" + "crypto/x509" + "fmt" "log/slog" "os" "strings" @@ -137,7 +140,7 @@ func initConfigWithViperCtx(c *config.Config) error { return nil } - mc, err := newApiClient(c.GetApiURL(), c.GetToken()) + mc, err := newApiClient(c.GetApiURL(), c.GetToken(), c.GetApiCA()) if err != nil { return err } @@ -148,7 +151,7 @@ func initConfigWithViperCtx(c *config.Config) error { return nil } -func newApiClient(apiURL, token string) (client.Client, error) { +func newApiClient(apiURL, token, apiCA string) (client.Client, error) { logLevel := slog.LevelInfo if viper.GetBool("debug") { logLevel = slog.LevelDebug @@ -161,6 +164,27 @@ func newApiClient(apiURL, token string) (client.Client, error) { Log: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel})), } + if apiCA != "" { + caPEM, err := os.ReadFile(apiCA) + + if err != nil { + return nil, fmt.Errorf("failed to read file %q: %w", apiCA, err) + } + pool, err := x509.SystemCertPool() + + if err != nil { + return nil, fmt.Errorf("failed to load system certificate: %w", err) + } + if ok := pool.AppendCertsFromPEM(caPEM); ok != true { + return nil, fmt.Errorf("failed to append pem certificate: %q", apiCA) + } + + tlsConfig := &tls.Config{ + RootCAs: pool, + } + + } + return client.New(dialConfig) } From 5ff55513a2fb9383c1bed7f6933837968d97650f Mon Sep 17 00:00:00 2001 From: pormirza Date: Wed, 18 Nov 2026 15:29:29 +0100 Subject: [PATCH 3/6] use api-ca for the api transport --- cmd/root.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 691c6d4..4371ba8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "fmt" "log/slog" + "net/http" "os" "strings" @@ -178,13 +179,14 @@ func newApiClient(apiURL, token, apiCA string) (client.Client, error) { if ok := pool.AppendCertsFromPEM(caPEM); ok != true { return nil, fmt.Errorf("failed to append pem certificate: %q", apiCA) } - tlsConfig := &tls.Config{ RootCAs: pool, } + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.TLSClientConfig = tlsConfig + dialConfig.Transport = transport } - return client.New(dialConfig) } From e71db643e6e24c9fa1963da4fb64ed17f7f9ccfa Mon Sep 17 00:00:00 2001 From: pormirza Date: Thu, 19 Nov 2026 11:18:51 +0100 Subject: [PATCH 4/6] describe api-ca flags --- cmd/context.go | 4 ++-- cmd/root.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/context.go b/cmd/context.go index acb178d..6ffdb28 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -87,7 +87,7 @@ func newContextCmd(c *config.Config) *cobra.Command { }, } contextAddCmd.Flags().String("api-url", "", "sets the api-url for this context") - contextAddCmd.Flags().String("api-ca", "", "sets the api-ca for this context") + contextAddCmd.Flags().String("api-ca", "", "sets the path to a PEM file with a CA in addition to the system CAs for this context") contextAddCmd.Flags().String("api-token", "", "sets the api-token for this context") contextAddCmd.Flags().String("default-project", "", "sets a default project to act on") contextAddCmd.Flags().Duration("timeout", 0, "sets a default request timeout") @@ -105,7 +105,7 @@ func newContextCmd(c *config.Config) *cobra.Command { ValidArgsFunction: c.ContextListCompletion, } contextUpdateCmd.Flags().String("api-url", "", "sets the api-url for this context") - contextUpdateCmd.Flags().String("api-ca", "", "sets the api-ca for this context") + contextUpdateCmd.Flags().String("api-ca", "", "sets the path to a PEM file with a CA in addition to the system CAs for this context") contextUpdateCmd.Flags().String("api-token", "", "sets the api-token for this context") contextUpdateCmd.Flags().String("default-project", "", "sets a default project to act on") contextUpdateCmd.Flags().Duration("timeout", 0, "sets a default request timeout") diff --git a/cmd/root.go b/cmd/root.go index 4371ba8..7438c83 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -76,7 +76,7 @@ func NewRootCmd(c *config.Config) *cobra.Command { rootCmd.PersistentFlags().Duration("timeout", 0, "request timeout used for api requests") rootCmd.PersistentFlags().String("api-url", "", "the url to the metal-stack.io api") - rootCmd.PersistentFlags().String("api-ca", "", "the path to the certificate authority to trust for the api endpoint") + rootCmd.PersistentFlags().String("api-ca", "", "the path to a PEM file to trust for the api endpoint in addition to the system CAs") rootCmd.PersistentFlags().String("api-token", "", "the token used for api requests") genericcli.Must(viper.BindPFlags(rootCmd.PersistentFlags())) From 2e8040aef59d4c643489ffd13a229c0639ce86df Mon Sep 17 00:00:00 2001 From: pormirza Date: Thu, 19 Nov 2026 14:55:42 +0100 Subject: [PATCH 5/6] simplify bool check --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 7438c83..6658b64 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -176,7 +176,7 @@ func newApiClient(apiURL, token, apiCA string) (client.Client, error) { if err != nil { return nil, fmt.Errorf("failed to load system certificate: %w", err) } - if ok := pool.AppendCertsFromPEM(caPEM); ok != true { + if ok := pool.AppendCertsFromPEM(caPEM); !ok { return nil, fmt.Errorf("failed to append pem certificate: %q", apiCA) } tlsConfig := &tls.Config{ From ff3a06dcd401c3488d70094c8c5ccd0348332e12 Mon Sep 17 00:00:00 2001 From: pormirza Date: Fri, 20 Nov 2026 09:40:25 +0100 Subject: [PATCH 6/6] pass config to newApiClient --- cmd/login.go | 4 ++-- cmd/root.go | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index 0fa4623..4a2818a 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -123,7 +123,7 @@ func (l *login) login() error { } if viper.IsSet("admin-role") { - mc, err := newApiClient(l.c.GetApiURL(), token, l.c.GetApiCA()) + mc, err := newApiClient(l.c, token) if err != nil { return err } @@ -143,7 +143,7 @@ func (l *login) login() error { ctx.Token = token if ctx.DefaultProject == "" { - mc, err := newApiClient(l.c.GetApiURL(), token, l.c.GetApiCA()) + mc, err := newApiClient(l.c, token) if err != nil { return err } diff --git a/cmd/root.go b/cmd/root.go index 6658b64..6d78734 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -141,7 +141,7 @@ func initConfigWithViperCtx(c *config.Config) error { return nil } - mc, err := newApiClient(c.GetApiURL(), c.GetToken(), c.GetApiCA()) + mc, err := newApiClient(c, c.GetToken()) if err != nil { return err } @@ -152,7 +152,10 @@ func initConfigWithViperCtx(c *config.Config) error { return nil } -func newApiClient(apiURL, token, apiCA string) (client.Client, error) { +func newApiClient(c *config.Config, token string) (client.Client, error) { + apiURL := c.GetApiURL() + apiCA := c.GetApiCA() + logLevel := slog.LevelInfo if viper.GetBool("debug") { logLevel = slog.LevelDebug