|
| 1 | +package io.sentrius.sso.controllers.api; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 5 | +import io.sentrius.sso.core.annotations.LimitAccess; |
| 6 | +import io.sentrius.sso.core.model.security.enums.ApplicationAccessEnum; |
| 7 | +import io.sentrius.sso.core.services.ATPLPolicyService; |
| 8 | +import io.sentrius.sso.core.model.ATPLPolicyEntity; |
| 9 | +import io.sentrius.sso.core.trust.ATPLPolicy; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.web.bind.annotation.*; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +@RestController |
| 19 | +@RequestMapping("/api/v1/policies") |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Slf4j |
| 22 | +public class ATPLPolicyController { |
| 23 | + |
| 24 | + private final ATPLPolicyService policyService; |
| 25 | + private final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); |
| 26 | + |
| 27 | + @PostMapping(consumes = {"application/x-yaml", "application/yaml", "text/yaml", "application/json"}) |
| 28 | + public ResponseEntity<?> uploadPolicy(@RequestBody String rawPolicy) { |
| 29 | + try { |
| 30 | + ATPLPolicy policy = yamlMapper.readValue(rawPolicy, ATPLPolicy.class); |
| 31 | + |
| 32 | + // Optional: Do deeper schema validation or approval here |
| 33 | + if (policy.getPolicyId() == null || policy.getVersion() == null) { |
| 34 | + return ResponseEntity.badRequest().body("Missing required fields: policy_id and version."); |
| 35 | + } |
| 36 | + |
| 37 | + policyService.savePolicy(policy); |
| 38 | + return ResponseEntity.status(HttpStatus.CREATED).body("Policy uploaded successfully."); |
| 39 | + |
| 40 | + } catch (Exception e) { |
| 41 | + log.error("Invalid policy submission", e); |
| 42 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST) |
| 43 | + .body("Invalid policy format: " + e.getMessage()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping |
| 48 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 49 | + public ResponseEntity<List<ATPLPolicyEntity>> listPolicies() { |
| 50 | + return ResponseEntity.ok(policyService.findAll()); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/{policyId}") |
| 54 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 55 | + public ResponseEntity<?> getPolicy(@PathVariable String policyId) { |
| 56 | + ATPLPolicy policy = policyService.getPolicy(policyId); |
| 57 | + if (policy == null) { |
| 58 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Policy not found."); |
| 59 | + } |
| 60 | + return ResponseEntity.ok(policy); |
| 61 | + } |
| 62 | +} |
0 commit comments