|
7 | 7 | "fmt" |
8 | 8 | "log" |
9 | 9 | "net/http" |
| 10 | + "net/url" |
10 | 11 | "os" |
11 | 12 | "reflect" |
12 | 13 | "strings" |
@@ -237,6 +238,11 @@ func (c *DatabricksClient) Authenticate(ctx context.Context) error { |
237 | 238 | if c.authVisitor != nil { |
238 | 239 | return nil |
239 | 240 | } |
| 241 | + // Fix host prior to auth, because it may be used in the OIDC flow as "audience" field. |
| 242 | + // If necessary, this function adds a scheme and strips a trailing slash. |
| 243 | + if err := c.fixHost(); err != nil { |
| 244 | + return err |
| 245 | + } |
240 | 246 | type auth struct { |
241 | 247 | configure func(context.Context) (func(*http.Request) error, error) |
242 | 248 | name string |
@@ -326,12 +332,34 @@ func (c *DatabricksClient) niceAuthError(message string) error { |
326 | 332 | return fmt.Errorf("%s%s. Please check %s for details", message, info, docUrl) |
327 | 333 | } |
328 | 334 |
|
329 | | -func (c *DatabricksClient) fixHost() { |
330 | | - if c.Host != "" && !(strings.HasPrefix(c.Host, "https://") || strings.HasPrefix(c.Host, "http://")) { |
331 | | - // azurerm_databricks_workspace.*.workspace_url is giving URL without scheme |
332 | | - // so that is why this line is here |
333 | | - c.Host = "https://" + c.Host |
| 335 | +func (c *DatabricksClient) fixHost() error { |
| 336 | + // Nothing to fix if the host isn't set. |
| 337 | + if c.Host == "" { |
| 338 | + return nil |
| 339 | + } |
| 340 | + |
| 341 | + u, err := url.Parse(c.Host) |
| 342 | + if err != nil { |
| 343 | + return err |
| 344 | + } |
| 345 | + |
| 346 | + // If the host is empty, assume the scheme wasn't included. |
| 347 | + if u.Host == "" { |
| 348 | + u, err = url.Parse("https://" + c.Host) |
| 349 | + if err != nil { |
| 350 | + return err |
| 351 | + } |
334 | 352 | } |
| 353 | + |
| 354 | + // Create new instance to ensure other fields are initialized as empty. |
| 355 | + u = &url.URL{ |
| 356 | + Scheme: u.Scheme, |
| 357 | + Host: u.Host, |
| 358 | + } |
| 359 | + |
| 360 | + // Store sanitized version of c.Host. |
| 361 | + c.Host = u.String() |
| 362 | + return nil |
335 | 363 | } |
336 | 364 |
|
337 | 365 | func (c *DatabricksClient) configureWithPat(ctx context.Context) (func(*http.Request) error, error) { |
|
0 commit comments