-
Notifications
You must be signed in to change notification settings - Fork 214
Reject dangling ${resources.*} refs at validate time #6300
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
Draft
radakam
wants to merge
1
commit into
main
Choose a base branch
from
fix-dangling-resource-refs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Reject ${resources.*} references to resources that are not defined in the bundle. |
10 changes: 10 additions & 0 deletions
10
acceptance/bundle/validate/dangling_resource_refs/databricks.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| bundle: | ||
| name: dangling-resource-refs | ||
|
|
||
| resources: | ||
| jobs: | ||
| my_job: | ||
| name: my_job | ||
| permissions: | ||
| - level: CAN_VIEW | ||
| group_name: ${resources.jobs.does_not_exist.id} |
2 changes: 2 additions & 0 deletions
2
acceptance/bundle/validate/dangling_resource_refs/out.test.toml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
acceptance/bundle/validate/dangling_resource_refs/output.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| >>> [CLI] bundle validate --strict | ||
| Error: reference does not exist: ${resources.jobs.does_not_exist.id} | ||
| at resources.jobs.my_job.permissions[0].group_name | ||
|
|
||
| Name: dangling-resource-refs | ||
| Target: default | ||
| Workspace: | ||
| User: [USERNAME] | ||
| Path: /Workspace/Users/[USERNAME]/.bundle/dangling-resource-refs/default | ||
|
|
||
| Found 1 error | ||
|
|
||
| >>> [CLI] bundle deploy | ||
| Error: reference does not exist: ${resources.jobs.does_not_exist.id} | ||
| at resources.jobs.my_job.permissions[0].group_name | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Without the fix, validate --strict succeeds and deploy fails with | ||
| # "invalid dependency" (direct) or an unresolved terraform reference. | ||
| musterr trace $CLI bundle validate --strict | ||
| musterr trace $CLI bundle deploy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/dyn" | ||
| "github.com/databricks/cli/libs/dyn/dynvar" | ||
| ) | ||
|
|
||
| type danglingResourceReferences struct{} | ||
|
|
||
| // DanglingResourceReferences rejects ${resources.*} references whose target | ||
| // resource is not defined in the bundle. Deploy fails later with an invalid | ||
| // dependency (direct) or an unresolved reference (terraform); catch it here. | ||
| func DanglingResourceReferences() bundle.Mutator { | ||
| return &danglingResourceReferences{} | ||
| } | ||
|
|
||
| func (m *danglingResourceReferences) Name() string { | ||
| return "validate:dangling_resource_references" | ||
| } | ||
|
|
||
| func (m *danglingResourceReferences) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics { | ||
| var diags diag.Diagnostics | ||
|
|
||
| _ = dyn.WalkReadOnly(b.Config.Value(), func(path dyn.Path, v dyn.Value) error { | ||
| ref, ok := dynvar.NewRef(v) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| for _, r := range ref.References() { | ||
| if !strings.HasPrefix(r, "resources.") { | ||
| continue | ||
| } | ||
| if d := checkDanglingResourceReference(b, r, path, v.Locations()); d != nil { | ||
| diags = append(diags, *d) | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| return diags | ||
| } | ||
|
|
||
| // checkDanglingResourceReference checks a reference like | ||
| // "resources.jobs.missing.id" and returns a diagnostic when the resource | ||
| // (resources.jobs.missing) is not defined. | ||
| func checkDanglingResourceReference(b *bundle.Bundle, ref string, path dyn.Path, locs []dyn.Location) *diag.Diagnostic { | ||
| p, err := dyn.NewPathFromString(ref) | ||
| // resources.<group>.<name>[.<field>...] | ||
| if err != nil || len(p) < 3 || p[0].Key() != "resources" { | ||
| return nil | ||
| } | ||
|
|
||
| // Identity is resources.<group>.<name>; trailing fields (.id, .permissions, …) | ||
| // are resolved at deploy time and are not required to exist in config. | ||
| resourceKey := p[:3] | ||
| v, err := dyn.GetByPath(b.Config.Value(), resourceKey) | ||
| if err == nil && v.Kind() != dyn.KindInvalid && v.Kind() != dyn.KindNil { | ||
| return nil | ||
| } | ||
|
|
||
| d := &diag.Diagnostic{ | ||
| Severity: diag.Error, | ||
| Summary: fmt.Sprintf("reference does not exist: ${%s}", ref), | ||
| Paths: []dyn.Path{path}, | ||
| } | ||
| // ApplyBundlePermissions rewrites permission entries without locations; skip | ||
| // empty ones so we don't print "in :0:0". | ||
| for _, loc := range locs { | ||
| if loc.File != "" { | ||
| d.Locations = append(d.Locations, loc) | ||
| } | ||
| } | ||
| return d | ||
| } | ||
73 changes: 73 additions & 0 deletions
73
bundle/config/validate/dangling_resource_references_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package validate | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/bundle" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/resources" | ||
| "github.com/databricks/cli/bundle/internal/bundletest" | ||
| "github.com/databricks/cli/libs/diag" | ||
| "github.com/databricks/cli/libs/dyn" | ||
| "github.com/databricks/databricks-sdk-go/service/jobs" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDanglingResourceReferences_MissingResource(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Jobs: map[string]*resources.Job{ | ||
| "my_job": {JobSettings: jobs.JobSettings{Name: "my_job"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| bundletest.Mutate(t, b, func(v dyn.Value) (dyn.Value, error) { | ||
| return dyn.Set(v, "resources.jobs.my_job.name", dyn.V("${resources.jobs.does_not_exist.id}")) | ||
| }) | ||
|
|
||
| diags := DanglingResourceReferences().Apply(t.Context(), b) | ||
| require.Len(t, diags, 1) | ||
| assert.Equal(t, diag.Error, diags[0].Severity) | ||
| assert.Equal(t, "reference does not exist: ${resources.jobs.does_not_exist.id}", diags[0].Summary) | ||
| } | ||
|
|
||
| func TestDanglingResourceReferences_ExistingResource(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Jobs: map[string]*resources.Job{ | ||
| "src": {JobSettings: jobs.JobSettings{Name: "src"}}, | ||
| "dst": {JobSettings: jobs.JobSettings{Name: "dst"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| bundletest.Mutate(t, b, func(v dyn.Value) (dyn.Value, error) { | ||
| return dyn.Set(v, "resources.jobs.dst.name", dyn.V("${resources.jobs.src.id}")) | ||
| }) | ||
|
|
||
| diags := DanglingResourceReferences().Apply(t.Context(), b) | ||
| assert.Empty(t, diags) | ||
| } | ||
|
|
||
| func TestDanglingResourceReferences_UnknownType(t *testing.T) { | ||
| b := &bundle.Bundle{ | ||
| Config: config.Root{ | ||
| Resources: config.Resources{ | ||
| Jobs: map[string]*resources.Job{ | ||
| "my_job": {JobSettings: jobs.JobSettings{Name: "my_job"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| bundletest.Mutate(t, b, func(v dyn.Value) (dyn.Value, error) { | ||
| return dyn.Set(v, "resources.jobs.my_job.name", dyn.V("${resources.unknown.foo.id}")) | ||
| }) | ||
|
|
||
| diags := DanglingResourceReferences().Apply(t.Context(), b) | ||
| require.Len(t, diags, 1) | ||
| assert.Contains(t, diags[0].Summary, "${resources.unknown.foo.id}") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This checks dynamic tree but references can be to things that are not in the config (remote references in direct and terraform-specific fields).
So it's too strict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think once we remove terraform we can think about validating these but for now not worth it.