From 428abe2a833d1da41202c6498c3de09748b7af35 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 | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/runtime/crt-lists.go b/runtime/crt-lists.go index d1a9283c..6a79f8b2 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 5151d2fc..60944f55 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) { @@ -404,7 +406,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 e6b02ed1..fcfc4dec 100644 --- a/runtime/interface.go +++ b/runtime/interface.go @@ -144,6 +144,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 13bb8ef8..0b0e2d76 100644 --- a/runtime/runtime_client.go +++ b/runtime/runtime_client.go @@ -1304,3 +1304,37 @@ func (c *client) AddCrtListEntry(crtList string, entry CrtListEntry) error { } return nil } + +func (c *client) DeleteCrtListEntry(crtList, filename string, lineNumber *int64) error { + if len(c.runtimes) == 0 { + return fmt.Errorf("no valid runtimes found") + } + var lastErr error + for _, runtime := range c.runtimes { + err := runtime.DeleteCrtListEntry(crtList, filename, lineNumber) + if err != nil { + lastErr = err + } + } + if lastErr != nil { + return lastErr + } + return nil +} + +func (c *client) DeleteCertEntry(filename string) error { + if len(c.runtimes) == 0 { + return fmt.Errorf("no valid runtimes found") + } + var lastErr error + for _, runtime := range c.runtimes { + err := runtime.DeleteCertEntry(filename) + if err != nil { + lastErr = err + } + } + if lastErr != nil { + return lastErr + } + return nil +}