Skip to content

Commit 38cf3c7

Browse files
committed
fix(examples): reject non-positive JWT_TTL at config load
1 parent fae5ea3 commit 38cf3c7

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

examples/hello-mysql/cmd/api/main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,12 @@ func TestLoadAppOptions_WithOverrides(t *testing.T) {
218218
t.Fatalf("unexpected rate limits: %v/%d", got.RateLimitPerSecond, got.RateLimitBurst)
219219
}
220220
}
221+
222+
func TestLoadAppOptions_InvalidNonPositiveJWTTTL(t *testing.T) {
223+
t.Setenv("JWT_TTL", "0s")
224+
225+
_, err := loadAppOptions()
226+
if err == nil {
227+
t.Fatalf("expected error for non-positive JWT_TTL")
228+
}
229+
}

examples/hello-mysql/internal/modules/config/module.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"time"
56

67
mkconfig "github.com/go-modkit/modkit/modkit/config"
@@ -89,7 +90,7 @@ func (m *Module) Definition() module.ModuleDef {
8990
mkconfig.WithTyped(TokenJWTTTL, mkconfig.ValueSpec[time.Duration]{
9091
Key: "JWT_TTL",
9192
Default: &jwtTTLDefault,
92-
Parse: mkconfig.ParseDuration,
93+
Parse: parsePositiveDuration,
9394
}, true),
9495
mkconfig.WithTyped(TokenAuthUsername, mkconfig.ValueSpec[string]{
9596
Key: "AUTH_USERNAME",
@@ -141,3 +142,14 @@ func (m *Module) Definition() module.ModuleDef {
141142
Exports: exportedTokens,
142143
}
143144
}
145+
146+
func parsePositiveDuration(raw string) (time.Duration, error) {
147+
d, err := mkconfig.ParseDuration(raw)
148+
if err != nil {
149+
return 0, err
150+
}
151+
if d <= 0 {
152+
return 0, fmt.Errorf("duration must be > 0")
153+
}
154+
return d, nil
155+
}

examples/hello-mysql/internal/modules/config/module_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package config
22

33
import (
4+
"errors"
45
"reflect"
56
"testing"
67
"time"
78

9+
mkconfig "github.com/go-modkit/modkit/modkit/config"
810
"github.com/go-modkit/modkit/modkit/kernel"
911
"github.com/go-modkit/modkit/modkit/module"
1012
)
@@ -114,3 +116,22 @@ func TestResolvesSourceOverrides(t *testing.T) {
114116
t.Fatalf("unexpected RATE_LIMIT_BURST: %d err=%v", burst, err)
115117
}
116118
}
119+
120+
func TestRejectsNonPositiveJWTTTL(t *testing.T) {
121+
src := mapSource{"JWT_TTL": "0s"}
122+
123+
app, err := kernel.Bootstrap(&rootModule{imports: []module.Module{NewModule(Options{Source: src})}})
124+
if err != nil {
125+
t.Fatalf("bootstrap failed: %v", err)
126+
}
127+
128+
_, err = module.Get[time.Duration](app, TokenJWTTTL)
129+
if err == nil {
130+
t.Fatalf("expected JWT_TTL parse error")
131+
}
132+
133+
var parseErr *mkconfig.ParseError
134+
if !errors.As(err, &parseErr) {
135+
t.Fatalf("expected ParseError, got %T", err)
136+
}
137+
}

0 commit comments

Comments
 (0)