Skip to content

Commit

Permalink
MINOR: add interface to delete certificates via runtime
Browse files Browse the repository at this point in the history
and also delete a certificate from a crt-list
  • Loading branch information
hdurand0710 authored and mjuraga committed Jan 30, 2025
1 parent 7283b65 commit fb8ca63
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 6 additions & 2 deletions runtime/crt-lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion runtime/crt-lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package runtime
import (
"reflect"
"testing"

"github.com/haproxytech/client-native/v5/misc"
)

func TestSingleRuntime_ShowCrtLists(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
Expand Down
2 changes: 2 additions & 0 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions runtime/runtime_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fb8ca63

Please sign in to comment.