refactor(docker): update TLS config validation to require both CertFile and KeyFile exists or both empty

This commit is contained in:
yusing
2025-12-23 12:23:48 +08:00
parent 8340d93ab7
commit 1687f1d6b9
2 changed files with 19 additions and 3 deletions

View File

@@ -27,8 +27,8 @@ type DockerProviderConfigDetailed struct {
type DockerTLSConfig struct {
CAFile string `json:"ca_file,omitempty" validate:"required"`
CertFile string `json:"cert_file,omitempty" validate:"required"`
KeyFile string `json:"key_file,omitempty" validate:"required"`
CertFile string `json:"cert_file,omitempty" validate:"required_with=KeyFile"`
KeyFile string `json:"key_file,omitempty" validate:"required_with=CertFile"`
} // @name DockerTLSConfig
func (cfg *DockerProviderConfig) MarshalJSON() ([]byte, error) {

View File

@@ -98,13 +98,29 @@ func TestDockerProviderConfigValidation(t *testing.T) {
cert_file: /etc/ssl/cert.crt
key_file: /etc/ssl/key.crt
`, wantErr: false},
{name: "invalid tls (missing cert file and key file)", yamlStr: `
{name: "valid tls (only ca file)", yamlStr: `
test:
scheme: tls
host: localhost
port: 2375
tls:
ca_file: /etc/ssl/ca.crt
`, wantErr: false},
{name: "invalid tls (missing cert file)", yamlStr: `
test:
scheme: tls
host: localhost
port: 2375
tls:
key_file: /etc/ssl/key.crt
`, wantErr: true},
{name: "invalid tls (missing key file)", yamlStr: `
test:
scheme: tls
host: localhost
port: 2375
tls:
cert_file: /etc/ssl/cert.crt
`, wantErr: true},
}