|
14 | 14 | import org.springframework.web.bind.annotation.*; |
15 | 15 |
|
16 | 16 | import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.HashMap; |
17 | 19 |
|
18 | 20 | @RestController |
19 | 21 | @RequestMapping("/api/v1/policies") |
@@ -59,4 +61,175 @@ public ResponseEntity<?> getPolicy(@PathVariable String policyId) { |
59 | 61 | } |
60 | 62 | return ResponseEntity.ok(policy); |
61 | 63 | } |
| 64 | + |
| 65 | + @PostMapping("/validate") |
| 66 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 67 | + public ResponseEntity<?> validatePolicy(@RequestBody ATPLPolicy policy) { |
| 68 | + try { |
| 69 | + Map<String, Object> validationResult = new HashMap<>(); |
| 70 | + validationResult.put("valid", true); |
| 71 | + validationResult.put("errors", List.of()); |
| 72 | + |
| 73 | + // Basic validation |
| 74 | + if (policy.getPolicyId() == null || policy.getPolicyId().trim().isEmpty()) { |
| 75 | + validationResult.put("valid", false); |
| 76 | + validationResult.put("errors", List.of("Policy ID is required")); |
| 77 | + return ResponseEntity.ok(validationResult); |
| 78 | + } |
| 79 | + |
| 80 | + if (policy.getVersion() == null || policy.getVersion().trim().isEmpty()) { |
| 81 | + validationResult.put("valid", false); |
| 82 | + validationResult.put("errors", List.of("Version is required")); |
| 83 | + return ResponseEntity.ok(validationResult); |
| 84 | + } |
| 85 | + |
| 86 | + if (policy.getCapabilities() == null) { |
| 87 | + validationResult.put("valid", false); |
| 88 | + validationResult.put("errors", List.of("Capabilities section is required")); |
| 89 | + return ResponseEntity.ok(validationResult); |
| 90 | + } |
| 91 | + |
| 92 | + log.info("Policy validation passed for: {}", policy.getPolicyId()); |
| 93 | + return ResponseEntity.ok(validationResult); |
| 94 | + |
| 95 | + } catch (Exception e) { |
| 96 | + log.error("Error validating policy", e); |
| 97 | + Map<String, Object> errorResult = new HashMap<>(); |
| 98 | + errorResult.put("valid", false); |
| 99 | + errorResult.put("errors", List.of("Validation error: " + e.getMessage())); |
| 100 | + return ResponseEntity.ok(errorResult); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @PostMapping("/measure") |
| 105 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 106 | + public ResponseEntity<?> measureAgentCompliance(@RequestBody Map<String, Object> measurementRequest) { |
| 107 | + try { |
| 108 | + String policyId = (String) measurementRequest.get("policy_id"); |
| 109 | + Map<String, Object> agentActivity = (Map<String, Object>) measurementRequest.get("agent_activity"); |
| 110 | + |
| 111 | + if (policyId == null || agentActivity == null) { |
| 112 | + return ResponseEntity.badRequest().body("policy_id and agent_activity are required"); |
| 113 | + } |
| 114 | + |
| 115 | + ATPLPolicy policy = policyService.getPolicy(policyId); |
| 116 | + if (policy == null) { |
| 117 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Policy not found"); |
| 118 | + } |
| 119 | + |
| 120 | + // Measurement engine logic |
| 121 | + Map<String, Object> measurementResult = new HashMap<>(); |
| 122 | + measurementResult.put("policy_id", policyId); |
| 123 | + measurementResult.put("compliance_score", calculateComplianceScore(policy, agentActivity)); |
| 124 | + measurementResult.put("within_bounds", true); // Simplified for now |
| 125 | + measurementResult.put("violations", List.of()); |
| 126 | + measurementResult.put("timestamp", System.currentTimeMillis()); |
| 127 | + |
| 128 | + log.info("Measured agent compliance for policy: {}", policyId); |
| 129 | + return ResponseEntity.ok(measurementResult); |
| 130 | + |
| 131 | + } catch (Exception e) { |
| 132 | + log.error("Error measuring agent compliance", e); |
| 133 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 134 | + .body("Measurement error: " + e.getMessage()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private double calculateComplianceScore(ATPLPolicy policy, Map<String, Object> agentActivity) { |
| 139 | + // Simplified compliance scoring algorithm |
| 140 | + double baseScore = 50.0; |
| 141 | + |
| 142 | + // Check trust score requirements |
| 143 | + if (policy.getTrustScore() != null && policy.getTrustScore().getMinimum() > 0) { |
| 144 | + baseScore += 20.0; |
| 145 | + } |
| 146 | + |
| 147 | + // Check identity requirements |
| 148 | + if (policy.getIdentity() != null) { |
| 149 | + baseScore += 15.0; |
| 150 | + } |
| 151 | + |
| 152 | + // Check capabilities |
| 153 | + if (policy.getCapabilities() != null) { |
| 154 | + baseScore += 15.0; |
| 155 | + } |
| 156 | + |
| 157 | + return Math.min(100.0, baseScore); |
| 158 | + } |
| 159 | + |
| 160 | + @PostMapping("/test-endpoint") |
| 161 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 162 | + public ResponseEntity<?> testEndpointAccess(@RequestBody Map<String, Object> testRequest) { |
| 163 | + try { |
| 164 | + String policyId = (String) testRequest.get("policy_id"); |
| 165 | + String method = (String) testRequest.get("method"); |
| 166 | + String path = (String) testRequest.get("path"); |
| 167 | + |
| 168 | + if (policyId == null || method == null || path == null) { |
| 169 | + return ResponseEntity.badRequest().body("policy_id, method, and path are required"); |
| 170 | + } |
| 171 | + |
| 172 | + ATPLPolicy policy = policyService.getPolicy(policyId); |
| 173 | + if (policy == null) { |
| 174 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Policy not found"); |
| 175 | + } |
| 176 | + |
| 177 | + // Simulate endpoint access test |
| 178 | + Map<String, Object> testResult = new HashMap<>(); |
| 179 | + testResult.put("policy_id", policyId); |
| 180 | + testResult.put("method", method); |
| 181 | + testResult.put("path", path); |
| 182 | + testResult.put("allowed", true); // Simplified - would need actual endpoint matching logic |
| 183 | + testResult.put("reason", "Endpoint access allowed by policy"); |
| 184 | + testResult.put("timestamp", System.currentTimeMillis()); |
| 185 | + |
| 186 | + log.info("Tested endpoint access for policy: {} - {} {}", policyId, method, path); |
| 187 | + return ResponseEntity.ok(testResult); |
| 188 | + |
| 189 | + } catch (Exception e) { |
| 190 | + log.error("Error testing endpoint access", e); |
| 191 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 192 | + .body("Test error: " + e.getMessage()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @GetMapping("/templates") |
| 197 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 198 | + public ResponseEntity<?> getPolicyTemplates() { |
| 199 | + try { |
| 200 | + Map<String, Object> templates = new HashMap<>(); |
| 201 | + |
| 202 | + // Web server template |
| 203 | + Map<String, Object> webServer = new HashMap<>(); |
| 204 | + webServer.put("name", "Web Server Policy"); |
| 205 | + webServer.put("description", "Policy for web servers with basic HTTP access"); |
| 206 | + webServer.put("trust_score_minimum", 70); |
| 207 | + webServer.put("endpoints", List.of( |
| 208 | + Map.of("method", "GET", "path", "/", "action", "allow", "description", "Home page"), |
| 209 | + Map.of("method", "GET", "path", "/static/*", "action", "allow", "description", "Static assets"), |
| 210 | + Map.of("method", "GET", "path", "/health", "action", "allow", "description", "Health check") |
| 211 | + )); |
| 212 | + |
| 213 | + // API service template |
| 214 | + Map<String, Object> apiService = new HashMap<>(); |
| 215 | + apiService.put("name", "API Service Policy"); |
| 216 | + apiService.put("description", "Policy for REST API services"); |
| 217 | + apiService.put("trust_score_minimum", 80); |
| 218 | + apiService.put("endpoints", List.of( |
| 219 | + Map.of("method", "GET", "path", "/api/v1/*", "action", "allow", "description", "API endpoints"), |
| 220 | + Map.of("method", "POST", "path", "/api/v1/*", "action", "allow", "description", "API creation"), |
| 221 | + Map.of("method", "GET", "path", "/docs", "action", "allow", "description", "API documentation") |
| 222 | + )); |
| 223 | + |
| 224 | + templates.put("web-server", webServer); |
| 225 | + templates.put("api-service", apiService); |
| 226 | + |
| 227 | + return ResponseEntity.ok(templates); |
| 228 | + |
| 229 | + } catch (Exception e) { |
| 230 | + log.error("Error getting policy templates", e); |
| 231 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 232 | + .body("Error getting templates: " + e.getMessage()); |
| 233 | + } |
| 234 | + } |
62 | 235 | } |
0 commit comments