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

Use the friendly name of service principals when shortening their name #1770

Merged
Merged
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
2 changes: 1 addition & 1 deletion bundle/config/mutator/populate_current_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag.
}

b.Config.Workspace.CurrentUser = &config.User{
ShortName: auth.GetShortUserName(me.UserName),
ShortName: auth.GetShortUserName(me),
User: me,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestAccBundleInitHelpers(t *testing.T) {
}{
{
funcName: "{{short_name}}",
expected: auth.GetShortUserName(me.UserName),
expected: auth.GetShortUserName(me),
},
{
funcName: "{{user_name}}",
Expand Down
9 changes: 7 additions & 2 deletions libs/auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"strings"

"github.com/databricks/cli/libs/textutil"
"github.com/databricks/databricks-sdk-go/service/iam"
)

// Get a short-form username, based on the user's primary email address.
// We leave the full range of unicode letters in tact, but remove all "special" characters,
// including dots, which are not supported in e.g. experiment names.
func GetShortUserName(emailAddress string) string {
local, _, _ := strings.Cut(emailAddress, "@")
func GetShortUserName(user *iam.User) string {
name := user.UserName
if IsServicePrincipal(user.UserName) && user.DisplayName != "" {
name = user.DisplayName
}
local, _, _ := strings.Cut(name, "@")
lennartkats-db marked this conversation as resolved.
Show resolved Hide resolved
return textutil.NormalizeString(local)
}
71 changes: 56 additions & 15 deletions libs/auth/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,111 @@ package auth
import (
"testing"

"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/stretchr/testify/assert"
)

func TestGetShortUserName(t *testing.T) {
tests := []struct {
name string
email string
user *iam.User
expected string
}{
{
email: "[email protected]",
user: &iam.User{
UserName: "[email protected]",
},
expected: "test_user_1234",
},
{
email: "tést.ü[email protected]",
user: &iam.User{
UserName: "tést.ü[email protected]",
},
expected: "tést_üser",
},
{
email: "[email protected]",
user: &iam.User{
UserName: "[email protected]",
},
expected: "test_user",
},
{
email: `jöhn.dœ@domain.com`, // Using non-ASCII characters.
user: &iam.User{
UserName: `jöhn.dœ@domain.com`, // Using non-ASCII characters.
},
expected: "jöhn_dœ",
},
{
email: `[email protected]`, // The plus (+) sign is used for "sub-addressing" in some email services.
user: &iam.User{
UserName: `[email protected]`, // The plus (+) sign is used for "sub-addressing" in some email services.
},
expected: "first_tag",
},
{
email: `[email protected]`, // Using a sub-domain.
user: &iam.User{
UserName: `[email protected]`, // Using a sub-domain.
},
expected: "email",
},
{
email: `"_quoted"@domain.com`, // Quoted strings can be part of the local-part.
user: &iam.User{
UserName: `"_quoted"@domain.com`, // Quoted strings can be part of the local-part.
},
expected: "quoted",
},
{
email: `name-o'[email protected]`, // Single quote in the local-part.
user: &iam.User{
UserName: `name-o'[email protected]`, // Single quote in the local-part.
},
expected: "name_o_mally",
},
{
email: `user%[email protected]`, // Percent sign can be used for email routing in legacy systems.
user: &iam.User{
UserName: `user%[email protected]`, // Percent sign can be used for email routing in legacy systems.
},
expected: "user_domain",
},
{
email: `[email protected]`, // Multiple dots in the local-part.
user: &iam.User{
UserName: `[email protected]`, // Multiple dots in the local-part.
},
expected: "long_name_with_dots",
},
{
email: `me&[email protected]`, // Using an ampersand (&) in the local-part.
user: &iam.User{
UserName: `me&[email protected]`, // Using an ampersand (&) in the local-part.
},
expected: "me_you",
},
{
email: `[email protected]`, // The exclamation mark can be valid in some legacy systems.
user: &iam.User{
UserName: `[email protected]`, // The exclamation mark can be valid in some legacy systems.
},
expected: "user_def_xyz",
},
{
email: `admin@ιντερνετ.com`, // Domain in non-ASCII characters (IDN or Internationalized Domain Name).
user: &iam.User{
UserName: `admin@ιντερνετ.com`, // Domain in non-ASCII characters (IDN or Internationalized Domain Name).
},
expected: "admin",
},
{
user: &iam.User{
UserName: `1706906c-c0a2-4c25-9f57-3a7aa3cb8123`,
DisplayName: "my-service-principal",
},
expected: "my_service_principal",
},
{
user: &iam.User{
UserName: `1706906c-c0a2-4c25-9f57-3a7aa3cb8123`,
// This service princpal has DisplayName (it's an optional property)
},
expected: "1706906c_c0a2_4c25_9f57_3a7aa3cb8123",
},
}

for _, tt := range tests {
assert.Equal(t, tt.expected, GetShortUserName(tt.email))
assert.Equal(t, tt.expected, GetShortUserName(tt.user))
}
}
2 changes: 1 addition & 1 deletion libs/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func loadHelpers(ctx context.Context) template.FuncMap {
return "", err
}
}
return auth.GetShortUserName(cachedUser.UserName), nil
return auth.GetShortUserName(cachedUser), nil
},
// Get the default workspace catalog. If there is no default, or if
// Unity Catalog is not enabled, return an empty string.
Expand Down