Skip to content

Commit 5cfbb53

Browse files
authored
Extract time validation and network port magic numbers to constants (#14545)
1 parent 4c1cd86 commit 5cfbb53

6 files changed

Lines changed: 79 additions & 27 deletions

File tree

pkg/constants/constants.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,28 @@ const MaxExpressionLineLength LineLength = 120
274274
// ExpressionBreakThreshold is the threshold for breaking long lines at logical points.
275275
const ExpressionBreakThreshold LineLength = 100
276276

277+
// Network port constants
278+
//
279+
// These constants define standard network port values used throughout the codebase
280+
// for MCP servers, gateway services, and validation ranges.
281+
282+
const (
283+
// DefaultMCPGatewayPort is the default port for the MCP gateway HTTP service
284+
DefaultMCPGatewayPort = 80
285+
286+
// DefaultMCPServerPort is the default port for MCP servers (safe-inputs server)
287+
DefaultMCPServerPort = 3000
288+
289+
// DefaultMCPInspectorPort is the default port for the MCP inspector (safe-outputs server)
290+
DefaultMCPInspectorPort = 3001
291+
292+
// MinNetworkPort is the minimum valid network port number
293+
MinNetworkPort = 1
294+
295+
// MaxNetworkPort is the maximum valid network port number
296+
MaxNetworkPort = 65535
297+
)
298+
277299
// DefaultMCPRegistryURL is the default MCP registry URL.
278300
const DefaultMCPRegistryURL URL = "https://api.mcp.github.com/v0.1"
279301

pkg/workflow/mcp_gateway_constants.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,35 @@
22
//
33
// # MCP Gateway Constants
44
//
5-
// This file defines default values and constants used by the MCP gateway
6-
// throughout the workflow compilation process. These constants ensure
7-
// consistent configuration across different components.
5+
// This file provides access to MCP gateway configuration constants.
86
//
97
// Gateway default values:
10-
// - Port: 80 (HTTP standard port)
8+
// - Port: 80 (HTTP standard port) - defined in pkg/constants
119
//
1210
// The MCP gateway port is used when:
1311
// - No custom port is specified in sandbox.mcp.port
1412
// - Building gateway configuration in mcp_gateway_config.go
1513
// - Generating gateway startup commands in mcp_setup_generator.go
1614
//
1715
// Historical note:
18-
// This constant was originally used with the awmg gateway binary.
19-
// The binary has been removed but the constant is retained for
20-
// backwards compatibility with existing workflow configurations.
16+
// This constant was originally defined locally but has been moved to pkg/constants
17+
// for centralization with other network port constants.
2118
//
2219
// Related files:
2320
// - mcp_gateway_config.go: Uses DefaultMCPGatewayPort for configuration
2421
// - mcp_setup_generator.go: Uses port for gateway startup
25-
// - constants/constants.go: Other MCP-related constants (versions, containers)
22+
// - constants/constants.go: Defines all MCP-related constants (versions, containers, ports)
2623
//
2724
// Related constants in pkg/constants:
25+
// - DefaultMCPGatewayPort: Gateway port (80)
2826
// - DefaultMCPGatewayVersion: Gateway container version
2927
// - DefaultMCPGatewayContainer: Gateway container image
3028
// - DefaultGitHubMCPServerVersion: GitHub MCP server version
3129
package workflow
3230

33-
const (
34-
// DefaultMCPGatewayPort is the default port for the MCP gateway
35-
// This constant is kept for backwards compatibility with existing configurations
36-
// even though the awmg gateway binary has been removed.
37-
DefaultMCPGatewayPort = 80
38-
)
31+
import "github.com/github/gh-aw/pkg/constants"
32+
33+
// DefaultMCPGatewayPort is the default port for the MCP gateway
34+
// This is now an alias to the constant defined in pkg/constants
35+
// for backwards compatibility with existing code.
36+
const DefaultMCPGatewayPort = constants.DefaultMCPGatewayPort

pkg/workflow/mcp_setup_generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (c *Compiler) generateMCPSetup(yaml *strings.Builder, tools map[string]any,
259259
yaml.WriteString(" # Generate a secure random API key (360 bits of entropy, 40+ chars)\n")
260260
yaml.WriteString(" API_KEY=\"\"\n")
261261
yaml.WriteString(" API_KEY=$(openssl rand -base64 45 | tr -d '/+=')\n")
262-
yaml.WriteString(" PORT=3001\n")
262+
fmt.Fprintf(yaml, " PORT=%d\n", constants.DefaultMCPInspectorPort)
263263
yaml.WriteString(" \n")
264264
yaml.WriteString(" # Register API key as secret to mask it from logs\n")
265265
yaml.WriteString(" echo \"::add-mask::${API_KEY}\"\n")
@@ -382,7 +382,7 @@ func (c *Compiler) generateMCPSetup(yaml *strings.Builder, tools map[string]any,
382382
yaml.WriteString(" # Generate a secure random API key (360 bits of entropy, 40+ chars)\n")
383383
yaml.WriteString(" API_KEY=\"\"\n")
384384
yaml.WriteString(" API_KEY=$(openssl rand -base64 45 | tr -d '/+=')\n")
385-
yaml.WriteString(" PORT=3000\n")
385+
fmt.Fprintf(yaml, " PORT=%d\n", constants.DefaultMCPServerPort)
386386
yaml.WriteString(" \n")
387387
yaml.WriteString(" # Register API key as secret to mask it from logs\n")
388388
yaml.WriteString(" echo \"::add-mask::${API_KEY}\"\n")

pkg/workflow/sandbox_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func validateSandboxConfig(workflowData *WorkflowData) error {
155155

156156
// Validate MCP gateway port if configured
157157
if sandboxConfig.MCP != nil && sandboxConfig.MCP.Port != 0 {
158-
if err := validateIntRange(sandboxConfig.MCP.Port, 1, 65535, "sandbox.mcp.port"); err != nil {
158+
if err := validateIntRange(sandboxConfig.MCP.Port, constants.MinNetworkPort, constants.MaxNetworkPort, "sandbox.mcp.port"); err != nil {
159159
return err
160160
}
161161
sandboxValidationLog.Printf("Validated MCP gateway port: %d", sandboxConfig.MCP.Port)

pkg/workflow/time_delta.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,20 @@ func parseTimeDeltaWithMinutes(deltaStr string, allowMinutes bool) (*TimeDelta,
139139
}
140140

141141
// Validate reasonable limits
142-
if delta.Months > 12 {
143-
return nil, fmt.Errorf("time delta too large: %d months exceeds maximum of 12 months", delta.Months)
142+
if delta.Months > MaxTimeDeltaMonths {
143+
return nil, fmt.Errorf("time delta too large: %d months exceeds maximum of %d months", delta.Months, MaxTimeDeltaMonths)
144144
}
145-
if delta.Weeks > 52 {
146-
return nil, fmt.Errorf("time delta too large: %d weeks exceeds maximum of 52 weeks", delta.Weeks)
145+
if delta.Weeks > MaxTimeDeltaWeeks {
146+
return nil, fmt.Errorf("time delta too large: %d weeks exceeds maximum of %d weeks", delta.Weeks, MaxTimeDeltaWeeks)
147147
}
148-
if delta.Days > 365 {
149-
return nil, fmt.Errorf("time delta too large: %d days exceeds maximum of 365 days", delta.Days)
148+
if delta.Days > MaxTimeDeltaDays {
149+
return nil, fmt.Errorf("time delta too large: %d days exceeds maximum of %d days", delta.Days, MaxTimeDeltaDays)
150150
}
151-
if delta.Hours > 8760 { // 365 * 24
152-
return nil, fmt.Errorf("time delta too large: %d hours exceeds maximum of 8760 hours", delta.Hours)
151+
if delta.Hours > MaxTimeDeltaHours {
152+
return nil, fmt.Errorf("time delta too large: %d hours exceeds maximum of %d hours", delta.Hours, MaxTimeDeltaHours)
153153
}
154-
if delta.Minutes > 525600 { // 365 * 24 * 60
155-
return nil, fmt.Errorf("time delta too large: %d minutes exceeds maximum of 525600 minutes", delta.Minutes)
154+
if delta.Minutes > MaxTimeDeltaMinutes {
155+
return nil, fmt.Errorf("time delta too large: %d minutes exceeds maximum of %d minutes", delta.Minutes, MaxTimeDeltaMinutes)
156156
}
157157

158158
timeDeltaLog.Printf("Parsed time delta successfully: %s", delta.String())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package workflow
2+
3+
// Time delta validation limits
4+
//
5+
// Policy: Maximum stop-after time is 1 year to prevent scheduling too far in the future.
6+
// These constants define the maximum allowed values for each time unit when parsing
7+
// time deltas in workflow schedules. The limits ensure workflows don't schedule actions
8+
// unreasonably far into the future, which could indicate configuration errors or create
9+
// operational challenges.
10+
//
11+
// All limits are equivalent to approximately 1 year:
12+
// - 12 months = 1 year (exact)
13+
// - 52 weeks = 364 days ≈ 1 year
14+
// - 365 days = 1 year (non-leap year)
15+
// - 8760 hours = 365 days * 24 hours
16+
// - 525600 minutes = 365 days * 24 hours * 60 minutes
17+
const (
18+
// MaxTimeDeltaMonths is the maximum allowed months in a time delta (1 year)
19+
MaxTimeDeltaMonths = 12
20+
21+
// MaxTimeDeltaWeeks is the maximum allowed weeks in a time delta (approximately 1 year)
22+
MaxTimeDeltaWeeks = 52
23+
24+
// MaxTimeDeltaDays is the maximum allowed days in a time delta (1 year, non-leap)
25+
MaxTimeDeltaDays = 365
26+
27+
// MaxTimeDeltaHours is the maximum allowed hours in a time delta (365 days * 24 hours)
28+
MaxTimeDeltaHours = 8760
29+
30+
// MaxTimeDeltaMinutes is the maximum allowed minutes in a time delta (365 days * 24 hours * 60 minutes)
31+
MaxTimeDeltaMinutes = 525600
32+
)

0 commit comments

Comments
 (0)