Skip to content

Commit

Permalink
fix sonar finding (#2592)
Browse files Browse the repository at this point in the history
* fix sonar finding

first only the found smell
https://sonarcloud.io/project/issues?impactSeverities=HIGH&resolved=false&sinceLeakPeriod=true&types=CODE_SMELL&id=cloudfoundry-identity-parent

later also implemented recommendations from sonar linter

* check null
  • Loading branch information
strehle authored Nov 8, 2023
1 parent 12ed027 commit 77ad0fd
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class MultitenantJdbcClientDetailsService extends MultitenantClientServic
"update oauth_client_details set ";

private static final String DEFAULT_UPDATE_STATEMENT =
BASE_UPDATE_STATEMENT + CLIENT_FIELDS_FOR_UPDATE.replaceAll(", ", "=?, ")
BASE_UPDATE_STATEMENT + CLIENT_FIELDS_FOR_UPDATE.replace(", ", "=?, ")
+ "=? where client_id = ? and identity_zone_id = ?";

private static final String DEFAULT_UPDATE_SECRET_STATEMENT =
Expand All @@ -103,6 +103,7 @@ public class MultitenantJdbcClientDetailsService extends MultitenantClientServic

private static final String DELETE_CLIENTS_BY_ZONE =
"delete from oauth_client_details where identity_zone_id = ?";
private static final String NO_CLIENT_FOUND_WITH_ID = "No client found with id = ";

private RowMapper<ClientDetails> rowMapper = new ClientDetailsRowMapper();

Expand Down Expand Up @@ -147,33 +148,30 @@ public void addClientDetails(ClientDetails clientDetails, String zoneId) throws

private boolean exists(String clientId, String zoneId) {
List<String> idResults = jdbcTemplate.queryForList(SINGLE_SELECT_STATEMENT, String.class, clientId, zoneId);
if (idResults != null && idResults.size() == 1) {
return true;
}
return false;
return idResults != null && idResults.size() == 1;
}

@Override
public void updateClientDetails(ClientDetails clientDetails, String zoneId) throws NoSuchClientException {
int count = jdbcTemplate.update(DEFAULT_UPDATE_STATEMENT, getFieldsForUpdate(clientDetails, zoneId));
if (count != 1) {
throw new NoSuchClientException("No client found with id = " + clientDetails.getClientId() + " in identity zone id=" + zoneId);
throw new NoSuchClientException(NO_CLIENT_FOUND_WITH_ID + clientDetails.getClientId() + " in identity zone id=" + zoneId);
}
}

@Override
public void updateClientSecret(String clientId, String secret, String zoneId) throws NoSuchClientException {
int count = jdbcTemplate.update(DEFAULT_UPDATE_SECRET_STATEMENT, secret != null ? passwordEncoder.encode(secret) : null, clientId, zoneId);
if (count != 1) {
throw new NoSuchClientException("No client found with id = " + clientId);
throw new NoSuchClientException(NO_CLIENT_FOUND_WITH_ID + clientId);
}
}

@Override
public void updateClientJwtConfig(String clientId, String keyConfig, String zoneId) throws NoSuchClientException {
int count = jdbcTemplate.update(DEFAULT_UPDATE_CLIENT_JWT_CONFIG_STATEMENT, keyConfig, clientId, zoneId);
if (count != 1) {
throw new NoSuchClientException("No client found with id = " + clientId);
throw new NoSuchClientException(NO_CLIENT_FOUND_WITH_ID + clientId);
}
}

Expand Down Expand Up @@ -201,7 +199,7 @@ private Object[] getInsertClientDetailsFields(ClientDetails clientDetails, Strin

private Object[] getFieldsForUpdate(ClientDetails clientDetails, String zoneId) {

Map<String, Object> additionalInformation = new HashMap(clientDetails.getAdditionalInformation());
Map<String, Object> additionalInformation = new HashMap<>(clientDetails.getAdditionalInformation());
Collection<String> requiredGroups = (Collection<String>) additionalInformation.remove(REQUIRED_USER_GROUPS);

String json;
Expand Down Expand Up @@ -242,7 +240,7 @@ private String getAutoApproveScopes(ClientDetails clientDetails) {
if (clientDetails.isAutoApprove("true")) {
return "true"; // all scopes autoapproved
}
Set<String> scopes = new HashSet<String>();
Set<String> scopes = new HashSet<>();
for (String scope : clientDetails.getScope()) {
if (clientDetails.isAutoApprove(scope)) {
scopes.add(scope);
Expand All @@ -260,7 +258,7 @@ public int deleteByIdentityZone(String zoneId) {
public int deleteByClient(String clientId, String zoneId) {
int count = jdbcTemplate.update(DEFAULT_DELETE_STATEMENT, clientId, zoneId);
if (count == 0) {
throw new NoSuchClientException("No client found with id = " + clientId);
throw new NoSuchClientException(NO_CLIENT_FOUND_WITH_ID + clientId);
}
return count;
}
Expand All @@ -279,7 +277,7 @@ public void addClientSecret(String clientId, String newSecret, String zoneId) th
.append(encodedNewSecret);
int count = jdbcTemplate.update(DEFAULT_UPDATE_SECRET_STATEMENT, newSecretBuilder.toString(), clientId, zoneId);
if (count != 1) {
throw new NoSuchClientException("No client found with id = " + clientId);
throw new NoSuchClientException(NO_CLIENT_FOUND_WITH_ID + clientId);
}
}

Expand Down Expand Up @@ -385,7 +383,7 @@ public ClientDetails mapRow(ResultSet rs, int rowNum) throws SQLException {

//required_user_groups
String requiredUserGroups = rs.getString(14);
if (StringUtils.isEmpty(requiredUserGroups)) {
if (!StringUtils.hasLength(requiredUserGroups)) {
details.addAdditionalInformation(REQUIRED_USER_GROUPS, emptySet());
} else {
details.addAdditionalInformation(REQUIRED_USER_GROUPS, commaDelimitedListToSet(requiredUserGroups));
Expand Down

0 comments on commit 77ad0fd

Please sign in to comment.