Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions bundle/config/mutator/resourcemutator/run_as.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/databricks/databricks-sdk-go/service/pipelines"
"github.com/databricks/databricks-sdk-go/service/sql"
)

type setRunAs struct{}
Expand Down Expand Up @@ -110,16 +111,6 @@ func validateRunAs(b *bundle.Bundle) diag.Diagnostics {
))
}

// Alerts do not support run_as in the API.
if len(b.Config.Resources.Alerts) > 0 {
diags = diags.Extend(reportRunAsNotSupported(
"alerts",
b.Config.GetLocation("resources.alerts"),
b.Config.Workspace.CurrentUser.UserName,
identity,
))
}

// Apps do not support run_as in the API.
if len(b.Config.Resources.Apps) > 0 {
diags = diags.Extend(reportRunAsNotSupported(
Expand Down Expand Up @@ -169,6 +160,24 @@ func setRunAsForPipelines(b *bundle.Bundle) {
}
}

func setRunAsForAlerts(b *bundle.Bundle) {
runAs := b.Config.RunAs
if runAs == nil {
return
}

for i := range b.Config.Resources.Alerts {
alert := b.Config.Resources.Alerts[i]
if alert.RunAs != nil {
continue
}
alert.RunAs = &sql.AlertV2RunAs{
ServicePrincipalName: runAs.ServicePrincipalName,
UserName: runAs.UserName,
}
}
}

// Legacy behavior of run_as for DLT pipelines. Available under the experimental.use_run_as_legacy flag.
// Only available to unblock customers stuck due to breaking changes in https://github.com/databricks/cli/pull/1233
func setPipelineOwnersToRunAsIdentity(b *bundle.Bundle) {
Expand Down Expand Up @@ -216,6 +225,7 @@ func (m *setRunAs) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {
if b.Config.Experimental != nil && b.Config.Experimental.UseLegacyRunAs {
setPipelineOwnersToRunAsIdentity(b)
setRunAsForJobs(b)
setRunAsForAlerts(b)
return diag.Diagnostics{
{
Severity: diag.Warning,
Expand All @@ -234,5 +244,6 @@ func (m *setRunAs) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {

setRunAsForJobs(b)
setRunAsForPipelines(b)
setRunAsForAlerts(b)
return nil
}
8 changes: 8 additions & 0 deletions bundle/config/mutator/resourcemutator/run_as_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func TestRunAsWorksForAllowedResources(t *testing.T) {
Pipelines: map[string]*resources.Pipeline{
"pipeline_one": {},
},
Alerts: map[string]*resources.Alert{
"alert_one": {},
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an assertion below that the run as identity is correctly set, similar to the assert that ecisgts today for jobs?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, fixed in followup commit.

},
}

Expand All @@ -112,6 +115,10 @@ func TestRunAsWorksForAllowedResources(t *testing.T) {
for _, job := range b.Config.Resources.Jobs {
assert.Equal(t, "bob", job.RunAs.UserName)
}

for _, alert := range b.Config.Resources.Alerts {
assert.Equal(t, "bob", alert.RunAs.UserName)
}
}

// Bundle "run_as" has two modes of operation, each with a different set of
Expand Down Expand Up @@ -158,6 +165,7 @@ var allowList = []string{
"secret_scopes",
"sql_warehouses",
"volumes",
"alerts",
}

func TestRunAsErrorForUnsupportedResources(t *testing.T) {
Expand Down