Skip to content

Commit

Permalink
add debug to audit
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Apr 27, 2023
1 parent a116dde commit a4df08e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/add-debug-to-audit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Add debug server to audit

We added a debug server to audit.

https://github.com/owncloud/ocis/pull/6178
https://github.com/owncloud/ocis/issues/5002
51 changes: 43 additions & 8 deletions services/audit/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import (
"github.com/cs3org/reva/v2/pkg/events"
"github.com/cs3org/reva/v2/pkg/events/stream"
"github.com/go-micro/plugins/v4/events/natsjs"
"github.com/oklog/run"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
"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/audit/pkg/config"
"github.com/owncloud/ocis/v2/services/audit/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/audit/pkg/logging"
Expand All @@ -30,13 +34,17 @@ func Server(cfg *config.Config) *cli.Command {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
var (
gr = run.Group{}
logger = logging.Configure(cfg.Service.Name, cfg.Log)

ctx := cfg.Context
if ctx == nil {
ctx = context.Background()
}
ctx, cancel := context.WithCancel(ctx)
ctx, cancel = func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
)
defer cancel()

evtsCfg := cfg.Events
Expand Down Expand Up @@ -76,8 +84,35 @@ func Server(cfg *config.Config) *cli.Command {
return err
}

svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger)
return nil
gr.Add(func() error {
svc.AuditLoggerFromConfig(ctx, cfg.Auditlog, evts, logger)
return nil
}, func(err error) {
logger.Error().
Err(err).
Msg("Shutting down server")
cancel()
})

{
server := debug.NewService(
debug.Logger(logger),
debug.Name(cfg.Service.Name),
debug.Version(version.GetString()),
debug.Address(cfg.Debug.Addr),
debug.Token(cfg.Debug.Token),
debug.Pprof(cfg.Debug.Pprof),
debug.Zpages(cfg.Debug.Zpages),
debug.Health(handlers.Health),
debug.Ready(handlers.Ready),
)

gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
})
}
return gr.Run()
},
}
}
13 changes: 11 additions & 2 deletions services/audit/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type Config struct {

Service Service `yaml:"-"`

Log *Log `yaml:"log"`
Debug Debug `yaml:"debug"`
Tracing *Tracing `yaml:"tracing"`
Log *Log `yaml:"log"`
Debug Debug `yaml:"debug"`

Events Events `yaml:"events"`
Auditlog Auditlog `yaml:"auditlog"`
Expand All @@ -38,3 +39,11 @@ type Auditlog struct {
FilePath string `yaml:"filepath" env:"AUDIT_FILEPATH" desc:"Filepath to the logfile. Mandatory if LogToFile is true."`
Format string `yaml:"format" env:"AUDIT_FORMAT" desc:"Log format. Using json is advised."`
}

// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;AUDIT_TRACING_ENABLED" desc:"Activates tracing."`
Type string `yaml:"type" env:"OCIS_TRACING_TYPE;AUDIT_TRACING_TYPE" desc:"The type of tracing. Defaults to \"\", which is the same as \"jaeger\". Allowed tracing types are \"jaeger\" and \"\" as of now."`
Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;AUDIT_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."`
Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;AUDIT_TRACING_COLLECTOR" desc:"The HTTP endpoint for sending spans directly to a collector, i.e. http://jaeger-collector:14268/api/traces. Only used if the tracing endpoint is unset."`
}
16 changes: 15 additions & 1 deletion services/audit/pkg/config/defaults/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func FullDefaultConfig() *config.Config {
func DefaultConfig() *config.Config {
return &config.Config{
Debug: config.Debug{
Addr: "127.0.0.1:9234",
Addr: "127.0.0.1:9234",
Zpages: false,
Pprof: false,
},
Service: config.Service{
Name: "audit",
Expand Down Expand Up @@ -47,6 +49,18 @@ func EnsureDefaults(cfg *config.Config) {
} else if cfg.Log == nil {
cfg.Log = &config.Log{}
}

// provide with defaults for shared tracing, since we need a valid destination address for "envdecode".
if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil {
cfg.Tracing = &config.Tracing{
Enabled: cfg.Commons.Tracing.Enabled,
Type: cfg.Commons.Tracing.Type,
Endpoint: cfg.Commons.Tracing.Endpoint,
Collector: cfg.Commons.Tracing.Collector,
}
} else if cfg.Tracing == nil {
cfg.Tracing = &config.Tracing{}
}
}

// Sanitize sanitized the configuration
Expand Down

0 comments on commit a4df08e

Please sign in to comment.