-
Notifications
You must be signed in to change notification settings - Fork 50
Add support for OIDC ID token authentication using an environment variable. #1215
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
a298741
d1e4c59
8e50a80
8a76075
7f96ced
13d248f
ee02c8f
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 |
---|---|---|
|
@@ -108,6 +108,9 @@ type Config struct { | |
// specified by this argument. This argument also holds currently selected auth. | ||
AuthType string `name:"auth_type" env:"DATABRICKS_AUTH_TYPE" auth:"-"` | ||
|
||
// Environment variable name that contains an OIDC ID token. | ||
OIDCTokenEnv string `name:"oidc_token_env" env:"DATABRICKS_OIDC_TOKEN_ENV" auth:"-"` | ||
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. Does it make sense to read this dynamically from the environment to support "refreshes"? 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 actual environment variable referred to by this environment variable is read each time the IDTokenSource is called. I'm not sure it's worth also reading this one dynamically. 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. True. I missread this as if |
||
|
||
// Skip SSL certificate verification for HTTP calls. | ||
// Use at your own risk or for unit testing purposes. | ||
InsecureSkipVerify bool `name:"skip_verify" auth:"-"` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Package oidc provides utilities for working with OIDC ID tokens. | ||
// | ||
// This package is experimental and subject to change. | ||
package oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// IDToken represents an OIDC ID token that can be exchanged for a Databricks | ||
// access token. | ||
type IDToken struct { | ||
Value string | ||
} | ||
|
||
// IDTokenSource is anything that returns an IDToken given an audience. | ||
type IDTokenSource interface { | ||
IDToken(ctx context.Context, audience string) (*IDToken, error) | ||
} | ||
|
||
// IDTokenSourceFn is an adapter to allow the use of ordinary functions as | ||
// IDTokenSource. | ||
// | ||
// Example: | ||
// | ||
// ts := IDTokenSourceFn(func(ctx context.Context, aud string) (*IDToken, error) { | ||
// return &IDToken{}, nil | ||
// }) | ||
type IDTokenSourceFn func(ctx context.Context, audience string) (*IDToken, error) | ||
|
||
func (fn IDTokenSourceFn) IDToken(ctx context.Context, audience string) (*IDToken, error) { | ||
return fn(ctx, audience) | ||
} | ||
|
||
// NewEnvIDTokenSource returns an IDTokenSource that reads the token from | ||
// environment variable env. | ||
// | ||
// Note that the IDTokenSource does not cache the token and will read the token | ||
// from environment variable env each time. | ||
func NewEnvIDTokenSource(env string) IDTokenSource { | ||
return IDTokenSourceFn(func(ctx context.Context, _ string) (*IDToken, error) { | ||
t := os.Getenv(env) | ||
if t == "" { | ||
return nil, fmt.Errorf("missing env var %q", env) | ||
} | ||
return &IDToken{Value: t}, nil | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package oidc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestIDTokenSourceFn(t *testing.T) { | ||
wantToken := &IDToken{Value: "from-func"} | ||
wantErr := fmt.Errorf("test error") | ||
wantAud := "func-audience" | ||
wantCtx := context.Background() | ||
|
||
ts := IDTokenSourceFn(func(gotCtx context.Context, gotAud string) (*IDToken, error) { | ||
if gotCtx != wantCtx { | ||
t.Errorf("unexpected context: got %v, want %v", gotCtx, wantCtx) | ||
} | ||
if gotAud != wantAud { | ||
t.Errorf("unexpected audience: got %q, want %q", gotAud, wantAud) | ||
} | ||
return wantToken, wantErr | ||
}) | ||
|
||
gotToken, gotErr := ts.IDToken(wantCtx, wantAud) | ||
|
||
if gotErr != wantErr { | ||
t.Errorf("IDToken() want error: %v, got error: %v", wantErr, gotErr) | ||
} | ||
if !cmp.Equal(gotToken, wantToken) { | ||
t.Errorf("IDToken() token = %v, want %v", gotToken, wantToken) | ||
} | ||
} | ||
|
||
func TestNewEnvIDTokenSource(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
envName string | ||
envValue string | ||
audience string | ||
want *IDToken | ||
wantErr bool | ||
}{ | ||
{ | ||
desc: "Success - variable set", | ||
envName: "OIDC_TEST_TOKEN_SUCCESS", | ||
envValue: "test-token-123", | ||
audience: "test-audience-1", | ||
want: &IDToken{Value: "test-token-123"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
desc: "Failure - variable not set", | ||
envName: "OIDC_TEST_TOKEN_MISSING", | ||
envValue: "", | ||
audience: "test-audience-2", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
desc: "Failure - variable set to empty string", | ||
envName: "OIDC_TEST_TOKEN_EMPTY", | ||
envValue: "", | ||
audience: "test-audience-3", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
desc: "Success - different variable name", | ||
envName: "ANOTHER_OIDC_TOKEN", | ||
envValue: "another-token-456", | ||
audience: "test-audience-4", | ||
want: &IDToken{Value: "another-token-456"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
desc: "Success - empty audience string", | ||
envName: "OIDC_TEST_TOKEN_NO_AUDIENCE", | ||
envValue: "token-no-audience", | ||
audience: "", | ||
want: &IDToken{Value: "token-no-audience"}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
t.Setenv(tc.envName, tc.envValue) | ||
|
||
ts := NewEnvIDTokenSource(tc.envName) | ||
got, gotErr := ts.IDToken(context.Background(), tc.audience) | ||
|
||
if tc.wantErr && gotErr == nil { | ||
t.Fatalf("IDToken() want error, got none") | ||
} | ||
if !tc.wantErr && gotErr != nil { | ||
t.Fatalf("IDToken() want no error, got error: %v", gotErr) | ||
} | ||
if !cmp.Equal(got, tc.want) { | ||
t.Errorf("IDToken() token = %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.