feat(api): implement JWT authentication for data collectors#24
feat(api): implement JWT authentication for data collectors#24shark0F0497 merged 3 commits intomainfrom
Conversation
| var passwordHash sql.NullString | ||
| password := req.Password | ||
| if password == "" { | ||
| password = "123456" |
There was a problem hiding this comment.
CRITICAL: Hardcoded default password "123456" is a severe security vulnerability
Using a predictable default password allows attackers to easily gain unauthorized access to any data collector account created without an explicit password. This violates security best practices.
Recommendations:
- Require password to be explicitly provided during creation
- Or generate a secure random password and return it in the response
- At minimum, use a cryptographically secure random default that's unique per account
| password = "123456" | |
| password = generateSecureRandomPassword() |
Alternatively, require password and remove the default entirely.
There was a problem hiding this comment.
Just ignore that.
| return | ||
| } | ||
|
|
||
| _, _ = h.db.Exec("UPDATE data_collectors SET last_login_at = NOW() WHERE id = ?", row.ID) |
There was a problem hiding this comment.
WARNING: Database update error is silently ignored
The last_login_at update failure is silently swallowed. While this is a non-critical operation, silently ignoring errors can mask underlying database issues.
| _, _ = h.db.Exec("UPDATE data_collectors SET last_login_at = NOW() WHERE id = ?", row.ID) | |
| if _, err := h.db.Exec("UPDATE data_collectors SET last_login_at = NOW() WHERE id = ?", row.ID); err != nil { | |
| logger.Printf("[Auth] Failed to update last_login_at for collector %d: %v", row.ID, err) | |
| } |
At minimum, log the error for observability.
There was a problem hiding this comment.
Just ignore that
Code Review SummaryStatus: 3 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)CRITICAL
WARNING
Files Reviewed (7 files)
SummaryThis PR implements JWT-based authentication for data collectors. The implementation is well-structured overall, but has critical security issues that should be addressed before merging. Key concerns:
Positive observations:
|
…ensitive fields in request and response DTOs
| Storage StorageConfig | ||
| QA QAConfig | ||
| Sync SyncConfig | ||
| Auth AuthConfig |
There was a problem hiding this comment.
CRITICAL: JWT secret validation was removed from Validate() function
The previous validation if c.Auth.JWTSecret == "" && !c.Auth.AllowNoAuthOnDev was removed from the Validate() function. Without this check, a production deployment could start with an empty JWT secret, allowing anyone to forge valid authentication tokens.
Either restore the validation or add a new check that ensures JWTSecret is non-empty when the application needs authentication.
There was a problem hiding this comment.
Just Ignore that
Pull Request Checklist
Please ensure your PR meets the following requirements:
Summary
This PR implements JWT-based authentication for data collectors, enabling secure login, token-based session management, and protected API endpoints.
Motivation
Data collectors need a secure, standardized authentication mechanism to access the Keystone Edge API. This feature introduces:
Changes
Modified Files
go.mod- Addedgithub.com/golang-jwt/jwt/v5dependencygo.sum- Updated dependency checksumsinternal/api/handlers/batch.go- Added transfer count to batch response via JOINinternal/api/handlers/data_collector.go- Added password hashing support on create/update operationsinternal/config/config.go- AddedAuthConfigstruct and environment variable loadinginternal/config/config_test.go- Updated tests to includeAuthConfiginternal/server/server.go- IntegratedAuthHandlerand registered auth routesinternal/storage/database/migrations/000001_initial_schema.up.sql- Addedpassword_hashandlast_login_atcolumnsAdded Files
internal/api/handlers/auth.go- Authentication handler with login, logout, and me endpointsinternal/auth/claims.go- JWT claims types and constructorsinternal/auth/jwt.go- JWT generation and parsing utilitiesinternal/middleware/jwt_auth.go- JWT authentication middlewareType of Change
Impact Analysis
Breaking Changes
None - existing functionality is preserved.
Backward Compatibility
Fully backward compatible. The new
password_hashandlast_login_atcolumns are nullable and won't affect existing deployments.Testing
Test Environment
Development environment with MySQL database.
Test Cases
Manual Testing Steps
/api/v1/data-collectors/api/v1/auth/login/api/v1/auth/mewith Bearer token to verify authentication/api/v1/auth/logoutTest Coverage
Screenshots / Recordings
Performance Impact
Documentation
Related Issues
Additional Notes
Environment Variables
The following environment variables are used:
KEYSTONE_JWT_SECRETKEYSTONE_JWT_ISSUERkeystone-edgeKEYSTONE_JWT_EXPIRY_HOURS24KEYSTONE_AUTH_ALLOW_NO_AUTH_ON_DEVfalseAPI Endpoints
/api/v1/auth/login/api/v1/auth/logout/api/v1/auth/meReviewers
@kilo-code-bot
Notes for Reviewers
Checklist for Reviewers