-
Notifications
You must be signed in to change notification settings - Fork 607
feat: implement simple api key auth with TODOs to replace after demo #4010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,4 +24,5 @@ dist | |
| .secrets.json | ||
| certs/ | ||
| deployment/data/* | ||
| metald.db | ||
| bin/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Environment variables with secrets | ||
| .env | ||
| .env.local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "connectrpc.com/connect" | ||
| ) | ||
|
|
||
| // AuthConfig contains configuration for the authentication middleware | ||
| type AuthConfig struct { | ||
| // APIKey is the expected API key for authentication | ||
| APIKey string | ||
| } | ||
|
|
||
| // AuthMiddleware provides simple API key authentication | ||
| // TODO: Replace with JWT authentication when moving to private IP | ||
| type AuthMiddleware struct { | ||
| config AuthConfig | ||
| } | ||
|
|
||
| // NewAuthMiddleware creates a new authentication middleware | ||
| func NewAuthMiddleware(config AuthConfig) *AuthMiddleware { | ||
| return &AuthMiddleware{ | ||
| config: config, | ||
| } | ||
| } | ||
|
|
||
| // ConnectInterceptor returns a Connect interceptor for gRPC-like services | ||
| func (m *AuthMiddleware) ConnectInterceptor() connect.UnaryInterceptorFunc { | ||
mcstepp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return func(next connect.UnaryFunc) connect.UnaryFunc { | ||
| return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { | ||
| procedure := req.Spec().Procedure | ||
|
|
||
| // Skip authentication for health check endpoint | ||
| if procedure == "/ctrl.v1.CtrlService/Liveness" { | ||
| return next(ctx, req) | ||
| } | ||
|
|
||
mcstepp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Extract API key from Authorization header | ||
| // TODO: Replace with JWT token extraction when moving to private IP | ||
| authHeader := strings.TrimSpace(req.Header().Get("Authorization")) | ||
| if authHeader == "" { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("Missing Authorization header")) | ||
| } | ||
|
|
||
| // Parse authorization header with case-insensitive Bearer scheme | ||
| const bearerScheme = "bearer" | ||
| if len(authHeader) < len(bearerScheme)+1 { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("Invalid Authorization header format. Expected: Bearer <api_key>")) | ||
| } | ||
|
|
||
| // Extract scheme and check case-insensitively | ||
| schemePart := strings.ToLower(authHeader[:len(bearerScheme)]) | ||
| if schemePart != bearerScheme { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("Invalid Authorization header format. Expected: Bearer <api_key>")) | ||
| } | ||
|
|
||
| // Ensure there's a space after the scheme | ||
| if authHeader[len(bearerScheme)] != ' ' { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("Invalid Authorization header format. Expected: Bearer <api_key>")) | ||
| } | ||
|
|
||
| // Extract and trim the token | ||
| apiKey := strings.TrimSpace(authHeader[len(bearerScheme)+1:]) | ||
| if apiKey == "" { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("API key cannot be empty")) | ||
| } | ||
|
|
||
| // Simple API key validation against environment variable | ||
| // TODO: Replace with JWT validation when moving to private IP | ||
| if apiKey != m.config.APIKey { | ||
| return nil, connect.NewError(connect.CodeUnauthenticated, | ||
| fmt.Errorf("Invalid API key")) | ||
| } | ||
|
|
||
| // Continue to next handler | ||
| return next(ctx, req) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.