-
Notifications
You must be signed in to change notification settings - Fork 389
/
client.go
245 lines (221 loc) · 7.56 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package common
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"sync"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/golang-jwt/jwt/v4"
)
type cachedMe struct {
internalImpl iam.CurrentUserService
cachedUser *iam.User
mu sync.Mutex
}
func (a *cachedMe) Me(ctx context.Context) (*iam.User, error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.cachedUser != nil {
return a.cachedUser, nil
}
user, err := a.internalImpl.Me(ctx)
if err != nil {
return user, err
}
a.cachedUser = user
return user, err
}
// DatabricksClient holds properties needed for authentication and HTTP client setup
// fields with `name` struct tags become Terraform provider attributes. `env` struct tag
// can hold one or more coma-separated env variable names to find value, if not specified
// directly. `auth` struct tag describes the type of conflicting authentication used.
type DatabricksClient struct {
*client.DatabricksClient
// callback used to create API1.2 call wrapper, which simplifies unit tessting
commandFactory func(context.Context, *DatabricksClient) CommandExecutor
cachedWorkspaceClient *databricks.WorkspaceClient
mu sync.Mutex
}
func (c *DatabricksClient) WorkspaceClient() (*databricks.WorkspaceClient, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.cachedWorkspaceClient != nil {
return c.cachedWorkspaceClient, nil
}
w, err := databricks.NewWorkspaceClient((*databricks.Config)(c.DatabricksClient.Config))
if err != nil {
return nil, err
}
internalImpl := w.CurrentUser.Impl()
w.CurrentUser.WithImpl(&cachedMe{
internalImpl: internalImpl,
})
c.cachedWorkspaceClient = w
return w, nil
}
// Get on path
func (c *DatabricksClient) Get(ctx context.Context, path string, request any, response any) error {
return c.Do(ctx, http.MethodGet, path, request, response, c.addApiPrefix)
}
// Post on path
func (c *DatabricksClient) Post(ctx context.Context, path string, request any, response any) error {
return c.Do(ctx, http.MethodPost, path, request, response, c.addApiPrefix)
}
// Delete on path
func (c *DatabricksClient) Delete(ctx context.Context, path string, request any) error {
return c.Do(ctx, http.MethodDelete, path, request, nil, c.addApiPrefix)
}
// Patch on path
func (c *DatabricksClient) Patch(ctx context.Context, path string, request any) error {
return c.Do(ctx, http.MethodPatch, path, request, nil, c.addApiPrefix)
}
// Put on path
func (c *DatabricksClient) Put(ctx context.Context, path string, request any) error {
return c.Do(ctx, http.MethodPut, path, request, nil, c.addApiPrefix)
}
type ApiVersion string
const (
API_1_2 ApiVersion = "1.2"
API_2_0 ApiVersion = "2.0"
API_2_1 ApiVersion = "2.1"
)
func (c *DatabricksClient) addApiPrefix(r *http.Request) error {
if r.URL == nil {
return fmt.Errorf("no URL found in request")
}
ctx := r.Context()
av, ok := ctx.Value(Api).(ApiVersion)
if !ok {
av = API_2_0
}
r.URL.Path = fmt.Sprintf("/api/%s%s", av, r.URL.Path)
return nil
}
// scimVisitor is a separate method for the sake of unit tests
func (c *DatabricksClient) scimVisitor(r *http.Request) error {
r.Header.Set("Content-Type", "application/scim+json; charset=utf-8")
if c.Config.IsAccountClient() && c.Config.AccountID != "" {
// until `/preview` is there for workspace scim,
// `/api/2.0` is added by completeUrl visitor
r.URL.Path = strings.ReplaceAll(r.URL.Path, "/api/2.0/preview",
fmt.Sprintf("/api/2.0/accounts/%s", c.Config.AccountID))
}
return nil
}
// Scim sets SCIM headers
func (c *DatabricksClient) Scim(ctx context.Context, method, path string, request any, response any) error {
return c.Do(ctx, method, path, request, response, c.addApiPrefix, c.scimVisitor)
}
// IsAzure returns true if client is configured for Azure Databricks - either by using AAD auth or with host+token combination
func (c *DatabricksClient) IsAzure() bool {
return c.Config.IsAzure()
}
// IsAws returns true if client is configured for AWS
func (c *DatabricksClient) IsAws() bool {
return !c.IsGcp() && !c.IsAzure()
}
// IsGcp returns true if client is configured for GCP
func (c *DatabricksClient) IsGcp() bool {
return c.Config.GoogleServiceAccount != "" || c.Config.IsGcp()
}
// FormatURL creates URL from the client Host and additional strings
func (c *DatabricksClient) FormatURL(strs ...string) string {
host := c.Config.Host
if !strings.HasSuffix(host, "/") {
host += "/"
}
data := append([]string{host}, strs...)
return strings.Join(data, "")
}
// ClientForHost creates a new DatabricksClient instance with the same auth parameters,
// but for the given host. Authentication has to be reinitialized, as Google OIDC has
// different authorizers, depending if it's workspace or Accounts API we're talking to.
func (c *DatabricksClient) ClientForHost(ctx context.Context, url string) (*DatabricksClient, error) {
// create dummy http request
req, _ := http.NewRequestWithContext(ctx, "GET", "/", nil)
// Ensure that client is authenticated
err := c.DatabricksClient.Config.Authenticate(req)
if err != nil {
return nil, fmt.Errorf("cannot authenticate parent client: %w", err)
}
cfg := &config.Config{
Host: url,
Username: c.Config.Username,
Password: c.Config.Password,
Token: c.Config.Token,
ClientID: c.Config.ClientID,
ClientSecret: c.Config.ClientSecret,
GoogleServiceAccount: c.Config.GoogleServiceAccount,
GoogleCredentials: c.Config.GoogleCredentials,
InsecureSkipVerify: c.Config.InsecureSkipVerify,
HTTPTimeoutSeconds: c.Config.HTTPTimeoutSeconds,
DebugTruncateBytes: c.Config.DebugTruncateBytes,
DebugHeaders: c.Config.DebugHeaders,
RateLimitPerSecond: c.Config.RateLimitPerSecond,
}
client, err := client.New(cfg)
if err != nil {
return nil, fmt.Errorf("cannot configure new client: %w", err)
}
// copy all client configuration options except Databricks CLI profile
return &DatabricksClient{
DatabricksClient: client,
commandFactory: c.commandFactory,
}, nil
}
func (aa *DatabricksClient) GetAzureJwtProperty(key string) (any, error) {
if !aa.IsAzure() {
return "", fmt.Errorf("can't get Azure JWT token in non-Azure environment")
}
if key == "tid" && aa.Config.AzureTenantID != "" {
return aa.Config.AzureTenantID, nil
}
request, err := http.NewRequest("GET", aa.Config.Host, nil)
if err != nil {
return nil, err
}
err = aa.Config.Authenticate(request)
if err != nil {
return nil, err
}
header := request.Header.Get("Authorization")
var stoken string
if len(header) > 0 && strings.HasPrefix(string(header), "Bearer ") {
log.Printf("[DEBUG] Got Bearer token")
stoken = strings.TrimSpace(strings.TrimPrefix(string(header), "Bearer "))
}
if stoken == "" {
return nil, fmt.Errorf("can't obtain Azure JWT token")
}
if strings.HasPrefix(stoken, "dapi") {
return nil, fmt.Errorf("can't use Databricks PAT")
}
parser := jwt.Parser{SkipClaimsValidation: true}
token, _, err := parser.ParseUnverified(stoken, jwt.MapClaims{})
if err != nil {
return nil, err
}
claims := token.Claims.(jwt.MapClaims)
v, ok := claims[key]
if !ok {
return nil, fmt.Errorf("can't find field '%s' in parsed JWT", key)
}
return v, nil
}
func CommonEnvironmentClient() *DatabricksClient {
c, err := client.New(&config.Config{})
if err != nil {
panic(err)
}
return &DatabricksClient{
DatabricksClient: c,
commandFactory: func(ctx context.Context, dc *DatabricksClient) CommandExecutor {
panic("command executor not initalized")
},
}
}