|
| 1 | +package cmd |
| 2 | + |
| 3 | +// Scan-time org quality-gate enforcement override. |
| 4 | +// |
| 5 | +// When a scan runs under a real (non-community) authenticated org, the org's |
| 6 | +// stored quality-gate policy is fetched once and applied over the scan's |
| 7 | +// control flags. The decided semantics are: ORG POLICY ALWAYS WINS — a set org |
| 8 | +// value overrides even an explicitly-passed CLI flag. A setting the org left |
| 9 | +// null leaves the caller's flag (or builtin default) untouched. Verbose output |
| 10 | +// notes every supersede / application; non-authenticated scans use only the |
| 11 | +// CLI flags (this function returns early). |
| 12 | + |
| 13 | +import ( |
| 14 | + "fmt" |
| 15 | + "os" |
| 16 | + |
| 17 | + "github.com/spf13/cobra" |
| 18 | + "github.com/vulnetix/cli/v3/pkg/auth" |
| 19 | + "github.com/vulnetix/cli/v3/pkg/vdb" |
| 20 | +) |
| 21 | + |
| 22 | +// qualityGateOverridePointers bundles pointers to the nine scan-time control |
| 23 | +// locals in runScanWithFeatures so applyOrgQualityGate can overwrite each in |
| 24 | +// place when the org enforces a value. Field names mirror the camelCase config |
| 25 | +// keys returned by /v2/cli.quality-gate-get. |
| 26 | +type qualityGateOverridePointers struct { |
| 27 | + blockEol *bool |
| 28 | + blockMalware *bool |
| 29 | + blockUnpinned *bool |
| 30 | + cooldown *int |
| 31 | + versionLag *int |
| 32 | + scaAutofixMaxMajorBump *int |
| 33 | + exploits *string |
| 34 | + severity *string |
| 35 | + scaAutofixStrategy *string |
| 36 | +} |
| 37 | + |
| 38 | +// applyOrgQualityGate fetches the authenticated org's quality-gate policy and |
| 39 | +// applies every set (non-null) enforcement value over the caller's scan flags. |
| 40 | +// It is a no-op (caller/builtin values stand) when the scan is unauthenticated |
| 41 | +// or community-tier, when credentials cannot be loaded, when the lookup fails, |
| 42 | +// or when the org has no policy row. All diagnostic output is gated on the |
| 43 | +// existing --verbose flag. |
| 44 | +func applyOrgQualityGate(cmd *cobra.Command, p qualityGateOverridePointers) { |
| 45 | + creds, err := auth.LoadCredentials() |
| 46 | + if err != nil || creds == nil || auth.IsCommunity(creds) { |
| 47 | + if verbose { |
| 48 | + fmt.Fprintln(os.Stderr, "Org quality gate: skipped (no authenticated org — using scan flags only).") |
| 49 | + } |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + client := vdb.NewClientFromCredentials(creds) |
| 54 | + client.APIVersion = "/v2" |
| 55 | + client.BaseURL = vdb.DefaultBaseURL |
| 56 | + if f := cmd.Flags().Lookup("base-url"); f != nil { |
| 57 | + if baseURL, _ := cmd.Flags().GetString("base-url"); baseURL != "" { |
| 58 | + client.BaseURL = baseURL |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + resp, err := client.CliQualityGateGet(envForCli()) |
| 63 | + if err != nil { |
| 64 | + if verbose { |
| 65 | + fmt.Fprintf(os.Stderr, "Org quality gate: lookup failed (%v) — using scan flags only.\n", err) |
| 66 | + } |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + config, _ := resp.Data["config"].(map[string]any) |
| 71 | + if config == nil { |
| 72 | + if verbose { |
| 73 | + fmt.Fprintln(os.Stderr, "Org quality gate: no policy configured — using scan flags only.") |
| 74 | + } |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + applied := 0 |
| 79 | + |
| 80 | + applyBool := func(flag, key string, target *bool) { |
| 81 | + orgVal, ok := qgConfigBool(config, key) |
| 82 | + if !ok { |
| 83 | + return |
| 84 | + } |
| 85 | + callerVal := *target |
| 86 | + *target = orgVal |
| 87 | + applied++ |
| 88 | + noteOverride(cmd, flag, fmt.Sprintf("%t", callerVal), fmt.Sprintf("%t", orgVal)) |
| 89 | + } |
| 90 | + applyInt := func(flag, key string, target *int) { |
| 91 | + orgVal, ok := qgConfigInt(config, key) |
| 92 | + if !ok { |
| 93 | + return |
| 94 | + } |
| 95 | + callerVal := *target |
| 96 | + *target = orgVal |
| 97 | + applied++ |
| 98 | + noteOverride(cmd, flag, fmt.Sprintf("%d", callerVal), fmt.Sprintf("%d", orgVal)) |
| 99 | + } |
| 100 | + applyString := func(flag, key string, target *string) { |
| 101 | + orgVal, ok := qgConfigString(config, key) |
| 102 | + if !ok { |
| 103 | + return |
| 104 | + } |
| 105 | + callerVal := *target |
| 106 | + *target = orgVal |
| 107 | + applied++ |
| 108 | + noteOverride(cmd, flag, callerVal, orgVal) |
| 109 | + } |
| 110 | + |
| 111 | + applyBool("block-eol", "blockEol", p.blockEol) |
| 112 | + applyBool("block-malware", "blockMalware", p.blockMalware) |
| 113 | + applyBool("block-unpinned", "blockUnpinned", p.blockUnpinned) |
| 114 | + applyInt("cooldown", "cooldown", p.cooldown) |
| 115 | + applyInt("version-lag", "versionLag", p.versionLag) |
| 116 | + applyInt("sca-autofix-max-major-bump", "scaAutofixMaxMajorBump", p.scaAutofixMaxMajorBump) |
| 117 | + applyString("exploits", "exploits", p.exploits) |
| 118 | + applyString("severity", "severity", p.severity) |
| 119 | + applyString("sca-autofix-strategy", "scaAutofixStrategy", p.scaAutofixStrategy) |
| 120 | + |
| 121 | + if applied > 0 && verbose { |
| 122 | + fmt.Fprintf(os.Stderr, "Org quality gate: applied %s from org policy (org policy always wins).\n", |
| 123 | + pluralise("setting", applied)) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// noteOverride emits the verbose supersede/applied line for one enforcement |
| 128 | +// field. When the caller explicitly set the flag and the org value differs, it |
| 129 | +// notes the supersede; when the caller did not set it, it notes the org policy |
| 130 | +// application. Output is gated on --verbose. callerVal/orgVal are already |
| 131 | +// stringified by the caller. |
| 132 | +func noteOverride(cmd *cobra.Command, flag, callerVal, orgVal string) { |
| 133 | + if !verbose { |
| 134 | + return |
| 135 | + } |
| 136 | + if cmd.Flags().Changed(flag) { |
| 137 | + if callerVal != orgVal { |
| 138 | + fmt.Fprintf(os.Stderr, "--%s %s superseded by org policy: %s\n", flag, callerVal, orgVal) |
| 139 | + } |
| 140 | + return |
| 141 | + } |
| 142 | + fmt.Fprintf(os.Stderr, "org policy applied: --%s %s\n", flag, orgVal) |
| 143 | +} |
| 144 | + |
| 145 | +// qgConfigBool / qgConfigInt / qgConfigString read one nullable enforcement |
| 146 | +// field from the cli.quality-gate-get config map. The second return is false |
| 147 | +// when the org left the field null (absent or JSON null), so the caller leaves |
| 148 | +// its local untouched. JSON numbers decode as float64. |
| 149 | +func qgConfigBool(m map[string]any, key string) (bool, bool) { |
| 150 | + v, ok := m[key].(bool) |
| 151 | + return v, ok |
| 152 | +} |
| 153 | + |
| 154 | +func qgConfigInt(m map[string]any, key string) (int, bool) { |
| 155 | + if v, ok := m[key].(float64); ok { |
| 156 | + return int(v), true |
| 157 | + } |
| 158 | + return 0, false |
| 159 | +} |
| 160 | + |
| 161 | +func qgConfigString(m map[string]any, key string) (string, bool) { |
| 162 | + if v, ok := m[key].(string); ok && v != "" { |
| 163 | + return v, true |
| 164 | + } |
| 165 | + return "", false |
| 166 | +} |
0 commit comments