From fb8ca63ef92ccd15ee07dee8d5354d0ba2908692 Mon Sep 17 00:00:00 2001 From: Helene Durand Date: Mon, 20 Jan 2025 14:24:45 +0100 Subject: [PATCH] MINOR: add interface to delete certificates via runtime and also delete a certificate from a crt-list --- runtime/crt-lists.go | 8 ++++++-- runtime/crt-lists_test.go | 4 +++- runtime/interface.go | 2 ++ runtime/runtime_client.go | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/runtime/crt-lists.go b/runtime/crt-lists.go index 1cbd9773..16604642 100644 --- a/runtime/crt-lists.go +++ b/runtime/crt-lists.go @@ -160,8 +160,12 @@ func (s *SingleRuntime) AddCrtListEntry(crtList string, entry CrtListEntry) erro } // DeleteCrtListEntry deletes all the CrtList entries from the CrtList by its id -func (s *SingleRuntime) DeleteCrtListEntry(crtList, certFile string, lineNumber int) error { - cmd := fmt.Sprintf("del ssl crt-list %s %s:%v", crtList, certFile, lineNumber) +func (s *SingleRuntime) DeleteCrtListEntry(crtList, certFile string, lineNumber *int64) error { + lineNumberPart := "" + if lineNumber != nil { + lineNumberPart = fmt.Sprintf(":%v", *lineNumber) + } + cmd := fmt.Sprintf("del ssl crt-list %s %s%s", crtList, certFile, lineNumberPart) response, err := s.ExecuteWithResponse(cmd) if err != nil { return fmt.Errorf("%s %w", err.Error(), native_errors.ErrNotFound) diff --git a/runtime/crt-lists_test.go b/runtime/crt-lists_test.go index bde543c3..334f0bdf 100644 --- a/runtime/crt-lists_test.go +++ b/runtime/crt-lists_test.go @@ -3,6 +3,8 @@ package runtime import ( "reflect" "testing" + + "github.com/haproxytech/client-native/v5/misc" ) func TestSingleRuntime_ShowCrtLists(t *testing.T) { @@ -399,7 +401,7 @@ func TestSingleRuntime_DeleteCrtListEntry(t *testing.T) { t.Errorf("SingleRuntime.Init() error = %v", err) return } - if err := s.DeleteCrtListEntry(tt.args.crtList, tt.args.certFile, tt.args.lineNumber); (err != nil) != tt.wantErr { + if err := s.DeleteCrtListEntry(tt.args.crtList, tt.args.certFile, misc.Int64P(tt.args.lineNumber)); (err != nil) != tt.wantErr { t.Errorf("SingleRuntime.DeleteCrtListEntry() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/runtime/interface.go b/runtime/interface.go index 17795e4c..584dfbb7 100644 --- a/runtime/interface.go +++ b/runtime/interface.go @@ -148,6 +148,8 @@ type Cert interface { CommitCertEntry(filename string) error AbortCertEntry(filename string) error AddCrtListEntry(crtList string, entry CrtListEntry) error + DeleteCrtListEntry(crtList, filename string, lineNumber *int64) error + DeleteCertEntry(filename string) error } type Runtime interface { diff --git a/runtime/runtime_client.go b/runtime/runtime_client.go index 72fd54be..4b9b86c1 100644 --- a/runtime/runtime_client.go +++ b/runtime/runtime_client.go @@ -1080,3 +1080,27 @@ func (c *client) AddCrtListEntry(crtList string, entry CrtListEntry) error { return nil } + +func (c *client) DeleteCrtListEntry(crtList, filename string, lineNumber *int64) error { + if !c.runtime.IsValid() { + return errors.New("no valid runtime found") + } + + if err := c.runtime.DeleteCrtListEntry(crtList, filename, lineNumber); err != nil { + return fmt.Errorf("%s %w", c.runtime.socketPath, err) + } + + return nil +} + +func (c *client) DeleteCertEntry(filename string) error { + if !c.runtime.IsValid() { + return errors.New("no valid runtime found") + } + + if err := c.runtime.DeleteCertEntry(filename); err != nil { + return fmt.Errorf("%s %w", c.runtime.socketPath, err) + } + + return nil +}