Skip to content

Latest commit

 

History

History
273 lines (197 loc) · 6.79 KB

File metadata and controls

273 lines (197 loc) · 6.79 KB

Security & Testing Improvements

Overview

This PR addresses critical security concerns around admin API key management and adds comprehensive test coverage for leaderboard functionality and the complete enrollment-to-milestone student journey.


Commit Message

feat: add admin API key rotation and comprehensive test coverage

Security improvements:
- Implement admin API key rotation system with 1-hour transition window
- Add key versioning and rotation history tracking
- Implement 90-day rotation alert for security compliance
- Add key revocation capability

Testing improvements:
- Add comprehensive Leaderboard component unit tests
  * Verify top 10 scholars render with correct rank, address, and LRN balance
  * Highlight current user in leaderboard
  * Test pagination controls
  * Verify loading skeleton display
  * Test truncated address display
  * Verify reputation rank badge coloring (gold/silver/bronze)

- Add complete enrollment-to-milestone E2E test
  * Student wallet connection → course enrollment → lesson navigation
  * Milestone evidence submission with verification
  * Admin approval workflow
  * LRN token balance verification
  * Leaderboard rank update verification

Database:
- Migration 017: admin_api_keys and admin_key_rotation_history tables

Files added:
- /server/src/db/migrations/017_admin_api_keys.sql
- /server/src/db/migrations/017_admin_api_keys.undo.sql
- /server/src/services/admin-key-rotation.service.ts
- /server/src/controllers/admin-key-rotation.controller.ts
- /server/src/routes/admin-key-rotation.routes.ts
- /src/pages/Leaderboard.test.tsx
- /e2e/enrollment-to-milestone.spec.ts

PR Description

🔒 Security: Admin API Key Rotation System

Problem

The admin API key was static with no rotation mechanism, creating a catastrophic single point of failure. A compromised key would grant unlimited access to all critical contract functions (minting, approvals, upgrades).

Solution

Implemented a comprehensive key rotation system:

Key Rotation Endpoints:

  • POST /api/admin/rotate-key - Rotate admin API key with reason tracking
  • GET /api/admin/keys/active - List active API keys
  • GET /api/admin/keys/rotation-status - Check if rotation is needed (90-day alert)
  • POST /api/admin/keys/revoke - Revoke compromised keys immediately

Features:

  • 1-hour transition window: Both old and new keys valid during rotation
  • Rotation history tracking: Audit trail of all key rotations with timestamps
  • 90-day rotation alerts: Automated notifications if key not rotated
  • Key versioning: Track multiple generations of keys per admin
  • Revocation support: Immediately invalidate compromised keys

Database Schema:

admin_api_keys:
  - id (PK)
  - admin_address
  - key_hash (SHA256)
  - key_name
  - is_active
  - last_rotated_at
  - created_at
  - revoked_at
  - rotation_reason

admin_key_rotation_history:
  - id (PK)
  - admin_address
  - old_key_hash
  - new_key_hash
  - rotation_reason
  - rotated_by
  - carried_out_at

✅ Testing: Leaderboard Component Suite

Problem

Leaderboard page had no unit test coverage, risking regressions in ranking display and user experience.

Solution

Added comprehensive unit test suite covering:

Test Cases:

  • ✅ Top 10 scholars render with correct rank, address, and LRN balance
  • ✅ Current user is highlighted with "You" badge when in top 10
  • ✅ User's rank displayed in footer
  • ✅ LRN balance formatted correctly
  • ✅ Addresses truncated appropriately
  • ✅ Reputation rank badges show correct tier colors:
    • Rank 1: Gold (bg-yellow-500)
    • Rank 2: Silver (bg-slate-300)
    • Rank 3: Bronze (bg-amber-600)
    • Rank 4+: Neutral (bg-white/10)
  • ✅ Pagination controls functional
  • ✅ Loading skeleton displayed during fetch
  • ✅ Completed milestones count shown

File: src/pages/Leaderboard.test.tsx


🚀 Testing: Enrollment-to-Milestone E2E Flow

Problem

No E2E test coverage for the critical student learning journey:

  1. Wallet connection
  2. Course enrollment
  3. Lesson/milestone navigation
  4. Evidence submission
  5. Admin approval
  6. Reward verification
  7. Leaderboard rank update

Solution

Added comprehensive E2E spec with two test scenarios:

Scenario 1: Complete Flow (enrollment-to-milestone.spec.ts)

  1. ✅ Navigate to courses page
  2. ✅ Connect wallet (Freighter mock)
  3. ✅ Find and enroll in course
  4. ✅ View course details and milestones
  5. ✅ Submit milestone evidence
  6. ✅ Switch to admin wallet
  7. ✅ Approve milestone from admin dashboard
  8. ✅ Verify LRN balance increased (100 tokens per milestone)
  9. ✅ Verify reputation rank updated in leaderboard
  10. ✅ Verify "You" badge shows in leaderboard

Scenario 2: Multiple Milestones

  • Enroll in course
  • Submit multiple consecutive milestone evidences
  • Verify all submissions are pending review

Features:

  • Full API mocking for enrollment, milestone, and leaderboard endpoints
  • Admin wallet switching simulation
  • Balance and ranking verification
  • Network wait handling for realistic conditions

Testing Instructions

Run Leaderboard Unit Tests

npm run test -- Leaderboard.test.tsx

Run E2E Enrollment-to-Milestone Tests

npx playwright test e2e/enrollment-to-milestone.spec.ts

Run All Tests

npm run test
npx playwright test

Migration Instructions

# Run the new migration
npm run db:migrate

# Verify migration
npm run db:migrate:verify

# To rollback (if needed)
npm run db:migrate:rollback

Next Steps / Future Work

Soroban Multi-Sig Support (Phase 2)

  • Research Soroban multi-signature capabilities
  • Assess feasibility of requiring 2-of-3 admin threshold for critical operations
  • Define admin roles (viewer, approver, admin)
  • Implement authorization layer in contracts

Key Management Documentation

  • Add key rotation playbook for operations
  • Create runbook for key compromise response
  • Document key storage best practices
  • Add monitoring alerts for rotation overdue

Additional Test Coverage

  • Milestone approval rejection flow
  • Evidence validation and rejection
  • Course completion and certification
  • LRN burn and token economics
  • Peer review process

Breaking Changes

None. This is a purely additive change with new endpoints and tables.


Related Issues

  • Security audit finding: Static admin key with no rotation
  • Test coverage gap: Leaderboard functionality
  • Unreliable E2E: Student learning journey critical path

Checklist

  • Database migrations created and tested
  • Admin key rotation service implemented
  • API endpoints documented with OpenAPI
  • Unit tests for Leaderboard added
  • E2E test for enrollment-to-milestone added
  • No breaking changes
  • Security review ready