-
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
Open
dylanhuff-at-coder
wants to merge
1
commit into
main
Choose a base branch
from
dylan/plat-100-secret-requirements
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.
Open
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,54 @@ | ||
| package extract | ||
|
|
||
| import ( | ||
| "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) (*types.SecretRequirement, hcl.Diagnostics) { | ||
| // help_message is required AND must be a string; requiredString | ||
| // handles both checks and emits a proper type diagnostic. | ||
| var diags hcl.Diagnostics | ||
| help, helpDiag := requiredString(block, "help_message") | ||
| if helpDiag != nil { | ||
| diags = diags.Append(helpDiag) | ||
| } | ||
|
|
||
| env := optionalString(block, "env") | ||
| file := optionalString(block, "file") | ||
|
|
||
| // Mutual exclusivity: exactly one of env/file must be set. | ||
| switch { | ||
| case env == "" && file == "": | ||
| r := block.HCLBlock().Body.MissingItemRange() | ||
| 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 env != "" && file != "": | ||
| r := block.HCLBlock().Body.MissingItemRange() | ||
| 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
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,30 @@ | ||
| package preview | ||
|
|
||
| import ( | ||
| "github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
| "github.com/hashicorp/hcl/v2" | ||
|
|
||
| "github.com/coder/preview/extract" | ||
| "github.com/coder/preview/types" | ||
| ) | ||
|
|
||
| func secrets(modules terraform.Modules) ([]types.SecretRequirement, hcl.Diagnostics) { | ||
| diags := make(hcl.Diagnostics, 0) | ||
| reqs := make([]types.SecretRequirement, 0) | ||
|
|
||
| for _, mod := range modules { | ||
| blocks := mod.GetDatasByType(types.BlockTypeSecret) | ||
| for _, block := range blocks { | ||
| req, rDiags := extract.SecretFromBlock(block) | ||
| if len(rDiags) > 0 { | ||
| diags = diags.Extend(rDiags) | ||
| } | ||
| if req != nil { | ||
| reqs = append(reqs, *req) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| types.SortSecretRequirements(reqs) | ||
| return reqs, 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,9 @@ | ||
| data "coder_secret" "gh" { | ||
| env = "GITHUB_TOKEN" | ||
| help_message = "Add a GitHub PAT" | ||
| } | ||
|
|
||
| data "coder_secret" "aws" { | ||
| file = "~/.aws/credentials" | ||
| help_message = "Add AWS creds" | ||
| } |
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 @@ | ||
| coder_secret is not yet in the released coder provider |
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,12 @@ | ||
| data "coder_parameter" "use_github" { | ||
| name = "use_github" | ||
| type = "bool" | ||
| default = "false" | ||
| mutable = true | ||
| } | ||
|
|
||
| data "coder_secret" "gh" { | ||
| count = data.coder_parameter.use_github.value == "true" ? 1 : 0 | ||
| env = "GITHUB_TOKEN" | ||
| help_message = "Add a GitHub PAT" | ||
| } |
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 @@ | ||
| coder_secret is not yet in the released coder provider |
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,29 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
| ) | ||
|
|
||
| // @typescript-ignore BlockTypeSecret | ||
| const BlockTypeSecret = "coder_secret" | ||
|
|
||
| // SecretRequirement describes a `data "coder_secret"` block declared in a | ||
| // template. Exactly one of Env or File will be non-empty; validation of that | ||
| // invariant happens during extraction. | ||
| type SecretRequirement struct { | ||
| Env string `json:"env"` | ||
| File string `json:"file"` | ||
| HelpMessage string `json:"help_message"` | ||
| } | ||
|
|
||
| // SortSecretRequirements orders requirements first by Env then by File so | ||
| // diagnostic output is stable across runs. | ||
| func SortSecretRequirements(reqs []SecretRequirement) { | ||
| slices.SortFunc(reqs, func(a, b SecretRequirement) int { | ||
| if c := strings.Compare(a.Env, b.Env); c != 0 { | ||
| return c | ||
| } | ||
| return strings.Compare(a.File, b.File) | ||
| }) | ||
| } |
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.
Reccomend doing a panic recover guard:
https://github.com/coder/preview/blob/main/extract/preset.go#L13-L30
Things can panic in the type system pretty easily if misused.