Skip to content

Commit

Permalink
Add support for TLS files via config.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchoi-viant committed Feb 23, 2024
1 parent b0599e5 commit 48c8606
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions service/endpoint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Config struct {
EnableMemProf bool
EnableCPUProf bool

TLS *TLSConfig

AllowedSubnet []string `json:",omitempty" yaml:",omitempty"`
}

Expand Down
12 changes: 12 additions & 0 deletions service/endpoint/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ func (s *Service) Listen() (net.Listener, error) {

func (s *Service) Serve(l net.Listener) error {
log.Printf("starting mly service endpoint: %v\n", s.server.Addr)
tls := s.config.TLS

var err error
if tls == nil {
err = tls.Valid()
if err != nil {
return err
}

return s.server.ServeTLS(l, tls.CertFile, tls.KeyFile)
}

return s.server.Serve(l)
}

Expand Down
37 changes: 37 additions & 0 deletions service/endpoint/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package endpoint

import (
"fmt"
"os"
)

type TLSConfig struct {
CertFile string
KeyFile string
}

func (t *TLSConfig) Valid() error {
if t.CertFile == "" {
return fmt.Errorf("CertFile not set")
}

if t.KeyFile == "" {
return fmt.Errorf("KeyFile not set")
}

fp, err := os.Open(t.CertFile)
if err != nil {
fp.Close()
} else {
return fmt.Errorf("could not open %s, %w", t.CertFile, err)
}

fp, err = os.Open(t.KeyFile)
if err != nil {
fp.Close()
} else {
return fmt.Errorf("could not open %s, %w", t.KeyFile, err)
}

return nil
}

0 comments on commit 48c8606

Please sign in to comment.