|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/databricks/cli/libs/env" |
| 17 | + "github.com/databricks/cli/libs/testserver" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +const dmsAPIPath = "/api/2.0/bundle/deployments" |
| 22 | + |
| 23 | +// Deployment TreeNodes use the generic workspace-objects permissions API. |
| 24 | +var liteboxPermissionTypes = []string{"directories", "files", "notebooks", "workspace-objects"} |
| 25 | + |
| 26 | +// configureDMSLitebox routes DMS to the local service and workspace files/ACLs |
| 27 | +// to its shared fakes, retaining request recording and mocks for other APIs. |
| 28 | +func configureDMSLitebox(t *testing.T, server *testserver.Server) { |
| 29 | + t.Helper() |
| 30 | + endpoint := env.Get(t.Context(), "DMS_LITEBOX_URL") |
| 31 | + if endpoint == "" { |
| 32 | + return |
| 33 | + } |
| 34 | + dmsURL := liteboxURL(t, "DMS_LITEBOX_URL") |
| 35 | + workspaceURL := liteboxURL(t, "DMS_LITEBOX_WORKSPACE_URL") |
| 36 | + |
| 37 | + certFile, keyFile := env.Get(t.Context(), "DMS_LITEBOX_CERT"), env.Get(t.Context(), "DMS_LITEBOX_KEY") |
| 38 | + caFile := env.Get(t.Context(), "DMS_LITEBOX_CA") |
| 39 | + require.NotEmpty(t, certFile, "DMS_LITEBOX_CERT is required with DMS_LITEBOX_URL") |
| 40 | + require.NotEmpty(t, keyFile, "DMS_LITEBOX_KEY is required with DMS_LITEBOX_URL") |
| 41 | + require.NotEmpty(t, caFile, "DMS_LITEBOX_CA is required with DMS_LITEBOX_URL") |
| 42 | + cert, err := tls.LoadX509KeyPair(certFile, keyFile) |
| 43 | + require.NoError(t, err, "load Litebox client certificate and key") |
| 44 | + pem, err := os.ReadFile(caFile) |
| 45 | + require.NoError(t, err, "read DMS_LITEBOX_CA") |
| 46 | + roots := x509.NewCertPool() |
| 47 | + require.True(t, roots.AppendCertsFromPEM(pem), "invalid Litebox certificate") |
| 48 | + // Both upstreams must use LITE's test certificate for *.svc.cluster.local. |
| 49 | + transport := &http.Transport{ |
| 50 | + TLSClientConfig: &tls.Config{ |
| 51 | + MinVersion: tls.VersionTLS12, |
| 52 | + Certificates: []tls.Certificate{cert}, |
| 53 | + RootCAs: roots, |
| 54 | + ServerName: "deployment-metadata-service.svc.cluster.local", |
| 55 | + }, |
| 56 | + ForceAttemptHTTP2: true, |
| 57 | + } |
| 58 | + t.Cleanup(transport.CloseIdleConnections) |
| 59 | + client := &http.Client{ |
| 60 | + Transport: transport, |
| 61 | + Timeout: 30 * time.Second, |
| 62 | + CheckRedirect: func(_ *http.Request, _ []*http.Request) error { |
| 63 | + return http.ErrUseLastResponse |
| 64 | + }, |
| 65 | + } |
| 66 | + handler := func(r testserver.Request) any { |
| 67 | + target := *workspaceURL |
| 68 | + if isDMSPath(r.URL.Path) { |
| 69 | + target = *dmsURL |
| 70 | + } |
| 71 | + target.Path, target.RawPath, target.RawQuery = r.URL.Path, r.URL.RawPath, r.URL.RawQuery |
| 72 | + req, err := http.NewRequestWithContext(r.Context, r.Method, target.String(), bytes.NewReader(r.Body)) |
| 73 | + if err != nil { |
| 74 | + return liteboxFailure(err) |
| 75 | + } |
| 76 | + req.Header = r.Headers.Clone() |
| 77 | + req.Header.Del("Authorization") |
| 78 | + req.Header.Del("Accept-Encoding") |
| 79 | + // Fixed test identity; this adapter only connects to a local LITE fixture. |
| 80 | + req.Header.Set("X-Databricks-Org-Id", "456") |
| 81 | + req.Header.Set("X-Databricks-Workspace-Id", "456") |
| 82 | + req.Header.Set("X-Databricks-User-Id", testserver.TestUser.Id) |
| 83 | + req.Header.Set("X-Databricks-User-Name", testserver.TestUser.UserName) |
| 84 | + resp, err := client.Do(req) |
| 85 | + if err != nil { |
| 86 | + return liteboxFailure(err) |
| 87 | + } |
| 88 | + defer resp.Body.Close() |
| 89 | + body, err := io.ReadAll(resp.Body) |
| 90 | + if err != nil { |
| 91 | + return liteboxFailure(err) |
| 92 | + } |
| 93 | + return testserver.Response{StatusCode: resp.StatusCode, Headers: resp.Header, Body: body} |
| 94 | + } |
| 95 | + routeLitebox(server, handler) |
| 96 | +} |
| 97 | + |
| 98 | +func liteboxURL(t *testing.T, name string) *url.URL { |
| 99 | + t.Helper() |
| 100 | + endpoint := env.Get(t.Context(), name) |
| 101 | + require.NotEmpty(t, endpoint, "%s is required with DMS_LITEBOX_URL", name) |
| 102 | + u, err := url.Parse(endpoint) |
| 103 | + require.NoError(t, err, "invalid %s", name) |
| 104 | + ip := net.ParseIP(u.Hostname()) |
| 105 | + require.True(t, u.Scheme == "https" && (u.Hostname() == "localhost" || (ip != nil && ip.IsLoopback())) && |
| 106 | + u.User == nil && u.RawQuery == "" && u.Fragment == "" && (u.Path == "" || u.Path == "/"), |
| 107 | + "%s must be an HTTPS loopback origin", name) |
| 108 | + return u |
| 109 | +} |
| 110 | + |
| 111 | +func isDMSPath(path string) bool { |
| 112 | + return path == dmsAPIPath || strings.HasPrefix(path, dmsAPIPath+"/") |
| 113 | +} |
| 114 | + |
| 115 | +func isLiteboxPath(path string) bool { |
| 116 | + if isDMSPath(path) || |
| 117 | + strings.HasPrefix(path, "/api/2.0/workspace/") || |
| 118 | + strings.HasPrefix(path, "/api/2.0/workspace-files/") { |
| 119 | + return true |
| 120 | + } |
| 121 | + for _, objectType := range liteboxPermissionTypes { |
| 122 | + if strings.HasPrefix(path, "/api/2.0/permissions/"+objectType+"/") { |
| 123 | + return true |
| 124 | + } |
| 125 | + } |
| 126 | + return false |
| 127 | +} |
| 128 | + |
| 129 | +func routeLitebox(server *testserver.Server, handler testserver.HandlerFunc) { |
| 130 | + // Without a PATCH route, ServeMux returns 405 before Dispatch or NotFound. |
| 131 | + for _, objectType := range liteboxPermissionTypes { |
| 132 | + server.Handle(http.MethodPatch, "/api/2.0/permissions/"+objectType+"/{object_id}", handler) |
| 133 | + } |
| 134 | + dispatch := server.Dispatch |
| 135 | + server.Dispatch = func(w http.ResponseWriter, r *http.Request, h testserver.HandlerFunc, vars map[string]string) { |
| 136 | + if isLiteboxPath(r.URL.Path) { |
| 137 | + h = handler |
| 138 | + } |
| 139 | + dispatch(w, r, h, vars) |
| 140 | + } |
| 141 | + notFound := server.NotFound |
| 142 | + server.NotFound = func(w http.ResponseWriter, r *http.Request) { |
| 143 | + if isLiteboxPath(r.URL.Path) { |
| 144 | + dispatch(w, r, handler, nil) |
| 145 | + return |
| 146 | + } |
| 147 | + notFound(w, r) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func liteboxFailure(err error) testserver.Response { |
| 152 | + return testserver.Response{StatusCode: http.StatusBadGateway, Body: map[string]string{ |
| 153 | + "error_code": "LITEBOX_CONNECTION_ERROR", |
| 154 | + "message": err.Error(), |
| 155 | + }} |
| 156 | +} |
0 commit comments