Files
ocis/services/proxy/pkg/server/debug/server.go
T
Roman PerekhodandClaude Opus 5.5 3c56d2cffc fix: [OCSRE-1629] apply the scope rules to the NATS username and fix the NATS checks
- A service-specific username or password now takes precedence over the
  global NATS JWT credentials. Before, OCIS_NATS_CREDS_FILE always
  replaced a value like GRAPH_CACHE_AUTH_USERNAME. The service registry
  uses the same rule for MICRO_REGISTRY_AUTH_USERNAME and
  MICRO_REGISTRY_AUTH_PASSWORD.
- The NATS health check keeps one connection and uses it again for each
  probe. Before, each probe read the .creds file and opened a new
  connection. A probe could read a rotated file before the write was
  complete. The proxy, ocs and gateway services now build the check once.
- The startup check of the store credentials runs only for the nats-js
  and nats-js-kv store types. Other store types do not use the
  credentials.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 19:25:09 +01:00

71 lines
2.3 KiB
Go

package debug
import (
"context"
"encoding/json"
"net/http"
"github.com/ggwhite/go-masker"
"github.com/owncloud/ocis/v2/ocis-pkg/checks"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"github.com/owncloud/ocis/v2/ocis-pkg/service/debug"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
"github.com/owncloud/reva/v2/pkg/events/stream"
)
// Server initializes the debug service and server.
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)
healthHandlerConfiguration := handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger).
WithCheck("web reachability", checks.NewHTTPCheck(options.Config.HTTP.Addr))
readyHandlerConfiguration := healthHandlerConfiguration.
WithCheck("nats reachability", natsCheck(options))
var configDumpFunc http.HandlerFunc = configDump(options.Config)
return debug.NewService(
debug.Logger(options.Logger),
debug.Name(options.Config.Service.Name),
debug.Version(version.GetString()),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(handlers.NewCheckHandler(healthHandlerConfiguration)),
debug.Ready(handlers.NewCheckHandler(readyHandlerConfiguration)),
debug.ConfigDump(configDumpFunc),
), nil
}
// natsCheck returns the check of the events broker. The proxy uses NATS only to publish
// events. An empty endpoint disables the events, so the check then always succeeds.
func natsCheck(options Options) func(context.Context) error {
if options.Config.Events.Endpoint == "" {
return func(context.Context) error { return nil }
}
return checks.NewNatsCheckWithConfig(options.Config.Events.Endpoint, stream.NatsConfig(options.Config.Events))
}
// configDump implements the config dump
func configDump(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
maskedCfg, err := masker.Struct(cfg)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
b, err := json.Marshal(maskedCfg)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
_, _ = w.Write(b)
}
}