Skip to content

Commit 1982317

Browse files
authored
Use OIDC for tests (#4698)
## Changes Use newly implemented github-oidc auth type for running account-level tests and UC workspace-level AWS tests. No more long-lived service principal secrets! ## Tests Existing tests should continue to run. NO_CHANGELOG=true
1 parent 314622b commit 1982317

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

internal/acceptance/init.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ func ProvidersWithResourceFallbacks(resourceFallbacks []string) (*schema.Provide
180180

181181
// SdkV2ProviderForTest creates a test provider with the default config customizer.
182182
func SdkV2ProviderForTest(sdkV2Options ...sdkv2.SdkV2ProviderOption) *schema.Provider {
183-
opts := append(sdkV2Options, sdkv2.WithConfigCustomizer(DefaultConfigCustomizer))
183+
opts := append(sdkV2Options, sdkv2.WithConfigCustomizer(DefaultConfigCustomizer), sdkv2.WithConfigCustomizer(OidcConfigCustomizer))
184184
return sdkv2.DatabricksProvider(opts...)
185185
}
186186

187187
// PluginFrameworkProviderForTest creates a test provider with the default config customizer.
188188
func PluginFrameworkProviderForTest(pluginFwOptions ...pluginfw.PluginFrameworkOption) provider.Provider {
189-
opts := append(pluginFwOptions, pluginfw.WithConfigCustomizer(DefaultConfigCustomizer))
189+
opts := append(pluginFwOptions, pluginfw.WithConfigCustomizer(DefaultConfigCustomizer), pluginfw.WithConfigCustomizer(OidcConfigCustomizer))
190190
return pluginfw.GetDatabricksProviderPluginFramework(opts...)
191191
}
192192

@@ -294,6 +294,35 @@ func DefaultConfigCustomizer(cfg *config.Config) error {
294294
return nil
295295
}
296296

297+
// OidcConfigCustomizer customizes the SDK configuration to use OIDC when running in Github Actions without
298+
// busting Go test caching.
299+
// The Go test cache is busted when using OIDC because the URL and token in Github are different in each test run.
300+
// The environment variables are cleared in the action to prevent the Go SDK from reading them during test runs.
301+
// The resulting values are written to a hard-coded location, which we read from if present to use OIDC.
302+
// It is not an error if these files are not present.
303+
func OidcConfigCustomizer(cfg *config.Config) error {
304+
// This is a no-op for non-AWS and for non-UC AWS workspace environments because the OIDC auth is not supported.
305+
if !slices.Contains([]string{"MWS", "ucws", "ucacct"}, os.Getenv("CLOUD_ENV")) {
306+
return nil
307+
}
308+
if _, err := os.Stat("/tmp/ACTIONS_ID_TOKEN_REQUEST_URL"); err == nil {
309+
bs, err := os.ReadFile("/tmp/ACTIONS_ID_TOKEN_REQUEST_URL")
310+
if err != nil {
311+
return fmt.Errorf("cannot read /tmp/ACTIONS_ID_TOKEN_REQUEST_URL: %w", err)
312+
}
313+
cfg.ActionsIDTokenRequestURL = strings.TrimSpace(string(bs))
314+
}
315+
if _, err := os.Stat("/tmp/ACTIONS_ID_TOKEN_REQUEST_TOKEN"); err == nil {
316+
bs, err := os.ReadFile("/tmp/ACTIONS_ID_TOKEN_REQUEST_TOKEN")
317+
if err != nil {
318+
return fmt.Errorf("cannot read /tmp/ACTIONS_ID_TOKEN_REQUEST_TOKEN: %w", err)
319+
}
320+
cfg.ActionsIDTokenRequestToken = strings.TrimSpace(string(bs))
321+
}
322+
cfg.AuthType = "github-oidc"
323+
return nil
324+
}
325+
297326
// resourceCheck calls back a function with client and resource id
298327
func ResourceCheck(name string,
299328
cb func(ctx context.Context, client *common.DatabricksClient, id string) error) resource.TestCheckFunc {

0 commit comments

Comments
 (0)