-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/min io #10
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
Feature/min io #10
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3680aaa
feat: Add API key authentication filter for admin endpoints
Yoo-SH b0903e2
feat: Add MinIO configuration for object storage
Yoo-SH e6e7115
feat: Integrate API key authentication filter into security config
Yoo-SH 670bbae
feat: Add DTOs for source document API responses
Yoo-SH 0fcdb27
feat: Add core services for document management
Yoo-SH 2d75806
feat: Add source document REST API controllers
Yoo-SH a7719fe
feat: Enhance CommonResponse with error handling support
Yoo-SH e0dd7d9
feat: Add comprehensive error code definitions
Yoo-SH 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
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
92 changes: 92 additions & 0 deletions
92
core/src/main/java/com/opencontext/config/ApiKeyAuthenticationFilter.java
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,92 @@ | ||
| package com.opencontext.config; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.opencontext.common.CommonResponse; | ||
| import com.opencontext.enums.ErrorCode; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Filter for API Key authentication on admin endpoints. | ||
| * | ||
| * This filter validates the X-API-KEY header for endpoints that require | ||
| * admin access, specifically the document management APIs. | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class ApiKeyAuthenticationFilter extends OncePerRequestFilter { | ||
|
|
||
| private final ObjectMapper objectMapper; | ||
|
|
||
| @Value("${opencontext.api.key:dev-api-key-123}") | ||
| private String validApiKey; | ||
|
|
||
| // Endpoints that require API Key authentication | ||
| private static final List<String> PROTECTED_ENDPOINTS = Arrays.asList( | ||
| "/api/v1/sources" | ||
| ); | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain) throws ServletException, IOException { | ||
|
|
||
| String requestPath = request.getRequestURI(); | ||
|
|
||
| // Check if this endpoint requires API Key authentication | ||
| boolean requiresAuth = PROTECTED_ENDPOINTS.stream() | ||
| .anyMatch(requestPath::startsWith); | ||
|
|
||
| if (requiresAuth) { | ||
| String apiKey = request.getHeader("X-API-KEY"); | ||
|
|
||
| if (!StringUtils.hasText(apiKey)) { | ||
| log.warn("API Key missing for protected endpoint: {}", requestPath); | ||
| sendErrorResponse(response, ErrorCode.INSUFFICIENT_PERMISSION, | ||
| "API Key is required. Please provide X-API-KEY header."); | ||
| return; | ||
| } | ||
|
|
||
| if (!validApiKey.equals(apiKey)) { | ||
| log.warn("Invalid API Key provided for endpoint: {}", requestPath); | ||
| sendErrorResponse(response, ErrorCode.INSUFFICIENT_PERMISSION, | ||
| "Invalid API Key provided."); | ||
| return; | ||
| } | ||
|
|
||
| log.debug("API Key authentication successful for endpoint: {}", requestPath); | ||
| } | ||
|
|
||
| filterChain.doFilter(request, response); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an error response in JSON format. | ||
| */ | ||
| private void sendErrorResponse(HttpServletResponse response, ErrorCode errorCode, String message) | ||
| throws IOException { | ||
|
|
||
| response.setStatus(errorCode.getHttpStatus().value()); | ||
| response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
| response.setCharacterEncoding("UTF-8"); | ||
|
|
||
| CommonResponse<Void> errorResponse = CommonResponse.error(errorCode, message); | ||
|
|
||
| String jsonResponse = objectMapper.writeValueAsString(errorResponse); | ||
| response.getWriter().write(jsonResponse); | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
core/src/main/java/com/opencontext/config/MinIOConfig.java
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,41 @@ | ||
| package com.opencontext.config; | ||
|
|
||
| import io.minio.MinioClient; | ||
| import lombok.Data; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** | ||
| * MinIO client configuration for object storage operations. | ||
| * | ||
| * This configuration creates a MinIO client bean that can be used throughout | ||
| * the application for file upload, download, and management operations. | ||
| */ | ||
| @Slf4j | ||
| @Data | ||
| @Configuration | ||
| @ConfigurationProperties(prefix = "minio") | ||
| public class MinIOConfig { | ||
|
|
||
| private String endpoint; | ||
| private String accessKey; | ||
| private String secretKey; | ||
| private String bucketName; | ||
|
|
||
| /** | ||
| * Creates and configures the MinIO client bean. | ||
| * | ||
| * @return configured MinioClient instance | ||
| */ | ||
| @Bean | ||
| public MinioClient minioClient() { | ||
| log.info("Initializing MinIO client with endpoint: {}", endpoint); | ||
|
|
||
| return MinioClient.builder() | ||
| .endpoint(endpoint) | ||
| .credentials(accessKey, secretKey) | ||
| .build(); | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default API key 'dev-api-key-123' is weak and predictable. Consider using a stronger default or requiring the API key to be explicitly configured in production environments.