-
Notifications
You must be signed in to change notification settings - Fork 3
feat: extract coder_secret requirements into Output #198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0edca31
feat: extract coder_secret requirements into Output
dylanhuff-at-coder c46a4a1
chore: recover from panics in SecretFromBlock
dylanhuff-at-coder 184188d
chore: bump hc-install to v0.9.4 to fix expired GPG key
dylanhuff-at-coder b99b63d
fix: tighten coder_secret validation, add duplicate detection, and ad…
dylanhuff-at-coder 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,92 @@ | ||
| package extract | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
| "github.com/hashicorp/hcl/v2" | ||
|
|
||
| "github.com/coder/preview/types" | ||
| ) | ||
|
|
||
| // SecretFromBlock decodes a `data "coder_secret" {}` Terraform block into a | ||
| // SecretRequirement. Exactly one of `env` or `file` must be set, and | ||
| // `help_message` is required. Returns (nil, diags) on validation failure. | ||
| func SecretFromBlock(block *terraform.Block) (req *types.SecretRequirement, diags hcl.Diagnostics) { | ||
| defer func() { | ||
| // Extra safety mechanism to ensure that if a panic occurs, we do not break | ||
| // everything else. | ||
| if r := recover(); r != nil { | ||
| req = nil | ||
| diags = hcl.Diagnostics{ | ||
| { | ||
| Severity: hcl.DiagError, | ||
| Summary: "Panic occurred in extracting secret requirement. This should not happen, please report this to Coder.", | ||
| Detail: fmt.Sprintf("panic in secret extract: %+v", r), | ||
| }, | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // help_message is required AND must be a string; requiredString | ||
| // handles both checks and emits a proper type diagnostic. | ||
| help, helpDiag := requiredString(block, "help_message") | ||
| if helpDiag != nil { | ||
| diags = diags.Append(helpDiag) | ||
| } | ||
|
|
||
| // Check presence separately from value so we can distinguish "attribute | ||
| // absent" from "attribute present but wrong type"; the latter must produce | ||
| // a type diagnostic instead of being silently treated as unset. | ||
| envAttr := block.GetAttribute("env") | ||
| fileAttr := block.GetAttribute("file") | ||
| envSet := envAttr != nil && !envAttr.IsNil() | ||
| fileSet := fileAttr != nil && !fileAttr.IsNil() | ||
|
|
||
| var env, file string | ||
| if envSet { | ||
| v, d := requiredString(block, "env") | ||
| if d != nil { | ||
| diags = diags.Append(d) | ||
| } | ||
| env = v | ||
| } | ||
| if fileSet { | ||
| v, d := requiredString(block, "file") | ||
| if d != nil { | ||
| diags = diags.Append(d) | ||
| } | ||
| file = v | ||
| } | ||
|
|
||
| // Mutual exclusivity is based on presence, not parsed value, so a | ||
| // wrong-type attribute still counts as "set" here. | ||
| switch { | ||
| case !envSet && !fileSet: | ||
| r := block.HCLBlock().DefRange | ||
| diags = diags.Append(&hcl.Diagnostic{ | ||
| Severity: hcl.DiagError, | ||
| Summary: `Invalid "coder_secret" block`, | ||
| Detail: `Exactly one of "env" or "file" must be set, neither were set`, | ||
| Subject: &r, | ||
| }) | ||
| case envSet && fileSet: | ||
| r := block.HCLBlock().DefRange | ||
| diags = diags.Append(&hcl.Diagnostic{ | ||
| Severity: hcl.DiagError, | ||
| Summary: `Invalid "coder_secret" block`, | ||
| Detail: `Exactly one of "env" or "file" must be set, both were set`, | ||
| Subject: &r, | ||
| }) | ||
| } | ||
|
|
||
| if diags.HasErrors() { | ||
| return nil, diags | ||
| } | ||
|
|
||
| return &types.SecretRequirement{ | ||
| Env: env, | ||
| File: file, | ||
| HelpMessage: help, | ||
| }, diags | ||
| } |
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,32 @@ | ||
| package extract_test | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/hcl/v2" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/coder/preview/extract" | ||
| ) | ||
|
|
||
| // Test_SecretFromBlock_PanicRecover verifies that a panic inside | ||
| // SecretFromBlock is converted into an error diagnostic rather than crashing | ||
| // the whole extraction pass. A nil block triggers a nil pointer dereference | ||
| // inside requiredString, which the deferred recover should catch. | ||
| func Test_SecretFromBlock_PanicRecover(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| req, diags := extract.SecretFromBlock(nil) | ||
| require.Nil(t, req) | ||
| require.True(t, diags.HasErrors(), "expected diagnostics; got %v", diags) | ||
|
|
||
| var found bool | ||
| for _, d := range diags { | ||
| if d.Severity == hcl.DiagError && strings.Contains(d.Summary, "Panic occurred in extracting secret requirement") { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| require.True(t, found, "expected panic diagnostic; got: %v", diags) | ||
| } |
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
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.
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.
One last thought:
coder/previewpreviously seemed to exclusively deal with "workspace parameters" (i.e.coder_parameter) in a template. We're extending the modules scope, so we should probably update the docs. It's something we can do in a follow-up PR. To avoid it falling through the cracks I created https://linear.app/codercom/issue/PLAT-140/update-coderpreview-docs-to-reflect-new-coder-secret-scope