Repository navigation
feat(kernel): custom authenticators and token providers on the kernel backend #610
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
Open
cathleeny
wants to merge
8
commits into
main
Choose a base branch
from
cathleeny/ddf-custom-credential-provider-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23a9b8d
feat(kernel): custom authenticators and token providers on the kernel…
cathleeny f434049
fix(kernel): keep a port written in the host in the authenticator URL
cathleeny 6e295b8
Fix IPv6 authenticator request URLs
cathleeny ac58623
Merge main into custom authenticators on the kernel backend
cathleeny b32e535
Merge main into custom authenticators on the kernel backend
cathleeny 1bb3dbe
fix(kernel): preserve authenticator error identity
cathleeny a0592d5
Pin the kernel to merged #527 (6d6c1e4)
cathleeny 9fc382f
Document kernel-managed authentication headers
cathleeny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| a29235cbea9532efa5572ebff010045583a19cbe | ||
| 6d6c1e4eb70e3d5bebea75b76c5b19c459d9a467 | ||
|
cathleeny marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| //go:build cgo && databricks_kernel | ||
|
|
||
| package kernel | ||
|
|
||
| /* | ||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
| #include "databricks_kernel.h" | ||
|
|
||
| // Adapters with the exact KernelAuthHeadersCallback / KernelAuthReleaseCallback | ||
| // signatures that forward to the Go exports below, so no function-pointer cast | ||
| // is needed (same pattern as the log callback). | ||
| int32_t kernelAuthHeadersTrampoline(void* user_data, KernelAuthHeaders* headers); | ||
| void kernelAuthReleaseTrampoline(void* user_data); | ||
| static int32_t kernelAuthHeadersAdapter(void* user_data, KernelAuthHeaders* headers) { | ||
| return kernelAuthHeadersTrampoline(user_data, headers); | ||
| } | ||
| static void kernelAuthReleaseAdapter(void* user_data) { kernelAuthReleaseTrampoline(user_data); } | ||
| static KernelAuthHeadersCallback kernel_auth_headers_cb(void) { return kernelAuthHeadersAdapter; } | ||
| static KernelAuthReleaseCallback kernel_auth_release_cb(void) { return kernelAuthReleaseAdapter; } | ||
| */ | ||
| import "C" | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "runtime/cgo" | ||
| "sync/atomic" | ||
| "unsafe" | ||
|
|
||
| "github.com/databricks/databricks-sql-go/v2/auth" | ||
| ) | ||
|
|
||
| // externalAuth is what the kernel's per-request auth callback reaches through | ||
| // its user_data: the driver authenticator and the URL of the throwaway request. | ||
| type externalAuth struct { | ||
| authr auth.Authenticator | ||
| url string | ||
| lastErr atomic.Pointer[authError] | ||
| } | ||
|
|
||
| type authError struct{ err error } | ||
|
|
||
| // Authenticate records only failures from the authenticator, not header validation | ||
| // failures. Callbacks for a statement and a detached cancel can run concurrently. | ||
| func (e *externalAuth) Authenticate(req *http.Request) error { | ||
| err := e.authr.Authenticate(req) | ||
| if err != nil { | ||
| e.lastErr.Store(&authError{err: err}) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (e *externalAuth) wrapProviderFailure(err error) error { | ||
| if e == nil { | ||
| return err | ||
| } | ||
| if last := e.lastErr.Load(); last != nil { | ||
| return wrapProviderFailure(err, last.err) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // setAuthExternal registers ext as the session's credentials callback. The | ||
| // cgo.Handle lives in C memory (never in a C pointer slot itself); the kernel's | ||
| // release callback deletes it and frees that memory exactly once, after the | ||
| // session is closed or the config is freed. If the setter fails, the kernel | ||
| // does not call release, so this function cleans up instead. | ||
| func setAuthExternal(cfg *C.KernelSessionConfig, ext *externalAuth) error { | ||
| h := cgo.NewHandle(ext) | ||
| slot := (*C.uintptr_t)(C.malloc(C.size_t(unsafe.Sizeof(C.uintptr_t(0))))) | ||
| *slot = C.uintptr_t(h) | ||
| err := call(func() C.KernelStatusCode { | ||
| return C.kernel_session_config_set_auth_external( | ||
| cfg, C.kernel_auth_headers_cb(), C.kernel_auth_release_cb(), unsafe.Pointer(slot)) | ||
|
cathleeny marked this conversation as resolved.
Resolved
cathleeny marked this conversation as resolved.
Resolved
|
||
| }) | ||
| if err != nil { | ||
| h.Delete() | ||
| C.free(unsafe.Pointer(slot)) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| //export kernelAuthHeadersTrampoline | ||
| func kernelAuthHeadersTrampoline(userData unsafe.Pointer, headers *C.KernelAuthHeaders) (status C.int32_t) { | ||
| // A panic must never cross the C ABI: fail this request instead. | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| setAuthHeadersError(headers, "databricks: the authenticator panicked") | ||
| status = 1 | ||
| } | ||
| }() | ||
| ext := cgo.Handle(*(*C.uintptr_t)(userData)).Value().(*externalAuth) | ||
| pairs, err := externalHeaders(ext, ext.url) | ||
| if err != nil { | ||
| setAuthHeadersError(headers, err.Error()) | ||
|
cathleeny marked this conversation as resolved.
Resolved
|
||
| return 1 | ||
| } | ||
| for _, p := range pairs { | ||
| name, value := newCStr(p[0]), newCStr(p[1]) | ||
| st := C.kernel_auth_headers_add(headers, name.c, value.c) | ||
| name.free() | ||
| value.free() | ||
| if st != C.KernelStatusCode_Success { | ||
| setAuthHeadersError(headers, fmt.Sprintf("databricks: the kernel rejected header %q", p[0])) | ||
| return 1 | ||
| } | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| //export kernelAuthReleaseTrampoline | ||
| func kernelAuthReleaseTrampoline(userData unsafe.Pointer) { | ||
| defer func() { _ = recover() }() | ||
| cgo.Handle(*(*C.uintptr_t)(userData)).Delete() | ||
| C.free(userData) | ||
| } | ||
|
|
||
| func setAuthHeadersError(headers *C.KernelAuthHeaders, msg string) { | ||
| m := newCStr(msg) | ||
| defer m.free() | ||
| C.kernel_auth_headers_set_error(headers, m.c) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.