Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Use custom http client and customizable timeout for jwks
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Feb 4, 2021
1 parent 14ddedb commit 0f392f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions internal/jwks/config.go
Expand Up @@ -31,6 +31,10 @@ type Config struct {

Port string `env:"PORT, default=8080"`

// RequestTimeout is the per-request amount of time to wait for a response
// before timing out.
RequestTimeout time.Duration `env:"REQUEST_TIMEOUT, default=5s"`

KeyCleanupTTL time.Duration `env:"HEALTH_AUTHORITY_KEY_CLEANUP_TTL, default=720h"` // 30 days
}

Expand Down
10 changes: 8 additions & 2 deletions internal/jwks/jwks.go
Expand Up @@ -42,17 +42,23 @@ import (
// URI.
type Manager struct {
db *database.DB
client *http.Client
cleanupTTL time.Duration
}

// NewManager creates a new Manager.
func NewManager(db *database.DB, cleanupTTL time.Duration) (*Manager, error) {
func NewManager(db *database.DB, cleanupTTL, requestTimeout time.Duration) (*Manager, error) {
if cleanupTTL < 0 {
cleanupTTL *= -1
}

client := &http.Client{
Timeout: requestTimeout,
}

return &Manager{
db: db,
client: client,
cleanupTTL: cleanupTTL,
}, nil
}
Expand All @@ -74,7 +80,7 @@ func (mgr *Manager) getKeys(ctx context.Context, ha *model.HealthAuthority) ([]b
return nil, fmt.Errorf("creating connection: %w", err)
}

resp, err := http.DefaultClient.Do(req)
resp, err := mgr.client.Do(req)
if err != nil {
return nil, fmt.Errorf("reading connection: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/jwks/jwks_test.go
Expand Up @@ -139,7 +139,7 @@ func TestUpdateHA(t *testing.T) {
// Set up the test.
ctx := context.Background()
testDB, _ := testDatabaseInstance.NewDatabase(t)
mgr, err := NewManager(testDB, time.Minute)
mgr, err := NewManager(testDB, time.Minute, 5*time.Second)
if err != nil {
t.Fatalf("[%d] unexpected error: %v", i, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/jwks/server.go
Expand Up @@ -39,7 +39,7 @@ func NewServer(cfg *Config, env *serverenv.ServerEnv) (*Server, error) {
return nil, fmt.Errorf("missing database in server env")
}

manager, err := NewManager(env.Database(), cfg.KeyCleanupTTL)
manager, err := NewManager(env.Database(), cfg.KeyCleanupTTL, cfg.RequestTimeout)
if err != nil {
return nil, fmt.Errorf("failed to create manager: %w", err)
}
Expand Down

0 comments on commit 0f392f4

Please sign in to comment.