ЛР7 на 4: HTTPS, mTLS, OpenBao AppRole и ротация leaf.
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gavrilenko-sxdees/lab7/internal/config"
|
||||
"gavrilenko-sxdees/lab7/internal/tlslisten"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func Healthcheck() int {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: load env:", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
scheme := "http"
|
||||
if tlslisten.TLSEnabled() {
|
||||
scheme = "https"
|
||||
}
|
||||
url := fmt.Sprintf("%s://localhost:%s/health/ready", scheme, cfg.Server.Port)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: build request:", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
client := http.DefaultClient
|
||||
if tlslisten.TLSEnabled() {
|
||||
hc, herr := tlslisten.ClientHTTP(
|
||||
os.Getenv("TLS_HEALTH_CLIENT_CERT"),
|
||||
os.Getenv("TLS_HEALTH_CLIENT_KEY"),
|
||||
cfg.Server.TLSCAFile,
|
||||
)
|
||||
if herr != nil {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: tls client:", herr)
|
||||
return 1
|
||||
}
|
||||
client = hc
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: request failed:", err)
|
||||
return 1
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: unexpected status", resp.StatusCode)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user