From 83cf5344057430b47db0c63cc8563dfe386a76cd Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Sun, 2 Oct 2022 06:40:55 +0300 Subject: [PATCH 1/2] Implement --debug-global flag & dbus api --- cmd/syringe/main.go | 10 +++++++++- cmd/syringe/server.go | 1 + internal/ctx/ctx.go | 11 +++++++++++ internal/ctx/values.go | 1 + internal/dbus/syringe_svc.go | 10 ++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/syringe/main.go b/cmd/syringe/main.go index 1cf9e10..b85459f 100644 --- a/cmd/syringe/main.go +++ b/cmd/syringe/main.go @@ -29,6 +29,14 @@ func main() { "SYRINGE_DEBUG", }, }, + &cli.BoolFlag{ + Name: "debug-global", + Usage: "Whether to enable debug logging for all Syringe clients. Useful only when dbus is enabled", + Value: false, + EnvVars: []string{ + "SYRINGE_DEBUG_GLOBAL", + }, + }, }, Commands: []*cli.Command{ { @@ -91,7 +99,7 @@ func main() { }, }, Before: func(cctx *cli.Context) (err error) { - if err = setupLogging(cctx.Bool("debug")); err != nil { + if err = setupLogging(cctx.Bool("debug") || cctx.Bool("debug-global")); err != nil { return } diff --git a/cmd/syringe/server.go b/cmd/syringe/server.go index 356b9c9..75990ae 100644 --- a/cmd/syringe/server.go +++ b/cmd/syringe/server.go @@ -90,6 +90,7 @@ func serverEntrypoint(clictx *cli.Context) (err error) { cctx.WithVaultClient(vault), cctx.WithTemplateMap(tm), cctx.WithSocketPaths(socketPaths), + cctx.WithGlobalDebug(clictx.Bool("debug-global")), ) if clictx.Bool("dbus") { diff --git a/internal/ctx/ctx.go b/internal/ctx/ctx.go index 5edbf58..f07c4f0 100644 --- a/internal/ctx/ctx.go +++ b/internal/ctx/ctx.go @@ -70,3 +70,14 @@ func SocketPaths(ctx context.Context) (v []string) { v = ctx.Value(socketPath).([]string) return } + +func WithGlobalDebug(gd bool) ContextValueFunc { + return func(ctx context.Context) context.Context { + return context.WithValue(ctx, globalDebug, gd) + } +} + +func GlobalDebug(ctx context.Context) (v bool) { + v = ctx.Value(globalDebug).(bool) + return +} diff --git a/internal/ctx/values.go b/internal/ctx/values.go index 779c26e..2400ee8 100644 --- a/internal/ctx/values.go +++ b/internal/ctx/values.go @@ -8,4 +8,5 @@ var ( credentialReqUnit contextValue = "ctx:cru" credentialReqCred contextValue = "ctx:crc" socketPath contextValue = "ctx:sp" + globalDebug contextValue = "ctx:gd" ) diff --git a/internal/dbus/syringe_svc.go b/internal/dbus/syringe_svc.go index 6b5d8a1..450186c 100644 --- a/internal/dbus/syringe_svc.go +++ b/internal/dbus/syringe_svc.go @@ -23,6 +23,11 @@ const intro = introspect.IntrospectDeclarationString + ` Path to the Unix sockets where Syringe is currently listening on + + + Whether global debugging is enabled + + ` + introspect.IntrospectDataString + ` @@ -39,6 +44,11 @@ func (s *syringeService) GetSocketPaths() (v []string, err *dbus.Error) { return } +func (s *syringeService) GetGlobalDebug() (v bool, err *dbus.Error) { + v = cctx.GlobalDebug(s.ctx) + return +} + func RegisterSyringeService(ctx context.Context) (err error) { var conn *dbus.Conn From 85b106fd29de9beecf4b7a0915a0914b8411f689 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Fri, 5 May 2023 11:59:27 +0300 Subject: [PATCH 2/2] Implement global debug support for client --- cmd/syringe/update_linux.go | 11 +++++++++++ internal/dbus/syringe.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmd/syringe/update_linux.go b/cmd/syringe/update_linux.go index 0ab606b..1b3ea0e 100644 --- a/cmd/syringe/update_linux.go +++ b/cmd/syringe/update_linux.go @@ -30,6 +30,17 @@ func updateEntrypoint(clictx *cli.Context) (err error) { runtime.GOMAXPROCS(1) ctx := clictx.Context + // Reconfigure logger according to debug value via dbus service + // We need dbus service to be available in update mode anyway + if debug, derr := dbus.GetGlobalDebug(ctx); derr != nil { + zap.L().Debug("unable to get global debug flag", zap.Error(err)) + } if debug && !(clictx.Bool("debug") || clictx.Bool("global-debug")) { + if err = setupLogging(true); err != nil { + return + } + zap.L().Debug("global debugging enabled") + } + if os.Getuid() != 0 && os.Geteuid() != 0 { zap.L().Error("effective uid is not 0, very likely unable to update credentials", zap.Int("uid", os.Getuid()), zap.Int("euid", os.Geteuid())) } diff --git a/internal/dbus/syringe.go b/internal/dbus/syringe.go index 3cd439d..9889cfc 100644 --- a/internal/dbus/syringe.go +++ b/internal/dbus/syringe.go @@ -33,3 +33,24 @@ func GetServiceSocketPaths(ctx context.Context) (socketPaths map[string]bool, er return } + +func GetGlobalDebug(ctx context.Context) (debug bool, err error) { + var conn *dbus.Conn + + if conn, err = dbus.ConnectSystemBus(); err != nil { + err = fmt.Errorf("failed to establish dbus connection: %w", err) + return + } + defer conn.Close() + + var globalDebugValue dbus.Variant + obj := conn.Object(intf, objPath) + err = obj.CallWithContext(ctx, "ee.zentria.syringe1.Syringe.GetGlobalDebug", 0).Store(&globalDebugValue) + if err != nil { + err = fmt.Errorf("failed to call GetGlobalDebug: %w", err) + return + } + + debug = globalDebugValue.Value().(bool) + return +}