Repository navigation
feat(kernel): honor WithPort on the kernel backend #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d5a6fdf
ba84162
263c741
7bc6a2a
5615750
52bd5ac
cb7a05d
c2bcd48
79f43a0
b34b6fa
646848a
18c3b83
ff5c0aa
e561594
d8e6d02
1a2a075
6e241a0
922e73a
1c3bcc5
0d70980
4b6c022
9a63050
aaadbb9
1e46843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| c8113e4f1e55dabb9125d4272721fa11b2d6f32d | ||
| 3b691d2a2c235ef847d61b308c31604979830994 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "fmt" | ||
| "strings" | ||
|
|
||
| dbsqlerr "github.com/databricks/databricks-sql-go/v2/errors" | ||
| dbsqlerrint "github.com/databricks/databricks-sql-go/v2/internal/errors" | ||
| ) | ||
|
|
||
|
|
@@ -203,3 +204,29 @@ func toStatementError(err error) error { | |
| } | ||
| return err | ||
| } | ||
|
|
||
| // kernelDefaultPort is the https default. The kernel accepts it and changes nothing. | ||
| const kernelDefaultPort = 443 | ||
|
|
||
| // portRejection applies the WithPort error contract to a kernel_session_config_set_port | ||
| // rejection. The kernel validates the port (range and host pair) and returns InvalidArgument | ||
| // for anything it rejects, so that is the kernel's verdict on the port. A non-default port | ||
| // wraps both ErrInvalidKernelConfig and ErrNotSupportedByKernel, so a caller can fall back to | ||
| // the default backend with errors.Is. The kernel accepts the default port without validation; | ||
| // were it rejected, only ErrInvalidKernelConfig is wrapped. Port 0 (unset), other codes and | ||
| // non-kernel errors pass through unchanged. Host setter errors are not passed here: they | ||
| // run before the port is set. The kernel's message is already redacted, so the wrapper | ||
| // adds no host text. | ||
| func portRejection(port int, err error) error { | ||
| if err == nil || port == 0 { | ||
| return err | ||
| } | ||
| var ke *KernelError | ||
| if !errors.As(err, &ke) || ke.Code != statusInvalidArgument { | ||
| return err | ||
| } | ||
| if port == kernelDefaultPort { | ||
| return fmt.Errorf("%w: %w", err, dbsqlerr.ErrInvalidKernelConfig) | ||
| } | ||
| return fmt.Errorf("%w: %w: %w", err, dbsqlerr.ErrInvalidKernelConfig, dbsqlerr.ErrNotSupportedByKernel) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //go:build cgo && databricks_kernel | ||
|
|
||
| package kernel | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| dbsqlerr "github.com/databricks/databricks-sql-go/v2/errors" | ||
| ) | ||
|
|
||
| const portTestSecret = "pSecretPw" | ||
|
|
||
| // TestOpenSessionPortRejection checks the WithPort contract end to end through the real | ||
| // kernel. OpenSession sets the host before the port. The kernel's kernel_session_config_set_port | ||
| // then rejects the credentialed host with InvalidArgument, before any connection is made. The | ||
| // error must wrap both sentinels and must not carry the credentials. | ||
| func TestOpenSessionPortRejection(t *testing.T) { | ||
| k := New(Config{ | ||
| Host: "user:" + portTestSecret + "@h.databricks.com", | ||
| Port: 8443, | ||
| WarehouseID: "wh", | ||
| Auth: Auth{Mode: AuthPAT, Token: "test-token"}, | ||
| }) | ||
| err := k.OpenSession(context.Background()) | ||
| if err == nil { | ||
| t.Fatal("OpenSession accepted credentials with a custom port; want the kernel to reject it") | ||
| } | ||
| if !errors.Is(err, dbsqlerr.ErrInvalidKernelConfig) || !errors.Is(err, dbsqlerr.ErrNotSupportedByKernel) { | ||
| t.Errorf("err = %v, want both ErrInvalidKernelConfig and ErrNotSupportedByKernel", err) | ||
| } | ||
| if msg := err.Error(); strings.Contains(msg, portTestSecret) || strings.Contains(msg, "user:") { | ||
| t.Errorf("error leaks credentials: %q", msg) | ||
| } | ||
| } | ||
|
|
||
| // TestOpenSessionOutOfRangePort checks that a port the kernel's range check rejects (70000 is | ||
| // above 65535) fails at session open with both sentinels, since it is never the default port. | ||
| func TestOpenSessionOutOfRangePort(t *testing.T) { | ||
| k := New(Config{ | ||
| Host: "h.databricks.com", | ||
| Port: 70000, | ||
| WarehouseID: "wh", | ||
| Auth: Auth{Mode: AuthPAT, Token: "test-token"}, | ||
| }) | ||
| err := k.OpenSession(context.Background()) | ||
| if err == nil { | ||
| t.Fatal("OpenSession accepted port 70000; want the kernel to reject it") | ||
| } | ||
| if !errors.Is(err, dbsqlerr.ErrInvalidKernelConfig) || !errors.Is(err, dbsqlerr.ErrNotSupportedByKernel) { | ||
| t.Errorf("err = %v, want both ErrInvalidKernelConfig and ErrNotSupportedByKernel", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the default port the kernel skips validation:
fold_host_portreturns as soon asport == default_port, before the@and path checks. Souser:pw@horh/xwith no WithPort still passes, as on main. Could the docs say the rejection applies only with a non-default port? "Errors never include the host" is also too broad. Kernel network errors include the request URL, and Thrift's connect error hashost=. Only the port-validation errors leave it out. The CHANGELOG line about both sentinels doesn't hold at 443 either.