Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release History

## Unreleased
- Add kernel-backend mTLS client certificate support through `WithKernelClientCertificate(certPEM, keyPEM)`
- Support `WithFederatedTokenProvider*` on the kernel backend by resolving its token once for kernel-side federation and forwarding the optional SP-wide client ID
- Improve telemetry error reporting: driver failures are now categorized by cause instead of reported as a generic error (databricks/databricks-sql-go#414, #415, #417, #419, #424)
- Fix OAuth U2M on the kernel backend (`WithUseKernel(true)`) against **Azure** workspaces: `resolveKernelAuth` now forwards the in-house OAuth app (`databricks-sql-connector`) + `sql offline_access` scopes **uniformly across all clouds**, instead of the Azure Entra-direct app id + `user_impersonation` scope that the Thrift path infers from the host. The kernel runs a single cloud-blind in-house workspace-federated U2M flow (it uses the workspace's OIDC-discovered authorize endpoint verbatim), so feeding it the Entra-direct app made the workspace federation route the browser to a broken AAD authorize URL. AWS/GCP behavior is unchanged (those already resolved to the in-house app + scopes). The Thrift path is unaffected.
Expand Down
1 change: 1 addition & 0 deletions CONNECTION_PARAMETERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ BINARY as `sql.RawBytes`).
| `WithSkipTLSHostVerify()` | ✅ | ✅ | Disable TLS chain + hostname verification. **Use only for internal private-link hostnames** — susceptible to machine-in-the-middle attacks. |
| `WithTransport(http.RoundTripper)` | ✅ | ❌ | Supply a custom HTTP transport (custom CA, mTLS, or proxy). Rejected on the kernel path — the kernel uses its own HTTP stack; use `WithKernelTrustedCerts` / `WithKernelProxy` there. |
| `WithKernelTrustedCerts(pem)` | ❌ | ✅ | Add a PEM CA bundle on top of the system roots (for a re-signing proxy / on-prem CA). Needed because the kernel's TLS stack does not read `SSL_CERT_FILE`. |
| `WithKernelClientCertificate(certPEM, keyPEM)` | ❌ | ✅ | Configure a paired mTLS client certificate and unencrypted private key. Both must be non-empty; the certificate may include intermediates and PKCS#8 keys are recommended. |
| `WithKernelSkipHostnameVerify()` | ❌ | ✅ | Skip **only** the hostname check while keeping chain validation (finer-grained than `WithSkipTLSHostVerify`). |

## Proxy
Expand Down
2 changes: 1 addition & 1 deletion KERNEL_REV
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f4c3556d4f57642cf813aef56437214f26685dc4
e8d60c93b7459947d2e3e4113f62e6bda37fde8e
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ token:<pat>@<host>:443[path]?useCloudFetch=false
| `WithSkipTLSHostVerify()` | Both | Disable TLS chain + hostname verification. **Use only for internal private-link hostnames** — this is susceptible to machine-in-the-middle attacks. |
| `WithTransport(http.RoundTripper)` | Thrift only | Supply a custom HTTP transport (e.g. a custom CA, mTLS, or proxy). **Rejected** on the kernel path (wraps `ErrNotSupportedByKernel`) — the kernel uses its own HTTP stack; use `WithKernelTrustedCerts` / `WithKernelProxy` there. |
| `WithKernelTrustedCerts(pem)` | SEA only | Add a PEM CA bundle on top of the system roots (for a re-signing proxy / on-prem CA). Needed because the kernel's TLS stack does not read `SSL_CERT_FILE`. |
| `WithKernelClientCertificate(certPEM, keyPEM)` | SEA only | Configure a paired mTLS client certificate and unencrypted private key. Both must be non-empty; PKCS#8 keys are recommended. |
| `WithKernelSkipHostnameVerify()` | SEA only | Skip **only** the hostname check while keeping chain validation (finer-grained than `WithSkipTLSHostVerify`). |

## Proxy
Expand Down
21 changes: 21 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,27 @@ func WithKernelTrustedCerts(pem []byte) ConnOption {
}
}

// WithKernelClientCertificate configures the PEM-encoded client certificate and
// matching private key used when a server requires mutual TLS (mTLS). certPEM
// contains the leaf certificate followed by any intermediate certificates;
// keyPEM contains the matching unencrypted private key. PKCS#8 is recommended
// for portability across the kernel's TLS backends.
//
// Both values are required and must be non-empty. The driver copies them
// defensively and validates the pair at connect time. Server trust remains
// independent and strict by default; use WithKernelTrustedCerts when the server
// certificate chains to a private CA.
//
// EXPERIMENTAL, kernel-only: the default (Thrift) backend rejects this at connect.
func WithKernelClientCertificate(certPEM, keyPEM []byte) ConnOption {
return func(c *config.Config) {
ke := kernelExperimental(c)
ke.TLSClientCertConfigured = true
ke.TLSClientCertPEM = append([]byte(nil), certPEM...)
ke.TLSClientKeyPEM = append([]byte(nil), keyPEM...)
}
}

// WithKernelSkipHostnameVerify skips only the certificate hostname-vs-SNI check on
// the kernel backend, while keeping chain validation. This is finer-grained than
// WithSkipTLSHostVerify, which relaxes both chain and hostname checks.
Expand Down
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ WithKernel* prefix marks them experimental):
- WithKernelTrustedCerts(pem) adds a PEM CA bundle on top of the system roots (for
a re-signing proxy or on-prem CA). Required because the kernel's TLS stack does
not read SSL_CERT_FILE.
- WithKernelClientCertificate(certPEM, keyPEM) configures a paired mTLS client
identity. Both PEM buffers must be non-empty; the certificate may include
intermediates and an unencrypted PKCS#8 private key is recommended.
- WithKernelSkipHostnameVerify() skips only the hostname check while keeping chain
validation (finer-grained than WithSkipTLSHostVerify).
- WithKernelProxy(KernelProxy{URL, Username, Password, BypassHosts}) sets an explicit
Expand Down
24 changes: 20 additions & 4 deletions internal/backend/kernel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,9 @@ func (k *KernelBackend) OpenSession(ctx context.Context) error {
}

// applyKernelTLS forwards the experimental kernel-only TLS knobs to the session
// config: a custom CA bundle and an independent hostname-skip. Each is a no-op
// when its field is unset, so this is safe to call unconditionally. (mTLS client
// cert/key is a separate follow-up — it needs a kernel C-ABI setter that is not
// yet on kernel main.)
// config: a custom CA bundle, paired mTLS client identity, and an independent
// hostname-skip. Each is a no-op when its field is unset, so this is safe to call
// unconditionally.
func (k *KernelBackend) applyKernelTLS(cfg *C.KernelSessionConfig) error {
if len(k.cfg.TLSTrustedCertsPEM) > 0 {
ca := newCBytes(k.cfg.TLSTrustedCertsPEM)
Expand All @@ -303,6 +302,23 @@ func (k *KernelBackend) applyKernelTLS(cfg *C.KernelSessionConfig) error {
return fmt.Errorf("kernel: set_tls_trusted_certs: %w", toConnError(err))
}
}
if len(k.cfg.TLSClientCertPEM) > 0 && len(k.cfg.TLSClientKeyPEM) > 0 {
cert := newCBytes(k.cfg.TLSClientCertPEM)
defer cert.free()
key := newCBytes(k.cfg.TLSClientKeyPEM)
defer key.free()
if err := call(func() C.KernelStatusCode {
return C.kernel_session_config_set_tls_client_certificate(
cfg,
cert.ptr,
cert.len,
key.ptr,
key.len,
)
}); err != nil {
return fmt.Errorf("kernel: set_tls_client_certificate: %w", toConnError(err))
}
}
if k.cfg.TLSSkipHostnameVerify {
if err := call(func() C.KernelStatusCode {
return C.kernel_session_config_set_tls_skip_hostname_verification(cfg, C.bool(true))
Expand Down
5 changes: 4 additions & 1 deletion internal/backend/kernel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ type Config struct {
// Experimental kernel-only TLS knobs (from the WithKernel* options). These
// have no Thrift-path equivalent and are set via config.KernelExperimental.
// Empty/false fields are simply not applied. TLSTrustedCertsPEM is a custom CA
// bundle added on top of the system roots; TLSSkipHostnameVerify skips only the
// bundle added on top of the system roots; TLSClientCertPEM/TLSClientKeyPEM
// are the paired mTLS client identity; TLSSkipHostnameVerify skips only the
// hostname check (finer-grained than the blanket TLSSkipVerify above).
TLSTrustedCertsPEM []byte
TLSClientCertPEM []byte
TLSClientKeyPEM []byte
TLSSkipHostnameVerify bool

// ProxyURL configures an HTTP proxy. It is either resolved for this endpoint
Expand Down
19 changes: 13 additions & 6 deletions internal/backend/kernel/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ func TestSetAuthByMode(t *testing.T) {
}

// TestSetKernelTLS exercises the real cgo setters for the experimental kernel-only
// TLS knobs (the byte-buffer trusted-CA bundle + the hostname-skip bool) via the
// trySetKernelTLS seam — proving the (*C.uint8_t, C.size_t) marshalling and the C
// signatures. A failure here means the field→setter wiring or a C signature
// drifted.
// TLS knobs (trusted CA, paired mTLS identity, and hostname-skip bool) via the
// trySetKernelTLS seam — proving the (*C.uint8_t, C.size_t) marshalling and C
// signatures. A failure here means field→setter wiring or a C signature drifted.
func TestSetKernelTLS(t *testing.T) {
ca := []byte("-----BEGIN CERTIFICATE-----\nca\n-----END CERTIFICATE-----\n")
clientCert := []byte("client-cert")
clientKey := []byte("client-key")
cases := []struct {
name string
cfg Config
}{
{"trusted certs only", Config{TLSTrustedCertsPEM: ca}},
{"mTLS identity only", Config{TLSClientCertPEM: clientCert, TLSClientKeyPEM: clientKey}},
{"skip hostname only", Config{TLSSkipHostnameVerify: true}},
{"both together", Config{TLSTrustedCertsPEM: ca, TLSSkipHostnameVerify: true}},
{"all together", Config{
TLSTrustedCertsPEM: ca,
TLSClientCertPEM: clientCert,
TLSClientKeyPEM: clientKey,
TLSSkipHostnameVerify: true,
}},
{"none (no-op)", Config{}},
}
for _, c := range cases {
Expand All @@ -90,7 +97,7 @@ func TestSetProxy(t *testing.T) {
}{
{"url only", Config{ProxyURL: "http://proxy:3128"}},
{"url + credentials", Config{ProxyURL: "http://proxy:3128", ProxyUsername: "u", ProxyPassword: "p"}},
{"url + bypass", Config{ProxyURL: "http://proxy:3128", ProxyBypassHosts: "localhost,*.internal"}}, //nolint:gosec // G101: test literals, not real credentials
{"url + bypass", Config{ProxyURL: "http://proxy:3128", ProxyBypassHosts: "localhost,*.internal"}}, //nolint:gosec // G101: test literals, not real credentials
{"all fields", Config{ProxyURL: "http://proxy:3128", ProxyUsername: "u", ProxyPassword: "p", ProxyBypassHosts: "localhost,*.internal"}}, //nolint:gosec // G101: test literals, not real credentials
{"none (no-op)", Config{}},
}
Expand Down
30 changes: 22 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ type KernelExperimentalConfig struct {
// Needed because the kernel's rustls stack ignores SSL_CERT_FILE, so a custom
// CA must be handed to the kernel explicitly.
TLSTrustedCertsPEM []byte
// TLSClientCertPEM / TLSClientKeyPEM are the paired mTLS client identity
// forwarded through kernel_session_config_set_tls_client_certificate.
// TLSClientCertConfigured distinguishes an explicit call with empty values
// from the option never being set, so invalid input cannot fail open.
TLSClientCertPEM []byte
TLSClientKeyPEM []byte
TLSClientCertConfigured bool
// TLSSkipHostnameVerify skips only the certificate hostname check, independent
// of the blanket InsecureSkipVerify (which relaxes both chain and hostname).
// Maps to kernel_session_config_set_tls_skip_hostname_verification.
Expand Down Expand Up @@ -121,18 +128,25 @@ func (k *KernelExperimentalConfig) DeepCopy() *KernelExperimentalConfig {
return nil
}
cp := &KernelExperimentalConfig{
TLSSkipHostnameVerify: k.TLSSkipHostnameVerify,
ProxyURL: k.ProxyURL,
ProxyUsername: k.ProxyUsername,
ProxyPassword: k.ProxyPassword,
ProxyBypassHosts: k.ProxyBypassHosts,
RetryOverallTimeout: k.RetryOverallTimeout,
MaxChunksInMemory: k.MaxChunksInMemory,
DecimalAsFloat: k.DecimalAsFloat,
TLSClientCertConfigured: k.TLSClientCertConfigured,
TLSSkipHostnameVerify: k.TLSSkipHostnameVerify,
ProxyURL: k.ProxyURL,
ProxyUsername: k.ProxyUsername,
ProxyPassword: k.ProxyPassword,
ProxyBypassHosts: k.ProxyBypassHosts,
RetryOverallTimeout: k.RetryOverallTimeout,
MaxChunksInMemory: k.MaxChunksInMemory,
DecimalAsFloat: k.DecimalAsFloat,
}
if k.TLSTrustedCertsPEM != nil {
cp.TLSTrustedCertsPEM = append([]byte(nil), k.TLSTrustedCertsPEM...)
}
if k.TLSClientCertPEM != nil {
cp.TLSClientCertPEM = append([]byte(nil), k.TLSClientCertPEM...)
}
if k.TLSClientKeyPEM != nil {
cp.TLSClientKeyPEM = append([]byte(nil), k.TLSClientKeyPEM...)
}
return cp
}

Expand Down
13 changes: 11 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,11 @@ func TestConfig_DeepCopy(t *testing.T) {
ThriftProtocolVersion: cli_service.TProtocolVersion_SPARK_CLI_SERVICE_PROTOCOL_V8,
ThriftDebugClientProtocol: false,
KernelExperimental: &KernelExperimentalConfig{
TLSTrustedCertsPEM: []byte("ca-bundle"),
TLSSkipHostnameVerify: true,
TLSTrustedCertsPEM: []byte("ca-bundle"),
TLSClientCertPEM: []byte("client-cert"),
TLSClientKeyPEM: []byte("client-key"),
TLSClientCertConfigured: true,
TLSSkipHostnameVerify: true,
},
}

Expand All @@ -787,5 +790,11 @@ func TestConfig_DeepCopy(t *testing.T) {
if cfg.KernelExperimental.TLSTrustedCertsPEM[0] == 'X' {
t.Error("DeepCopy aliased the KernelExperimental CA byte slice")
}
cfg_copy.KernelExperimental.TLSClientCertPEM[0] = 'X'
cfg_copy.KernelExperimental.TLSClientKeyPEM[0] = 'X'
if cfg.KernelExperimental.TLSClientCertPEM[0] == 'X' ||
cfg.KernelExperimental.TLSClientKeyPEM[0] == 'X' {
t.Error("DeepCopy aliased the KernelExperimental mTLS byte slices")
}
})
}
10 changes: 10 additions & 0 deletions kernel_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func validateKernelConfigContext(ctx context.Context, cfg *config.Config) (kerne
"(want a scheme and host, e.g. http://proxy:3128)", ke.ProxyURL, dbsqlerr.ErrInvalidKernelConfig)
}
}
if ke := cfg.KernelExperimental; ke != nil && ke.TLSClientCertConfigured {
if len(ke.TLSClientCertPEM) == 0 || len(ke.TLSClientKeyPEM) == 0 {
return kernel.Auth{}, fmt.Errorf(
"databricks: WithKernelClientCertificate requires a non-empty certificate and private key: %w",
dbsqlerr.ErrInvalidKernelConfig,
)
}
}
return kauth, nil
}

Expand Down Expand Up @@ -152,6 +160,8 @@ func buildKernelConfig(cfg *config.Config, kauth kernel.Auth) kernel.Config {
// kernel C ABI in OpenSession.
if ke := cfg.KernelExperimental; ke != nil {
kc.TLSTrustedCertsPEM = ke.TLSTrustedCertsPEM
kc.TLSClientCertPEM = ke.TLSClientCertPEM
kc.TLSClientKeyPEM = ke.TLSClientKeyPEM
kc.TLSSkipHostnameVerify = ke.TLSSkipHostnameVerify
// Kernel-only CloudFetch in-memory-chunk knob (WithKernelMaxChunksInMemory).
// Injected into the kernel backend's own SessionConf ONLY (not via
Expand Down
48 changes: 43 additions & 5 deletions kernel_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,35 @@ func TestValidateKernelConfig(t *testing.T) {
}
})

t.Run("valid WithKernelClientCertificate pair accepted", func(t *testing.T) {
c := baseKernelConfig()
WithKernelClientCertificate([]byte("cert"), []byte("key"))(c)
if _, err := validateKernelConfig(c); err != nil {
t.Errorf("a complete mTLS identity should validate, got %v", err)
}
})

t.Run("incomplete WithKernelClientCertificate rejected as ErrInvalidKernelConfig", func(t *testing.T) {
for _, tc := range []struct {
name string
cert []byte
key []byte
}{
{"both empty", nil, nil},
{"certificate empty", nil, []byte("key")},
{"key empty", []byte("cert"), nil},
} {
t.Run(tc.name, func(t *testing.T) {
c := baseKernelConfig()
WithKernelClientCertificate(tc.cert, tc.key)(c)
_, err := validateKernelConfig(c)
if !errors.Is(err, dbsqlerr.ErrInvalidKernelConfig) {
t.Errorf("incomplete mTLS identity should be ErrInvalidKernelConfig, got %v", err)
}
})
}
})

t.Run("malformed WithKernelProxy URL rejected as ErrInvalidKernelConfig", func(t *testing.T) {
// A malformed URL must be caught in the Go layer with an errors.Is-able
// config error, not surface as an opaque "kernel: set_proxy: …" wrap at
Expand Down Expand Up @@ -457,8 +486,11 @@ func TestBuildKernelConfig(t *testing.T) {
t.Run("experimental TLS fields forwarded", func(t *testing.T) {
c := baseKernelConfig()
c.KernelExperimental = &config.KernelExperimentalConfig{
TLSTrustedCertsPEM: []byte("ca-bundle"),
TLSSkipHostnameVerify: true,
TLSTrustedCertsPEM: []byte("ca-bundle"),
TLSClientCertPEM: []byte("client-cert"),
TLSClientKeyPEM: []byte("client-key"),
TLSClientCertConfigured: true,
TLSSkipHostnameVerify: true,
}
kc := buildKernelConfig(c, kernel.Auth{Mode: kernel.AuthPAT, Token: "dapi-x"})
if got := string(kc.TLSTrustedCertsPEM); got != "ca-bundle" {
Expand All @@ -467,14 +499,20 @@ func TestBuildKernelConfig(t *testing.T) {
if !kc.TLSSkipHostnameVerify {
t.Error("TLSSkipHostnameVerify = false, want true (WithKernelSkipHostnameVerify not forwarded)")
}
if got := string(kc.TLSClientCertPEM); got != "client-cert" {
t.Errorf("TLSClientCertPEM = %q, want client-cert", got)
}
if got := string(kc.TLSClientKeyPEM); got != "client-key" {
t.Errorf("TLSClientKeyPEM = %q, want client-key", got)
}
})

t.Run("nil KernelExperimental leaves TLS fields zero", func(t *testing.T) {
c := baseKernelConfig() // KernelExperimental nil
kc := buildKernelConfig(c, kernel.Auth{Mode: kernel.AuthPAT, Token: "dapi-x"})
if kc.TLSTrustedCertsPEM != nil || kc.TLSSkipHostnameVerify {
t.Errorf("expected zero experimental TLS fields with nil KernelExperimental, got certs=%v skipHost=%v",
kc.TLSTrustedCertsPEM, kc.TLSSkipHostnameVerify)
if kc.TLSTrustedCertsPEM != nil || kc.TLSClientCertPEM != nil ||
kc.TLSClientKeyPEM != nil || kc.TLSSkipHostnameVerify {
t.Errorf("expected zero experimental TLS fields with nil KernelExperimental, got %+v", kc)
}
})

Expand Down
Loading
Loading