From 47a9ca1a1494214da037f83cf0213b181f60de0d Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Fri, 20 Dec 2024 15:46:06 +0530 Subject: [PATCH] Require authentication when a user runs bundle init --- libs/template/helpers.go | 21 --------------------- libs/template/materialize.go | 10 ++++++++++ 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 4550e5fa2b..b4642f400b 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -110,13 +110,6 @@ 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 - } - } result := cachedUser.UserName if result == "" { result = cachedUser.Id @@ -124,13 +117,6 @@ func loadHelpers(ctx context.Context) template.FuncMap { return result, nil }, "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 }, // Get the default workspace catalog. If there is no default, or if @@ -156,13 +142,6 @@ func loadHelpers(ctx context.Context) template.FuncMap { if cachedIsServicePrincipal != nil { return *cachedIsServicePrincipal, nil } - if cachedUser == nil { - var err error - cachedUser, err = w.CurrentUser.Me(ctx) - if err != nil { - return false, err - } - } result := iamutil.IsServicePrincipal(cachedUser) cachedIsServicePrincipal = &result return result, nil 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 {