Skip to content

Commit

Permalink
Add validation logic for server.proxies
Browse files Browse the repository at this point in the history
- Implemented validation to ensure each server has at least one proxy defined.
- Added checks to verify that each referenced proxy exists in the global proxies configuration.
- Included error recording and wrapping for validation failures.
  • Loading branch information
sinadarbouy committed Jul 15, 2024
1 parent d35c2ab commit d476631
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,33 +449,21 @@ func (c *Config) ValidateGlobalConfig(ctx context.Context) *gerr.GatewayDError {
}
}

// if len(globalConfig.Clients) > 1 {
// seenConfigObjects = append(seenConfigObjects, "clients")
// }

// for configGroup := range globalConfig.Pools {
// if globalConfig.Pools[configGroup] == nil {
// err := fmt.Errorf("\"pools.%s\" is nil or empty", configGroup)
// span.RecordError(err)
// errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
// }
// }

// if len(globalConfig.Pools) > 1 {
// seenConfigObjects = append(seenConfigObjects, "pools")
// }

// for configGroup := range globalConfig.Proxies {
// if globalConfig.Proxies[configGroup] == nil {
// err := fmt.Errorf("\"proxies.%s\" is nil or empty", configGroup)
// span.RecordError(err)
// errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
// }
// }

// if len(globalConfig.Proxies) > 1 {
// seenConfigObjects = append(seenConfigObjects, "proxies")
// }
for configGroup := range globalConfig.Pools {
if globalConfig.Pools[configGroup] == nil {
err := fmt.Errorf("\"pools.%s\" is nil or empty", configGroup)
span.RecordError(err)
errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
}
}

for configGroup := range globalConfig.Proxies {
if globalConfig.Proxies[configGroup] == nil {
err := fmt.Errorf("\"proxies.%s\" is nil or empty", configGroup)
span.RecordError(err)
errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
}
}

for configGroup := range globalConfig.Servers {
if globalConfig.Servers[configGroup] == nil {
Expand All @@ -489,6 +477,24 @@ func (c *Config) ValidateGlobalConfig(ctx context.Context) *gerr.GatewayDError {
seenConfigObjects = append(seenConfigObjects, "servers")
}

// Each server configuration should have at least one proxy defined.
// Each proxy in the server configuration should be referenced in proxies configuration.
for serverName, server := range globalConfig.Servers {
if len(server.Proxies) == 0 {
err := fmt.Errorf("\"servers.%s\" has no proxies defined", serverName)
span.RecordError(err)
errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
continue
}
for _, proxyName := range server.Proxies {
if _, exists := c.globalDefaults.Proxies[proxyName]; !exists {
err := fmt.Errorf("\"servers.%s\" references a non-existent proxy \"%s\"", serverName, proxyName)
span.RecordError(err)
errors = append(errors, gerr.ErrValidationFailed.Wrap(err))
}
}
}

sort.Strings(seenConfigObjects)

if len(seenConfigObjects) > 0 && !reflect.DeepEqual(configObjects, seenConfigObjects) {
Expand Down

0 comments on commit d476631

Please sign in to comment.