Skip to content

Commit a1ed308

Browse files
authored
Development (#53)
* tests can now fail * ci: fixed tests * get tests working again * remove flag from workflow * fix: fixed a couple of TLS related issues. TLS1.3 is now required * fix: minor code adaptability issues fixed in http.go * fix: code adaptability issues
1 parent d860980 commit a1ed308

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

.idea/inspectionProfiles/Project_Default.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
66
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=coverage)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
77
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
8+
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
9+
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
10+
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
11+
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
12+
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
13+
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=MadsRC_sophrosyne&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=MadsRC_sophrosyne)
814
[![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/MadsRC/sophrosyne/badge)](https://securityscorecards.dev/viewer/?uri=github.com/MadsRC/sophrosyne)
915
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/8804/badge)](https://www.bestpractices.dev/projects/8804)
1016
[![CodeQL](https://github.com/MadsRC/sophrosyne/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/MadsRC/sophrosyne/actions/workflows/github-code-scanning/codeql)

internal/http/http.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ import (
3232
)
3333

3434
type Server struct {
35-
appConfig *sophrosyne.Config `validate:"required"`
36-
mux *http.ServeMux `validate:"required"`
37-
validator sophrosyne.Validator `validate:"required"`
38-
middleware []func(http.Handler) http.Handler
35+
appConfig *sophrosyne.Config `validate:"required"`
36+
mux *http.ServeMux `validate:"required"`
37+
validator sophrosyne.Validator `validate:"required"`
3938
logger *slog.Logger `validate:"required"`
4039
http *http.Server `validate:"required"`
4140
tracingService sophrosyne.TracingService `validate:"required"`
@@ -82,30 +81,37 @@ func (s *Server) Handle(path string, handler http.Handler) {
8281
s.mux.Handle(path, handler)
8382
}
8483

84+
const JSONContentType = "application/json"
85+
const PlainTextContentType = "text/plain"
86+
8587
func RPCHandler(logger *slog.Logger, rpcService sophrosyne.RPCServer) http.Handler {
8688
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8789

8890
body, err := io.ReadAll(r.Body) // Find a way to implement a limit on the body size
91+
if err != nil {
92+
logger.ErrorContext(r.Context(), "failed to read request body", "error", err)
93+
WriteInternalServerError(r.Context(), w, logger)
94+
return
95+
}
8996
b, err := rpcService.HandleRPCRequest(r.Context(), body)
9097
if err != nil {
9198
logger.ErrorContext(r.Context(), "error handling rpc request", "error", err)
9299
WriteInternalServerError(r.Context(), w, logger)
93100
return
94101
}
95-
WriteResponse(r.Context(), w, http.StatusOK, "application/json", b, logger)
102+
WriteResponse(r.Context(), w, http.StatusOK, JSONContentType, b, logger)
96103
})
97104
}
98105

99106
func HealthcheckHandler(logger *slog.Logger, healthcheckService sophrosyne.HealthCheckService) http.Handler {
100107
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
101108
ok := healthcheckService.UnauthenticatedHealthcheck(r.Context())
102109
if ok {
103-
WriteResponse(r.Context(), w, http.StatusOK, "application/json", nil, logger)
110+
WriteResponse(r.Context(), w, http.StatusOK, JSONContentType, nil, logger)
104111
return
105112
}
106113
w.Header().Set("Retry-After", "5")
107-
WriteResponse(r.Context(), w, http.StatusServiceUnavailable, "application/json", nil, logger)
108-
return
114+
WriteResponse(r.Context(), w, http.StatusServiceUnavailable, JSONContentType, nil, logger)
109115
})
110116
}
111117

internal/http/middleware/middleware.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func Authentication(exceptions []string, config *sophrosyne.Config, userService
8484
if !strings.HasPrefix(authHeader, "Bearer ") {
8585
logger.DebugContext(r.Context(), "unable to extract token from Authorization header", "header", authHeader)
8686
logger.InfoContext(r.Context(), "authentication", "result", "failed")
87-
ownHttp.WriteResponse(r.Context(), w, http.StatusUnauthorized, "text/plain", nil, logger)
87+
ownHttp.WriteResponse(r.Context(), w, http.StatusUnauthorized, ownHttp.PlainTextContentType, nil, logger)
8888
return
8989
}
9090

@@ -93,7 +93,7 @@ func Authentication(exceptions []string, config *sophrosyne.Config, userService
9393
if err != nil {
9494
logger.DebugContext(r.Context(), "unable to decode token", "token", token, "error", err)
9595
logger.InfoContext(r.Context(), "authentication", "result", "failed")
96-
ownHttp.WriteResponse(r.Context(), w, http.StatusUnauthorized, "text/plain", nil, logger)
96+
ownHttp.WriteResponse(r.Context(), w, http.StatusUnauthorized, ownHttp.PlainTextContentType, nil, logger)
9797
return
9898
}
9999

@@ -135,8 +135,6 @@ func (w *responseWrapper) WriteHeader(status int) {
135135
w.status = status
136136
w.ResponseWriter.WriteHeader(status)
137137
w.wroteHeader = true
138-
139-
return
140138
}
141139

142140
func (w *responseWrapper) Status() int {

internal/tls/tls.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,14 @@ func NewTLSServerConfig(config *sophrosyne.Config, randSource io.Reader) (*tls.C
227227

228228
return &tls.Config{
229229
Certificates: []tls.Certificate{cert},
230+
MinVersion: tls.VersionTLS13,
230231
}, nil
231232
}
232233

233234
func NewTLSClientConfig(config *sophrosyne.Config) (*tls.Config, error) {
234-
c := &tls.Config{}
235+
c := &tls.Config{
236+
MinVersion: tls.VersionTLS13,
237+
}
235238
if config.Security.TLS.InsecureSkipVerify {
236239
c.InsecureSkipVerify = true
237240
}

tests/integration/startup_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func newHTTPClient(t *testing.T) *http.Client {
215215
return &http.Client{
216216
Transport: &http.Transport{
217217
TLSClientConfig: &tls.Config{
218-
InsecureSkipVerify: true,
218+
InsecureSkipVerify: true, //nolint:gosec
219219
},
220220
},
221221
}
@@ -232,7 +232,7 @@ func TestStartup(t *testing.T) {
232232

233233
t.Run("API served via TLS", func(t *testing.T) {
234234
conf := &tls.Config{
235-
InsecureSkipVerify: true,
235+
InsecureSkipVerify: true, //nolint:gosec
236236
}
237237
tlsConn, err := tls.Dial("tcp", te.endpoint, conf)
238238
require.NoError(t, err)
@@ -263,7 +263,13 @@ func TestStartup(t *testing.T) {
263263
// of the LogConsumer added to setupEnv, if this log cannot be unmarshalled as JSON, it fails. Thus this test
264264
// ensures that it is logged as JSON.
265265
t.Run("client remote error logged as non-json", func(t *testing.T) {
266-
tlsConn, err := tls.Dial("tcp", te.endpoint, &tls.Config{})
266+
tlsConn, err := tls.Dial("tcp", te.endpoint, &tls.Config{MinVersion: tls.VersionTLS13})
267+
require.Error(t, err)
268+
require.Nil(t, tlsConn)
269+
})
270+
271+
t.Run("TLS1.3 or better required", func(t *testing.T) {
272+
tlsConn, err := tls.Dial("tcp", te.endpoint, &tls.Config{MinVersion: tls.VersionTLS12, MaxVersion: tls.VersionTLS12}) //nolint:gosec
267273
require.Error(t, err)
268274
require.Nil(t, tlsConn)
269275
})

0 commit comments

Comments
 (0)