Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed May 17, 2024
1 parent 3ce2dd3 commit 94b78ca
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
5 changes: 3 additions & 2 deletions apierr/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ func GetAPIError(ctx context.Context, resp common.ResponseWrapper) error {
return apiError
}
// Attempts to access private link workspaces are redirected to the login page with a specific query parameter.
if strings.Contains(resp.Response.Request.URL.RawQuery, "error=private-link-validation-error") {
return PrivateLinkValidationError(resp.Response.Request.URL)
requestUrl := resp.Response.Request.URL
if isPrivateLinkRedirect(requestUrl) {
return privateLinkValidationError(requestUrl)
}
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions apierr/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func TestApiErrorMapsPrivateLinkRedirect(t *testing.T) {
Response: &http.Response{
Request: &http.Request{
URL: &url.URL{
Host: "adb-12345678910.1.azuredatabricks.net",
Host: "adb-12345678910.1.azuredatabricks.net",
Path: "/login.html",
RawQuery: "error=private-link-validation-error",
},
},
Expand All @@ -71,4 +72,4 @@ func TestApiErrorMapsPrivateLinkRedirect(t *testing.T) {
err := GetAPIError(context.Background(), resp)
assert.ErrorIs(t, err, ErrPermissionDenied)
assert.Equal(t, err.(*APIError).ErrorCode, "PRIVATE_LINK_VALIDATION_ERROR")
}
}
26 changes: 23 additions & 3 deletions apierr/private_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ import (
"github.com/databricks/databricks-sdk-go/common/environment"
)

// Metadata about the private link product. Private link redirects users to the
// login page with a query parameter that indicates the error. This struct
// contains information about the private link service, the endpoint name, and a
// reference page for more information.
//
// Eventually, the REST API should return an error directly when a request is
// made from a network that does not have access to the workspace. Once that
// happens, this struct can be removed.
type privateLinkInfo struct {
serviceName string
endpointName string
// The name of the private link service (e.g. AWS PrivateLink, Azure Private
// Link, etc.)
serviceName string

// The name of the private link endpoint (e.g. AWS VPC endpoint, Azure Private
// Link endpoint, etc.)
endpointName string

// A reference page for more information about the private link service.
referencePage string
}

Expand All @@ -23,6 +38,7 @@ device has access to the %s. For more information, see %s.`,
return strings.ReplaceAll(privateLinkValidationError, "\n", " ")
}

// Map of private link information by cloud.
var privateLinkInfoMap = map[environment.Cloud]privateLinkInfo{
environment.CloudAWS: {
serviceName: "AWS PrivateLink",
Expand All @@ -41,7 +57,11 @@ var privateLinkInfoMap = map[environment.Cloud]privateLinkInfo{
},
}

func PrivateLinkValidationError(url *url.URL) *APIError {
func isPrivateLinkRedirect(url *url.URL) bool {
return strings.Contains(url.RawQuery, "error=private-link-validation-error") && url.EscapedPath() == "/login.html"
}

func privateLinkValidationError(url *url.URL) *APIError {
env := environment.GetEnvironmentForHostname(url.Host)
info := privateLinkInfoMap[env.Cloud]
return &APIError{
Expand Down
3 changes: 1 addition & 2 deletions common/environment/azure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package environment


type azureEnvironment struct {
Name string `json:"name"`
ServiceManagementEndpoint string `json:"serviceManagementEndpoint"`
Expand Down Expand Up @@ -30,4 +29,4 @@ var (
ResourceManagerEndpoint: "https://management.chinacloudapi.cn/",
ActiveDirectoryEndpoint: "https://login.chinacloudapi.cn/",
}
)
)

0 comments on commit 94b78ca

Please sign in to comment.