-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
283 lines (242 loc) · 8.16 KB
/
Copy pathmain.go
File metadata and controls
283 lines (242 loc) · 8.16 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/app-vault/app-vault/internal/api"
"github.com/app-vault/app-vault/internal/db"
"github.com/app-vault/app-vault/internal/metrics"
"github.com/app-vault/app-vault/internal/ratelimit"
"github.com/app-vault/app-vault/internal/security"
"github.com/app-vault/app-vault/internal/service"
)
// Config holds application configuration
type Config struct {
ServerPort string
DatabaseURL string
JWTSecret string
MigrationsPath string
EnableTLS bool
TLSCertFile string
TLSKeyFile string
RateLimitEnabled bool
RateLimitMax int
RateLimitWindow time.Duration
MaxRequestSize int64
}
// loadConfig loads configuration from environment variables
func loadConfig() *Config {
enableTLS := getEnv("ENABLE_TLS", "false") == "true"
rateLimitEnabled := getEnv("RATE_LIMIT_ENABLED", "true") == "true"
rateLimitMax, _ := strconv.Atoi(getEnv("RATE_LIMIT_MAX", "100"))
rateLimitWindowSec, _ := strconv.Atoi(getEnv("RATE_LIMIT_WINDOW_SECONDS", "60"))
maxRequestSizeMB, _ := strconv.ParseInt(getEnv("MAX_REQUEST_SIZE_MB", "1"), 10, 64)
return &Config{
ServerPort: getEnv("SERVER_PORT", "8888"),
DatabaseURL: getEnv("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/appvault?sslmode=disable"),
JWTSecret: getEnv("JWT_SECRET", "your-secret-key-change-this-in-production"),
MigrationsPath: getEnv("MIGRATIONS_PATH", "migrations"),
EnableTLS: enableTLS,
TLSCertFile: getEnv("TLS_CERT_FILE", "certs/server.crt"),
TLSKeyFile: getEnv("TLS_KEY_FILE", "certs/server.key"),
RateLimitEnabled: rateLimitEnabled,
RateLimitMax: rateLimitMax,
RateLimitWindow: time.Duration(rateLimitWindowSec) * time.Second,
MaxRequestSize: maxRequestSizeMB * 1024 * 1024,
}
}
// getEnv gets an environment variable with a default value
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
func main() {
log.Println("Starting App Vault server...")
cfg := loadConfig()
database, err := db.New(cfg.DatabaseURL)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer database.Close()
log.Println("Connected to database")
migrationsPath := cfg.MigrationsPath
if !filepath.IsAbs(migrationsPath) {
wd, _ := os.Getwd()
migrationsPath = filepath.Join(wd, migrationsPath)
}
if err := database.RunMigrations(migrationsPath); err != nil {
log.Fatalf("Failed to run migrations: %v", err)
}
log.Println("Database migrations completed")
authService := service.NewAuthService(database, cfg.JWTSecret)
vaultService := service.NewVaultService(database)
rotationService := service.NewRotationService(database)
handler := api.NewHandler(authService, vaultService, rotationService)
// Initialize metrics
metricsInstance := metrics.GetMetrics()
// Initialize rate limiter
var rateLimiter *ratelimit.RateLimiter
if cfg.RateLimitEnabled {
rateLimiter = ratelimit.NewRateLimiter(cfg.RateLimitWindow, cfg.RateLimitMax)
log.Printf("Rate limiting enabled: %d requests per %s", cfg.RateLimitMax, cfg.RateLimitWindow)
}
mux := http.NewServeMux()
// Metrics endpoint (no auth required for monitoring)
mux.HandleFunc("/metrics", metricsInstance.MetricsHandler())
// Health check with database status
mux.HandleFunc("/health", metrics.HealthHandler(func() bool {
return database.Ping() == nil
}))
mux.HandleFunc("/api/v1/auth/register", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.Register(w, r)
})
mux.HandleFunc("/api/v1/auth/login", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.Login(w, r)
})
authMiddleware := api.AuthMiddleware(authService)
mux.Handle("/api/v1/secrets", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
handler.CreateSecret(w, r)
case http.MethodGet:
handler.ListSecrets(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})))
mux.Handle("/api/v1/secrets/by-name", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.GetSecretByName(w, r)
})))
mux.Handle("/api/v1/secrets/", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handler.GetSecretByID(w, r)
case http.MethodDelete:
handler.DeleteSecret(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})))
mux.Handle("/api/v1/keys/rotate", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.RotateVaultKey(w, r)
})))
mux.Handle("/api/v1/keys/status", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.GetKeyStatus(w, r)
})))
mux.Handle("/api/v1/auth/change-password", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler.ChangePassword(w, r)
})))
// Service Principal endpoints
mux.Handle("/api/v1/service-principals", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
handler.CreateServicePrincipal(w, r)
case http.MethodGet:
handler.ListServicePrincipals(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})))
mux.Handle("/api/v1/service-principals/", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if this is a regenerate request
if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/regenerate") {
handler.RegenerateServicePrincipalSecret(w, r)
return
}
switch r.Method {
case http.MethodGet:
handler.GetServicePrincipal(w, r)
case http.MethodDelete:
handler.DeleteServicePrincipal(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
})))
// Build middleware chain
finalHandler := api.RecoveryMiddleware(
api.LoggingMiddleware(
security.SecurityHeadersMiddleware(
security.ContentTypeMiddleware(
security.RequestSizeLimitMiddleware(cfg.MaxRequestSize)(
metrics.MetricsMiddleware(
api.CORSMiddleware(mux),
),
),
),
),
),
)
// Add rate limiting if enabled
if rateLimiter != nil {
finalHandler = rateLimiter.Middleware(finalHandler)
}
server := &http.Server{
Addr: ":" + cfg.ServerPort,
Handler: finalHandler,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
// Configure TLS if enabled
if cfg.EnableTLS {
server.TLSConfig = security.GetTLSConfig()
log.Println("TLS enabled with TLS 1.3")
}
go func() {
log.Printf("Server listening on port %s", cfg.ServerPort)
var err error
if cfg.EnableTLS {
log.Printf("Starting HTTPS server with cert: %s", cfg.TLSCertFile)
err = server.ListenAndServeTLS(cfg.TLSCertFile, cfg.TLSKeyFile)
} else {
log.Println("Starting HTTP server (WARNING: TLS disabled)")
err = server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server stopped")
}