Skip to content
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

Require authentication when a user runs bundle init #2040

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions libs/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go text templates use the error returned in the second value as purely a signal to terminate materializing the template. The second value is not something that a user template can access and, thus, should be safe to remove from the type signature here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
},
}
}
10 changes: 10 additions & 0 deletions libs/template/materialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
Loading