Pull request #564: RED-5293 A

Merge in RED/persistence-service from RED-5293-A to master

* commit '5a47fb5c214d23d4fad98270ee7abe7e9ac34b4b':
  RED-5293: adjusted ControllerAdvice for some SQLExceptions
  RED-5293: added catch block for URISyntaxExceptions
  RED-5293: jdbcUrl is checked for protocol and sql
  RED-5293: jdbcUrl is checked for protocol and sql
  RED-5293: fixed jdbcUrl-check by adding more checks; removed URL-check because it does not work
  RED-5293: fixed jdbcUrl-check by adding more checks
  RED-5293: fixed jdbcUrl-check by adding more checks
  RED-5293: fixed jdbcUrl-check by adding URL check
  RED-5293: fixed jdbcUrl-check by adding URL check
  RED-5293: fixed jdbcUrl-check by adding URL check
  RED-5293: fixed jdbcUrl-check by adding URL check
This commit is contained in:
Ali Oezyetimoglu 2022-11-11 12:54:23 +01:00
commit fd2aa4e876
2 changed files with 16 additions and 2 deletions

View File

@ -90,6 +90,8 @@ public class ControllerAdvice {
if (e.getMessage().contains("violates unique constraint")) {
return new ResponseEntity<>(new ErrorMessage(OffsetDateTime.now(), "Unique constraint violation"), HttpStatus.CONFLICT);
} else if (e.getMessage().contains("No suitable driver found")){
return new ResponseEntity<>(new ErrorMessage(OffsetDateTime.now(), "JDBC URL is incorrect"), HttpStatus.BAD_REQUEST);
} else {
log.error("PLSQL Exception occurred: {}", e.getMessage(), e);
return new ResponseEntity<>(new ErrorMessage(OffsetDateTime.now(), "SQL Exception"), HttpStatus.INTERNAL_SERVER_ERROR);

View File

@ -1,6 +1,7 @@
package com.iqser.red.service.peristence.v1.server.service;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
@ -36,6 +37,7 @@ import lombok.extern.slf4j.Slf4j;
@EnableConfigurationProperties(LiquibaseProperties.class)
public class TenantManagementService {
private static final Set<String> SUPPORTED_DATABASES = Set.of("postgresql");
private static final Set<String> SQL_CONNECTION_ERROR_CODES = Set.of(
// connection_exception
"08000",
@ -131,8 +133,18 @@ public class TenantManagementService {
@SneakyThrows
private void validateJdbcUrl(String jdbcUrl) {
// just create a URI object to check if the string is a valid URI
new URI(jdbcUrl);
try {
// just create a URI object to check if the string is a valid URI
var uri = new URI(jdbcUrl);
var subUri = new URI(uri.getSchemeSpecificPart());
if (uri.getScheme() == null || subUri.getScheme() == null || !uri.getScheme().equals("jdbc") || !SUPPORTED_DATABASES.contains(subUri.getScheme())) {
throw new IllegalArgumentException("Your jdbcUrl is not valid.");
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Your jdbcUrl is not valid.", e);
}
}