Skip to content

Commit 165d129

Browse files
alexotttanmay-db
andauthored
[Fix] Fixed reading of permissions for SQL objects (#3800)
## Changes <!-- Summary of your changes that are easy to understand --> The `object_id` for legacy SQL objects didn't have a correct format, and now requires a special handling. The error wasn't caught because we didn't have integration tests for SQL objects permissions, and object IDs in the unit test weren't matching to actual payload. Fixes #3799 ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - [x] `make test` run locally - [ ] ~relevant change in `docs/` folder~ - [x] tested manually import of resources - [x] covered with integration tests in `internal/acceptance` - [x] relevant acceptance tests are passing - [ ] ~using Go SDK~ Co-authored-by: Tanmay Rustagi <[email protected]>
1 parent 8fb39fb commit 165d129

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

docs/resources/permissions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ resource "databricks_group" "eng" {
739739
display_name = "Engineering"
740740
}
741741
742-
resource "databricks_permissions" "endpoint_usage" {
742+
resource "databricks_permissions" "query_usage" {
743743
sql_query_id = "3244325"
744744
745745
access_control {
@@ -767,7 +767,7 @@ resource "databricks_group" "eng" {
767767
display_name = "Engineering"
768768
}
769769
770-
resource "databricks_permissions" "endpoint_usage" {
770+
resource "databricks_permissions" "alert_usage" {
771771
sql_alert_id = "3244325"
772772
773773
access_control {

internal/acceptance/sql_alert_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ func TestAccAlert(t *testing.T) {
1313
query = "SELECT 1 AS p1, 2 as p2"
1414
}
1515
16+
resource "databricks_permissions" "alert_usage" {
17+
sql_alert_id = databricks_sql_alert.alert.id
18+
access_control {
19+
group_name = "users"
20+
permission_level = "CAN_RUN"
21+
}
22+
}
23+
1624
resource "databricks_sql_alert" "alert" {
1725
query_id = databricks_sql_query.this.id
1826
name = "tf-alert-{var.RANDOM}"
@@ -31,6 +39,14 @@ func TestAccAlert(t *testing.T) {
3139
query = "SELECT 1 AS p1, 2 as p2"
3240
}
3341
42+
resource "databricks_permissions" "alert_usage" {
43+
sql_alert_id = databricks_sql_alert.alert.id
44+
access_control {
45+
group_name = "users"
46+
permission_level = "CAN_RUN"
47+
}
48+
}
49+
3450
resource "databricks_sql_alert" "alert" {
3551
query_id = databricks_sql_query.this.id
3652
name = "tf-alert-{var.RANDOM}"

internal/acceptance/sql_dashboard_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ func TestAccDashboard(t *testing.T) {
4141
}
4242
}
4343
44+
resource "databricks_permissions" "sql_dashboard_usage" {
45+
sql_dashboard_id = databricks_sql_dashboard.d1.id
46+
access_control {
47+
group_name = "users"
48+
permission_level = "CAN_RUN"
49+
}
50+
}
51+
52+
resource "databricks_permissions" "query_usage" {
53+
sql_query_id = databricks_sql_query.q1.id
54+
access_control {
55+
group_name = "users"
56+
permission_level = "CAN_RUN"
57+
}
58+
}
59+
4460
resource "databricks_sql_query" "q1" {
4561
data_source_id = "{env.TEST_DEFAULT_WAREHOUSE_DATASOURCE_ID}"
4662
name = "tf-{var.RANDOM}-query"

permissions/resource_permissions.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,23 @@ type PermissionsEntity struct {
326326
AccessControlList []AccessControlChange `json:"access_control" tf:"slice_set"`
327327
}
328328

329+
func (oa *ObjectACL) isMatchingMapping(mapping permissionsIDFieldMapping) bool {
330+
if mapping.objectType != oa.ObjectType {
331+
return false
332+
}
333+
if oa.ObjectID != "" && oa.ObjectID[0] == '/' {
334+
return strings.HasPrefix(oa.ObjectID[1:], mapping.resourceType)
335+
}
336+
if strings.HasPrefix(oa.ObjectID, "dashboards/") || strings.HasPrefix(oa.ObjectID, "alerts/") || strings.HasPrefix(oa.ObjectID, "queries/") {
337+
idx := strings.Index(oa.ObjectID, "/")
338+
if idx != -1 {
339+
return mapping.resourceType == "sql/"+oa.ObjectID[:idx]
340+
}
341+
}
342+
343+
return false
344+
}
345+
329346
func (oa *ObjectACL) ToPermissionsEntity(d *schema.ResourceData, me string) (PermissionsEntity, error) {
330347
entity := PermissionsEntity{}
331348
for _, accessControl := range oa.AccessControlList {
@@ -342,10 +359,9 @@ func (oa *ObjectACL) ToPermissionsEntity(d *schema.ResourceData, me string) (Per
342359
}
343360
}
344361
for _, mapping := range permissionsResourceIDFields() {
345-
if mapping.objectType != oa.ObjectType || !strings.HasPrefix(oa.ObjectID[1:], mapping.resourceType) {
362+
if !oa.isMatchingMapping(mapping) {
346363
continue
347364
}
348-
log.Printf("[DEBUG] mapping %v for object %v", mapping, oa)
349365
entity.ObjectType = mapping.objectType
350366
var pathVariant any
351367
if mapping.objectType == "file" {

permissions/resource_permissions_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func TestResourcePermissionsRead_SQLA_Asset(t *testing.T) {
336336
Method: http.MethodGet,
337337
Resource: "/api/2.0/preview/sql/permissions/dashboards/abc",
338338
Response: ObjectACL{
339-
ObjectID: "/sql/dashboards/abc",
339+
ObjectID: "dashboards/abc",
340340
ObjectType: "dashboard",
341341
AccessControlList: []AccessControl{
342342
{
@@ -812,7 +812,7 @@ func TestResourcePermissionsCreate_SQLA_Asset(t *testing.T) {
812812
Method: http.MethodGet,
813813
Resource: "/api/2.0/preview/sql/permissions/dashboards/abc",
814814
Response: ObjectACL{
815-
ObjectID: "/sql/dashboards/abc",
815+
ObjectID: "dashboards/abc",
816816
ObjectType: "dashboard",
817817
AccessControlList: []AccessControl{
818818
{
@@ -871,7 +871,7 @@ func TestResourcePermissionsCreate_SQLA_Endpoint(t *testing.T) {
871871
Method: http.MethodGet,
872872
Resource: "/api/2.0/permissions/sql/warehouses/abc",
873873
Response: ObjectACL{
874-
ObjectID: "/sql/dashboards/abc",
874+
ObjectID: "dashboards/abc",
875875
ObjectType: "dashboard",
876876
AccessControlList: []AccessControl{
877877
{
@@ -934,7 +934,7 @@ func TestResourcePermissionsCreate_SQLA_Endpoint_WithOwner(t *testing.T) {
934934
Method: http.MethodGet,
935935
Resource: "/api/2.0/permissions/sql/warehouses/abc",
936936
Response: ObjectACL{
937-
ObjectID: "/sql/dashboards/abc",
937+
ObjectID: "dashboards/abc",
938938
ObjectType: "dashboard",
939939
AccessControlList: []AccessControl{
940940
{
@@ -1651,7 +1651,7 @@ func TestResourcePermissionsCreate_Sql_Queries(t *testing.T) {
16511651
Method: http.MethodGet,
16521652
Resource: "/api/2.0/preview/sql/permissions/queries/id111",
16531653
Response: ObjectACL{
1654-
ObjectID: "/sql/queries/id111",
1654+
ObjectID: "queries/id111",
16551655
ObjectType: "query",
16561656
AccessControlList: []AccessControl{
16571657
{
@@ -1711,7 +1711,7 @@ func TestResourcePermissionsUpdate_Sql_Queries(t *testing.T) {
17111711
Method: http.MethodGet,
17121712
Resource: "/api/2.0/preview/sql/permissions/queries/id111",
17131713
Response: ObjectACL{
1714-
ObjectID: "/sql/queries/id111",
1714+
ObjectID: "queries/id111",
17151715
ObjectType: "query",
17161716
AccessControlList: []AccessControl{
17171717
{

0 commit comments

Comments
 (0)