Compare commits
No commits in common. "main" and "v0.3.1" have entirely different histories.
@ -30,6 +30,7 @@ func CAPool() (*x509.CertPool, error) {
|
||||
// GetCertman sets up certman for the specified cert / key pair. It is
|
||||
// used in the monitor-api and (for now) in the client
|
||||
func GetCertman(certFile, keyFile string) (*certman.CertMan, error) {
|
||||
|
||||
cm, err := certman.New(certFile, keyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"go.ntppool.org/common/logger"
|
||||
)
|
||||
|
||||
//go:generate go tool github.com/masaushi/accessory -type Config
|
||||
//go:generate accessory -type Config
|
||||
|
||||
type Config struct {
|
||||
deploymentMode string `accessor:"getter"`
|
||||
@ -50,10 +50,6 @@ func (c *Config) WebURL(path string, query *url.Values) string {
|
||||
return baseURL(c.webHostname, c.webTLS, path, query)
|
||||
}
|
||||
|
||||
func (c *Config) ManageURL(path string, query *url.Values) string {
|
||||
return baseURL(c.manageHostname, c.webTLS, path, query)
|
||||
}
|
||||
|
||||
func baseURL(host string, tls bool, path string, query *url.Values) string {
|
||||
uri := url.URL{}
|
||||
uri.Host = host
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestBaseURL(t *testing.T) {
|
||||
|
||||
os.Setenv("web_hostname", "www.ntp.dev, web.ntppool.dev")
|
||||
os.Setenv("web_tls", "yes")
|
||||
|
||||
@ -21,4 +22,5 @@ func TestBaseURL(t *testing.T) {
|
||||
if u != "https://www.ntp.dev/foo?foo=bar" {
|
||||
t.Fatalf("unexpected WebURL: %s", u)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package depenv
|
||||
|
||||
import "context"
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// NewContext adds the deployment environment to the context
|
||||
func NewContext(ctx context.Context, d DeploymentEnvironment) context.Context {
|
||||
return context.WithValue(ctx, contextKey{}, d)
|
||||
}
|
||||
|
||||
// FromContext retrieves the deployment environment from the context
|
||||
func FromContext(ctx context.Context) DeploymentEnvironment {
|
||||
if d, ok := ctx.Value(contextKey{}).(DeploymentEnvironment); ok {
|
||||
return d
|
||||
}
|
||||
return DeployUndefined
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package depenv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var manageServers = map[DeploymentEnvironment]string{
|
||||
DeployDevel: "https://manage.askdev.grundclock.com",
|
||||
DeployTest: "https://manage.beta.grundclock.com",
|
||||
DeployProd: "https://manage.ntppool.org",
|
||||
}
|
||||
|
||||
var apiServers = map[DeploymentEnvironment]string{
|
||||
DeployDevel: "https://dev-api.ntppool.dev",
|
||||
DeployTest: "https://beta-api.ntppool.dev",
|
||||
DeployProd: "https://api.ntppool.dev",
|
||||
}
|
||||
|
||||
// var validationServers = map[DeploymentEnvironment]string{
|
||||
// DeployDevel: "https://v.ntp.dev/d/",
|
||||
// DeployTest: "https://v.ntp.dev/b/",
|
||||
// DeployProd: "https://v.ntp.dev/p/",
|
||||
// }
|
||||
|
||||
const (
|
||||
DeployUndefined DeploymentEnvironment = iota
|
||||
DeployDevel
|
||||
DeployTest
|
||||
DeployProd
|
||||
)
|
||||
|
||||
type DeploymentEnvironment uint8
|
||||
|
||||
func DeploymentEnvironmentFromString(s string) DeploymentEnvironment {
|
||||
switch s {
|
||||
case "devel", "dev":
|
||||
return DeployDevel
|
||||
case "test", "beta":
|
||||
return DeployTest
|
||||
case "prod":
|
||||
return DeployProd
|
||||
default:
|
||||
return DeployUndefined
|
||||
}
|
||||
}
|
||||
|
||||
func (d DeploymentEnvironment) String() string {
|
||||
switch d {
|
||||
case DeployProd:
|
||||
return "prod"
|
||||
case DeployTest:
|
||||
return "test"
|
||||
case DeployDevel:
|
||||
return "devel"
|
||||
default:
|
||||
panic("invalid DeploymentEnvironment")
|
||||
}
|
||||
}
|
||||
|
||||
func (d DeploymentEnvironment) APIHost() string {
|
||||
if apiHost := os.Getenv("API_HOST"); apiHost != "" {
|
||||
return apiHost
|
||||
}
|
||||
return apiServers[d]
|
||||
}
|
||||
|
||||
func (d DeploymentEnvironment) ManageURL(path string) string {
|
||||
return manageServers[d] + path
|
||||
}
|
||||
|
||||
func (d DeploymentEnvironment) MonitorDomain() string {
|
||||
return d.String() + ".mon.ntppool.dev"
|
||||
}
|
||||
|
||||
func (d *DeploymentEnvironment) UnmarshalText(text []byte) error {
|
||||
s := string(text)
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
env := DeploymentEnvironmentFromString(s)
|
||||
if env == DeployUndefined {
|
||||
return fmt.Errorf("invalid deployment environment: %s", s)
|
||||
}
|
||||
*d = env
|
||||
return nil
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package depenv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var monitorApiServers = map[DeploymentEnvironment]string{
|
||||
DeployDevel: "https://api.devel.mon.ntppool.dev",
|
||||
DeployTest: "https://api.test.mon.ntppool.dev",
|
||||
DeployProd: "https://api.mon.ntppool.dev",
|
||||
}
|
||||
|
||||
func (d DeploymentEnvironment) MonitorAPIHost() string {
|
||||
return monitorApiServers[d]
|
||||
}
|
||||
|
||||
func GetDeploymentEnvironmentFromName(clientName string) (DeploymentEnvironment, error) {
|
||||
clientName = strings.ToLower(clientName)
|
||||
|
||||
if !strings.HasSuffix(clientName, ".mon.ntppool.dev") {
|
||||
return DeployUndefined, fmt.Errorf("invalid client name %s", clientName)
|
||||
}
|
||||
|
||||
if clientName == "api.mon.ntppool.dev" {
|
||||
return DeployProd, nil
|
||||
}
|
||||
|
||||
prefix := clientName[:strings.Index(clientName, ".mon.ntppool.dev")]
|
||||
parts := strings.Split(prefix, ".")
|
||||
if len(parts) != 2 {
|
||||
return DeployUndefined, fmt.Errorf("invalid client name %s", clientName)
|
||||
}
|
||||
|
||||
if d := DeploymentEnvironmentFromString(parts[1]); d != DeployUndefined {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return DeployUndefined, fmt.Errorf("invalid client name %s (unknown environment %s)", clientName, parts[1])
|
||||
}
|
12
ekko/ekko.go
12
ekko/ekko.go
@ -16,7 +16,6 @@ import (
|
||||
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@ -51,8 +50,7 @@ func (ek *Ekko) Start(ctx context.Context) error {
|
||||
g.Go(func() error {
|
||||
e.Server.Addr = fmt.Sprintf(":%d", ek.port)
|
||||
log.Info("server starting", "port", ek.port)
|
||||
// err := e.Server.ListenAndServe()
|
||||
err := e.StartH2CServer(e.Server.Addr, &http2.Server{})
|
||||
err := e.Server.ListenAndServe()
|
||||
if err == http.ErrServerClosed {
|
||||
return nil
|
||||
}
|
||||
@ -122,13 +120,7 @@ func (ek *Ekko) setup(ctx context.Context) (*echo.Echo, error) {
|
||||
e.Use(middleware.Gzip())
|
||||
}
|
||||
|
||||
secureConfig := middleware.DefaultSecureConfig
|
||||
// secureConfig.ContentSecurityPolicy = "default-src *"
|
||||
secureConfig.ContentSecurityPolicy = ""
|
||||
secureConfig.HSTSMaxAge = int(time.Hour * 168 * 30 / time.Second)
|
||||
secureConfig.HSTSPreloadEnabled = true
|
||||
|
||||
e.Use(middleware.SecureWithConfig(secureConfig))
|
||||
e.Use(middleware.Secure())
|
||||
|
||||
e.Use(
|
||||
func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
4
go.mod
4
go.mod
@ -1,6 +1,8 @@
|
||||
module go.ntppool.org/common
|
||||
|
||||
go 1.23.5
|
||||
go 1.23
|
||||
|
||||
toolchain go1.23.4
|
||||
|
||||
require (
|
||||
github.com/abh/certman v0.4.0
|
||||
|
@ -59,10 +59,11 @@ func (srv *Server) Listen(ctx context.Context, port int) error {
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
g.Go(func() error {
|
||||
shCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
if err := hsrv.Shutdown(shCtx); err != nil {
|
||||
|
||||
g.Go(func() error {
|
||||
if err := hsrv.Shutdown(ctx); err != nil {
|
||||
srv.log.Error("health check server shutdown failed", "err", err)
|
||||
return err
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestHealthHandler(t *testing.T) {
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/__health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
@ -42,9 +42,11 @@ type Kafka struct {
|
||||
l *log.Logger
|
||||
|
||||
// wr *kafka.Writer
|
||||
|
||||
}
|
||||
|
||||
func (k *Kafka) tlsConfig() (*tls.Config, error) {
|
||||
|
||||
cm, err := certman.New(k.tls.Cert, k.tls.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -187,6 +189,7 @@ func (k *Kafka) brokerAddrs() []string {
|
||||
}
|
||||
|
||||
func (k *Kafka) NewWriter(topic string) (*kafka.Writer, error) {
|
||||
|
||||
// https://pkg.go.dev/github.com/segmentio/kafka-go#Writer
|
||||
w := &kafka.Writer{
|
||||
Addr: kafka.TCP(k.brokerAddrs()...),
|
||||
|
@ -17,6 +17,7 @@ type logfmt struct {
|
||||
}
|
||||
|
||||
func newLogFmtHandler(next slog.Handler) slog.Handler {
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
h := &logfmt{
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestLogFmt(t *testing.T) {
|
||||
|
||||
var buf bytes.Buffer
|
||||
jsonh := slog.NewJSONHandler(&buf, nil)
|
||||
h := newLogFmtHandler(jsonh)
|
||||
@ -38,4 +39,5 @@ func TestLogFmt(t *testing.T) {
|
||||
t.Log("didn't find message in output")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,21 +15,17 @@ import (
|
||||
|
||||
var ConfigPrefix = ""
|
||||
|
||||
var (
|
||||
textLogger *slog.Logger
|
||||
otlpLogger *slog.Logger
|
||||
multiLogger *slog.Logger
|
||||
)
|
||||
var textLogger *slog.Logger
|
||||
var otlpLogger *slog.Logger
|
||||
var multiLogger *slog.Logger
|
||||
|
||||
var (
|
||||
setupText sync.Once // this sets the default
|
||||
setupOtlp sync.Once // this never sets the default
|
||||
setupMulti sync.Once // this sets the default, and will always run after the others
|
||||
mu sync.Mutex
|
||||
)
|
||||
var setupText sync.Once // this sets the default
|
||||
var setupOtlp sync.Once // this never sets the default
|
||||
var setupMulti sync.Once // this sets the default, and will always run after the others
|
||||
var mu sync.Mutex
|
||||
|
||||
func setupStdErrHandler() slog.Handler {
|
||||
programLevel := new(slog.LevelVar) // Info by default
|
||||
var programLevel = new(slog.LevelVar) // Info by default
|
||||
|
||||
envVar := "DEBUG"
|
||||
if len(ConfigPrefix) > 0 {
|
||||
|
@ -27,15 +27,15 @@ func NewStdLog(key string, debug bool, log *slog.Logger) *stdLoggerish {
|
||||
return sl
|
||||
}
|
||||
|
||||
func (l stdLoggerish) Println(msg ...any) {
|
||||
func (l stdLoggerish) Println(msg ...interface{}) {
|
||||
l.f(l.key, "msg", msg)
|
||||
}
|
||||
|
||||
func (l stdLoggerish) Printf(msg string, args ...any) {
|
||||
func (l stdLoggerish) Printf(msg string, args ...interface{}) {
|
||||
l.f(l.key, "msg", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
func (l stdLoggerish) Fatalf(msg string, args ...any) {
|
||||
func (l stdLoggerish) Fatalf(msg string, args ...interface{}) {
|
||||
l.log.Error(l.key, "msg", fmt.Sprintf(msg, args...))
|
||||
panic("fatal error") // todo: does this make sense at all?
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ func (m *Metrics) Registry() *prometheus.Registry {
|
||||
}
|
||||
|
||||
func (m *Metrics) Handler() http.Handler {
|
||||
|
||||
log := logger.NewStdLog("prom http", false, nil)
|
||||
|
||||
return promhttp.HandlerFor(m.r, promhttp.HandlerOpts{
|
||||
@ -45,6 +46,7 @@ func (m *Metrics) Handler() http.Handler {
|
||||
// the specified port. The server will shutdown and return when
|
||||
// the provided context is done
|
||||
func (m *Metrics) ListenAndServe(ctx context.Context, port int) error {
|
||||
|
||||
log := logger.Setup()
|
||||
|
||||
srv := &http.Server{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
go install github.com/goreleaser/goreleaser/v2@v2.8.2
|
||||
go install github.com/goreleaser/goreleaser/v2@v2.5.0
|
||||
|
||||
if [ ! -z "${harbor_username:-}" ]; then
|
||||
DOCKER_FILE=~/.docker/config.json
|
||||
|
@ -15,7 +15,7 @@ func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||
var v any
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -18,4 +18,5 @@ func TestDuration(t *testing.T) {
|
||||
if foo.Foo.Seconds() != 30 {
|
||||
t.Fatalf("parsed time.Duration wasn't 30 seconds: %s", foo.Foo)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -196,6 +196,7 @@ func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc,
|
||||
}
|
||||
|
||||
func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (sdktrace.SpanExporter, error) {
|
||||
|
||||
log := logger.Setup()
|
||||
|
||||
var tlsConfig *tls.Config
|
||||
@ -237,7 +238,7 @@ func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (sdktrace.SpanExpor
|
||||
}
|
||||
|
||||
client = otlptracegrpc.NewClient(opts...)
|
||||
case "http/protobuf", "http/json":
|
||||
case "http/protobuf":
|
||||
opts := []otlptracehttp.Option{
|
||||
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
@ -17,4 +18,5 @@ func TestInit(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
defer shutdownFn(ctx)
|
||||
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func (lsa *LogScoreAttributes) Value() (driver.Value, error) {
|
||||
return json.Marshal(lsa)
|
||||
}
|
||||
|
||||
func (lsa *LogScoreAttributes) Scan(value any) error {
|
||||
func (lsa *LogScoreAttributes) Scan(value interface{}) error {
|
||||
var source []byte
|
||||
_t := LogScoreAttributes{}
|
||||
|
||||
|
@ -14,7 +14,8 @@ import (
|
||||
)
|
||||
|
||||
var monotonicPool = sync.Pool{
|
||||
New: func() any {
|
||||
New: func() interface{} {
|
||||
|
||||
log := logger.Setup()
|
||||
|
||||
var seed int64
|
||||
@ -38,6 +39,7 @@ var monotonicPool = sync.Pool{
|
||||
}
|
||||
|
||||
func MakeULID(t time.Time) (*oklid.ULID, error) {
|
||||
|
||||
mono := monotonicPool.Get().(io.Reader)
|
||||
|
||||
id, err := oklid.New(oklid.Timestamp(t), mono)
|
||||
|
@ -13,12 +13,10 @@ import (
|
||||
)
|
||||
|
||||
// VERSION has the current software version (set in the build process)
|
||||
var (
|
||||
VERSION string
|
||||
buildTime string
|
||||
gitVersion string
|
||||
gitModified bool
|
||||
)
|
||||
var VERSION string
|
||||
var buildTime string
|
||||
var gitVersion string
|
||||
var gitModified bool
|
||||
|
||||
var info Info
|
||||
|
||||
@ -30,6 +28,7 @@ type Info struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
info.BuildTime = buildTime
|
||||
info.GitRev = gitVersion
|
||||
|
||||
@ -91,15 +90,6 @@ func VersionCmd(name string) *cobra.Command {
|
||||
return versionCmd
|
||||
}
|
||||
|
||||
type KongVersionCmd struct {
|
||||
Name string `kong:"-"`
|
||||
}
|
||||
|
||||
func (cmd *KongVersionCmd) Run() error {
|
||||
fmt.Printf("%s %s\n", cmd.Name, Version())
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterMetric(name string, registry prometheus.Registerer) {
|
||||
if len(name) > 0 {
|
||||
name = strings.ReplaceAll(name, "-", "_")
|
||||
@ -168,9 +158,6 @@ func CheckVersion(version, minimumVersion string) bool {
|
||||
if version == "dev-snapshot" {
|
||||
return true
|
||||
}
|
||||
if idx := strings.Index(version, "/"); idx >= 0 {
|
||||
version = version[0:idx]
|
||||
}
|
||||
if semver.Compare(version, minimumVersion) < 0 {
|
||||
// log.Debug("version too old", "v", cl.Version.Version)
|
||||
return false
|
||||
|
@ -3,12 +3,14 @@ package fastlyxff
|
||||
import "testing"
|
||||
|
||||
func TestFastlyIPRanges(t *testing.T) {
|
||||
|
||||
fastlyxff, err := New("fastly.json")
|
||||
if err != nil {
|
||||
t.Fatalf("could not load test data: %s", err)
|
||||
}
|
||||
|
||||
data, err := fastlyxff.EchoTrustOption()
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse test data: %s", err)
|
||||
}
|
||||
@ -17,4 +19,5 @@ func TestFastlyIPRanges(t *testing.T) {
|
||||
t.Logf("only got %d prefixes, expected more", len(data))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user