Skip to content

Commit 428abe2

Browse files
committed
MINOR: add interface to delete certificates via runtime
and also delete a certificate from a crt-list
1 parent d7abc10 commit 428abe2

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

runtime/crt-lists.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ func (s *SingleRuntime) AddCrtListEntry(crtList string, entry CrtListEntry) erro
160160
}
161161

162162
// DeleteCrtListEntry deletes all the CrtList entries from the CrtList by its id
163-
func (s *SingleRuntime) DeleteCrtListEntry(crtList, certFile string, lineNumber int) error {
164-
cmd := fmt.Sprintf("del ssl crt-list %s %s:%v", crtList, certFile, lineNumber)
163+
func (s *SingleRuntime) DeleteCrtListEntry(crtList, certFile string, lineNumber *int64) error {
164+
lineNumberPart := ""
165+
if lineNumber != nil {
166+
lineNumberPart = fmt.Sprintf(":%v", *lineNumber)
167+
}
168+
cmd := fmt.Sprintf("del ssl crt-list %s %s%s", crtList, certFile, lineNumberPart)
165169
response, err := s.ExecuteWithResponse(cmd)
166170
if err != nil {
167171
return fmt.Errorf("%s %w", err.Error(), native_errors.ErrNotFound)

runtime/crt-lists_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package runtime
33
import (
44
"reflect"
55
"testing"
6+
7+
"github.com/haproxytech/client-native/v5/misc"
68
)
79

810
func TestSingleRuntime_ShowCrtLists(t *testing.T) {
@@ -404,7 +406,7 @@ func TestSingleRuntime_DeleteCrtListEntry(t *testing.T) {
404406
t.Errorf("SingleRuntime.Init() error = %v", err)
405407
return
406408
}
407-
if err := s.DeleteCrtListEntry(tt.args.crtList, tt.args.certFile, tt.args.lineNumber); (err != nil) != tt.wantErr {
409+
if err := s.DeleteCrtListEntry(tt.args.crtList, tt.args.certFile, misc.Int64P(tt.args.lineNumber)); (err != nil) != tt.wantErr {
408410
t.Errorf("SingleRuntime.DeleteCrtListEntry() error = %v, wantErr %v", err, tt.wantErr)
409411
}
410412
})

runtime/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ type Cert interface {
144144
CommitCertEntry(filename string) error
145145
AbortCertEntry(filename string) error
146146
AddCrtListEntry(crtList string, entry CrtListEntry) error
147+
DeleteCrtListEntry(crtList, filename string, lineNumber *int64) error
148+
DeleteCertEntry(filename string) error
147149
}
148150

149151
type Runtime interface {

runtime/runtime_client.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,3 +1304,37 @@ func (c *client) AddCrtListEntry(crtList string, entry CrtListEntry) error {
13041304
}
13051305
return nil
13061306
}
1307+
1308+
func (c *client) DeleteCrtListEntry(crtList, filename string, lineNumber *int64) error {
1309+
if len(c.runtimes) == 0 {
1310+
return fmt.Errorf("no valid runtimes found")
1311+
}
1312+
var lastErr error
1313+
for _, runtime := range c.runtimes {
1314+
err := runtime.DeleteCrtListEntry(crtList, filename, lineNumber)
1315+
if err != nil {
1316+
lastErr = err
1317+
}
1318+
}
1319+
if lastErr != nil {
1320+
return lastErr
1321+
}
1322+
return nil
1323+
}
1324+
1325+
func (c *client) DeleteCertEntry(filename string) error {
1326+
if len(c.runtimes) == 0 {
1327+
return fmt.Errorf("no valid runtimes found")
1328+
}
1329+
var lastErr error
1330+
for _, runtime := range c.runtimes {
1331+
err := runtime.DeleteCertEntry(filename)
1332+
if err != nil {
1333+
lastErr = err
1334+
}
1335+
}
1336+
if lastErr != nil {
1337+
return lastErr
1338+
}
1339+
return nil
1340+
}

0 commit comments

Comments
 (0)