Skip to content

Commit

Permalink
feat: Add TLS support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume authored Jan 27, 2024
1 parent c4f8090 commit e5ea137
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
48 changes: 37 additions & 11 deletions cmd/sepp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package main

import (
"flag"
"log"
"net/http"
"os"

n32c "github.com/dot-5g/sepp/internal/n32"

"github.com/dot-5g/sepp/config"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)

var configFilePath string
Expand All @@ -19,20 +22,43 @@ func init() {

func main() {
flag.Parse()
config, err := config.ReadConfig(configFilePath)
config, err := loadConfiguration(configFilePath)
if err != nil {
log.Printf("Failed to read config file: %s\n", err)
return
log.Fatalf("Failed to read config file: %s", err)
}
server := initializeServer(config)
startServer(server, config)
}

n32c := n32c.N32C{
FQDN: n32c.FQDN(config.SEPP.FQDN),
func loadConfiguration(filePath string) (*config.Config, error) {
conf, err := config.ReadConfig(filePath)
if err != nil {
return nil, err
}
return conf, nil
}

echoServer := echo.New()

n32cHandshakeGroup := echoServer.Group("/n32c-handshake/v1")
n32cHandshakeGroup.POST("/exchange-capability", n32c.HandlePostExchangeCapability)
func initializeServer(conf *config.Config) *echo.Echo {
e := echo.New()
e.Logger.SetLevel(log.INFO)
e.Logger.SetOutput(os.Stdout)
e.Use(middleware.Logger())
n32c := n32c.N32C{FQDN: n32c.FQDN(conf.SEPP.FQDN)}
n32cGroup := e.Group("/n32c-handshake/v1")
n32cGroup.POST("/exchange-capability", n32c.HandlePostExchangeCapability)
return e
}

echoServer.Logger.Fatal(echoServer.Start(":1323"))
func startServer(e *echo.Echo, config *config.Config) {
address := ":" + config.SEPP.Port
if config.SEPP.TLS.Enabled {
if err := e.StartTLS(address, config.SEPP.TLS.Cert, config.SEPP.TLS.Key); err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
} else {
e.Logger.Warn("TLS is disabled")
if err := e.Start(address); err != http.ErrServerClosed {
e.Logger.Fatal(err)
}
}
}
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
SEPP:
FQDN: "1.2.3.5"
Port: 1234
TLS:
Enabled: true
Cert: "/etc/sepp/certs/sepp.crt"
Key: "/etc/sepp/certs/sepp.key"
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
type Config struct {
SEPP struct {
FQDN string `yaml:"FQDN"`
Port string `yaml:"Port"`
TLS struct {
Enabled bool `yaml:"Enabled"`
Cert string `yaml:"Cert"`
Key string `yaml:"Key"`
} `yaml:"TLS"`
} `yaml:"SEPP"`
}

Expand All @@ -30,5 +36,9 @@ func ReadConfig(configPath string) (*Config, error) {
return nil, fmt.Errorf("FQDN is required")
}

if config.SEPP.TLS.Enabled && config.SEPP.TLS.Cert == "" && config.SEPP.TLS.Key == "" {
return nil, fmt.Errorf("TLS.Cert and TLS.Key are required")
}

return &config, nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.21.6

require (
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/gommon v0.4.2
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -20,5 +21,6 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand Down Expand Up @@ -27,6 +29,8 @@ golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down

0 comments on commit e5ea137

Please sign in to comment.