diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/conflict/databricks.yml b/acceptance/bundle/telemetry/config-remote-sync-error-message/conflict/databricks.yml new file mode 100644 index 00000000000..3584241403e --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/conflict/databricks.yml @@ -0,0 +1,15 @@ +bundle: + name: config-remote-sync-telemetry-host-conflict + +workspace: + host: https://other.test + +targets: + bad: + variables: + undefined_var: value + own-host: + workspace: + host: $DATABRICKS_HOST + variables: + undefined_var: value diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/databricks.yml b/acceptance/bundle/telemetry/config-remote-sync-error-message/databricks.yml new file mode 100644 index 00000000000..ced813ab3a0 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/databricks.yml @@ -0,0 +1,21 @@ +bundle: + name: config-remote-sync-telemetry-error-message + +variables: + myvar: + description: a required variable + +targets: + dev: + default: true + bad: + variables: + undefined_var: value + other: + workspace: + host: https://other.test + variables: + undefined_var: value + auth-error: + workspace: + client_id: x diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/default-target/databricks.yml b/acceptance/bundle/telemetry/config-remote-sync-error-message/default-target/databricks.yml new file mode 100644 index 00000000000..ea55df91dbb --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/default-target/databricks.yml @@ -0,0 +1,11 @@ +bundle: + name: config-remote-sync-telemetry-default-target + +targets: + bad: + default: true + variables: + undefined_var: value + other: + workspace: + host: https://other.test diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/other-workspace/databricks.yml b/acceptance/bundle/telemetry/config-remote-sync-error-message/other-workspace/databricks.yml new file mode 100644 index 00000000000..cba3a6f717e --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/other-workspace/databricks.yml @@ -0,0 +1,16 @@ +bundle: + name: config-remote-sync-telemetry-other-workspace + +workspace: + host: $DATABRICKS_HOST + workspace_id: "1111" + +targets: + bad: + variables: + undefined_var: value + profile-workspace: + workspace: + workspace_id: "2222" + variables: + undefined_var: value diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/out.test.toml b/acceptance/bundle/telemetry/config-remote-sync-error-message/out.test.toml new file mode 100644 index 00000000000..aadf9e4a151 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/out.test.toml @@ -0,0 +1,3 @@ +Cloud = false +GOOS.windows = false +EnvMatrix.DMS = [""] diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/output.txt b/acceptance/bundle/telemetry/config-remote-sync-error-message/output.txt new file mode 100644 index 00000000000..16a2dac71a4 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/output.txt @@ -0,0 +1,115 @@ + +>>> musterr [CLI] bundle config-remote-sync +Error: no value assigned to required variable myvar. Variables are usually assigned in databricks.yml, and they can be overridden using "--var", the BUNDLE_VAR_myvar environment variable, or .databricks/bundle//variable-overrides.json + + +>>> print_requests.py //telemetry-ext +{ + "error_message": "no value assigned to required variable myvar. Variables are usually assigned in databricks.yml, and they can be overridden using \"--var\", the BUNDLE_VAR_myvar environment variable, or [REDACTED_REL_PATH](json)", + "error_category": "BUNDLE_LOAD_FAILED" +} + +>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> print_requests.py //telemetry-ext +{ + "error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} + +>>> musterr [CLI] bundle config-remote-sync -t bad +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t own-host -p myprofile +Error: failed to perform target override for target=own-host: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t profile-workspace -p workspace2222 +Error: failed to perform target override for target=profile-workspace: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -p myprofile +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> print_requests.py //telemetry-ext +{ + "error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} +{ + "error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} +{ + "error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} +{ + "error_message": "failed to perform target override for target=own-host: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} +{ + "error_message": "failed to perform target override for target=profile-workspace: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} +{ + "error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value", + "error_category": "BUNDLE_LOAD_FAILED" +} + +>>> musterr [CLI] bundle config-remote-sync -p myprofile +Error: Invalid bundle root DATABRICKS_BUNDLE_ROOT="missing": stat missing: no such file or directory + +The DATABRICKS_BUNDLE_ROOT environment variable must point to an existing directory that contains a bundle configuration file. + + +>>> print_requests.py //telemetry-ext +{ + "error_message": "Invalid bundle root DATABRICKS_BUNDLE_ROOT=\"missing\": stat missing: no such file or directory", + "error_category": "BUNDLE_LOAD_FAILED" +} + +>>> musterr [CLI] bundle config-remote-sync -t bad +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t bad -p missing +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t bad -p workspace2222 +Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t other -p myprofile +Error: failed to perform target override for target=other: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -p myprofile +Error: failed to perform target override for target=other: variable undefined_var is not defined but is assigned a value + + +>>> musterr [CLI] bundle config-remote-sync -t auth-error -p myprofile +Error: cannot resolve bundle auth configuration: validate: more than one authorization method configured: oauth and pat. Config: host=[DATABRICKS_URL], token=***, profile=myprofile, config_file=[TEST_TMP_DIR]/.databrickscfg, client_id=x, databricks_cli_path=[CLI]. Env: DATABRICKS_HOST, DATABRICKS_TOKEN, DATABRICKS_CONFIG_FILE, DATABRICKS_CLI_PATH + + +>>> errcode print_requests.py //telemetry-ext +File [OUTPUT_DIR]/out.requests.txt not found + +Exit code: 1 diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/same-host/databricks.yml b/acceptance/bundle/telemetry/config-remote-sync-error-message/same-host/databricks.yml new file mode 100644 index 00000000000..a34914c9fa7 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/same-host/databricks.yml @@ -0,0 +1,10 @@ +bundle: + name: config-remote-sync-telemetry-same-host + +workspace: + host: $DATABRICKS_HOST + +targets: + bad: + variables: + undefined_var: value diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/script b/acceptance/bundle/telemetry/config-remote-sync-error-message/script new file mode 100644 index 00000000000..7855baf7785 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/script @@ -0,0 +1,53 @@ +# The diagnostic is already printed, so telemetry records it instead of the AlreadyPrinted sentinel. +trace musterr $CLI bundle config-remote-sync + +trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}' + +cat > .databrickscfg < out && mv out same-host/databricks.yml +withdir same-host trace musterr $CLI bundle config-remote-sync -t bad -p myprofile +DATABRICKS_HOST=https://other.test withdir same-host trace musterr $CLI bundle config-remote-sync -t bad -p myprofile +envsubst < conflict/databricks.yml > out && mv out conflict/databricks.yml +withdir conflict trace musterr $CLI bundle config-remote-sync -t own-host -p myprofile +envsubst < other-workspace/databricks.yml > out && mv out other-workspace/databricks.yml +withdir other-workspace trace musterr $CLI bundle config-remote-sync -t profile-workspace -p workspace2222 +withdir default-target trace musterr $CLI bundle config-remote-sync -p myprofile + +trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}' + +# So is a bundle that is not found at all. +DATABRICKS_BUNDLE_ROOT=missing trace musterr $CLI bundle config-remote-sync -p myprofile + +trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}' + +# Without a profile, with one that does not resolve, with one for another host than the bundle's or the target's +# or for another workspace on the bundle's host, or when the bundle's own auth fails, nothing is sent. +trace musterr $CLI bundle config-remote-sync -t bad +trace musterr $CLI bundle config-remote-sync -t bad -p missing +withdir conflict trace musterr $CLI bundle config-remote-sync -t bad -p myprofile +withdir other-workspace trace musterr $CLI bundle config-remote-sync -t bad -p workspace2222 +trace musterr $CLI bundle config-remote-sync -t other -p myprofile +DATABRICKS_BUNDLE_TARGET=other trace musterr $CLI bundle config-remote-sync -p myprofile +trace musterr $CLI bundle config-remote-sync -t auth-error -p myprofile + +trace errcode print_requests.py //telemetry-ext diff --git a/acceptance/bundle/telemetry/config-remote-sync-error-message/test.toml b/acceptance/bundle/telemetry/config-remote-sync-error-message/test.toml new file mode 100644 index 00000000000..e94f71f3c37 --- /dev/null +++ b/acceptance/bundle/telemetry/config-remote-sync-error-message/test.toml @@ -0,0 +1,5 @@ +# config-remote-sync is not supported on Windows. +GOOS.windows = false + +# The script writes .databrickscfg. +Ignore = [".databrickscfg"] diff --git a/bundle/configsync/output.go b/bundle/configsync/output.go index 2e61e618b4e..35459aadc77 100644 --- a/bundle/configsync/output.go +++ b/bundle/configsync/output.go @@ -91,7 +91,9 @@ func WriteResult(out io.Writer, jsonOutput bool, stats *Stats, files []FileChang if stats.ErrorCategory == "" { stats.ErrorCategory = protos.BundleConfigRemoteSyncErrorCategoryBundleLoadFailed } - stats.ErrorMessage = telemetry.ScrubErrorMessage(err.Error()) + if stats.ErrorMessage == "" { + stats.ErrorMessage = telemetry.ScrubErrorMessage(err.Error()) + } // A selector that matched nothing: nothing to sync, not a failure. if errors.Is(err, ErrNoMatchingSelector) { status = StatusSkipped diff --git a/bundle/configsync/telemetry.go b/bundle/configsync/telemetry.go index 4370dded6dd..935f5713c55 100644 --- a/bundle/configsync/telemetry.go +++ b/bundle/configsync/telemetry.go @@ -1,17 +1,27 @@ package configsync import ( + "cmp" "context" "slices" "strings" + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/config/engine" "github.com/databricks/cli/bundle/direct/dresources" + bundleenv "github.com/databricks/cli/bundle/env" "github.com/databricks/cli/bundle/statemgmt" + "github.com/databricks/cli/libs/auth" + "github.com/databricks/cli/libs/cmdctx" + "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/structs/structpath" "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" + sdkconfig "github.com/databricks/databricks-sdk-go/config" ) // recoverTelemetry swallows any panic raised while collecting or emitting @@ -283,6 +293,89 @@ func (s *Stats) CollectStateIDs(typeAndIDs []string) { s.StateResourceIDs = collectResourceIDs(typeAndIDs) } +// WithProfileAuth sets the auth config from an explicitly selected profile when +// loading the bundle or its target failed before the bundle set its own, so the +// failure can still be reported. Without the target its workspace is unknown, so +// nothing is set without a profile or when the profile is not found. +// Only the profile is used: the bundle config can name other hosts, and +// telemetry must not be sent there. The config is left for the upload to +// resolve within its own timeout, and as the upload may be cut off at exit, its +// auth never refreshes an OAuth token. +func WithProfileAuth(ctx context.Context, b *bundle.Bundle, profileName, target string) (out context.Context) { + out = ctx + defer recoverTelemetry(ctx) + if cmdctx.HasConfigUsed(ctx) || telemetry.Disabled(ctx) { + return ctx + } + // With the target selected, only the bundle's own auth can have failed, and + // auth errors are not reported. + if b != nil && b.Config.Bundle.Target != "" { + log.Debugf(ctx, "config-remote-sync telemetry skipped: bundle auth failed") + return ctx + } + profileName = cmp.Or(profileName, env.Get(ctx, "DATABRICKS_CONFIG_PROFILE")) + if profileName == "" { + log.Debugf(ctx, "config-remote-sync telemetry skipped: no profile selected") + return ctx + } + profiles, err := profile.GetProfiler(ctx).LoadProfiles(ctx, profile.WithName(profileName)) + if err != nil || len(profiles) == 0 { + log.Debugf(ctx, "config-remote-sync telemetry skipped: profile %s not found", profileName) + return ctx + } + // As in bundle auth, a profile for another host than the bundle's is rejected, + // and so is one for another workspace on the same unified host. + if b != nil { + host, id := b.Config.Workspace.Host, b.Config.Workspace.WorkspaceID + // The target was not merged, so its own workspace is not in the root config yet. + if t := b.Config.Targets[selectedTarget(ctx, b, target)]; t != nil && t.Workspace != nil { + host, id = cmp.Or(t.Workspace.Host, host), cmp.Or(t.Workspace.WorkspaceID, id) + } + p := profiles[0] + bundleID, profileID := workspaceID(host, id), workspaceID(p.Host, p.WorkspaceID) + if host != "" && canonicalHost(host) != canonicalHost(p.Host) || bundleID != "" && profileID != "" && bundleID != profileID { + log.Debugf(ctx, "config-remote-sync telemetry skipped: profile %s is for another workspace than the bundle's %s", profileName, host) + return ctx + } + } + cfg := (&config.Workspace{Profile: profileName}).Config(ctx) + // As for a bundle's own profile, the profile wins over auth env vars, so the + // upload goes to the host checked above. + cfg.Loaders = databrickscfg.ProfileAuthLoaders + cfg.Credentials = auth.CredentialsWithoutRefresh() + return cmdctx.SetConfigUsed(ctx, cfg) +} + +// workspaceID returns the explicit workspace ID, else the one in the host URL. +func workspaceID(host, id string) string { + return cmp.Or(id, auth.ExtractHostQueryParams(host).WorkspaceID) +} + +func canonicalHost(host string) string { + return (&sdkconfig.Config{Host: host}).CanonicalHostName() +} + +// selectedTarget returns the target loading the bundle selects: the named one, +// else DATABRICKS_BUNDLE_TARGET, else the only target or the one marked default. +func selectedTarget(ctx context.Context, b *bundle.Bundle, name string) string { + if name == "" { + name, _ = bundleenv.Target(ctx) + } + if name != "" { + return name + } + var defaults []string + for n, t := range b.Config.Targets { + if len(b.Config.Targets) == 1 || (t != nil && config.IsExplicitlyEnabled(t.Default)) { + defaults = append(defaults, n) + } + } + if len(defaults) != 1 { + return "" + } + return defaults[0] +} + // LogTelemetry emits the BundleConfigRemoteSyncEvent for this run. func (s *Stats) LogTelemetry(ctx context.Context) { defer recoverTelemetry(ctx) diff --git a/cmd/bundle/config_remote_sync.go b/cmd/bundle/config_remote_sync.go index 972810d6f0f..8813cc234d3 100644 --- a/cmd/bundle/config_remote_sync.go +++ b/cmd/bundle/config_remote_sync.go @@ -1,6 +1,7 @@ package bundle import ( + "cmp" "context" "errors" "fmt" @@ -18,6 +19,8 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/log" + "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/cli/libs/telemetry" "github.com/databricks/cli/libs/telemetry/protos" "github.com/spf13/cobra" ) @@ -180,6 +183,13 @@ Examples: if b != nil { stats.InGitRepo = b.InGitRepo } + target := cmp.Or(cmd.Flag("target").Value.String(), cmd.Flag("environment").Value.String()) + cmd.SetContext(configsync.WithProfileAuth(cmd.Context(), b, cmd.Flag("profile").Value.String(), target)) + + // The diagnostics are already on stderr; record the first one instead of the sentinel. + if errors.Is(err, root.ErrAlreadyPrinted) { + stats.ErrorMessage = telemetry.ScrubErrorMessage(logdiag.GetFirstErrorSummary(cmd.Context())) + } return configsync.WriteResult(cmd.OutOrStdout(), root.OutputType(cmd) == flags.OutputJSON, &stats, files, changes, skippedChanges, err) }