Brijesh's Git Server — argus-core @ 48d4f681f4e2301b57e86a5b09ebe156a5115fee

Logging service

internal/database/database.go (view raw)

 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
package database

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/gocql/gocql"
	_ "github.com/joho/godotenv/autoload"
	"golang.org/x/exp/rand"
)

type User struct {
	ID           gocql.UUID `json:"id"`
	Email        string     `json:"email"`
	PasswordHash string     `json:"-"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
}

type Application struct {
	ID          gocql.UUID `json:"id"`
	UserID      gocql.UUID `json:"user_id"`
	Name        string     `json:"name"`
	Description string     `json:"description"`
	KeyHash     string     `json:"-"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`
}

type Log struct {
	ApplicationID gocql.UUID `json:"application_id"`
	LogID         gocql.UUID `json:"log_id"`
	UserID        gocql.UUID `json:"user_id"`
	Timestamp     time.Time  `json:"timestamp"`
	Level         string     `json:"level"`
	Message       string     `json:"message"`
}

type LogsFilter struct {
	ApplicationID gocql.UUID
	PageSize      int
	Cursor        *time.Time
	LogLevel      string
	StartTime     *time.Time
	EndTime       *time.Time
}

type Service interface {
	Health() map[string]string
	Close() error

	CreateUser(email, passwordHash string) (*User, error)
	GetUserByEmail(email string) (*User, error)
	GetUserByID(id gocql.UUID) (*User, error)

	CreateApplication(userID gocql.UUID, name, description, keyHash string) (*Application, error)
	GetApplication(id gocql.UUID) (*Application, error)
	ListApplications(userID gocql.UUID) ([]Application, error)
	UpdateApplication(id gocql.UUID, name, description string) (*Application, error)
	DeleteApplication(id gocql.UUID) error
	RegenerateApplicationKey(appID gocql.UUID, newKeyHash string) error

	BatchInsertLogs(logs []Log) error
	GetRecentLogs(filter LogsFilter) ([]Log, error)
	ValidateApplicationKey(keyHash string) (*Application, error) // Helper method for API key validation
	GenerateDummyLogs(applicationID gocql.UUID) (int, error)
}

type service struct {
	session *gocql.Session
}

func New() Service {
	// Load environment variables
	cassandraHost := os.Getenv("CASSANDRA_HOST")
	cassandraUsername := os.Getenv("CASSANDRA_USERNAME")
	cassandraPassword := os.Getenv("CASSANDRA_PASSWORD")
	cassandraKeyspace := os.Getenv("CASSANDRA_KEYSPACE")

	// Download certificate
	resp, err := http.Get("https://certs.secureserver.net/repository/sf-class2-root.crt")
	if err != nil {
		log.Fatal("Failed to download certificate:", err)
	}
	defer resp.Body.Close()

	tempCertFile, err := os.CreateTemp("", "cassandra-cert-*.crt")
	if err != nil {
		log.Fatal("Failed to create temp cert file:", err)
	}
	defer os.Remove(tempCertFile.Name())

	if _, err := io.Copy(tempCertFile, resp.Body); err != nil {
		log.Fatal("Failed to write certificate to file:", err)
	}

	cluster := gocql.NewCluster(cassandraHost)
	cluster.Port = 9142
	cluster.Authenticator = gocql.PasswordAuthenticator{
		Username: cassandraUsername,
		Password: cassandraPassword,
	}
	cluster.SslOpts = &gocql.SslOptions{
		CaPath:                 tempCertFile.Name(),
		EnableHostVerification: false,
	}
	cluster.Consistency = gocql.LocalQuorum
	cluster.Keyspace = cassandraKeyspace

	session, err := cluster.CreateSession()
	if err != nil {
		log.Fatal("Failed to create Cassandra session:", err)
	}

	return &service{session: session}
}

func (s *service) Health() map[string]string {
	stats := make(map[string]string)

	if err := s.session.Query("SELECT release_version FROM system.local").Exec(); err != nil {
		stats["status"] = "down"
		stats["error"] = fmt.Sprintf("Cassandra down: %v", err)
		return stats
	}

	stats["status"] = "up"
	stats["message"] = "Cassandra is healthy"
	return stats
}

func (s *service) Close() error {
	s.session.Close()
	return nil
}

// User operations
func (s *service) CreateUser(email, passwordHash string) (*User, error) {
	user := &User{
		ID:           gocql.TimeUUID(),
		Email:        email,
		PasswordHash: passwordHash,
		CreatedAt:    time.Now(),
		UpdatedAt:    time.Now(),
	}

	if err := s.session.Query(`
								INSERT INTO users (id, email, password_hash, created_at, updated_at)
								VALUES (?, ?, ?, ?, ?)`,
		user.ID, user.Email, user.PasswordHash, user.CreatedAt, user.UpdatedAt,
	).Exec(); err != nil {
		return nil, fmt.Errorf("error creating user: %w", err)
	}

	return user, nil
}

func (s *service) GetUserByEmail(email string) (*User, error) {
	var user User
	if err := s.session.Query(`
								SELECT id, email, password_hash, created_at, updated_at
								FROM users WHERE email = ? ALLOW FILTERING`,
		email,
	).Scan(&user.ID, &user.Email, &user.PasswordHash, &user.CreatedAt, &user.UpdatedAt); err != nil {
		return nil, fmt.Errorf("user not found: %w", err)
	}
	return &user, nil
}

func (s *service) GetUserByID(id gocql.UUID) (*User, error) {
	var user User
	if err := s.session.Query(`
								SELECT id, email, password_hash, created_at, updated_at
								FROM users WHERE id = ?`,
		id,
	).Scan(&user.ID, &user.Email, &user.PasswordHash, &user.CreatedAt, &user.UpdatedAt); err != nil {
		return nil, fmt.Errorf("user not found: %w", err)
	}
	return &user, nil
}

func (s *service) CreateApplication(userID gocql.UUID, name, description, keyHash string) (*Application, error) {
	app := &Application{
		ID:          gocql.TimeUUID(),
		UserID:      userID,
		Name:        name,
		Description: description,
		KeyHash:     keyHash,
		CreatedAt:   time.Now(),
		UpdatedAt:   time.Now(),
	}

	if err := s.session.Query(`
								INSERT INTO applications (id, user_id, name, description, key_hash, created_at, updated_at)
								VALUES (?, ?, ?, ?, ?, ?, ?)`,
		app.ID, app.UserID, app.Name, app.Description, app.KeyHash, app.CreatedAt, app.UpdatedAt,
	).Exec(); err != nil {
		return nil, fmt.Errorf("error creating application: %w", err)
	}

	return app, nil
}

func (s *service) GetApplication(id gocql.UUID) (*Application, error) {
	var app Application
	if err := s.session.Query(`
								SELECT id, user_id, name, description, key_hash, created_at, updated_at
								FROM applications WHERE id = ?`,
		id,
	).Scan(
		&app.ID, &app.UserID, &app.Name, &app.Description,
		&app.KeyHash, &app.CreatedAt, &app.UpdatedAt,
	); err != nil {
		return nil, fmt.Errorf("application not found: %w", err)
	}
	return &app, nil
}

func (s *service) ListApplications(userID gocql.UUID) ([]Application, error) {
	iter := s.session.Query(`
								SELECT id, user_id, name, description, key_hash, created_at, updated_at
								FROM applications WHERE user_id = ? ALLOW FILTERING`,
		userID,
	).Iter()

	var apps []Application
	var app Application
	for iter.Scan(
		&app.ID, &app.UserID, &app.Name, &app.Description,
		&app.KeyHash, &app.CreatedAt, &app.UpdatedAt,
	) {
		apps = append(apps, app)
	}

	if err := iter.Close(); err != nil {
		return nil, fmt.Errorf("error listing applications: %w", err)
	}

	return apps, nil
}

func (s *service) UpdateApplication(id gocql.UUID, name, description string) (*Application, error) {
	now := time.Now()
	if err := s.session.Query(`
								UPDATE applications
								SET name = ?,
												description = ?,
												updated_at = ?
								WHERE id = ?`,
		name, description, now, id,
	).Exec(); err != nil {
		return nil, fmt.Errorf("error updating application: %w", err)
	}

	return s.GetApplication(id)
}

func (s *service) DeleteApplication(id gocql.UUID) error {
	if err := s.session.Query(`
								DELETE FROM applications WHERE id = ?`,
		id,
	).Exec(); err != nil {
		return fmt.Errorf("error deleting application: %w", err)
	}

	return nil
}

func (s *service) RegenerateApplicationKey(appID gocql.UUID, newKeyHash string) error {
	now := time.Now()
	if err := s.session.Query(`
								UPDATE applications
								SET key_hash = ?,
												updated_at = ?
								WHERE id = ?`,
		newKeyHash, now, appID,
	).Exec(); err != nil {
		return fmt.Errorf("error regenerating application key: %w", err)
	}
	return nil
}

// BatchInsertLogs inserts multiple logs in a batch operation
func (s *service) BatchInsertLogs(logs []Log) error {
	batch := s.session.NewBatch(gocql.LoggedBatch)

	for _, log := range logs {
		batch.Query(`
            INSERT INTO logs (
                application_id,
                timestamp,
                log_id,
                user_id,
                log_level,
                message
            ) VALUES (?, ?, ?, ?, ?, ?)`,
			log.ApplicationID,
			log.Timestamp,
			log.LogID,
			log.UserID,
			log.Level,
			log.Message,
		)
	}

	if err := s.session.ExecuteBatch(batch); err != nil {
		return fmt.Errorf("failed to batch insert logs: %w", err)
	}

	return nil
}

func (s *service) GetRecentLogs(filter LogsFilter) ([]Log, error) {
	if filter.PageSize <= 0 || filter.PageSize > 100 {
		filter.PageSize = 100
	}

	// Build the query dynamically based on filters
	query := "SELECT application_id, timestamp, log_id, user_id, log_level, message FROM logs WHERE application_id = ?"
	args := []interface{}{filter.ApplicationID}

	// Add timestamp conditions
	if filter.Cursor != nil {
		query += " AND timestamp < ?"
		args = append(args, *filter.Cursor)
	}
	if filter.StartTime != nil {
		query += " AND timestamp >= ?"
		args = append(args, *filter.StartTime)
	}
	if filter.EndTime != nil {
		query += " AND timestamp <= ?"
		args = append(args, *filter.EndTime)
	}

	// Add log level filter if specified
	if filter.LogLevel != "" {
		query += " AND log_level = ?"
		args = append(args, filter.LogLevel)
	}

	// Add ordering and limit
	query += " ORDER BY timestamp DESC LIMIT ?"
	args = append(args, filter.PageSize)

	// Execute query with ALLOW FILTERING
	iter := s.session.Query(query+" ALLOW FILTERING", args...).Iter()

	var logs []Log
	var log Log
	for iter.Scan(
		&log.ApplicationID,
		&log.Timestamp,
		&log.LogID,
		&log.UserID,
		&log.Level,
		&log.Message,
	) {
		logs = append(logs, log)
	}

	if err := iter.Close(); err != nil {
		return nil, fmt.Errorf("error fetching logs: %w", err)
	}

	return logs, nil
}

// ValidateApplicationKey validates an API key and returns the associated application
func (s *service) ValidateApplicationKey(keyHash string) (*Application, error) {
	var app Application

	// Query the applications table using the key hash
	if err := s.session.Query(`
        SELECT id, user_id, name, description, key_hash, created_at, updated_at
        FROM applications
        WHERE key_hash = ?
        ALLOW FILTERING`,
		keyHash,
	).Scan(
		&app.ID,
		&app.UserID,
		&app.Name,
		&app.Description,
		&app.KeyHash,
		&app.CreatedAt,
		&app.UpdatedAt,
	); err != nil {
		if err == gocql.ErrNotFound {
			return nil, fmt.Errorf("invalid API key")
		}
		return nil, fmt.Errorf("error validating API key: %w", err)
	}

	return &app, nil
}

func (s *service) GenerateDummyLogs(applicationID gocql.UUID) (int, error) {
	// Get the application to ensure it exists and get its user ID
	var app Application
	if err := s.session.Query(`
								SELECT id, user_id FROM applications WHERE id = ?
				`, applicationID).Scan(&app.ID, &app.UserID); err != nil {
		return 0, fmt.Errorf("application not found: %w", err)
	}

	// Generate 30 logs spanning the last 5 minutes
	now := time.Now()
	startTime := now.Add(-5 * time.Minute)

	batch := s.session.NewBatch(gocql.UnloggedBatch)

	// Sample messages and levels for variety
	messages := []string{
		"User authentication successful",
		"Database connection established",
		"API request processed successfully",
		"Cache miss for key: %s",
		"Background job completed in %dms",
		"Memory usage: %d MB",
		"Request failed with status code: %d",
		"Rate limit exceeded for IP: %s",
		"Configuration reload initiated",
		"File upload completed: %s",
	}

	levelWeights := map[string]int{
		"DEBUG": 15,
		"INFO":  60,
		"WARN":  15,
		"ERROR": 8,
		"FATAL": 2,
	}

	// Random data for interpolation
	sampleIPs := []string{"192.168.1.1", "10.0.0.1", "172.16.0.1", "8.8.8.8"}
	sampleFiles := []string{"user.jpg", "data.csv", "config.json", "backup.zip"}
	sampleKeys := []string{"user:1234", "session:5678", "settings:9012", "temp:3456"}

	for i := 0; i < 30; i++ {
		// Calculate timestamp with even distribution over 5 minutes
		progress := float64(i) / 30.0
		timestamp := startTime.Add(time.Duration(progress * 5 * float64(time.Minute)))

		// Select log level based on weights
		rand.Seed(uint64(time.Now().UnixNano()))
		r := rand.Intn(100)
		var level string
		sum := 0
		for l, weight := range levelWeights {
			sum += weight
			if r < sum {
				level = l
				break
			}
		}

		// Select and format message
		msgTemplate := messages[rand.Intn(len(messages))]
		var msg string

		switch {
		case strings.Contains(msgTemplate, "IP:"):
			msg = fmt.Sprintf(msgTemplate, sampleIPs[rand.Intn(len(sampleIPs))])
		case strings.Contains(msgTemplate, "File"):
			msg = fmt.Sprintf(msgTemplate, sampleFiles[rand.Intn(len(sampleFiles))])
		case strings.Contains(msgTemplate, "key:"):
			msg = fmt.Sprintf(msgTemplate, sampleKeys[rand.Intn(len(sampleKeys))])
		case strings.Contains(msgTemplate, "MB"):
			msg = fmt.Sprintf(msgTemplate, rand.Intn(1000))
		case strings.Contains(msgTemplate, "ms"):
			msg = fmt.Sprintf(msgTemplate, rand.Intn(500))
		case strings.Contains(msgTemplate, "status code:"):
			codes := []int{400, 401, 403, 404, 500, 502, 503}
			msg = fmt.Sprintf(msgTemplate, codes[rand.Intn(len(codes))])
		default:
			msg = msgTemplate
		}

		// Add to batch
		batch.Query(`
												INSERT INTO logs (
																application_id,
																timestamp,
																log_id,
																user_id,
																log_level,
																message
												) VALUES (?, ?, ?, ?, ?, ?)`,
			applicationID,
			timestamp,
			gocql.TimeUUID(),
			app.UserID,
			level,
			msg,
		)
	}

	if err := s.session.ExecuteBatch(batch); err != nil {
		return 0, fmt.Errorf("failed to insert dummy logs: %w", err)
	}

	return 30, nil
}