feat(clients/go): mxgw-go -require-certificate-validation flag (mxaccessgw#199)

This commit is contained in:
Joseph Doherty
2026-11-09 12:03:00 -05:00
parent f13a747084
commit 0ec247e88c
3 changed files with 50 additions and 20 deletions
+4 -1
View File
@@ -501,7 +501,10 @@ buffer is widened to 16 MiB so a single long command line (e.g. a bulk write wit
thousands of handles) does not trip bufio's default 64 KiB token-too-long limit;
a line that still exceeds 16 MiB surfaces as a framed error and ends the session.
Use TLS options for a secured gateway:
Use TLS options for a secured gateway. Over TLS (no `-plaintext`) with
no `-ca-cert`, `mxgw-go` skips certificate verification by default (self-signed
gateway certs); pass `-require-certificate-validation` to verify against the
system trust roots instead:
```powershell
go run ./cmd/mxgw-go smoke -endpoint mxgateway.example.local:5001 -ca-cert C:\certs\mxgateway-ca.pem -server-name-override mxgateway.example.local -api-key-env MXGATEWAY_API_KEY -item Area001.Tag.Value -json
+24 -19
View File
@@ -33,13 +33,15 @@ type versionOutput struct {
}
type commonOptions struct {
Endpoint string `json:"endpoint"`
APIKey string `json:"apiKey"`
APIKeyEnv string `json:"apiKeyEnv,omitempty"`
Plaintext bool `json:"plaintext"`
CACertFile string `json:"caCertFile,omitempty"`
ServerName string `json:"serverNameOverride,omitempty"`
CallTimeout string `json:"callTimeout,omitempty"`
Endpoint string `json:"endpoint"`
APIKey string `json:"apiKey"`
APIKeyEnv string `json:"apiKeyEnv,omitempty"`
Plaintext bool `json:"plaintext"`
CACertFile string `json:"caCertFile,omitempty"`
ServerName string `json:"serverNameOverride,omitempty"`
RequireCertificateValidation bool `json:"requireCertificateValidation,omitempty"`
CallTimeout string `json:"callTimeout,omitempty"`
apiKeyValue string
timeout time.Duration
@@ -1295,6 +1297,7 @@ func bindCommonFlags(flags *flag.FlagSet) *commonOptions {
flags.StringVar(&common.APIKeyEnv, "api-key-env", "MXGATEWAY_API_KEY", "environment variable containing the API key")
flags.BoolVar(&common.Plaintext, "plaintext", false, "use plaintext transport")
flags.StringVar(&common.CACertFile, "ca-cert", "", "CA certificate file")
flags.BoolVar(&common.RequireCertificateValidation, "require-certificate-validation", false, "verify the gateway TLS certificate against system roots (default skips verification when no -ca-cert is given)")
flags.StringVar(&common.ServerName, "server-name-override", "", "TLS server name override")
flags.StringVar(&common.CallTimeout, "call-timeout", "30s", "per-call timeout")
return common
@@ -1307,12 +1310,13 @@ func dialForCommand(ctx context.Context, common *commonOptions) (*mxgateway.Clie
}
client, err := mxgateway.Dial(ctx, mxgateway.Options{
Endpoint: options.Endpoint,
APIKey: options.apiKeyValue,
Plaintext: options.Plaintext,
CACertFile: options.CACertFile,
ServerNameOverride: options.ServerName,
CallTimeout: options.timeout,
Endpoint: options.Endpoint,
APIKey: options.apiKeyValue,
Plaintext: options.Plaintext,
CACertFile: options.CACertFile,
ServerNameOverride: options.ServerName,
RequireCertificateValidation: options.RequireCertificateValidation,
CallTimeout: options.timeout,
})
return client, options, err
}
@@ -1523,12 +1527,13 @@ func dialGalaxyForCommand(ctx context.Context, common *commonOptions) (*mxgatewa
}
client, err := mxgateway.DialGalaxy(ctx, mxgateway.Options{
Endpoint: options.Endpoint,
APIKey: options.apiKeyValue,
Plaintext: options.Plaintext,
CACertFile: options.CACertFile,
ServerNameOverride: options.ServerName,
CallTimeout: options.timeout,
Endpoint: options.Endpoint,
APIKey: options.apiKeyValue,
Plaintext: options.Plaintext,
CACertFile: options.CACertFile,
ServerNameOverride: options.ServerName,
RequireCertificateValidation: options.RequireCertificateValidation,
CallTimeout: options.timeout,
})
return client, options, err
}
+22
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"flag"
"net"
"strings"
"testing"
@@ -859,3 +860,24 @@ func TestRunStreamEventsPrintsReplayGap(t *testing.T) {
t.Fatalf("stream-events -json gap cursors must be JSON numbers, got: %s", lines[0])
}
}
func TestRequireCertificateValidationFlag(t *testing.T) {
parse := func(args ...string) commonOptions {
flags := flag.NewFlagSet("t", flag.ContinueOnError)
common := bindCommonFlags(flags)
if err := flags.Parse(args); err != nil {
t.Fatalf("parse: %v", err)
}
resolved, err := common.resolved()
if err != nil {
t.Fatalf("resolved: %v", err)
}
return resolved
}
if parse().RequireCertificateValidation {
t.Fatal("default must stay lenient")
}
if !parse("-require-certificate-validation").RequireCertificateValidation {
t.Fatal("flag must set RequireCertificateValidation")
}
}