Repository navigation
fix(recipes): restore Terraform inputs for recipe deletion #13343
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,95 @@ | ||
| /* | ||
| Copyright 2026 The Radius Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // RecipeInputsOutputName is reserved for inputs retained in Terraform state for deletion. | ||
| const RecipeInputsOutputName = "_radius_recipe_inputs" | ||
|
|
||
| // AddRecipeInputs retains module inputs in a sensitive state output for destroy. | ||
| func (cfg *TerraformConfig) AddRecipeInputs(moduleName string) error { | ||
| module, ok := cfg.Module[moduleName] | ||
| if !ok { | ||
| return fmt.Errorf("module %q not found in the initialized terraform config", moduleName) | ||
| } | ||
| if _, exists := cfg.Output[RecipeInputsOutputName]; exists { | ||
| return fmt.Errorf("output %q is reserved for Radius recipe inputs", RecipeInputsOutputName) | ||
| } | ||
|
|
||
| inputs := RecipeParams{} | ||
| for name, value := range module { | ||
| if !isModuleMetadata(name) { | ||
| inputs[name] = value | ||
| } | ||
| } | ||
| data, err := json.Marshal(inputs) | ||
| if err != nil { | ||
| return fmt.Errorf("encoding terraform recipe inputs: %w", err) | ||
| } | ||
| if cfg.Output == nil { | ||
| cfg.Output = map[string]any{} | ||
| } | ||
| // Base64 prevents Terraform from evaluating interpolation syntax inside the saved JSON. | ||
| // It is not encryption; the state backend must protect this sensitive output. | ||
| cfg.Output[RecipeInputsOutputName] = map[string]any{ | ||
| "value": base64.StdEncoding.EncodeToString(data), | ||
| "sensitive": true, | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // RestoreRecipeInputs replaces module inputs with those saved during deployment. | ||
| // It must only be used for destroy, never for create or update. | ||
| func (cfg *TerraformConfig) RestoreRecipeInputs(moduleName, encoded string) error { | ||
| module, ok := cfg.Module[moduleName] | ||
| if !ok { | ||
| return fmt.Errorf("module %q not found in the initialized terraform config", moduleName) | ||
| } | ||
| data, err := base64.StdEncoding.DecodeString(encoded) | ||
| if err != nil { | ||
| return errors.New("invalid saved terraform recipe inputs: expected base64") | ||
| } | ||
| var inputs RecipeParams | ||
| if err := json.Unmarshal(data, &inputs); err != nil || inputs == nil { | ||
|
nicolejms marked this conversation as resolved.
Outdated
|
||
| // Do not include the decoder error: it can contain secret input data. | ||
| return errors.New("invalid saved terraform recipe inputs: expected a JSON object") | ||
| } | ||
| for name := range inputs { | ||
| if isModuleMetadata(name) { | ||
| return errors.New("invalid saved terraform recipe inputs: module metadata is not allowed") | ||
| } | ||
| } | ||
| for name := range module { | ||
| if !isModuleMetadata(name) { | ||
| delete(module, name) | ||
| } | ||
| } | ||
| for name, value := range inputs { | ||
| module[name] = value | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func isModuleMetadata(name string) bool { | ||
| return name == moduleSourceKey || name == moduleVersionKey || name == moduleProvidersKey | ||
| } | ||
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,109 @@ | ||
| /* | ||
| Copyright 2026 The Radius Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRecipeInputsRoundTrip(t *testing.T) { | ||
| t.Parallel() | ||
| inputs := RecipeParams{ | ||
| "context": map[string]any{ | ||
| "resource": map[string]any{ | ||
| "properties": map[string]any{ | ||
| "data": map[string]any{ | ||
| "password": map[string]any{"value": "secret-${literal}-%{literal}"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| "enabled": true, | ||
| "counted": float64(2), | ||
| "list": []any{"first", nil}, | ||
| "null": nil, | ||
| } | ||
| cfg := &TerraformConfig{Module: map[string]TFModuleConfig{ | ||
| "recipe": {moduleSourceKey: "original", moduleVersionKey: "1.0", moduleProvidersKey: map[string]string{"aws": "aws"}}, | ||
| }} | ||
| // Include an explicit null to verify that restoration preserves the saved input exactly. | ||
| for name, value := range inputs { | ||
| cfg.Module["recipe"][name] = value | ||
| } | ||
| require.NoError(t, cfg.AddRecipeInputs("recipe")) | ||
| output := cfg.Output[RecipeInputsOutputName].(map[string]any) | ||
| require.Equal(t, true, output["sensitive"]) | ||
| encoded := output["value"].(string) | ||
| require.NotContains(t, encoded, "secret") | ||
| require.NotContains(t, encoded, "${") | ||
| require.NotContains(t, encoded, "%{") | ||
|
|
||
| cfg.Module["recipe"] = TFModuleConfig{ | ||
| moduleSourceKey: "current", moduleVersionKey: "2.0", | ||
| moduleProvidersKey: map[string]string{"aws": "aws.current"}, | ||
| "context": nil, | ||
| "newInput": "must not survive", | ||
| } | ||
| require.NoError(t, cfg.RestoreRecipeInputs("recipe", encoded)) | ||
| expected := TFModuleConfig{ | ||
| moduleSourceKey: "current", moduleVersionKey: "2.0", | ||
| moduleProvidersKey: map[string]string{"aws": "aws.current"}, | ||
| } | ||
| for name, value := range inputs { | ||
| expected[name] = value | ||
| } | ||
| require.Equal(t, expected, cfg.Module["recipe"]) | ||
| } | ||
|
|
||
| func TestRecipeInputsRejectInvalidState(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| encoded string | ||
| }{ | ||
| {name: "not base64", encoded: "invalid secret"}, | ||
| {name: "not JSON", encoded: base64.StdEncoding.EncodeToString([]byte("secret"))}, | ||
| {name: "null", encoded: base64.StdEncoding.EncodeToString([]byte("null"))}, | ||
| {name: "array", encoded: base64.StdEncoding.EncodeToString([]byte("[]"))}, | ||
| {name: "source override", encoded: base64.StdEncoding.EncodeToString([]byte(`{"source":"secret"}`))}, | ||
| {name: "version override", encoded: base64.StdEncoding.EncodeToString([]byte(`{"version":"secret"}`))}, | ||
| {name: "provider override", encoded: base64.StdEncoding.EncodeToString([]byte(`{"providers":"secret"}`))}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| cfg := &TerraformConfig{Module: map[string]TFModuleConfig{"recipe": {"input": "unchanged"}}} | ||
| err := cfg.RestoreRecipeInputs("recipe", tt.encoded) | ||
| require.ErrorContains(t, err, "invalid saved terraform recipe inputs") | ||
| require.NotContains(t, err.Error(), "secret") | ||
| require.Equal(t, TFModuleConfig{"input": "unchanged"}, cfg.Module["recipe"]) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRecipeInputsRequireModuleAndReservedOutput(t *testing.T) { | ||
| t.Parallel() | ||
| cfg := &TerraformConfig{Module: map[string]TFModuleConfig{"recipe": {}}} | ||
| require.ErrorContains(t, cfg.AddRecipeInputs("missing"), `module "missing" not found`) | ||
| require.ErrorContains(t, cfg.RestoreRecipeInputs("missing", ""), `module "missing" not found`) | ||
| cfg.Output = map[string]any{RecipeInputsOutputName: map[string]any{"value": "user output"}} | ||
| require.ErrorContains(t, cfg.AddRecipeInputs("recipe"), "reserved") | ||
| require.Equal(t, map[string]any{"value": "user output"}, cfg.Output[RecipeInputsOutputName]) | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.