-
Notifications
You must be signed in to change notification settings - Fork 0
Add database setup, integrate key wrappinig, repositories, models and global auth #3
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
4 commits
Select commit
Hold shift + click to select a range
e1f4f5c
Integrate wrapping with key generation, add key rotation, update mode…
DimitrijeG c8f274c
Add mapper, security config, docker compose and update app properties
DimitrijeG 8e3c36b
Add ssl, users, sql init script, jwt auth and rbac
DimitrijeG 7cd0b24
Additionaly restrict session policy and headers list
DimitrijeG 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
15 changes: 15 additions & 0 deletions
15
MiniKms/src/main/java/ftn/security/minikms/config/JsonDeserializationConfig.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,15 @@ | ||
| package ftn.security.minikms.config; | ||
|
|
||
| import com.fasterxml.jackson.databind.MapperFeature; | ||
| import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class JsonDeserializationConfig { | ||
| @Bean | ||
| public Jackson2ObjectMapperBuilderCustomizer caseInsensitiveEnums() { | ||
| return builder -> builder | ||
| .featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
MiniKms/src/main/java/ftn/security/minikms/config/SecurityConfig.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,91 @@ | ||
| package ftn.security.minikms.config; | ||
|
|
||
| import ftn.security.minikms.service.auth.JwtAuthenticationFilter; | ||
| import ftn.security.minikms.service.auth.MiniKmsUserDetailsService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.config.Customizer; | ||
| import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
| import org.springframework.security.config.http.SessionCreationPolicy; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
| import org.springframework.web.cors.CorsConfiguration; | ||
| import org.springframework.web.cors.CorsConfigurationSource; | ||
| import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| public class SecurityConfig { | ||
| private final String jwtSecret; | ||
| private final MiniKmsUserDetailsService service; | ||
|
|
||
| @Autowired | ||
| public SecurityConfig( | ||
| @Value("${jwt.secret}") String jwtSecret, | ||
| MiniKmsUserDetailsService service) { | ||
| this.jwtSecret = jwtSecret; | ||
| this.service = service; | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
| return http | ||
| // Stateless, token-only API: no cookies => CSRF not applicable | ||
| .csrf(AbstractHttpConfigurer::disable) | ||
| .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
| .cors(Customizer.withDefaults()) | ||
|
|
||
| .formLogin(AbstractHttpConfigurer::disable) | ||
| .httpBasic(AbstractHttpConfigurer::disable) | ||
| .logout(AbstractHttpConfigurer::disable) | ||
|
|
||
| .authorizeHttpRequests(auth -> auth | ||
| .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() | ||
| .requestMatchers("/api/v1/auth/**").permitAll() | ||
| .requestMatchers(HttpMethod.GET, "/api/v1/keys/**").authenticated() // Allow all roles to GET | ||
| .requestMatchers("/api/v1/keys/**").hasRole("MANAGER") | ||
| .anyRequest().authenticated() | ||
| ) | ||
|
|
||
| .userDetailsService(service) | ||
| .addFilterBefore(new JwtAuthenticationFilter( | ||
| jwtSecret, | ||
| service | ||
| ), UsernamePasswordAuthenticationFilter.class) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
|
|
||
| @Bean | ||
| public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { | ||
| return configuration.getAuthenticationManager(); | ||
| } | ||
|
|
||
| @Bean | ||
| public CorsConfigurationSource corsConfigurationSource() { | ||
| var config = new CorsConfiguration(); | ||
| config.setAllowedOrigins(List.of("http://localhost:4200", "http://localhost")); | ||
| config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); | ||
| config.setAllowedHeaders(List.of("Authorization", "Content-Type")); | ||
| config.setAllowCredentials(false); | ||
|
|
||
| var source = new UrlBasedCorsConfigurationSource(); | ||
| source.registerCorsConfiguration("/**", config); | ||
| return source; | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
MiniKms/src/main/java/ftn/security/minikms/controller/AuthController.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,40 @@ | ||
| package ftn.security.minikms.controller; | ||
|
|
||
| import ftn.security.minikms.dto.AuthDTO; | ||
| import ftn.security.minikms.dto.TokenDTO; | ||
| import ftn.security.minikms.service.auth.JwtService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.AuthenticationException; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/auth") | ||
| public class AuthController { | ||
| private final AuthenticationManager authManager; | ||
| private final JwtService jwtService; | ||
|
|
||
| @Autowired | ||
| public AuthController(AuthenticationManager authManager, JwtService jwtService) { | ||
| this.authManager = authManager; | ||
| this.jwtService = jwtService; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<?> auth(@RequestBody AuthDTO dto) { | ||
| try { | ||
| var auth = authManager.authenticate( | ||
| new UsernamePasswordAuthenticationToken(dto.getUsername(), dto.getPassword()) | ||
| ); | ||
| var token = jwtService.generateToken(auth.getName()); | ||
| return ResponseEntity.ok(new TokenDTO(token)); | ||
| } catch (AuthenticationException e) { | ||
| return ResponseEntity.status(401).body("Invalid credentials"); | ||
| } | ||
| } | ||
| } |
57 changes: 43 additions & 14 deletions
57
MiniKms/src/main/java/ftn/security/minikms/controller/KeyManagementController.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 |
|---|---|---|
| @@ -1,35 +1,64 @@ | ||
| package ftn.security.minikms.controller; | ||
|
|
||
| import ftn.security.minikms.dto.KeyDTO; | ||
| import ftn.security.minikms.dto.KeyMapper; | ||
| import ftn.security.minikms.service.KeyService; | ||
| import org.mapstruct.factory.Mappers; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.GeneralSecurityException; | ||
| import java.security.InvalidParameterException; | ||
| import java.security.Principal; | ||
| import java.util.UUID; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/api/v1/keys") | ||
| public class KeyManagementController { | ||
| private final KeyService keyService; | ||
| private final KeyMapper mapper; | ||
|
|
||
| @Autowired | ||
| private KeyService keyService; | ||
| public KeyManagementController(KeyService keyService) { | ||
| this.keyService = keyService; | ||
| this.mapper = Mappers.getMapper(KeyMapper.class); | ||
| } | ||
|
|
||
| @PostMapping("/create") | ||
| public ResponseEntity<KeyDTO> createKey(@RequestBody KeyDTO dto) throws NoSuchAlgorithmException { | ||
| String id = keyService.createKey(dto.getKeyType()); | ||
| dto.setId(id); | ||
| return new ResponseEntity<>(dto, HttpStatus.CREATED); | ||
| public ResponseEntity<?> createKey(@RequestBody KeyDTO dto, Principal principal) throws GeneralSecurityException { | ||
| var username = principal.getName(); | ||
|
|
||
| try { | ||
| var created = keyService.createKey(dto.getAlias(), dto.getKeyType(), dto.getAllowedOperations(), username); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(mapper.toDto(created)); | ||
| } catch (InvalidParameterException e) { | ||
| return ResponseEntity.badRequest().body(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @PostMapping("/rotate") | ||
| public ResponseEntity<KeyDTO> rotateKey(@RequestBody KeyDTO dto){ | ||
| keyService.rotateKey(dto.getKeyType(), dto.getId()); | ||
| return new ResponseEntity<>(dto, HttpStatus.CREATED); | ||
| } | ||
| @PutMapping("/delete/{id}") | ||
| public ResponseEntity<String> deleteKey(@PathVariable String id){ | ||
| keyService.deleteKey(id); | ||
| return new ResponseEntity<>(id, HttpStatus.OK); | ||
| public ResponseEntity<?> rotateKey(@RequestBody KeyDTO dto, Principal principal) throws GeneralSecurityException { | ||
| var username = principal.getName(); | ||
|
|
||
| try { | ||
| var created = keyService.rotateKey(dto.getId(), username); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(mapper.toDto(created)); | ||
| } catch (InvalidParameterException e) { | ||
| return ResponseEntity.badRequest().body(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<?> deleteKey(@PathVariable UUID id, Principal principal) { | ||
| var username = principal.getName(); | ||
|
|
||
| try { | ||
| keyService.deleteKey(id, username); | ||
| return ResponseEntity.noContent().build(); | ||
| } catch (InvalidParameterException e) { | ||
| return ResponseEntity.badRequest().body(e.getMessage()); | ||
| } | ||
| } | ||
| } |
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,9 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class AuthDTO { | ||
| private String username; | ||
| private String password; | ||
| } |
19 changes: 10 additions & 9 deletions
19
MiniKms/src/main/java/ftn/security/minikms/dto/KeyDTO.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 |
|---|---|---|
| @@ -1,16 +1,17 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import ftn.security.minikms.enumeration.KeyOperation; | ||
| import ftn.security.minikms.enumeration.KeyType; | ||
| import lombok.*; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class KeyDTO { | ||
| private String id; | ||
| private UUID id; | ||
| private String alias; | ||
| private String keyType; | ||
| private KeyType keyType; | ||
| private List<KeyOperation> allowedOperations; | ||
| } |
9 changes: 9 additions & 0 deletions
9
MiniKms/src/main/java/ftn/security/minikms/dto/KeyMapper.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,9 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import ftn.security.minikms.entity.KeyMetadata; | ||
| import org.mapstruct.Mapper; | ||
|
|
||
| @Mapper | ||
| public interface KeyMapper { | ||
| KeyMetadataDTO toDto(KeyMetadata key); | ||
| } |
21 changes: 21 additions & 0 deletions
21
MiniKms/src/main/java/ftn/security/minikms/dto/KeyMetadataDTO.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,21 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import ftn.security.minikms.enumeration.KeyOperation; | ||
| import ftn.security.minikms.enumeration.KeyType; | ||
| import lombok.*; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| public class KeyMetadataDTO { | ||
| private UUID id; | ||
| private String alias; | ||
| private Integer primaryVersion; | ||
| private KeyType keyType; | ||
| private List<KeyOperation> allowedOperations; | ||
| private Instant createdAt; | ||
| private Instant rotatedAt; | ||
| } |
10 changes: 10 additions & 0 deletions
10
MiniKms/src/main/java/ftn/security/minikms/dto/TokenDTO.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,10 @@ | ||
| package ftn.security.minikms.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class TokenDTO { | ||
| private String token; | ||
| } |
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.