Skip to content

Commit 61d5e04

Browse files
committed
Add support for configuration of TLS
1 parent 833e666 commit 61d5e04

File tree

8 files changed

+65
-12
lines changed

8 files changed

+65
-12
lines changed

cmd/pushbits/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
log.L.Fatal(err)
8989
}
9090

91-
err = runner.Run(engine, c.HTTP.ListenAddress, c.HTTP.Port)
91+
err = runner.Run(engine, c)
9292
if err != nil {
9393
log.L.Fatal(err)
9494
}

config.example.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ http:
1616
# What proxies to trust.
1717
trustedproxies: []
1818

19+
# Filename of the TLS certificate.
20+
certfile: ''
21+
22+
# Filename of the TLS private key.
23+
keyfile: ''
24+
1925
database:
2026
# Currently sqlite3, mysql, and postgres are supported.
2127
dialect: 'sqlite3'

internal/api/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func SuccessOrAbort(ctx *gin.Context, code int, err error) bool {
1515
if err != nil {
1616
// If we know the error force error code
1717
switch err {
18-
case pberrors.ErrorMessageNotFound:
18+
case pberrors.ErrMessageNotFound:
1919
ctx.AbortWithError(http.StatusNotFound, err)
2020
default:
2121
ctx.AbortWithError(code, err)

internal/configuration/configuration.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package configuration
33

44
import (
55
"github.com/jinzhu/configor"
6+
"github.com/pushbits/server/internal/log"
7+
"github.com/pushbits/server/internal/pberrors"
68
)
79

810
// testMode indicates if the package is run in test mode
@@ -53,6 +55,8 @@ type Configuration struct {
5355
ListenAddress string `default:""`
5456
Port int `default:"8080"`
5557
TrustedProxies []string `default:"[]"`
58+
CertFile string `default:""`
59+
KeyFile string `default:""`
5660
}
5761
Database struct {
5862
Dialect string `default:"sqlite3"`
@@ -80,6 +84,21 @@ func configFiles() []string {
8084
return []string{"config.yml"}
8185
}
8286

87+
func validateHTTPConfiguration(c *Configuration) error {
88+
certAndKeyEmpty := (c.HTTP.CertFile == "" && c.HTTP.KeyFile == "")
89+
certAndKeyPopulated := (c.HTTP.CertFile != "" && c.HTTP.KeyFile != "")
90+
91+
if !certAndKeyEmpty && !certAndKeyPopulated {
92+
return pberrors.ErrConfigTLSFilesInconsistent
93+
}
94+
95+
return nil
96+
}
97+
98+
func validateConfiguration(c *Configuration) error {
99+
return validateHTTPConfiguration(c)
100+
}
101+
83102
// Get returns the configuration extracted from env variables or config file.
84103
func Get() *Configuration {
85104
config := &Configuration{}
@@ -93,5 +112,9 @@ func Get() *Configuration {
93112
panic(err)
94113
}
95114

115+
if err := validateConfiguration(config); err != nil {
116+
log.L.Fatal(err)
117+
}
118+
96119
return config
97120
}

internal/configuration/configuration_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/jinzhu/configor"
1010
"github.com/pushbits/server/internal/log"
11+
"github.com/pushbits/server/internal/pberrors"
1112
"github.com/stretchr/testify/assert"
1213
"gopkg.in/yaml.v2"
1314
)
@@ -231,3 +232,18 @@ func cleanUp() {
231232
log.L.Warnln("Cannot remove config file: ", err)
232233
}
233234
}
235+
236+
func TestConfigurationValidation_ConfigTLSFilesInconsistent(t *testing.T) {
237+
assert := assert.New(t)
238+
239+
c := Configuration{}
240+
c.Admin.MatrixID = "000000"
241+
c.Matrix.Username = "default-username"
242+
c.Matrix.Password = "default-password"
243+
c.HTTP.CertFile = "populated"
244+
c.HTTP.KeyFile = ""
245+
246+
is := validateConfiguration(&c)
247+
should := pberrors.ErrConfigTLSFilesInconsistent
248+
assert.Equal(is, should, "validateConfiguration() should return ConfigTLSFilesInconsistent")
249+
}

internal/dispatcher/notification.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot
9090
deleteMessage, err := d.getMessage(a, n.ID)
9191
if err != nil {
9292
log.L.Println(err)
93-
return pberrors.ErrorMessageNotFound
93+
return pberrors.ErrMessageNotFound
9494
}
9595

9696
oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage)
@@ -199,7 +199,7 @@ func (d *Dispatcher) getMessage(a *model.Application, id string) (*event.Event,
199199
start = messages.End
200200
}
201201

202-
return nil, pberrors.ErrorMessageNotFound
202+
return nil, pberrors.ErrMessageNotFound
203203
}
204204

205205
// Replaces the content of a matrix message
@@ -273,7 +273,7 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
273273
func bodiesFromMessage(message *event.Event) (body, formattedBody string, err error) {
274274
msgContent := message.Content.AsMessage()
275275
if msgContent == nil {
276-
return "", "", pberrors.ErrorMessageNotFound
276+
return "", "", pberrors.ErrMessageNotFound
277277
}
278278

279279
formattedBody = msgContent.Body

internal/pberrors/errors.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ package pberrors
33

44
import "errors"
55

6-
// ErrorMessageNotFound indicates that a message does not exist
7-
var ErrorMessageNotFound = errors.New("message not found")
6+
// ErrMessageNotFound indicates that a message does not exist
7+
var ErrMessageNotFound = errors.New("message not found")
8+
9+
// ErrConfigTLSFilesInconsistent indicates that either just a certfile or a keyfile was provided
10+
var ErrConfigTLSFilesInconsistent = errors.New("TLS certfile and keyfile must either both be provided or omitted")

internal/runner/runner.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import (
55
"fmt"
66

77
"github.com/gin-gonic/gin"
8+
"github.com/pushbits/server/internal/configuration"
89
)
910

1011
// Run starts the Gin engine.
11-
func Run(engine *gin.Engine, address string, port int) error {
12-
err := engine.Run(fmt.Sprintf("%s:%d", address, port))
13-
if err != nil {
14-
return err
12+
func Run(engine *gin.Engine, c *configuration.Configuration) error {
13+
var err error
14+
address := fmt.Sprintf("%s:%d", c.HTTP.ListenAddress, c.HTTP.Port)
15+
16+
if c.HTTP.CertFile != "" && c.HTTP.KeyFile != "" {
17+
err = engine.RunTLS(address, c.HTTP.CertFile, c.HTTP.KeyFile)
18+
} else {
19+
err = engine.Run(address)
1520
}
1621

17-
return nil
22+
return err
1823
}

0 commit comments

Comments
 (0)