Skip to content

Commit 610e8c2

Browse files
authored
refactor: consolidate guard policy env var names into constants, replace os.Getenv with envutil (#5546)
## ✨ Enhancement The five guard-policy env var names were duplicated as string literals across `flags_difc.go` and `guard_policy_parse.go`, and `guard_policy_parse.go` mixed raw `os.Getenv` calls with `envutil.GetEnvBool` — inconsistent with the rest of the codebase. **What does this improve?** - Single source of truth for env var names — adding or renaming a variable is a one-line change - Consistent `envutil.GetEnvString/Bool` usage throughout guard policy code **Implementation approach:** - **New `internal/config/guard_policy_env.go`** — five exported constants: ```go const ( EnvGuardPolicyJSON = "MCP_GATEWAY_GUARD_POLICY_JSON" EnvAllowOnlyScopePublic = "MCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC" EnvAllowOnlyScopeOwner = "MCP_GATEWAY_ALLOWONLY_SCOPE_OWNER" EnvAllowOnlyScopeRepo = "MCP_GATEWAY_ALLOWONLY_SCOPE_REPO" EnvAllowOnlyMinIntegrity = "MCP_GATEWAY_ALLOWONLY_MIN_INTEGRITY" ) ``` - **`guard_policy_parse.go`** — replaced `os.Getenv(...)` with `envutil.GetEnvString(Env..., "")`, replaced string literals with constants, removed the `os` import - **`flags_difc.go`** — replaced string literals with `config.EnvXxx` constants
2 parents ea4796b + aff2296 commit 610e8c2

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

internal/cmd/flags_difc.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"strings"
99

10+
"github.com/github/gh-aw-mcpg/internal/config"
1011
"github.com/github/gh-aw-mcpg/internal/difc"
1112
"github.com/github/gh-aw-mcpg/internal/envutil"
1213
"github.com/github/gh-aw-mcpg/internal/strutil"
@@ -28,11 +29,11 @@ func init() {
2829
RegisterFlag(func(cmd *cobra.Command) {
2930
cmd.Flags().StringVar(&difcMode, "guards-mode", getDefaultDIFCMode(), "Guards enforcement mode: strict (deny violations), filter (remove denied tools), or propagate (auto-adjust agent labels on reads)")
3031
cmd.Flags().StringVar(&difcSinkServerIDs, "guards-sink-server-ids", envutil.GetEnvString("MCP_GATEWAY_GUARDS_SINK_SERVER_IDS", ""), "Comma-separated server IDs whose RPC JSONL logs should include agent secrecy/integrity tag snapshots")
31-
cmd.Flags().StringVar(&guardPolicyJSON, "guard-policy-json", envutil.GetEnvString("MCP_GATEWAY_GUARD_POLICY_JSON", ""), "Guard policy JSON (e.g. {\"allow-only\":{\"repos\":\"public\",\"min-integrity\":\"none\"}})")
32-
cmd.Flags().BoolVar(&allowOnlyPublic, "allowonly-scope-public", envutil.GetEnvBool("MCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC", false), "Use public AllowOnly scope")
33-
cmd.Flags().StringVar(&allowOnlyOwner, "allowonly-scope-owner", envutil.GetEnvString("MCP_GATEWAY_ALLOWONLY_SCOPE_OWNER", ""), "AllowOnly owner scope value")
34-
cmd.Flags().StringVar(&allowOnlyRepo, "allowonly-scope-repo", envutil.GetEnvString("MCP_GATEWAY_ALLOWONLY_SCOPE_REPO", ""), "AllowOnly repo name (requires owner)")
35-
cmd.Flags().StringVar(&allowOnlyMinInt, "allowonly-min-integrity", envutil.GetEnvString("MCP_GATEWAY_ALLOWONLY_MIN_INTEGRITY", ""), "AllowOnly integrity: none|unapproved|approved|merged")
32+
cmd.Flags().StringVar(&guardPolicyJSON, "guard-policy-json", envutil.GetEnvString(config.EnvGuardPolicyJSON, ""), "Guard policy JSON (e.g. {\"allow-only\":{\"repos\":\"public\",\"min-integrity\":\"none\"}})")
33+
cmd.Flags().BoolVar(&allowOnlyPublic, "allowonly-scope-public", envutil.GetEnvBool(config.EnvAllowOnlyScopePublic, false), "Use public AllowOnly scope")
34+
cmd.Flags().StringVar(&allowOnlyOwner, "allowonly-scope-owner", envutil.GetEnvString(config.EnvAllowOnlyScopeOwner, ""), "AllowOnly owner scope value")
35+
cmd.Flags().StringVar(&allowOnlyRepo, "allowonly-scope-repo", envutil.GetEnvString(config.EnvAllowOnlyScopeRepo, ""), "AllowOnly repo name (requires owner)")
36+
cmd.Flags().StringVar(&allowOnlyMinInt, "allowonly-min-integrity", envutil.GetEnvString(config.EnvAllowOnlyMinIntegrity, ""), "AllowOnly integrity: none|unapproved|approved|merged")
3637
})
3738
}
3839

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
// Environment variable names for guard policy configuration.
4+
const (
5+
EnvGuardPolicyJSON = "MCP_GATEWAY_GUARD_POLICY_JSON"
6+
EnvAllowOnlyScopePublic = "MCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC"
7+
EnvAllowOnlyScopeOwner = "MCP_GATEWAY_ALLOWONLY_SCOPE_OWNER"
8+
EnvAllowOnlyScopeRepo = "MCP_GATEWAY_ALLOWONLY_SCOPE_REPO"
9+
EnvAllowOnlyMinIntegrity = "MCP_GATEWAY_ALLOWONLY_MIN_INTEGRITY"
10+
)

internal/config/guard_policy_parse.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,25 @@ func ResolveGuardPolicyOverride(
224224
return policy, "cli", nil
225225
}
226226

227-
if envPolicyJSON := strings.TrimSpace(os.Getenv("MCP_GATEWAY_GUARD_POLICY_JSON")); envPolicyJSON != "" {
227+
if envPolicyJSON := strings.TrimSpace(envutil.GetEnvString(EnvGuardPolicyJSON, "")); envPolicyJSON != "" {
228228
policy, err := ParseGuardPolicyJSON(envPolicyJSON)
229229
if err != nil {
230230
return nil, "", err
231231
}
232232
return policy, "env", nil
233233
}
234234

235-
_, hasScopePublic := os.LookupEnv("MCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC")
236-
_, hasScopeOwner := os.LookupEnv("MCP_GATEWAY_ALLOWONLY_SCOPE_OWNER")
237-
_, hasScopeRepo := os.LookupEnv("MCP_GATEWAY_ALLOWONLY_SCOPE_REPO")
238-
_, hasMinIntegrity := os.LookupEnv("MCP_GATEWAY_ALLOWONLY_MIN_INTEGRITY")
235+
_, hasScopePublic := os.LookupEnv(EnvAllowOnlyScopePublic)
236+
_, hasScopeOwner := os.LookupEnv(EnvAllowOnlyScopeOwner)
237+
_, hasScopeRepo := os.LookupEnv(EnvAllowOnlyScopeRepo)
238+
_, hasMinIntegrity := os.LookupEnv(EnvAllowOnlyMinIntegrity)
239239

240240
if hasScopePublic || hasScopeOwner || hasScopeRepo || hasMinIntegrity {
241241
policy, err := BuildAllowOnlyPolicy(
242-
envutil.GetEnvBool("MCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC", false),
243-
os.Getenv("MCP_GATEWAY_ALLOWONLY_SCOPE_OWNER"),
244-
os.Getenv("MCP_GATEWAY_ALLOWONLY_SCOPE_REPO"),
245-
os.Getenv("MCP_GATEWAY_ALLOWONLY_MIN_INTEGRITY"),
242+
envutil.GetEnvBool(EnvAllowOnlyScopePublic, false),
243+
envutil.GetEnvString(EnvAllowOnlyScopeOwner, ""),
244+
envutil.GetEnvString(EnvAllowOnlyScopeRepo, ""),
245+
envutil.GetEnvString(EnvAllowOnlyMinIntegrity, ""),
246246
)
247247
if err != nil {
248248
return nil, "", err

0 commit comments

Comments
 (0)