-
Notifications
You must be signed in to change notification settings - Fork 33
Feat/add gitlab variables controls #422
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
Joseph94m
wants to merge
2
commits into
main
Choose a base branch
from
feat/add-gitlab-variables-controls
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
Show all changes
2 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
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,42 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/getplumber/plumber/configuration" | ||
| ) | ||
|
|
||
| // applyVariableControls maps the two wizard booleans onto config: | ||
| // VarsProtectedEnabled -> CicdVariablesMustBeProtected and | ||
| // VarsMaskedEnabled -> CicdVariablesMustBeMasked. A field mix-up would silently | ||
| // produce a wrong .plumber.yaml from `plumber config init`, and no drift guard | ||
| // catches it (both controls ship disabled). This pins the mapping. | ||
| func TestApplyVariableControls_Mapping(t *testing.T) { | ||
| t.Run("protected only sets only the protected control", func(t *testing.T) { | ||
| gl := &configuration.ProviderConfig{} | ||
| (&initWizardState{VarsProtectedEnabled: true}).applyVariableControls(gl) | ||
| if gl.Controls.CicdVariablesMustBeProtected == nil || !gl.Controls.CicdVariablesMustBeProtected.IsEnabled() { | ||
| t.Fatal("protected control should be enabled") | ||
| } | ||
| if gl.Controls.CicdVariablesMustBeMasked != nil { | ||
| t.Fatal("masked control must stay unset when only protected was chosen (field mix-up)") | ||
| } | ||
| }) | ||
| t.Run("masked only sets only the masked control", func(t *testing.T) { | ||
| gl := &configuration.ProviderConfig{} | ||
| (&initWizardState{VarsMaskedEnabled: true}).applyVariableControls(gl) | ||
| if gl.Controls.CicdVariablesMustBeMasked == nil || !gl.Controls.CicdVariablesMustBeMasked.IsEnabled() { | ||
| t.Fatal("masked control should be enabled") | ||
| } | ||
| if gl.Controls.CicdVariablesMustBeProtected != nil { | ||
| t.Fatal("protected control must stay unset when only masked was chosen (field mix-up)") | ||
| } | ||
| }) | ||
| t.Run("neither sets nothing", func(t *testing.T) { | ||
| gl := &configuration.ProviderConfig{} | ||
| (&initWizardState{}).applyVariableControls(gl) | ||
| if gl.Controls.CicdVariablesMustBeProtected != nil || gl.Controls.CicdVariablesMustBeMasked != nil { | ||
| t.Fatal("no variable controls should be set when neither was chosen") | ||
| } | ||
| }) | ||
| } |
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,68 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/getplumber/plumber/control" | ||
| "github.com/getplumber/plumber/gitlab" | ||
| opaengine "github.com/getplumber/plumber/internal/engine/opa" | ||
| ) | ||
|
|
||
| // TestCicdVariablesJSONBlocks locks the machine-readable legacy-JSON contract | ||
| // for the two settings-variable controls: the top-level result key, the metric | ||
| // key, and which builder each control routes to are all hand-wired and mutually | ||
| // confusable (protected vs masked). A swapped block or a copy-pasted metric key | ||
| // would silently emit the wrong shape to JSON consumers with no other guard. | ||
| func TestCicdVariablesJSONBlocks(t *testing.T) { | ||
| // Five variables read: the JSON denominator totalVariablesChecked must | ||
| // report that regardless of how many were flagged. | ||
| result := &control.AnalysisResult{ | ||
| CiValid: true, | ||
| VariablesData: &gitlab.GitlabVariablesAnalysisData{Variables: make([]gitlab.CICDVariable, 5), Known: true}, | ||
| } | ||
|
|
||
| // Protected -> cicdVariablesProtectedResult, metric unprotectedFound. | ||
| protEntry := control.ControlEntry{ControlName: "cicdVariablesMustBeProtected"} | ||
| protFindings := []opaengine.Finding{ | ||
| {Code: "ISSUE-201", Data: map[string]any{"variableName": "AWS_KEY", "variableType": "env_var", "environment": "*"}}, | ||
| {Code: "ISSUE-201", Data: map[string]any{"variableName": "DEPLOY", "variableType": "file", "environment": "production"}}, | ||
| } | ||
| name, block := buildLegacyResult(protEntry, result, nil, protFindings) | ||
| if name != "cicdVariablesProtectedResult" { | ||
| t.Fatalf("protected block name = %q, want cicdVariablesProtectedResult (routing/copy-paste)", name) | ||
| } | ||
| m := block.(map[string]any) | ||
| if metrics, ok := m["metrics"].(map[string]any); !ok || metrics["unprotectedFound"] != 2 || metrics["totalVariablesChecked"] != 5 { | ||
| t.Errorf("protected metrics = %v, want unprotectedFound=2 totalVariablesChecked=5", m["metrics"]) | ||
| } | ||
| issues, ok := m["issues"].([]map[string]any) | ||
| if !ok || len(issues) != 2 { | ||
| t.Fatalf("protected issues = %v, want 2", m["issues"]) | ||
| } | ||
| // Settings-level findings carry the variable identity and no job key. | ||
| if issues[0]["variableName"] == nil || issues[0]["variableType"] == nil || issues[0]["environment"] == nil { | ||
| t.Errorf("issue must preserve variableName/variableType/environment: %v", issues[0]) | ||
| } | ||
| if _, hasJob := issues[0]["job"]; hasJob { | ||
| t.Errorf("settings-level finding must carry no job key: %v", issues[0]) | ||
| } | ||
|
|
||
| // Masked -> cicdVariablesMaskedResult, metric unmaskedFound, and must NOT | ||
| // leak the protected metric key. | ||
| maskEntry := control.ControlEntry{ControlName: "cicdVariablesMustBeMasked"} | ||
| maskFindings := []opaengine.Finding{ | ||
| {Code: "ISSUE-202", Data: map[string]any{"variableName": "PLAIN", "variableType": "env_var", "environment": "*"}}, | ||
| } | ||
| name, block = buildLegacyResult(maskEntry, result, nil, maskFindings) | ||
| if name != "cicdVariablesMaskedResult" { | ||
| t.Fatalf("masked block name = %q, want cicdVariablesMaskedResult (routing/copy-paste)", name) | ||
| } | ||
| m = block.(map[string]any) | ||
| metrics, ok := m["metrics"].(map[string]any) | ||
| if !ok || metrics["unmaskedFound"] != 1 || metrics["totalVariablesChecked"] != 5 { | ||
| t.Errorf("masked metrics = %v, want unmaskedFound=1 totalVariablesChecked=5", m["metrics"]) | ||
| } | ||
| if _, wrong := metrics["unprotectedFound"]; wrong { | ||
| t.Errorf("masked block leaked the protected metric key unprotectedFound: %v", metrics) | ||
| } | ||
| } |
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
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.