-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathproxy.go
More file actions
627 lines (519 loc) · 19.9 KB
/
proxy.go
File metadata and controls
627 lines (519 loc) · 19.9 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
package cmd
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/Infisical/infisical-merge/packages/proxy"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/Infisical/infisical-merge/packages/util/cache"
"github.com/awnumar/memguard"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
type CacheEvictionStrategy string
const (
CacheEvictionStrategyOptimistic CacheEvictionStrategy = "optimistic"
)
var proxyCmd = &cobra.Command{
Example: `infisical proxy start`,
Short: "Used to run Infisical proxy server",
Use: "proxy",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
}
var proxyStartCmd = &cobra.Command{
Example: `infisical proxy start --domain=https://app.infisical.com --listen-address=localhost:8081`,
Short: "Start the Infisical proxy server",
Use: "start",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: startProxyServer,
}
var proxyDebugCmd = &cobra.Command{
Example: `infisical proxy debug --listen-address=localhost:8081`,
Short: "Print cache debug information (dev mode only)",
Use: "debug",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Run: printCacheDebug,
Hidden: true,
}
func startProxyServer(cmd *cobra.Command, args []string) {
domain, err := cmd.Flags().GetString("domain")
if err != nil {
util.HandleError(err, "Unable to parse domain flag")
}
if domain == "" {
util.PrintErrorMessageAndExit("Domain flag is required")
}
listenAddress, err := cmd.Flags().GetString("listen-address")
if err != nil {
util.HandleError(err, "Unable to parse listen-address flag")
}
tlsEnabled, err := cmd.Flags().GetBool("tls-enabled")
if err != nil {
util.HandleError(err, "Unable to parse tls-enabled flag")
}
tlsCertFile, err := cmd.Flags().GetString("tls-cert-file")
if err != nil {
util.HandleError(err, "Unable to parse tls-cert-file flag")
}
tlsKeyFile, err := cmd.Flags().GetString("tls-key-file")
if err != nil {
util.HandleError(err, "Unable to parse tls-key-file flag")
}
if tlsEnabled && (tlsCertFile == "" || tlsKeyFile == "") {
util.PrintErrorMessageAndExit("`tls-cert-file` and `tls-key-file` are required when `tls-enabled` is set to true")
}
if listenAddress == "" {
util.PrintErrorMessageAndExit("Listen-address flag is required")
}
evictionStrategy, err := cmd.Flags().GetString("eviction-strategy")
if err != nil {
util.HandleError(err, "Unable to parse eviction-strategy flag")
}
if evictionStrategy != string(CacheEvictionStrategyOptimistic) {
util.PrintErrorMessageAndExit(fmt.Sprintf("Invalid eviction-strategy '%s'. Currently only 'optimistic' is supported.", evictionStrategy))
}
accessTokenCheckIntervalStr, err := cmd.Flags().GetString("access-token-check-interval")
if err != nil {
util.HandleError(err, "Unable to parse access-token-check-interval flag")
}
accessTokenCheckInterval, err := util.ParseTimeDurationString(accessTokenCheckIntervalStr, true)
if err != nil {
util.PrintErrorMessageAndExit(fmt.Sprintf("Invalid access-token-check-interval format '%s'. Use formats like 5m, 1h, 1d", accessTokenCheckIntervalStr))
}
staticSecretsRefreshIntervalStr, err := cmd.Flags().GetString("static-secrets-refresh-interval")
if err != nil {
util.HandleError(err, "Unable to parse static-secrets-refresh-interval flag")
}
staticSecretsRefreshInterval, err := util.ParseTimeDurationString(staticSecretsRefreshIntervalStr, true)
if err != nil {
util.PrintErrorMessageAndExit(fmt.Sprintf("Invalid static-secrets-refresh-interval format '%s'. Use formats like 30m, 1h, 1d", staticSecretsRefreshIntervalStr))
}
useSSE, err := cmd.Flags().GetBool("use-sse")
if err != nil {
util.HandleError(err, "Unable to parse use-sse flag")
}
clientId, err := cmd.Flags().GetString("client-id")
if err != nil {
util.HandleError(err, "Unable to parse client-id flag")
}
clientSecret, err := cmd.Flags().GetString("client-secret")
if err != nil {
util.HandleError(err, "Unable to parse client-secret flag")
}
if useSSE && (clientId == "" || clientSecret == "") {
util.PrintErrorMessageAndExit("--client-id and --client-secret are required when --use-sse is enabled")
}
domainURL, err := url.Parse(domain)
if err != nil {
util.HandleError(err, fmt.Sprintf("Invalid domain URL: %s", domain))
}
httpClient := &http.Client{
Timeout: 30 * time.Second,
}
// Create a separate client for streaming endpoints (no timeout for long-lived connections)
streamingClient := &http.Client{
Timeout: 0,
}
// Create in-memory cache (no persistence, no encryption needed for ephemeral data)
// For persistent cache with encryption, use proxy.NewCacheWithOptions
encryptionKey := memguard.NewBufferRandom(32)
defer encryptionKey.Destroy()
cache, err := proxy.NewCache(cache.EncryptedStorageOptions{
InMemory: true,
EncryptionKey: encryptionKey,
})
if err != nil {
util.PrintErrorMessageAndExit(fmt.Sprintf("Failed to create cache: %v", err))
}
defer cache.Close()
mux := http.NewServeMux()
// Debug endpoint (dev mode only)
if util.IsDevelopmentMode() {
mux.HandleFunc("/_debug/cache", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
debugInfo := cache.GetDebugInfo()
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(debugInfo); err != nil {
log.Error().Err(err).Msg("Failed to encode cache debug info")
http.Error(w, "Failed to encode debug info", http.StatusInternalServerError)
return
}
})
log.Info().Msg("Dev mode enabled: debug endpoint available at /_debug/cache")
}
var sseManager *proxy.SSEManager
var sseAuthState *proxy.SSEAuthState
if useSSE {
sseAuthState = proxy.NewSSEAuthState(clientId, clientSecret, domainURL, httpClient)
sseManager = proxy.NewSSEManager(context.Background(), cache, domainURL, streamingClient, httpClient, sseAuthState)
log.Info().Msg("SSE manager initialized for demand-driven cache updates")
}
proxyHandler := func(w http.ResponseWriter, r *http.Request) {
// Skip debug endpoints - they're handled by mux
if strings.HasPrefix(r.URL.Path, "/_debug/") {
http.NotFound(w, r)
return
}
token := proxy.ExtractTokenFromRequest(r)
isCacheable := proxy.IsCacheableRequest(r.URL.Path, r.Method)
isStreaming := isStreamingEndpoint(r.URL.Path)
// -- Cache Check --
if isCacheable && token != "" {
if served := serveCachedResponse(w, r, cache, token); served {
return
}
}
// -- Proxy Request --
// Read request body for mutation eviction (PATCH/DELETE) or restore for forwarding
var requestBodyBytes []byte
if r.Body != nil {
requestBodyBytes, err = io.ReadAll(r.Body)
if err != nil {
log.Error().Err(err).Msg("Failed to read request body")
http.Error(w, fmt.Sprintf("Failed to read request body: %v", err), http.StatusInternalServerError)
return
}
}
targetURL := *domainURL
targetURL.Path = domainURL.Path + r.URL.Path
targetURL.RawQuery = r.URL.RawQuery
var bodyReader io.Reader
if requestBodyBytes != nil {
bodyReader = bytes.NewReader(requestBodyBytes)
}
proxyReq, err := http.NewRequest(r.Method, targetURL.String(), bodyReader)
if err != nil {
log.Error().Err(err).Msg("Failed to create proxy request")
http.Error(w, fmt.Sprintf("Failed to create proxy request: %v", err), http.StatusInternalServerError)
return
}
proxy.CopyHeaders(proxyReq.Header, r.Header)
log.Debug().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("target", targetURL.String()).
Msg("Forwarding request")
// Use streaming client for SSE/streaming endpoints, regular client for others
clientToUse := httpClient
if isStreaming {
clientToUse = streamingClient
}
resp, err := clientToUse.Do(proxyReq)
if err != nil {
log.Error().Err(err).Msg("Failed to forward request")
http.Error(w, fmt.Sprintf("Failed to forward request: %v", err), http.StatusBadGateway)
return
}
defer resp.Body.Close()
// -- Proxy Response --
proxy.CopyHeaders(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
// For streaming endpoints, stream directly instead of buffering
if isStreaming {
// Flush headers immediately for SSE
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
// Stream with periodic flushing for SSE events
buf := make([]byte, 1024)
for {
n, err := resp.Body.Read(buf)
if n > 0 {
if _, writeErr := w.Write(buf[:n]); writeErr != nil {
log.Error().Err(writeErr).Msg("Failed to write streaming response")
return
}
// Flush after each write to send SSE events immediately
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
}
if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
break
}
if err != nil {
log.Error().Err(err).Msg("Failed to read streaming response")
return
}
}
return
}
// For non-streaming endpoints, read into memory for caching and serving
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("Failed to read response body")
http.Error(w, fmt.Sprintf("Failed to read response body: %v", err), http.StatusInternalServerError)
return
}
_, err = w.Write(bodyBytes)
if err != nil {
log.Error().Err(err).Msg("Failed to write response body")
return
}
// -- Secret Mutation Purging --
if (r.Method == http.MethodPatch || r.Method == http.MethodDelete || r.Method == http.MethodPost) &&
proxy.IsSecretsEndpoint(r.URL.Path) &&
resp.StatusCode >= 200 && resp.StatusCode < 300 {
var projectId, environment, secretPath string
if len(requestBodyBytes) > 0 {
var bodyData map[string]interface{}
if err := json.Unmarshal(requestBodyBytes, &bodyData); err == nil {
// Support both v3 (workspaceId/workspaceSlug) and v4 (projectId)
if projId, ok := bodyData["projectId"].(string); ok {
projectId = projId
} else if workspaceId, ok := bodyData["workspaceId"].(string); ok {
projectId = workspaceId
} else if workspaceSlug, ok := bodyData["workspaceSlug"].(string); ok {
projectId = workspaceSlug
}
if env, ok := bodyData["environment"].(string); ok {
environment = env
}
if path, ok := bodyData["secretPath"].(string); ok {
secretPath = path
}
} else {
log.Error().
Err(err).
Str("method", r.Method).
Str("path", r.URL.Path).
Msg("Failed to parse mutation request body for cache purging - cache may serve stale data")
}
}
if secretPath == "" {
secretPath = "/"
}
if projectId == "" || environment == "" {
log.Warn().
Str("method", r.Method).
Str("path", r.URL.Path).
Msg("Missing projectId or environment for cache purging - skipping cache purge")
return
} else {
log.Debug().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("projectId", projectId).
Str("environment", environment).
Str("secretPath", secretPath).
Msg("Attempting mutation purging across all tokens")
purgedCount := cache.PurgeByMutation(projectId, environment, secretPath)
if purgedCount == 1 {
log.Info().
Str("mutationPath", secretPath).
Msg("Entry purged")
} else {
log.Info().
Int("purgedCount", purgedCount).
Str("mutationPath", secretPath).
Msg("Entries purged")
}
}
}
// -- Cache Set --
if isCacheable && token != "" && resp.StatusCode == http.StatusOK {
cacheKey := proxy.GenerateCacheKey(r.Method, r.URL.Path, r.URL.RawQuery, token)
queryParams := r.URL.Query()
// Support both v3 (workspaceId/workspaceSlug) and v4 (projectId)
projectId := queryParams.Get("projectId")
if projectId == "" {
projectId = queryParams.Get("workspaceId")
}
if projectId == "" {
projectId = queryParams.Get("workspaceSlug")
}
environment := queryParams.Get("environment")
secretPath := queryParams.Get("secretPath")
if secretPath == "" {
secretPath = "/"
}
if r.URL.Path == "/api/v3/secrets" || r.URL.Path == "/api/v4/secrets" ||
r.URL.Path == "/api/v3/secrets/raw" || r.URL.Path == "/api/v4/secrets/raw" {
recursive := queryParams.Get("recursive")
if recursive == "true" {
secretPath = secretPath + "*"
}
}
indexEntry := proxy.IndexEntry{
CacheKey: cacheKey,
SecretPath: secretPath,
EnvironmentSlug: environment,
ProjectId: projectId,
}
cachedResp := &http.Response{
StatusCode: resp.StatusCode,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
}
proxy.CopyHeaders(cachedResp.Header, resp.Header)
if indexEntry.ProjectId != "" && indexEntry.EnvironmentSlug != "" {
cache.Set(cacheKey, r, cachedResp, token, indexEntry)
if sseManager != nil {
log.Info().Str("projectId", indexEntry.ProjectId).Msg("Ensuring SSE subscription for project")
// this will start the subscription using SSE for the project
sseManager.EnsureSubscription(indexEntry.ProjectId)
}
log.Debug().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("cacheKey", cacheKey).
Msg("Secret response cached successfully")
} else {
log.Warn().
Str("method", r.Method).
Str("path", r.URL.Path).
Str("cacheKey", cacheKey).
Msg("Secret response not cached because project ID or environment slug is empty")
}
}
log.Debug().
Str("method", r.Method).
Str("path", r.URL.Path).
Int("status", resp.StatusCode).
Msg("Request forwarded successfully")
}
// Add proxy handler to mux
mux.HandleFunc("/", proxyHandler)
var tlsConfig *tls.Config
if tlsEnabled {
cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
if err != nil {
util.HandleError(err, fmt.Sprintf("Failed to load TLS certificate and key: %s", err))
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
server := &http.Server{
Addr: listenAddress,
Handler: mux,
TLSConfig: tlsConfig,
}
resyncCtx, resyncCancel := context.WithCancel(context.Background())
defer resyncCancel()
proxy.StartBackgroundLoops(resyncCtx, cache, domainURL, httpClient, evictionStrategy, accessTokenCheckInterval, staticSecretsRefreshInterval, useSSE)
// Handle graceful shutdown
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
log.Info().Msgf("Received signal %v, shutting down proxy server...", sig)
// Cancel resync goroutine
resyncCancel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Error during server shutdown")
os.Exit(1)
}
log.Info().Msg("Proxy server shutdown complete")
os.Exit(0)
}()
if tlsEnabled {
log.Info().Msgf("Infisical proxy server starting on %s with TLS enabled", listenAddress)
} else {
log.Info().Msgf("Infisical proxy server starting on %s", listenAddress)
}
log.Info().Msgf("Forwarding requests to %s", domain)
if tlsEnabled {
if err := server.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
util.HandleError(err, "Failed to start proxy server with TLS")
}
} else {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
util.HandleError(err, "Failed to start proxy server")
}
}
}
func printCacheDebug(cmd *cobra.Command, args []string) {
if util.CLI_VERSION != "devel" {
util.PrintErrorMessageAndExit("This command is only available in dev mode (when CLI_VERSION is 'devel').")
}
listenAddress, err := cmd.Flags().GetString("listen-address")
if err != nil {
util.HandleError(err, "Unable to parse listen-address flag")
}
if listenAddress == "" {
util.PrintErrorMessageAndExit("Listen-address flag is required")
}
baseURL := "http://" + listenAddress
if strings.HasPrefix(listenAddress, ":") {
baseURL = "http://localhost" + listenAddress
}
debugURL := baseURL + "/_debug/cache"
resp, err := http.Get(debugURL)
if err != nil {
util.HandleError(err, fmt.Sprintf("Failed to connect to proxy at %s. Make sure the proxy is running in dev mode (CLI_VERSION='devel')", listenAddress))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
util.PrintErrorMessageAndExit(fmt.Sprintf("Failed to get cache debug info: %s", string(body)))
}
var debugInfo proxy.CacheDebugInfo
if err := json.NewDecoder(resp.Body).Decode(&debugInfo); err != nil {
util.HandleError(err, "Failed to decode cache debug info")
}
output, err := json.MarshalIndent(debugInfo, "", " ")
if err != nil {
util.HandleError(err, "Failed to marshal cache debug info")
}
fmt.Println("Cache Debug Information:")
fmt.Println(string(output))
}
// serveCachedResponse looks up the cache for the request using the caller's token.
// Returns true if a cached response was written to w.
func serveCachedResponse(w http.ResponseWriter, r *http.Request, c *proxy.Cache, token string) bool {
cacheKey := proxy.GenerateCacheKey(r.Method, r.URL.Path, r.URL.RawQuery, token)
if cachedResp, found := c.Get(cacheKey); found {
log.Info().Str("hash", cacheKey).Msg("Cache hit")
writeCachedResponse(w, cachedResp)
return true
}
log.Info().Str("hash", cacheKey).Msg("Cache miss")
return false
}
func writeCachedResponse(w http.ResponseWriter, cachedResp *http.Response) {
proxy.CopyHeaders(w.Header(), cachedResp.Header)
w.WriteHeader(cachedResp.StatusCode)
if _, err := io.Copy(w, cachedResp.Body); err != nil {
log.Error().Err(err).Msg("Failed to copy cached response body")
}
}
func isStreamingEndpoint(path string) bool {
return strings.HasPrefix(path, "/api/v1/events/")
}
func init() {
proxyStartCmd.Flags().String("domain", "", "Domain of your Infisical instance (e.g., https://app.infisical.com for cloud, https://my-self-hosted-instance.com for self-hosted)")
proxyStartCmd.Flags().String("listen-address", "localhost:8081", "The address for the proxy server to listen on. Defaults to localhost:8081")
proxyStartCmd.Flags().String("eviction-strategy", string(CacheEvictionStrategyOptimistic), "Cache eviction strategy. 'optimistic' keeps cached data when Infisical is unreachable for high availability. Currently only 'optimistic' is supported.")
proxyStartCmd.Flags().String("access-token-check-interval", "5m", "How often to validate that access tokens are still valid (e.g., 5m, 1h). Defaults to 5m.")
proxyStartCmd.Flags().String("static-secrets-refresh-interval", "1h", "How often to refresh cached secrets (e.g., 30m, 1h, 1d). Defaults to 1h.")
proxyStartCmd.Flags().String("tls-cert-file", "", "The path to the TLS certificate file for the proxy server. Required when `tls-enabled` is set to true (default)")
proxyStartCmd.Flags().String("tls-key-file", "", "The path to the TLS key file for the proxy server. Required when `tls-enabled` is set to true (default)")
proxyStartCmd.Flags().Bool("tls-enabled", true, "Whether to enable TLS for the proxy server. Defaults to true")
proxyStartCmd.Flags().Bool("use-sse", false, "Enable SSE (Server-Sent Events) mode for real-time cache invalidation. When enabled, the static secrets refresh loop is disabled and --client-id/--client-secret are required.")
proxyStartCmd.Flags().String("client-id", "", "Universal auth client ID for SSE (required when --use-sse is enabled)")
proxyStartCmd.Flags().String("client-secret", "", "Machine identity client secret for universal auth (required when --use-sse is enabled)")
proxyDebugCmd.Flags().String("listen-address", "localhost:8081", "The address where the proxy server is listening. Defaults to localhost:8081")
proxyCmd.AddCommand(proxyStartCmd)
proxyCmd.AddCommand(proxyDebugCmd)
RootCmd.AddCommand(proxyCmd)
}