Skip to content

Commit f0fb264

Browse files
author
Christian
committed
chore: upgrade go-authx to v1.2.2 and add configurable gRPC message size limits
1 parent 692c0d7 commit f0fb264

6 files changed

Lines changed: 53 additions & 6 deletions

File tree

cmd/server/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func run(ctx context.Context) error {
5353
log.Info().
5454
Int("grpc_port", cfg.GRPCPort).
5555
Int("metrics_port", cfg.MetricsPort).
56+
Int("grpc_max_recv_message_size", cfg.GRPCMaxRecvMessageSize).
57+
Int("grpc_max_send_message_size", cfg.GRPCMaxSendMessageSize).
5658
Str("log_level", cfg.LogLevel).
5759
Bool("auth_enabled", cfg.AuthEnabled).
5860
Msg("Starting NIST Statistical Test Service")
@@ -304,8 +306,19 @@ func authorizationEnabled(cfg *config.Config) bool {
304306
// is enabled, it constructs TLS credentials from the configured certificate, key,
305307
// optional CA file, client authentication mode, and minimum TLS version.
306308
func buildGRPCServerOptions(cfg *config.Config, unaryInterceptors []grpc.UnaryServerInterceptor) ([]grpc.ServerOption, error) {
309+
maxRecvMessageSize := cfg.GRPCMaxRecvMessageSize
310+
if maxRecvMessageSize <= 0 {
311+
maxRecvMessageSize = 10 * 1024 * 1024
312+
}
313+
maxSendMessageSize := cfg.GRPCMaxSendMessageSize
314+
if maxSendMessageSize <= 0 {
315+
maxSendMessageSize = 10 * 1024 * 1024
316+
}
317+
307318
opts := []grpc.ServerOption{
308319
grpc.ChainUnaryInterceptor(unaryInterceptors...),
320+
grpc.MaxRecvMsgSize(maxRecvMessageSize),
321+
grpc.MaxSendMsgSize(maxSendMessageSize),
309322
}
310323

311324
if !cfg.TLSEnabled {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/AmmannChristian/nist-sp800-22-rev1a
33
go 1.25.7
44

55
require (
6-
github.com/AmmannChristian/go-authx v1.2.0
6+
github.com/AmmannChristian/go-authx v1.2.2
77
github.com/golangci/golangci-lint v1.64.8
88
github.com/google/uuid v1.6.0
99
github.com/prometheus/client_golang v1.23.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ github.com/4meepo/tagalign v1.4.2 h1:0hcLHPGMjDyM1gHG58cS73aQF8J4TdVR96TZViorO9E
1212
github.com/4meepo/tagalign v1.4.2/go.mod h1:+p4aMyFM+ra7nb41CnFG6aSDXqRxU/w1VQqScKqDARI=
1313
github.com/Abirdcfly/dupword v0.1.3 h1:9Pa1NuAsZvpFPi9Pqkd93I7LIYRURj+A//dFd5tgBeE=
1414
github.com/Abirdcfly/dupword v0.1.3/go.mod h1:8VbB2t7e10KRNdwTVoxdBaxla6avbhGzb8sCTygUMhw=
15-
github.com/AmmannChristian/go-authx v1.2.0 h1:ETNvuugwVfztHRFGA+/slV7Jwz7Dr//cEe74FhXPCbY=
16-
github.com/AmmannChristian/go-authx v1.2.0/go.mod h1:eRp0jNgv25ARPG/dcakOPaU/a5UmqXphngCb0OnjtJg=
15+
github.com/AmmannChristian/go-authx v1.2.2 h1:wlBsZs2YwI/IE88VsommFRgJq+9lWqagWjhsiaWN9eI=
16+
github.com/AmmannChristian/go-authx v1.2.2/go.mod h1:eRp0jNgv25ARPG/dcakOPaU/a5UmqXphngCb0OnjtJg=
1717
github.com/Antonboom/errname v1.0.0 h1:oJOOWR07vS1kRusl6YRSlat7HFnb3mSfMl6sDMRoTBA=
1818
github.com/Antonboom/errname v1.0.0/go.mod h1:gMOBFzK/vrTiXN9Oh+HFs+e6Ndl0eTFbtsRTSRdXyGI=
1919
github.com/Antonboom/nilnil v1.0.1 h1:C3Tkm0KUxgfO4Duk3PM+ztPncTFlOf0b2qadmS0s4xs=

internal/config/config.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import (
1111
"strings"
1212
)
1313

14+
const defaultGRPCMaxMessageSize = 10 * 1024 * 1024
15+
1416
// Config holds all service configuration loaded from environment variables.
1517
// Fields map one-to-one to environment variables (e.g. GRPCPort from GRPC_PORT).
1618
type Config struct {
1719
// gRPC server configuration
18-
GRPCPort int
20+
GRPCPort int
21+
GRPCMaxRecvMessageSize int
22+
GRPCMaxSendMessageSize int
1923

2024
// TLS configuration for gRPC
2125
TLSEnabled bool
@@ -59,6 +63,8 @@ type Config struct {
5963
func Load() (*Config, error) {
6064
cfg := &Config{
6165
GRPCPort: getEnvInt("GRPC_PORT", 9090),
66+
GRPCMaxRecvMessageSize: getEnvInt("GRPC_MAX_RECV_MESSAGE_SIZE", defaultGRPCMaxMessageSize),
67+
GRPCMaxSendMessageSize: getEnvInt("GRPC_MAX_SEND_MESSAGE_SIZE", defaultGRPCMaxMessageSize),
6268
TLSEnabled: getEnvBool("TLS_ENABLED", false),
6369
TLSCertFile: getEnvString("TLS_CERT_FILE", ""),
6470
TLSKeyFile: getEnvString("TLS_KEY_FILE", ""),
@@ -102,6 +108,18 @@ func (c *Config) Validate() error {
102108
if c.GRPCPort < 1 || c.GRPCPort > 65535 {
103109
return fmt.Errorf("invalid GRPC_PORT: %d (must be 1-65535)", c.GRPCPort)
104110
}
111+
if c.GRPCMaxRecvMessageSize < 0 {
112+
return fmt.Errorf("invalid GRPC_MAX_RECV_MESSAGE_SIZE: %d (must be >= 0)", c.GRPCMaxRecvMessageSize)
113+
}
114+
if c.GRPCMaxSendMessageSize < 0 {
115+
return fmt.Errorf("invalid GRPC_MAX_SEND_MESSAGE_SIZE: %d (must be >= 0)", c.GRPCMaxSendMessageSize)
116+
}
117+
if c.GRPCMaxRecvMessageSize == 0 {
118+
c.GRPCMaxRecvMessageSize = defaultGRPCMaxMessageSize
119+
}
120+
if c.GRPCMaxSendMessageSize == 0 {
121+
c.GRPCMaxSendMessageSize = defaultGRPCMaxMessageSize
122+
}
105123

106124
if c.MetricsPort < 1 || c.MetricsPort > 65535 {
107125
return fmt.Errorf("invalid METRICS_PORT: %d (must be 1-65535)", c.MetricsPort)

internal/config/config_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
func TestLoadWithEnvOverrides(t *testing.T) {
99
t.Setenv("GRPC_PORT", "5000")
10+
t.Setenv("GRPC_MAX_RECV_MESSAGE_SIZE", "12582912")
11+
t.Setenv("GRPC_MAX_SEND_MESSAGE_SIZE", "12582912")
1012
t.Setenv("METRICS_PORT", "6000")
1113
t.Setenv("LOG_LEVEL", "debug")
1214
t.Setenv("AUTH_ENABLED", "true")
@@ -35,6 +37,12 @@ func TestLoadWithEnvOverrides(t *testing.T) {
3537
if cfg.GRPCPort != 5000 || cfg.MetricsPort != 6000 {
3638
t.Fatalf("unexpected ports: %+v", cfg)
3739
}
40+
if cfg.GRPCMaxRecvMessageSize != 12582912 {
41+
t.Fatalf("unexpected GRPCMaxRecvMessageSize: %d", cfg.GRPCMaxRecvMessageSize)
42+
}
43+
if cfg.GRPCMaxSendMessageSize != 12582912 {
44+
t.Fatalf("unexpected GRPCMaxSendMessageSize: %d", cfg.GRPCMaxSendMessageSize)
45+
}
3846
if cfg.LogLevel != "debug" {
3947
t.Fatalf("unexpected log level: %s", cfg.LogLevel)
4048
}
@@ -208,6 +216,8 @@ func TestValidateFailures(t *testing.T) {
208216
cfg Config
209217
}{
210218
{"bad grpc port", Config{GRPCPort: 0, MetricsPort: 9000, LogLevel: "info"}},
219+
{"bad grpc max recv size", Config{GRPCPort: 9000, GRPCMaxRecvMessageSize: -1, MetricsPort: 9000, LogLevel: "info"}},
220+
{"bad grpc max send size", Config{GRPCPort: 9000, GRPCMaxSendMessageSize: -1, MetricsPort: 9000, LogLevel: "info"}},
211221
{"bad metrics port", Config{GRPCPort: 9000, MetricsPort: 70000, LogLevel: "info"}},
212222
{"bad log level", Config{GRPCPort: 9000, MetricsPort: 9001, LogLevel: "verbose"}},
213223
{"auth enabled missing issuer", Config{GRPCPort: 9000, MetricsPort: 9001, LogLevel: "info", AuthEnabled: true, AuthAudience: "api"}},
@@ -290,7 +300,7 @@ func TestValidateOpaquePrivateKeyJWTAuthSuccess(t *testing.T) {
290300
func TestLoadDefaults(t *testing.T) {
291301
// Clear any environment variables
292302
for _, key := range []string{
293-
"GRPC_PORT", "METRICS_PORT", "LOG_LEVEL",
303+
"GRPC_PORT", "GRPC_MAX_RECV_MESSAGE_SIZE", "GRPC_MAX_SEND_MESSAGE_SIZE", "METRICS_PORT", "LOG_LEVEL",
294304
"AUTH_ENABLED", "AUTH_ISSUER", "AUTH_AUDIENCE", "AUTH_JWKS_URL", "AUTH_TOKEN_TYPE",
295305
"AUTH_INTROSPECTION_URL", "AUTH_INTROSPECTION_AUTH_METHOD", "AUTH_INTROSPECTION_CLIENT_ID", "AUTH_INTROSPECTION_CLIENT_SECRET",
296306
"AUTH_INTROSPECTION_PRIVATE_KEY", "AUTH_INTROSPECTION_PRIVATE_KEY_FILE",
@@ -311,6 +321,12 @@ func TestLoadDefaults(t *testing.T) {
311321
if cfg.GRPCPort != 9090 {
312322
t.Errorf("expected default GRPCPort=9090, got %d", cfg.GRPCPort)
313323
}
324+
if cfg.GRPCMaxRecvMessageSize != 10*1024*1024 {
325+
t.Errorf("expected default GRPCMaxRecvMessageSize=10485760, got %d", cfg.GRPCMaxRecvMessageSize)
326+
}
327+
if cfg.GRPCMaxSendMessageSize != 10*1024*1024 {
328+
t.Errorf("expected default GRPCMaxSendMessageSize=10485760, got %d", cfg.GRPCMaxSendMessageSize)
329+
}
314330
if cfg.MetricsPort != 9091 {
315331
t.Errorf("expected default MetricsPort=9091, got %d", cfg.MetricsPort)
316332
}

internal/nist/run_all.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func RunAllTests(bitstream []byte) ([]TestResult, error) {
3636
return nil, fmt.Errorf("insufficient bits: got %d, need at least %d", numBits, MinBits)
3737
}
3838
if numBits > MaxBits {
39-
return nil, fmt.Errorf("too many bits: got %d, maximum %d", numBits, MaxBits)
39+
return nil, fmt.Errorf("too many bits wass: got %d, maximum %d", numBits, MaxBits)
4040
}
4141

4242
results := make([]TestResult, 0, 15)

0 commit comments

Comments
 (0)