-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe5s.go
More file actions
747 lines (703 loc) · 23.1 KB
/
e5s.go
File metadata and controls
747 lines (703 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Package e5s provides a simple API for building production-grade mTLS services
// with SPIFFE identity verification using SPIRE.
//
// This package wraps the lower-level spiffehttp and spire packages,
// providing a config-file-driven approach that requires minimal code.
//
// See docs/ARCHITECTURE.md for layering details.
//
// Quick Start:
//
// Server (simple):
//
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// peer, ok := e5s.PeerInfo(r)
// if !ok {
// http.Error(w, "unauthorized", http.StatusUnauthorized)
// return
// }
// fmt.Fprintf(w, "Hello %s\n", peer.ID.String())
// })
//
// if err := e5s.Serve("e5s.yaml", handler); err != nil {
// log.Fatal(err)
// }
//
// Server (advanced - custom shutdown logic):
//
// shutdown, err := e5s.Start("e5s.yaml", handler)
// if err != nil {
// log.Fatal(err)
// }
// defer shutdown()
//
// Client (simple):
//
// err := e5s.WithClient("e5s.yaml", func(client *http.Client) error {
// resp, err := client.Get("https://secure-service:8443/api")
// if err != nil {
// return err
// }
// defer resp.Body.Close()
// // Process response...
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
//
// Client (advanced - long-lived client):
//
// client, shutdown, err := e5s.Client("e5s.yaml")
// if err != nil {
// log.Fatal(err)
// }
// defer shutdown()
//
// resp, err := client.Get("https://secure-service:8443/api")
package e5s
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/spiffe/go-spiffe/v2/workloadapi"
"github.com/sufield/e5s/internal/config"
"github.com/sufield/e5s/spiffehttp"
"github.com/sufield/e5s/spire"
)
// debugEnabled is set at package initialization based on E5S_DEBUG environment variable.
// Recognized values: "1", "true", "TRUE", "debug", "DEBUG"
var debugEnabled = func() bool {
v := os.Getenv("E5S_DEBUG")
return v == "1" || strings.EqualFold(v, "true") || strings.EqualFold(v, "debug")
}()
// debugf logs a debug message with consistent formatting if debug mode is enabled.
func debugf(format string, args ...any) {
if !debugEnabled {
return
}
log.Printf("e5s DEBUG: "+format, args...)
}
// firstErr returns the first non-nil error from the provided list.
// This is useful for combining multiple cleanup errors during shutdown.
func firstErr(errs ...error) error {
for _, e := range errs {
if e != nil {
return e
}
}
return nil
}
// newSPIRESource initializes the SPIRE identity source and returns:
// - x509Source: the X.509 source used for TLS
// - shutdown: an idempotent function that closes the source
func newSPIRESource(
ctx context.Context,
workloadSocket string,
initialFetchTimeout time.Duration,
) (x509Source *workloadapi.X509Source, shutdown func() error, err error) {
src, err := spire.NewIdentitySource(ctx, spire.Config{
WorkloadSocket: workloadSocket,
InitialFetchTimeout: initialFetchTimeout,
})
if err != nil {
return nil, nil, err
}
x509 := src.X509Source()
var once sync.Once
var shutdownErr error
shutdown = func() error {
once.Do(func() {
shutdownErr = src.Close()
})
return shutdownErr
}
return x509, shutdown, nil
}
// loadServerConfig loads and validates server configuration from the specified file.
// Returns the raw config and validated SPIRE config ready for use.
func loadServerConfig(path string) (config.ServerFileConfig, config.SPIREConfig, error) {
cfg, err := config.LoadServerConfig(path)
if err != nil {
return config.ServerFileConfig{}, config.SPIREConfig{}, fmt.Errorf("failed to load config: %w", err)
}
spireCfg, _, err := config.ValidateServerConfig(&cfg)
if err != nil {
return config.ServerFileConfig{}, config.SPIREConfig{}, fmt.Errorf("invalid server config: %w", err)
}
return cfg, spireCfg, nil
}
// loadClientConfig loads and validates client configuration from the specified file.
// Returns the raw config and validated SPIRE config ready for use.
func loadClientConfig(path string) (config.ClientFileConfig, config.SPIREConfig, error) {
cfg, err := config.LoadClientConfig(path)
if err != nil {
return config.ClientFileConfig{}, config.SPIREConfig{}, fmt.Errorf("failed to load config: %w", err)
}
spireCfg, _, err := config.ValidateClientConfig(&cfg)
if err != nil {
return config.ClientFileConfig{}, config.SPIREConfig{}, fmt.Errorf("invalid client config: %w", err)
}
return cfg, spireCfg, nil
}
// buildServerWithContext constructs the HTTP server and SPIRE identity source with a custom context.
//
// This is the context-aware version used internally by StartWithContext.
// The context is used for SPIRE source initialization and TLS config creation.
func buildServerWithContext(ctx context.Context, configPath string, handler http.Handler) (
srv *http.Server,
identityShutdown func() error,
err error,
) {
// Load and validate configuration
cfg, spireConfig, err := loadServerConfig(configPath)
if err != nil {
return nil, nil, err
}
// Centralized SPIRE setup with provided context
x509Source, identityShutdown, err := newSPIRESource(
ctx,
cfg.SPIRE.WorkloadSocket,
spireConfig.InitialFetchTimeout,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to create SPIRE source: %w", err)
}
// Build server TLS config with client verification
tlsCfg, err := spiffehttp.NewServerTLSConfig(
ctx,
x509Source,
x509Source,
spiffehttp.ServerConfig{
AllowedClientID: cfg.Server.AllowedClientSPIFFEID,
AllowedClientTrustDomain: cfg.Server.AllowedClientTrustDomain,
},
)
if err != nil {
if shutdownErr := identityShutdown(); shutdownErr != nil {
return nil, nil, fmt.Errorf("failed to create server TLS config: %w (cleanup error: %v)", err, shutdownErr)
}
return nil, nil, fmt.Errorf("failed to create server TLS config: %w", err)
}
// Wrap handler to inject peer identity into request context
wrapped := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if peer, ok := spiffehttp.PeerFromRequest(r); ok {
r = r.WithContext(spiffehttp.WithPeer(r.Context(), peer))
}
handler.ServeHTTP(w, r)
})
// Create HTTP server with mTLS
srv = &http.Server{
Addr: cfg.Server.ListenAddr,
Handler: wrapped,
TLSConfig: tlsCfg,
ReadHeaderTimeout: 10 * time.Second,
}
// Enable debug logging if E5S_DEBUG is set
if debugEnabled {
srv.ErrorLog = log.New(os.Stderr, "e5s/http: ", log.LstdFlags|log.Lshortfile)
debugf("server config_path=%q listen_addr=%q allowed_client_spiffe_id=%q allowed_client_trust_domain=%q",
configPath,
cfg.Server.ListenAddr,
cfg.Server.AllowedClientSPIFFEID,
cfg.Server.AllowedClientTrustDomain,
)
}
return srv, identityShutdown, nil
}
// buildServer constructs the HTTP server and SPIRE identity source used by both Start and StartSingleThread.
//
// This internal helper factors out common setup logic to ensure both execution modes use
// identical configuration, SPIRE wiring, and TLS setup. It uses context.Background() internally.
//
// Returns:
// - srv: configured HTTP server ready to serve
// - identityShutdown: function to release SPIRE resources (idempotent)
// - err: if config loading, SPIRE connection, or TLS setup fails
func buildServer(configPath string, handler http.Handler) (
srv *http.Server,
identityShutdown func() error,
err error,
) {
return buildServerWithContext(context.Background(), configPath, handler)
}
// Start starts a production-grade mTLS server using SPIRE.
//
// It loads configuration from the specified YAML file, connects to the SPIRE
// Workload API, configures mutual TLS with automatic certificate rotation,
// and starts serving the provided HTTP handler.
//
// The server enforces:
// - TLS 1.3 minimum
// - Mutual TLS (both server and client present certificates)
// - SPIFFE ID verification of clients based on config policy
// - Automatic certificate rotation (zero-downtime)
//
// Configuration (e5s.yaml):
//
// spire:
// workload_socket: unix:///tmp/spire-agent/public/api.sock
// server:
// listen_addr: ":8443"
// # Exactly one of these:
// allowed_client_spiffe_id: "spiffe://example.org/client"
// # OR
// allowed_client_trust_domain: "example.org"
//
// The handler can extract authenticated peer identity using PeerInfo(r) or PeerID(r).
//
// Returns:
// - shutdown: function to gracefully stop the server and release resources
// - error: if config loading, SPIRE connection, or server startup fails
//
// Shutdown semantics:
// - The shutdown function is safe to call multiple times (idempotent)
// - First call initiates graceful shutdown with 5-second timeout
// - Subsequent calls return the result of the first shutdown
// - Shutdown does NOT wait for in-flight requests (use sync.WaitGroup if needed)
// - After shutdown, the SPIRE source is closed and cannot be reused
//
// Usage:
//
// shutdown, err := e5s.Start("e5s.yaml", myHandler)
// if err != nil {
// log.Fatal(err)
// }
//
// // Wait for interrupt signal
// sigChan := make(chan os.Signal, 1)
// signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
// <-sigChan
//
// // Gracefully shutdown
// if err := shutdown(); err != nil {
// log.Printf("Shutdown error: %v", err)
// os.Exit(1)
// }
// os.Exit(0)
func Start(configPath string, handler http.Handler) (shutdown func() error, err error) {
return StartWithContext(context.Background(), configPath, handler)
}
// StartWithContext starts an mTLS server with a custom context for SPIRE initialization.
//
// This is identical to Start() but allows passing a context for more control over
// the SPIRE source initialization and TLS configuration lifecycle.
//
// The context is used for:
// - SPIRE Workload API connection
// - TLS configuration setup
//
// Use this when you need:
// - Parent context cancellation propagation
// - Custom timeouts during initialization
// - Integration with existing context hierarchies
//
// Otherwise, use Start() which uses context.Background() internally.
//
// Returns:
// - shutdown: function to gracefully stop the server and release resources
// - error: if config loading, SPIRE connection, or server startup fails
//
// Usage:
//
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// defer cancel()
//
// shutdown, err := e5s.StartWithContext(ctx, "e5s.yaml", myHandler)
// if err != nil {
// log.Fatal(err)
// }
// defer shutdown()
func StartWithContext(ctx context.Context, configPath string, handler http.Handler) (shutdown func() error, err error) {
srv, identityShutdown, err := buildServerWithContext(ctx, configPath, handler)
if err != nil {
return nil, err
}
// Channel to capture server startup errors
errCh := make(chan error, 1)
// Start server in background
go func() {
err := srv.ListenAndServeTLS("", "")
if err != nil && err != http.ErrServerClosed {
errCh <- err
}
}()
// Give server a moment to bind or fail
select {
case err := <-errCh:
if shutdownErr := identityShutdown(); shutdownErr != nil {
return nil, fmt.Errorf("server startup failed: %w (cleanup error: %v)", err, shutdownErr)
}
return nil, fmt.Errorf("server startup failed: %w", err)
case <-time.After(100 * time.Millisecond):
// Server started successfully
}
// Ensure shutdown is only executed once
var shutdownOnce sync.Once
var shutdownErr error
// Return shutdown function
shutdownFunc := func() error {
shutdownOnce.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err1 := srv.Shutdown(ctx)
err2 := identityShutdown()
shutdownErr = firstErr(err1, err2)
})
return shutdownErr
}
return shutdownFunc, nil
}
// Serve starts an mTLS server with graceful shutdown handling.
//
// This is a convenience wrapper around Start() that handles signal management
// and graceful shutdown automatically. It blocks until receiving SIGINT or SIGTERM,
// then performs graceful shutdown with a 5-second timeout.
//
// Use Serve() for typical servers that need standard signal handling.
// Use Start() if you need:
// - Custom shutdown triggers (not just signals)
// - Multiple servers or complex lifecycle management
// - Integration with existing shutdown orchestration
//
// Configuration is identical to Start() - same e5s.yaml format.
//
// The function blocks until:
// - SIGINT is received (e.g., user presses Ctrl+C)
// - SIGTERM is received (e.g., Kubernetes pod termination)
//
// Then it:
// - Stops accepting new connections
// - Waits up to 5 seconds for in-flight requests to complete
// - Closes SPIRE resources
// - Returns any shutdown errors
//
// Usage:
//
// func main() {
// r := chi.NewRouter()
// r.Get("/time", timeHandler)
//
// if err := e5s.Serve("e5s.yaml", r); err != nil {
// log.Fatal(err)
// }
// }
//
// For debug-friendly single-threaded execution, use StartSingleThread() instead.
func Serve(configPath string, handler http.Handler) error {
shutdown, err := Start(configPath, handler)
if err != nil {
return err
}
defer func() {
if shutdownErr := shutdown(); shutdownErr != nil {
fmt.Fprintf(os.Stderr, "Shutdown error: %v\n", shutdownErr)
}
}()
// Wait for interrupt signal using context-based cancellation
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
return nil
}
// StartSingleThread starts an mTLS server using SPIRE and blocks in the calling goroutine.
//
// This function provides a debug-friendly execution mode that uses the same configuration,
// SPIRE integration, and TLS setup as Start(), but runs the HTTP server synchronously in
// the current goroutine instead of spawning a background goroutine.
//
// go-spiffe behavior (including automatic certificate rotation) is unchanged; this mode
// only removes the goroutine that e5s itself creates for the HTTP server.
//
// This is useful for:
// - Debugging with IDE step-through (predictable call stack)
// - Isolating concurrency issues (is it e5s threading or something else?)
// - Simplified testing scenarios
// - Learning the library internals
//
// Configuration is identical to Start() - same e5s.yaml format:
//
// spire:
// workload_socket: unix:///tmp/spire-agent/public/api.sock
// server:
// listen_addr: ":8443"
// allowed_client_trust_domain: "example.org"
//
// Differences from Start():
// - BLOCKS in the current goroutine (does not return until server stops)
// - Does NOT return a shutdown function (process lifetime management)
// - Automatic cleanup when the function returns
// - Server stops on context cancellation or fatal error
//
// Usage:
//
// func main() {
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// id, ok := e5s.PeerID(r)
// if !ok {
// http.Error(w, "unauthorized", http.StatusUnauthorized)
// return
// }
// fmt.Fprintf(w, "Hello %s\n", id)
// })
//
// if err := e5s.StartSingleThread("e5s.yaml", handler); err != nil {
// log.Fatal(err)
// }
// }
//
// For production deployments with graceful shutdown, use Start() or Serve() instead.
func StartSingleThread(configPath string, handler http.Handler) error {
srv, identityShutdown, err := buildServer(configPath, handler)
if err != nil {
return err
}
defer func() {
// Best effort cleanup - ignore errors during shutdown
_ = identityShutdown()
}()
// Run server in the current goroutine (blocks here)
err = srv.ListenAndServeTLS("", "")
if err != nil && err != http.ErrServerClosed {
return fmt.Errorf("server exited with error: %w", err)
}
return nil
}
// PeerInfo extracts the authenticated caller's full identity from a request.
//
// This function retrieves the peer identity that was verified during the mTLS
// handshake and stored in the request context by the e5s server middleware.
//
// IMPORTANT: This only works for requests served by a server started with e5s.Start().
// If called on a request from a different server (or before mTLS verification),
// it returns false.
//
// Returns:
// - Peer: complete authenticated identity (SPIFFE ID, trust domain, cert expiry)
// - ok: true if identity was found, false otherwise
//
// Usage in handler:
//
// func myHandler(w http.ResponseWriter, r *http.Request) {
// peer, ok := e5s.PeerInfo(r)
// if !ok {
// http.Error(w, "unauthorized", http.StatusUnauthorized)
// return
// }
// // Use peer for authorization decisions
// log.Printf("Request from %s (trust domain: %s, expires: %s)",
// peer.ID.String(), peer.ID.TrustDomain().Name(), peer.ExpiresAt)
// }
func PeerInfo(r *http.Request) (spiffehttp.Peer, bool) {
return spiffehttp.PeerFromContext(r.Context())
}
// PeerID extracts the authenticated caller's SPIFFE ID from a request.
//
// This is a convenience wrapper around PeerInfo() that returns only the SPIFFE ID.
// Use PeerInfo() if you need access to trust domain or certificate expiry.
//
// IMPORTANT: This only works for requests served by a server started with e5s.Start().
// If called on a request from a different server (or before mTLS verification),
// it returns false.
//
// Returns:
// - spiffeID: the authenticated peer's SPIFFE ID (e.g., "spiffe://example.org/client")
// - ok: true if identity was found, false otherwise
//
// Usage in handler:
//
// func myHandler(w http.ResponseWriter, r *http.Request) {
// id, ok := e5s.PeerID(r)
// if !ok {
// http.Error(w, "unauthorized", http.StatusUnauthorized)
// return
// }
// // Use id for authorization decisions
// log.Printf("Request from %s", id)
// }
func PeerID(r *http.Request) (string, bool) {
peer, ok := PeerInfo(r)
if !ok {
return "", false
}
return peer.ID.String(), true
}
// WithClient creates an mTLS client, executes the provided function, and handles cleanup.
//
// This is a convenience wrapper around Client() that manages the client lifecycle
// automatically. It creates the client, executes your function, and ensures cleanup
// happens even if the function panics or returns an error.
//
// Use WithClient() for typical request patterns where you don't need long-lived clients.
// Use Client() if you need:
// - Long-lived clients making multiple requests over time
// - Manual control over client lifetime
// - Sharing a client across multiple goroutines
//
// Configuration is identical to Client() - same e5s.yaml format.
//
// The function executes synchronously and returns any error from:
// - Client creation
// - The callback function
// - Cleanup (logged but not returned unless callback succeeded)
//
// Usage:
//
// err := e5s.WithClient("e5s.yaml", func(client *http.Client) error {
// resp, err := client.Get("https://secure-service:8443/api")
// if err != nil {
// return err
// }
// defer resp.Body.Close()
//
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// return err
// }
// fmt.Println(string(body))
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
func WithClient(configPath string, fn func(*http.Client) error) error {
client, cleanup, err := Client(configPath)
if err != nil {
return err
}
defer func() {
if err := cleanup(); err != nil {
fmt.Fprintf(os.Stderr, "Client cleanup error: %v\n", err)
}
}()
return fn(client)
}
// Client returns an HTTP client configured for mTLS using SPIRE.
//
// The returned client will:
// - Present the workload's SPIFFE ID via mTLS
// - Verify the remote server's SPIFFE ID based on config policy
// - Handle automatic certificate rotation
// - Use TLS 1.3 minimum
//
// Configuration (e5s.yaml):
//
// spire:
// workload_socket: unix:///tmp/spire-agent/public/api.sock
// client:
// # Exactly one of these:
// expected_server_spiffe_id: "spiffe://example.org/api-server"
// # OR
// expected_server_trust_domain: "example.org"
//
// Returns:
// - client: HTTP client ready for mTLS connections
// - shutdown: function to release SPIRE resources (should be deferred)
// - error: if config loading, SPIRE connection, or TLS setup fails
//
// Shutdown semantics:
// - The shutdown function is safe to call multiple times (idempotent)
// - First call closes the SPIRE source
// - Subsequent calls return the result of the first close
// - After shutdown, the client can still complete in-flight requests
// - New requests after shutdown will fail with certificate errors
//
// Usage:
//
// client, shutdown, err := e5s.Client("e5s.yaml")
// if err != nil {
// log.Fatal(err)
// }
// defer shutdown()
//
// resp, err := client.Get("https://secure-service:8443/api")
// if err != nil {
// log.Fatal(err)
// }
// defer resp.Body.Close()
func Client(configPath string) (*http.Client, func() error, error) {
return ClientWithContext(context.Background(), configPath)
}
// ClientWithContext returns an HTTP client configured for mTLS using SPIRE with a custom context.
//
// This is identical to Client() but allows passing a context for more control over
// the SPIRE source initialization and TLS configuration lifecycle.
//
// The context is used for:
// - SPIRE Workload API connection
// - TLS configuration setup
//
// Use this when you need:
// - Parent context cancellation propagation
// - Custom timeouts during initialization
// - Integration with existing context hierarchies
//
// Otherwise, use Client() which uses context.Background() internally.
//
// Returns:
// - client: HTTP client ready for mTLS connections
// - shutdown: function to release SPIRE resources (should be deferred)
// - error: if config loading, SPIRE connection, or TLS setup fails
//
// Usage:
//
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// defer cancel()
//
// client, shutdown, err := e5s.ClientWithContext(ctx, "e5s.yaml")
// if err != nil {
// log.Fatal(err)
// }
// defer shutdown()
func ClientWithContext(ctx context.Context, configPath string) (*http.Client, func() error, error) {
// Load and validate configuration
cfg, spireConfig, err := loadClientConfig(configPath)
if err != nil {
return nil, nil, err
}
// Centralized SPIRE setup with provided context
x509Source, identityShutdown, err := newSPIRESource(
ctx,
cfg.SPIRE.WorkloadSocket,
spireConfig.InitialFetchTimeout,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to create SPIRE source: %w", err)
}
// Build client TLS config with server verification
tlsCfg, err := spiffehttp.NewClientTLSConfig(
ctx,
x509Source,
x509Source,
spiffehttp.ClientConfig{
ExpectedServerID: cfg.Client.ExpectedServerSPIFFEID,
ExpectedServerTrustDomain: cfg.Client.ExpectedServerTrustDomain,
},
)
if err != nil {
if shutdownErr := identityShutdown(); shutdownErr != nil {
return nil, nil, fmt.Errorf("failed to create client TLS config: %w (cleanup error: %v)", err, shutdownErr)
}
return nil, nil, fmt.Errorf("failed to create client TLS config: %w", err)
}
// Create HTTP client with mTLS
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
}
// Enable debug logging if E5S_DEBUG is set
debugf("client config_path=%q expected_server_spiffe_id=%q expected_server_trust_domain=%q",
configPath,
cfg.Client.ExpectedServerSPIFFEID,
cfg.Client.ExpectedServerTrustDomain,
)
return httpClient, identityShutdown, nil
}