initializeAuthAndObservability initializes authentication and observability components
()
| 98 | |
| 99 | // initializeAuthAndObservability initializes authentication and observability components |
| 100 | func (s *Server) initializeAuthAndObservability() { |
| 101 | // Initialize Auth Manager |
| 102 | if s.config.Auth.APIKey.Enabled || s.config.Auth.JWT.Enabled { |
| 103 | s.authManager = auth.NewManager(auth.AuthMethodAPIKey) |
| 104 | |
| 105 | // Register API Key authenticator |
| 106 | if s.config.Auth.APIKey.Enabled { |
| 107 | apiKeyStore := auth.NewMemoryAPIKeyStore() |
| 108 | apiKeyAuth := auth.NewAPIKeyAuthenticator(apiKeyStore) |
| 109 | s.authManager.Register(apiKeyAuth) |
| 110 | } |
| 111 | |
| 112 | // Register JWT authenticator |
| 113 | if s.config.Auth.JWT.Enabled { |
| 114 | jwtAuth := auth.NewJWTAuthenticator(auth.JWTConfig{ |
| 115 | SecretKey: s.config.Auth.JWT.Secret, |
| 116 | Issuer: "aster", |
| 117 | ExpiryDuration: time.Duration(s.config.Auth.JWT.Expiry) * time.Second, |
| 118 | }) |
| 119 | s.authManager.Register(jwtAuth) |
| 120 | } |
| 121 | |
| 122 | // Initialize RBAC |
| 123 | s.rbac = auth.NewRBAC() |
| 124 | } |
| 125 | |
| 126 | // Initialize Metrics |
| 127 | if s.config.Observability.Metrics.Enabled { |
| 128 | s.metrics = observability.NewMetricsManager("aster") |
| 129 | } |
| 130 | |
| 131 | // Initialize Health Checker |
| 132 | if s.config.Observability.HealthCheck.Enabled { |
| 133 | s.healthChecker = observability.NewHealthChecker(aster.Version) |
| 134 | |
| 135 | // Register store health check |
| 136 | storeCheck := observability.NewStoreHealthCheck("store", func(ctx context.Context) error { |
| 137 | // Simple ping check - try to list something |
| 138 | _, err := s.store.List(ctx, "health_check") |
| 139 | if err != nil && err.Error() != "bucket not found" && err.Error() != "not found" { |
| 140 | return err |
| 141 | } |
| 142 | return nil |
| 143 | }) |
| 144 | s.healthChecker.RegisterCheck(storeCheck) |
| 145 | } |
| 146 | |
| 147 | // Initialize Rate Limiter |
| 148 | if s.config.RateLimit.Enabled { |
| 149 | s.rateLimiter = ratelimit.NewLimiterFromConfig(ratelimit.Config{ |
| 150 | Enabled: s.config.RateLimit.Enabled, |
| 151 | RequestsPerIP: s.config.RateLimit.RequestsPerIP, |
| 152 | WindowSize: s.config.RateLimit.WindowSize, |
| 153 | BurstSize: s.config.RateLimit.BurstSize, |
| 154 | Algorithm: "token_bucket", // 默认使用令牌桶 |
| 155 | }) |
| 156 | } |
| 157 |
no test coverage detected