Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added run_as section for bundle configuration #692

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions bundle/config/mutator/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"github.com/databricks/cli/bundle"
)

var defaultMutators []bundle.Mutator = []bundle.Mutator{
ProcessRootIncludes(),
DefineDefaultTarget(),
LoadGitDetails(),
}

func DefaultMutators() []bundle.Mutator {
return []bundle.Mutator{
ProcessRootIncludes(),
DefineDefaultTarget(),
LoadGitDetails(),
}
return append(defaultMutators, SetRunAs())
}

func DefaultMutatorsForTarget(env string) []bundle.Mutator {
return append(DefaultMutators(), SelectTarget(env))
return append(defaultMutators, SelectTarget(env), SetRunAs())
}
49 changes: 49 additions & 0 deletions bundle/config/mutator/run_as.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mutator

import (
"context"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/databricks-sdk-go/service/jobs"
)

type setRunAs struct {
}

func SetRunAs() bundle.Mutator {
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
return &setRunAs{}
}

func (m *setRunAs) Name() string {
return "SetRunAs"
}

func (m *setRunAs) Apply(_ context.Context, b *bundle.Bundle) error {
runAs := b.Config.RunAs
if runAs == nil {
return nil
}

for i := range b.Config.Resources.Jobs {
job := b.Config.Resources.Jobs[i]
if job.RunAs != nil {
continue
}
job.RunAs = &jobs.JobRunAs{
ServicePrincipalName: runAs.ServicePrincipalName,
UserName: runAs.UserName,
}
}

for i := range b.Config.Resources.Pipelines {
pipeline := b.Config.Resources.Pipelines[i]
pipeline.Permissions = append(pipeline.Permissions, resources.Permission{
Level: "IS_OWNER",
ServicePrincipalName: runAs.ServicePrincipalName,
UserName: runAs.UserName,
})
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}
9 changes: 9 additions & 0 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/databricks/cli/bundle/config/variable"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/ghodss/yaml"
"github.com/imdario/mergo"
)
Expand Down Expand Up @@ -80,6 +81,9 @@ type Root struct {

// Sync section specifies options for files synchronization
Sync Sync `json:"sync"`

// RunAs section allows to define an execution identity for jobs and pipelines runs
RunAs *jobs.JobRunAs `json:"run_as,omitempty"`
}

func Load(path string) (*Root, error) {
Expand Down Expand Up @@ -237,6 +241,11 @@ func (r *Root) MergeTargetOverrides(target *Target) error {
}
}

if target.RunAs != nil {
r.RunAs = target.RunAs
fmt.Println("Merging run as")
andrewnester marked this conversation as resolved.
Show resolved Hide resolved
}

if target.Mode != "" {
r.Bundle.Mode = target.Mode
}
Expand Down
4 changes: 4 additions & 0 deletions bundle/config/target.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "github.com/databricks/databricks-sdk-go/service/jobs"

type Mode string

// Target defines overrides for a single target.
Expand Down Expand Up @@ -31,6 +33,8 @@ type Target struct {
Variables map[string]string `json:"variables,omitempty"`

Git Git `json:"git,omitempty"`

RunAs *jobs.JobRunAs `json:"run_as,omitempty"`
}

const (
Expand Down
13 changes: 7 additions & 6 deletions bundle/tests/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"github.com/stretchr/testify/require"
)

func load(t *testing.T, path string) *bundle.Bundle {
func loadBundle(t *testing.T, path string, mutators []bundle.Mutator) *bundle.Bundle {
ctx := context.Background()
b, err := bundle.Load(ctx, path)
require.NoError(t, err)
err = bundle.Apply(ctx, b, bundle.Seq(mutator.DefaultMutators()...))
err = bundle.Apply(ctx, b, bundle.Seq(mutators...))
require.NoError(t, err)
return b
}

func load(t *testing.T, path string) *bundle.Bundle {
return loadBundle(t, path, mutator.DefaultMutators())
}

func loadTarget(t *testing.T, path, env string) *bundle.Bundle {
b := load(t, path)
err := bundle.Apply(context.Background(), b, mutator.SelectTarget(env))
require.NoError(t, err)
return b
return loadBundle(t, path, mutator.DefaultMutatorsForTarget(env))
}
37 changes: 37 additions & 0 deletions bundle/tests/run_as/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
bundle:
name: "run_as"

run_as:
service_principal_name: "my_service_principal"

targets:
development:
mode: development
run_as:
user_name: "my_user_name"

resources:
pipelines:
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually remember there's one more caveat here: Terraform doesn't allow you to list yourself as the OWNER if you're the person deploying the pipeline. Does that case work in your implementation?

Copy link
Contributor Author

@andrewnester andrewnester Aug 23, 2023

Choose a reason for hiding this comment

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

@lennartkats-db I added some logic to not add current user as an owner permission in this case

nyc_taxi_pipeline:
name: "nyc taxi loader"
libraries:
- notebook:
path: ./dlt/nyc_taxi_loader
jobs:
job_one:
name: Job One
tasks:
- task:
notebook_path: "./test.py"
job_two:
name: Job Two
tasks:
- task:
notebook_path: "./test.py"
job_three:
name: Job Three
run_as:
service_principal_name: "my_service_principal_for_job"
tasks:
- task:
notebook_path: "./test.py"
54 changes: 54 additions & 0 deletions bundle/tests/run_as_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config_tests

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRunAsDefault(t *testing.T) {
b := load(t, "./run_as")
assert.Len(t, b.Config.Resources.Jobs, 3)
jobs := b.Config.Resources.Jobs

assert.NotNil(t, jobs["job_one"].RunAs)
assert.Equal(t, "my_service_principal", jobs["job_one"].RunAs.ServicePrincipalName)
assert.Equal(t, "", jobs["job_one"].RunAs.UserName)

assert.NotNil(t, jobs["job_two"].RunAs)
assert.Equal(t, "my_service_principal", jobs["job_two"].RunAs.ServicePrincipalName)
assert.Equal(t, "", jobs["job_two"].RunAs.UserName)

assert.NotNil(t, jobs["job_three"].RunAs)
assert.Equal(t, "my_service_principal_for_job", jobs["job_three"].RunAs.ServicePrincipalName)
assert.Equal(t, "", jobs["job_three"].RunAs.UserName)

pipelines := b.Config.Resources.Pipelines
assert.NotNil(t, pipelines["nyc_taxi_pipeline"].Permissions)
assert.Equal(t, pipelines["nyc_taxi_pipeline"].Permissions[0].Level, "IS_OWNER")
assert.Equal(t, pipelines["nyc_taxi_pipeline"].Permissions[0].ServicePrincipalName, "my_service_principal")
}

func TestRunAsDevelopment(t *testing.T) {
b := loadTarget(t, "./run_as", "development")
assert.Len(t, b.Config.Resources.Jobs, 3)

jobs := b.Config.Resources.Jobs

assert.NotNil(t, jobs["job_one"].RunAs)
assert.Equal(t, "", jobs["job_one"].RunAs.ServicePrincipalName)
assert.Equal(t, "my_user_name", jobs["job_one"].RunAs.UserName)

assert.NotNil(t, jobs["job_two"].RunAs)
assert.Equal(t, "", jobs["job_two"].RunAs.ServicePrincipalName)
assert.Equal(t, "my_user_name", jobs["job_two"].RunAs.UserName)

assert.NotNil(t, jobs["job_three"].RunAs)
assert.Equal(t, "my_service_principal_for_job", jobs["job_three"].RunAs.ServicePrincipalName)
assert.Equal(t, "", jobs["job_three"].RunAs.UserName)

pipelines := b.Config.Resources.Pipelines
assert.NotNil(t, pipelines["nyc_taxi_pipeline"].Permissions)
assert.Equal(t, pipelines["nyc_taxi_pipeline"].Permissions[0].Level, "IS_OWNER")
assert.Equal(t, pipelines["nyc_taxi_pipeline"].Permissions[0].UserName, "my_user_name")
}