Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `serving-endpoints get-permissions`, `set-permissions` and `update-permissions` now also accept the endpoint name, and resolve it to the endpoint ID. Passing a name previously failed with `is not a valid Inference Endpoint ID`. ([#7662](https://github.com/databricks/cli/pull/7662))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

=== Permissions commands accept the endpoint name

>>> [CLI] serving-endpoints set-permissions my-endpoint --json {"access_control_list": [{"group_name": "users", "permission_level": "CAN_QUERY"}]}
{"group_name":"users","permission_level":"CAN_QUERY"}

>>> [CLI] serving-endpoints get-permissions my-endpoint
{"object_id":"/serving-endpoints/[ENDPOINT_ID]"}

=== A 32-character ID and root are passed through with no lookup

>>> [CLI] serving-endpoints get-permissions 0123456789abcdef0123456789ABCDEF
{"object_id":"/serving-endpoints/0123456789abcdef0123456789ABCDEF"}

>>> [CLI] serving-endpoints get-permissions root
{"object_id":"/serving-endpoints/root"}

=== A name that doesn't resolve returns the lookup error

>>> musterr [CLI] serving-endpoints get-permissions no-such-endpoint
Error: "no-such-endpoint" is not a serving endpoint ID, and looking it up as an endpoint name failed: Resource serving.ServingEndpointDetailed not found: no-such-endpoint

=== Help shows the name is accepted

>>> [CLI] serving-endpoints get-permissions --help
NAME_OR_ID: The name or ID of the serving endpoint for which to get or manage
databricks serving-endpoints get-permissions NAME_OR_ID [flags]

>>> print_requests.py //serving-endpoints //permissions --get
{
"method": "POST",
"path": "/api/2.0/serving-endpoints",
"body": {
"name": "my-endpoint"
}
}
{
"method": "GET",
"path": "/api/2.0/serving-endpoints/my-endpoint"
}
{
"method": "GET",
"path": "/api/2.0/serving-endpoints/my-endpoint"
}
{
"method": "PUT",
"path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]",
"body": {
"access_control_list": [
{
"group_name": "users",
"permission_level": "CAN_QUERY"
}
]
}
}
{
"method": "GET",
"path": "/api/2.0/serving-endpoints/my-endpoint"
}
{
"method": "GET",
"path": "/api/2.0/permissions/serving-endpoints/[ENDPOINT_ID]"
}
{
"method": "GET",
"path": "/api/2.0/permissions/serving-endpoints/0123456789abcdef0123456789ABCDEF"
}
{
"method": "GET",
"path": "/api/2.0/permissions/serving-endpoints/root"
}
{
"method": "GET",
"path": "/api/2.0/serving-endpoints/no-such-endpoint"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$CLI serving-endpoints create my-endpoint --no-wait > /dev/null
endpoint_id=$($CLI serving-endpoints get my-endpoint | jq -r .id)
add_repl "$endpoint_id" ENDPOINT_ID


title "Permissions commands accept the endpoint name"
trace $CLI serving-endpoints set-permissions my-endpoint --json '{"access_control_list": [{"group_name": "users", "permission_level": "CAN_QUERY"}]}' | jq -c '.access_control_list[] | {group_name, permission_level: .all_permissions[0].permission_level}'
trace $CLI serving-endpoints get-permissions my-endpoint | jq -c '{object_id}'

title "A 32-character ID and root are passed through with no lookup"
trace $CLI serving-endpoints get-permissions 0123456789abcdef0123456789ABCDEF | jq -c '{object_id}'
trace $CLI serving-endpoints get-permissions root | jq -c '{object_id}'

title "A name that doesn't resolve returns the lookup error"
trace musterr $CLI serving-endpoints get-permissions no-such-endpoint

title "Help shows the name is accepted"
trace $CLI serving-endpoints get-permissions --help | grep -E 'get-permissions NAME_OR_ID|NAME_OR_ID: '

trace print_requests.py //serving-endpoints //permissions --get
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
RecordRequests = true
3 changes: 2 additions & 1 deletion bundle/direct/dresources/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,9 @@ var testDeps = map[string]prepareWorkspace{
return nil, err
}

// The permissions API takes the endpoint's ID, not its name.
return &PermissionsState{
ObjectID: "/serving-endpoints/" + waiter.Response.Name,
ObjectID: "/serving-endpoints/" + waiter.Response.Id,
EmbeddedSlice: []StatePermission{{
Level: "CAN_MANAGE",
UserName: "user@example.com",
Expand Down
85 changes: 85 additions & 0 deletions cmd/workspace/serving-endpoints/permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package serving_endpoints

import (
"context"
"fmt"
"strings"

"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/spf13/cobra"
)

const (
// endpointIDLength is the length of a serving endpoint ID (32 hex characters).
endpointIDLength = 32
// rootEndpointID is the ID of the root object that endpoints inherit permissions from.
rootEndpointID = "root"
)

// isEndpointID reports whether s is a value the permissions API accepts as an
// endpoint ID: "root" or 32 hex characters, matching the server's validation.
func isEndpointID(s string) bool {
if s == rootEndpointID {
return true
}
if len(s) != endpointIDLength {
return false
}
for _, r := range s {
if !strings.ContainsRune("0123456789abcdefABCDEF", r) {
return false
}
}
return true
}

// resolveEndpointID returns the ID of the endpoint named arg. The permissions
// API only accepts endpoint IDs, and anything else fails with "is not a valid
// Inference Endpoint ID", but users and agents know endpoints by name. Values
// shaped like an ID are passed through unchanged. Any other value can't be an
// ID the API accepts, so if the name lookup fails we return the lookup error,
// which says what is actually wrong (a typo, a missing permission), instead of
// sending the value on and getting the API's ID error.
func resolveEndpointID(ctx context.Context, arg string) (string, error) {
if isEndpointID(arg) {
return arg, nil
}
w := cmdctx.WorkspaceClient(ctx)
endpoint, err := w.ServingEndpoints.GetByName(ctx, arg)
if err != nil {
return "", fmt.Errorf("%q is not a serving endpoint ID, and looking it up as an endpoint name failed: %w", arg, err)
}
if endpoint.Id == "" {
// Endpoints without an ID, such as Foundation Model API pay-per-token
// endpoints, have no permissions object of their own.
return "", fmt.Errorf("serving endpoint %q has no endpoint ID, so its permissions can't be read or changed with this command", arg)
}
return endpoint.Id, nil
}

// permissionsOverride lets a permissions command take an endpoint name as well
// as an ID. It's generic over the request type of each permissions command.
func permissionsOverride[T any](cmd *cobra.Command, _ T) {
cmd.Use = strings.Replace(cmd.Use, "SERVING_ENDPOINT_ID", "NAME_OR_ID", 1)
cmd.Long = strings.Replace(cmd.Long,
"SERVING_ENDPOINT_ID: The serving endpoint for which to get or manage permissions.",
"NAME_OR_ID: The name or ID of the serving endpoint for which to get or manage\n permissions. A name is resolved to the ID with \"serving-endpoints get\".", 1)
generatedRunE := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
id, err := resolveEndpointID(cmd.Context(), args[0])
if err != nil {
return err
}
args = append([]string{id}, args[1:]...)
}
return generatedRunE(cmd, args)
}
}

func init() {
getPermissionsOverrides = append(getPermissionsOverrides, permissionsOverride[*serving.GetServingEndpointPermissionsRequest])
setPermissionsOverrides = append(setPermissionsOverrides, permissionsOverride[*serving.ServingEndpointPermissionsRequest])
updatePermissionsOverrides = append(updatePermissionsOverrides, permissionsOverride[*serving.ServingEndpointPermissionsRequest])
}
71 changes: 71 additions & 0 deletions cmd/workspace/serving-endpoints/permissions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package serving_endpoints

import (
"errors"
"testing"

"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/databricks-sdk-go/experimental/mocks"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestIsEndpointID(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"31ee674b7e6643f6930d5e12148fbe6f", true},
{"31EE674B7E6643F6930D5E12148FBE6F", true},
{"root", true},
{"31ee674b-7e66-43f6-930d-5e12148fbe6f", false},
{"31ee674b7e6643f6930d5e12148fbe6", false},
{"my-endpoint", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
assert.Equal(t, tt.want, isEndpointID(tt.in))
})
}
}

func TestResolveEndpointID(t *testing.T) {
lookupErr := errors.New("Endpoint with name 'no-such-endpoint' does not exist.")
tests := []struct {
name string
arg string
endpoint *serving.ServingEndpointDetailed
err error
want string
wantErr string
}{
{name: "an ID is passed through", arg: "31ee674b7e6643f6930d5e12148fbe6f", want: "31ee674b7e6643f6930d5e12148fbe6f"},
{name: "root is passed through", arg: "root", want: "root"},
{name: "a name resolves to its ID", arg: "my-endpoint", endpoint: &serving.ServingEndpointDetailed{Id: "31ee674b7e6643f6930d5e12148fbe6f"}, want: "31ee674b7e6643f6930d5e12148fbe6f"},
{name: "a failed lookup returns the lookup error", arg: "no-such-endpoint", err: lookupErr, wantErr: `"no-such-endpoint" is not a serving endpoint ID, and looking it up as an endpoint name failed: Endpoint with name 'no-such-endpoint' does not exist.`},
{name: "an endpoint without an ID is an error", arg: "databricks-gpt-5-4", endpoint: &serving.ServingEndpointDetailed{Name: "databricks-gpt-5-4"}, wantErr: `serving endpoint "databricks-gpt-5-4" has no endpoint ID`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := mocks.NewMockWorkspaceClient(t)
if tt.endpoint != nil || tt.err != nil {
m.GetMockServingEndpointsAPI().EXPECT().GetByName(mock.Anything, tt.arg).Return(tt.endpoint, tt.err)
}
ctx := cmdctx.SetWorkspaceClient(t.Context(), m.WorkspaceClient)

got, err := resolveEndpointID(ctx, tt.arg)
if tt.wantErr != "" {
require.ErrorContains(t, err, tt.wantErr)
if tt.err != nil {
assert.ErrorIs(t, err, tt.err)
}
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
33 changes: 33 additions & 0 deletions libs/testserver/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"slices"
"strconv"
"strings"
"uuid"

"github.com/databricks/databricks-sdk-go/service/iam"
)
Expand Down Expand Up @@ -151,6 +152,10 @@ func (s *FakeWorkspace) GetPermissions(req Request) any {
}
}

if resp := servingEndpointIDError(requestObjectType, objectId); resp != nil {
return *resp
}

responseObjectID := fmt.Sprintf("/%s/%s", requestObjectType, objectId)

// V2 permissions APIs cascade-delete ACLs with the parent, so the cloud
Expand Down Expand Up @@ -198,6 +203,30 @@ func (s *FakeWorkspace) permissionsParentExists(requestObjectType, objectId stri
return true
}

// servingEndpointIDError mirrors the permissions API, which rejects a value
// that isn't shaped like a serving endpoint ID, such as the endpoint's name.
// The real API accepts "root" or 32 hex characters (InferenceEndpointId). This
// fake also accepts dashed UUIDs, because that is the form nextUUID mints for
// its endpoint IDs; uuid.Parse accepts both that and the 32-character form.
func servingEndpointIDError(requestObjectType, objectId string) *Response {
if requestObjectType != "serving-endpoints" {
return nil
}
if objectId == "root" {
return nil
}
if _, err := uuid.Parse(objectId); err == nil {
Comment thread
Sankalp-Mittal marked this conversation as resolved.
Resolved
return nil
}
return &Response{
StatusCode: 400,
Body: map[string]string{
"error_code": "INVALID_PARAMETER_VALUE",
"message": fmt.Sprintf("'%s' is not a valid Inference Endpoint ID.", objectId),
},
}
}

func (s *FakeWorkspace) SetPermissions(req Request) any {
defer s.LockUnlock()()

Expand Down Expand Up @@ -230,6 +259,10 @@ func (s *FakeWorkspace) SetPermissions(req Request) any {
}
}

if resp := servingEndpointIDError(requestObjectType, objectId); resp != nil {
return *resp
}

var updateRequest iam.UpdateObjectPermissions
if err := json.Unmarshal(req.Body, &updateRequest); err != nil {
return Response{
Expand Down
Loading