136 lines
5.7 KiB
Java
136 lines
5.7 KiB
Java
package com.hithomelabs.CFTunnels.Controllers;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.hithomelabs.CFTunnels.Config.AuthoritiesToGroupMapping;
|
|
import com.hithomelabs.CFTunnels.Config.CloudflareConfig;
|
|
import com.hithomelabs.CFTunnels.Config.RestTemplateConfig;
|
|
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
|
import com.hithomelabs.CFTunnels.Models.Config;
|
|
import com.hithomelabs.CFTunnels.Models.Ingress;
|
|
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
|
|
import com.hithomelabs.CFTunnels.Services.CloudflareAPIService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
|
import org.springframework.http.*;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/cloudflare")
|
|
public class TunnelController implements ErrorController {
|
|
|
|
private final RestTemplate restTemplate = new RestTemplate();
|
|
private static final String ERROR_PATH = "/error";
|
|
|
|
@Autowired
|
|
private AuthoritiesToGroupMapping authoritiesToGroupMapping;
|
|
@Autowired
|
|
private CloudflareConfig cloudflareConfig;
|
|
|
|
@Autowired
|
|
private AuthKeyEmailHeader authKeyEmailHeader;
|
|
|
|
@Autowired
|
|
private RestTemplateConfig restTemplateConfig;
|
|
|
|
@Autowired
|
|
CloudflareAPIService cloudflareAPIService;
|
|
|
|
@PreAuthorize("hasAnyRole('USER')")
|
|
@GetMapping("/whoami")
|
|
public Map<String,Object> whoAmI(@AuthenticationPrincipal OidcUser oidcUser) {
|
|
|
|
List<String> authorities = oidcUser.getAuthorities().stream()
|
|
.map(GrantedAuthority::getAuthority)
|
|
.toList();
|
|
return Map.of(
|
|
"username", oidcUser.getPreferredUsername(),
|
|
"roles", authorities
|
|
);
|
|
}
|
|
|
|
@PreAuthorize("hasAnyRole('USER')")
|
|
@GetMapping("/tunnels")
|
|
public ResponseEntity<Map<String,Object>> getTunnels(){
|
|
|
|
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnels();
|
|
Map<String, Object> jsonResponse = new HashMap<>();
|
|
jsonResponse.put("status", "success");
|
|
jsonResponse.put("data", responseEntity.getBody());
|
|
|
|
return ResponseEntity.ok(jsonResponse);
|
|
}
|
|
|
|
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
|
@GetMapping("/tunnel/{tunnelId}")
|
|
public ResponseEntity<Map<String,Object>> getTunnelConfigurations(@PathVariable String tunnelId) {
|
|
|
|
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplate, Map.class);
|
|
Map<String, Object> jsonResponse = new HashMap<>();
|
|
jsonResponse.put("status", "success");
|
|
jsonResponse.put("data", responseEntity.getBody());
|
|
|
|
return ResponseEntity.ok(jsonResponse);
|
|
}
|
|
|
|
// 50df9101-f625-4618-b7c5-100338a57124
|
|
@PreAuthorize("hasAnyRole('ADMIN')")
|
|
@PutMapping("/tunnel/{tunnelId}/add")
|
|
public ResponseEntity<Map<String, Object>> addTunnelconfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
|
|
|
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
|
|
|
// * * Inserting new ingress value at second-to last position in list
|
|
Config config = responseEntity.getBody().getResult().getConfig();
|
|
List<Ingress> response_ingress = config.getIngress();
|
|
response_ingress.add(response_ingress.size()-1, ingress);
|
|
|
|
// * * Hitting put endpoint
|
|
ResponseEntity<TunnelResponse> response = cloudflareAPIService.putCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class, config);
|
|
|
|
// * * Displaying response
|
|
Map<String, Object> jsonResponse = new HashMap<>();
|
|
jsonResponse.put("status", response.getStatusCode().toString());
|
|
jsonResponse.put("data", response.getBody());
|
|
|
|
return ResponseEntity.ok(jsonResponse);
|
|
}
|
|
|
|
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
|
@PutMapping("/tunnel/{tunnelId}/delete")
|
|
public ResponseEntity<Map<String, Object>> deleteTunnelConfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
|
|
|
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
|
|
|
// * * Deleting the selected ingress value
|
|
Config config = responseEntity.getBody().getResult().getConfig();
|
|
List<Ingress> response_ingress = config.getIngress();
|
|
Boolean result = Ingress.deleteByHostName(response_ingress, ingress.getHostname());
|
|
|
|
// * * Hitting put endpoint
|
|
ResponseEntity<TunnelResponse> response = cloudflareAPIService.putCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class, config);
|
|
|
|
// * * Displaying response
|
|
Map<String, Object> jsonResponse = new HashMap<>();
|
|
|
|
if (result){
|
|
jsonResponse.put("status", response.getStatusCode().toString());
|
|
jsonResponse.put("data", response.getBody());
|
|
}
|
|
else{
|
|
jsonResponse.put("status", HttpStatus.CONFLICT);
|
|
jsonResponse.put("data", "Conflict: the resource to delete, does not exist");
|
|
}
|
|
|
|
return ResponseEntity.ok(jsonResponse);
|
|
}
|
|
}
|