Skip to content
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

Improve http runner protectoin #7

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---

name: tests

on:
Expand Down
5 changes: 3 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
# https://golangci-lint.run/usage/configuration/

run:
Expand Down Expand Up @@ -66,7 +67,7 @@ linters-settings:
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
Expand Down Expand Up @@ -98,7 +99,7 @@ linters-settings:
# There are three different modes: `original`, `strict`, and `lax`.
# Default: "original"
list-mode: original
# List of file globs that will match this list of settings to compare against.
# File globs that will match this list of settings to compare against.
# Default: $all
files:
- "$all"
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
exclude: "^docs/|/.vscode/"
default_stages: [commit]

Expand Down
3 changes: 2 additions & 1 deletion cmd/chkok.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
chkok "github.com/farzadghanei/chkok/internal"
)

const Version string = "0.2.0"
// Version of the app
const Version string = "0.3.0"

// ModeHTTP run checks in http server mode
const ModeHTTP string = "http"
Expand Down
2 changes: 2 additions & 0 deletions cmd/chkok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func TestRunHttp(t *testing.T) {
t.Fatalf("Failed to create HTTP request: %v", err)
}
req.Header.Set("X-Server-Shutdown", "test-shutdown-signal") // shutdown the server after the request
req.Header.Set("X-Required-Header", "required-value")
req.Header.Set("X-Required-Header2", "anything")

// Send the request multiple times, waiting for the server to
// start up and respond
Expand Down
12 changes: 9 additions & 3 deletions examples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ runners:
# response_timeout: "TIMEOUT"
cli: {} # override default runner only for CLI mode
http: # override default runner only for HTTP mode
# shutdown_signal_header is mainly useful for testing http mode, do not set it in production
# if set, better be treated like a secret, and a secure transport layer should be used.
listen_address: "127.0.0.1:51234"
# shutdown_signal_header is mainly useful for testing http mode,
# do not set it in production
# if set, better be treated like a secret, and a secure transport
# layer should be used.
# this is the value set on "X-Shutdown-Signal" header in the http request
# shutdown_signal_header: "test-shutdown-signal"
# listen_address: "127.0.0.1:51234"
# request_read_timeout: 2s
# response_write_timeout: 2s
# timeout: 5s
# max_header_bytes: 8192
# max_concurrent_requests: 1 # 0 means no limit
# request_required_headers:
# "X-Required-Header": "required-value"
# "X-Required-Header2": "" # header existance is required, not value


check_suites:
Expand Down
10 changes: 8 additions & 2 deletions examples/test-http.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
runners:
default:
timeout: 1m
request_required_headers:
"X-Required-Header": "required-value"
http:
listen_address: "127.0.0.1:51234"
request_read_timeout: 2s
response_write_timeout: 2s
# shutdown_signal_header is mainly useful for testing http mode, do not set it in production
# if set, better be treated like a secret, and a secure transport layer should be used.
# shutdown_signal_header is mainly useful for testing http mode,
# do not set it in production
# if set, better be treated like a secret, and a secure transport
# layer should be used.
# this is the value set on "X-Shutdown-Signal" header in the http request
shutdown_signal_header: "test-shutdown-signal"
timeout: 5s
request_required_headers:
"X-Required-Header2": "" # header existance is required


check_suites:
Expand Down
128 changes: 91 additions & 37 deletions internal/conf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chkok

import (
"maps"
"os"
"time"

Expand All @@ -21,15 +22,19 @@ type Conf struct {

// ConfRunner is config for the check runners
type ConfRunner struct {
Timeout *time.Duration
ShutdownSignalHeader *string `yaml:"shutdown_signal_header"`
MaxHeaderBytes *int `yaml:"max_header_bytes"`
ListenAddress string `yaml:"listen_address"`
RequestReadTimeout *time.Duration `yaml:"request_read_timeout"`
ResponseWriteTimeout *time.Duration `yaml:"response_write_timeout"`
ResponseOK *string `yaml:"response_ok"`
ResponseFailed *string `yaml:"response_failed"`
ResponseTimeout *string `yaml:"response_timeout"`
Timeout *time.Duration
ShutdownSignalHeader *string `yaml:"shutdown_signal_header"`
MaxHeaderBytes *int `yaml:"max_header_bytes"`
MaxConcurrentRequests *int `yaml:"max_concurrent_requests"`
ListenAddress string `yaml:"listen_address"`
RequestReadTimeout *time.Duration `yaml:"request_read_timeout"`
RequestRequiredHeaders map[string]string `yaml:"request_required_headers"`
ResponseWriteTimeout *time.Duration `yaml:"response_write_timeout"`
ResponseOK *string `yaml:"response_ok"`
ResponseFailed *string `yaml:"response_failed"`
ResponseTimeout *string `yaml:"response_timeout"`
ResponseUnavailable *string `yaml:"response_unavailable"`
ResponseInvalidRequest *string `yaml:"response_invalid_request"`
}

// ConfCheckSpec is the spec for each check configuration
Expand Down Expand Up @@ -58,23 +63,35 @@ func ReadConf(path string) (*Conf, error) {
return &conf, err
}

// GetDefaultConfRunner returns a ConfRunner based on the default configuration
func GetDefaultConfRunner(runners *ConfRunners) ConfRunner {
// GetBaseConfRunner returns a base ConfRunner with default literal values
func GetBaseConfRunner() ConfRunner {
var timeout, readTimeout, writeTimout time.Duration = 5 * time.Minute, 30 * time.Second, 30 * time.Second
var maxHeaderBytes int = 8 * 1024
var MaxConcurrentRequests int = 1
var respOK, respFailed, respTimeout string = "OK", "FAILED", "TIMEOUT"
var respUnavailable, respInvalidRequest string = "UNAVAILABLE", "INVALID REQUEST"

baseConf := ConfRunner{
Timeout: &timeout,
ShutdownSignalHeader: nil,
MaxHeaderBytes: &maxHeaderBytes,
ListenAddress: "127.0.0.1:8880",
RequestReadTimeout: &readTimeout,
ResponseWriteTimeout: &writeTimout,
ResponseOK: &respOK,
ResponseFailed: &respFailed,
ResponseTimeout: &respTimeout,
Timeout: &timeout,
ShutdownSignalHeader: nil,
MaxHeaderBytes: &maxHeaderBytes,
ListenAddress: "127.0.0.1:8880",
RequestReadTimeout: &readTimeout,
RequestRequiredHeaders: map[string]string{},
ResponseWriteTimeout: &writeTimout,
ResponseOK: &respOK,
ResponseFailed: &respFailed,
ResponseTimeout: &respTimeout,
ResponseInvalidRequest: &respInvalidRequest,
ResponseUnavailable: &respUnavailable,
MaxConcurrentRequests: &MaxConcurrentRequests,
}
return baseConf
}

// GetDefaultConfRunner returns a ConfRunner based on the default configuration
func GetDefaultConfRunner(runners *ConfRunners) ConfRunner {
baseConf := GetBaseConfRunner()

if defaultConf, defaultExists := (*runners)["default"]; defaultExists {
baseConf = MergedConfRunners(&baseConf, &defaultConf)
Expand All @@ -100,21 +117,7 @@ func GetConfRunner(runners *ConfRunners, name string) (ConfRunner, bool) {

// MergedConfRunners merges the baseConf with the overrideConf and returns the merged ConfRunner
func MergedConfRunners(baseConf, overrideConf *ConfRunner) ConfRunner {
mergedConf := ConfRunner{
Timeout: overrideConf.Timeout,
ShutdownSignalHeader: overrideConf.ShutdownSignalHeader,
ListenAddress: overrideConf.ListenAddress,
RequestReadTimeout: overrideConf.RequestReadTimeout,
ResponseWriteTimeout: overrideConf.ResponseWriteTimeout,
ResponseOK: overrideConf.ResponseOK,
ResponseFailed: overrideConf.ResponseFailed,
ResponseTimeout: overrideConf.ResponseTimeout,
MaxHeaderBytes: overrideConf.MaxHeaderBytes,
}

if mergedConf.Timeout == nil {
mergedConf.Timeout = baseConf.Timeout
}
mergedConf := CopyConfRunner(overrideConf)

if mergedConf.ShutdownSignalHeader == nil {
mergedConf.ShutdownSignalHeader = baseConf.ShutdownSignalHeader
Expand All @@ -127,15 +130,39 @@ func MergedConfRunners(baseConf, overrideConf *ConfRunner) ConfRunner {
if mergedConf.ListenAddress == "" {
mergedConf.ListenAddress = baseConf.ListenAddress
}
if mergedConf.MaxConcurrentRequests == nil {
mergedConf.MaxConcurrentRequests = baseConf.MaxConcurrentRequests
}

mergeConfRunnerTimeouts(&mergedConf, baseConf)

// Merge the request required headers map with the baseConf
for key, value := range baseConf.RequestRequiredHeaders {
if _, exists := mergedConf.RequestRequiredHeaders[key]; !exists {
mergedConf.RequestRequiredHeaders[key] = value
}
}

mergeConfRunnerResponses(&mergedConf, baseConf)

return mergedConf
}

// mergeConfRunnerTimeouts merges the timeout fields of the mergedConf with the baseConf in place
func mergeConfRunnerTimeouts(mergedConf, baseConf *ConfRunner) {
if mergedConf.Timeout == nil {
mergedConf.Timeout = baseConf.Timeout
}
if mergedConf.RequestReadTimeout == nil {
mergedConf.RequestReadTimeout = baseConf.RequestReadTimeout
}

if mergedConf.ResponseWriteTimeout == nil {
mergedConf.ResponseWriteTimeout = baseConf.ResponseWriteTimeout
}
}

// mergeConfRunnerResponses merges the response fields of the mergedConf with the baseConf in place
func mergeConfRunnerResponses(mergedConf, baseConf *ConfRunner) {
if mergedConf.ResponseOK == nil {
mergedConf.ResponseOK = baseConf.ResponseOK
}
Expand All @@ -148,5 +175,32 @@ func MergedConfRunners(baseConf, overrideConf *ConfRunner) ConfRunner {
mergedConf.ResponseTimeout = baseConf.ResponseTimeout
}

return mergedConf
if mergedConf.ResponseUnavailable == nil {
mergedConf.ResponseUnavailable = baseConf.ResponseUnavailable
}

if mergedConf.ResponseInvalidRequest == nil {
mergedConf.ResponseInvalidRequest = baseConf.ResponseInvalidRequest
}
}

// CopyConfRunner returns a copy of the ConfRunner with the same values
func CopyConfRunner(conf *ConfRunner) ConfRunner {
newConfRunner := ConfRunner{
Timeout: conf.Timeout,
ShutdownSignalHeader: conf.ShutdownSignalHeader,
ListenAddress: conf.ListenAddress,
RequestReadTimeout: conf.RequestReadTimeout,
RequestRequiredHeaders: map[string]string{},
ResponseWriteTimeout: conf.ResponseWriteTimeout,
ResponseOK: conf.ResponseOK,
ResponseFailed: conf.ResponseFailed,
ResponseTimeout: conf.ResponseTimeout,
ResponseUnavailable: conf.ResponseUnavailable,
ResponseInvalidRequest: conf.ResponseInvalidRequest,
MaxHeaderBytes: conf.MaxHeaderBytes,
MaxConcurrentRequests: conf.MaxConcurrentRequests,
}
maps.Copy(newConfRunner.RequestRequiredHeaders, conf.RequestRequiredHeaders)
return newConfRunner
}
Loading
Loading