diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 4550e5fa2b..863a30cc8d 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -109,29 +109,15 @@ func loadHelpers(ctx context.Context) template.FuncMap { } return w.Config.Host, nil }, - "user_name": func() (string, error) { - if cachedUser == nil { - var err error - cachedUser, err = w.CurrentUser.Me(ctx) - if err != nil { - return "", err - } - } + "user_name": func() string { result := cachedUser.UserName if result == "" { result = cachedUser.Id } - return result, nil + return result }, - "short_name": func() (string, error) { - if cachedUser == nil { - var err error - cachedUser, err = w.CurrentUser.Me(ctx) - if err != nil { - return "", err - } - } - return iamutil.GetShortUserName(cachedUser), nil + "short_name": func() string { + return iamutil.GetShortUserName(cachedUser) }, // Get the default workspace catalog. If there is no default, or if // Unity Catalog is not enabled, return an empty string. @@ -152,20 +138,13 @@ func loadHelpers(ctx context.Context) template.FuncMap { } return *cachedCatalog, nil }, - "is_service_principal": func() (bool, error) { + "is_service_principal": func() bool { if cachedIsServicePrincipal != nil { - return *cachedIsServicePrincipal, nil - } - if cachedUser == nil { - var err error - cachedUser, err = w.CurrentUser.Me(ctx) - if err != nil { - return false, err - } + return *cachedIsServicePrincipal } result := iamutil.IsServicePrincipal(cachedUser) cachedIsServicePrincipal = &result - return result, nil + return result }, } } diff --git a/libs/template/materialize.go b/libs/template/materialize.go index 86a6a8c37a..3509547838 100644 --- a/libs/template/materialize.go +++ b/libs/template/materialize.go @@ -6,6 +6,7 @@ import ( "fmt" "io/fs" + "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/filer" ) @@ -42,6 +43,15 @@ func Materialize(ctx context.Context, configFilePath string, templateFS fs.FS, o } } + // Assert that the user is authenticated when materializing a template. We restrict + // usage of templates to authenticated users to log telemetry, even if the template + // does not require any authenticated functionality. + w := root.WorkspaceClient(ctx) + cachedUser, err = w.CurrentUser.Me(ctx) + if err != nil { + return fmt.Errorf("could not fetch information about the current user: %w", err) + } + helpers := loadHelpers(ctx) r, err := newRenderer(ctx, config.values, helpers, templateFS, templateDirName, libraryDirName) if err != nil {