Skip to content

Commit

Permalink
Fix Sonar Issues
Browse files Browse the repository at this point in the history
Signed-off-by: Duane May <duane.may@broadcom.com>
  • Loading branch information
duanemay committed Oct 18, 2024
1 parent f105d8f commit f65c09e
Show file tree
Hide file tree
Showing 23 changed files with 337 additions and 633 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Serial;
import java.util.Date;
import java.util.Map;

public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();

public static String writeValueAsString(Object object) throws JsonUtilException {
try {
Expand Down Expand Up @@ -68,7 +67,7 @@ public static Map<String, Object> readValueAsMap(final String input) {

public static <T> T readValue(byte[] data, Class<T> clazz) throws JsonUtilException {
try {
if (data!=null && data.length>0) {
if (data != null && data.length > 0) {
return objectMapper.readValue(data, clazz);
} else {
return null;
Expand All @@ -92,7 +91,7 @@ public static <T> T readValue(String s, TypeReference<T> typeReference) {

public static <T> T readValue(byte[] data, TypeReference<T> typeReference) {
try {
if (data!=null && data.length>0) {
if (data != null && data.length > 0) {
return objectMapper.readValue(data, typeReference);
} else {
return null;
Expand Down Expand Up @@ -135,20 +134,18 @@ public static JsonNode readTree(String s) {
}

public static class JsonUtilException extends RuntimeException {

private static final long serialVersionUID = -4804245225960963421L;

public JsonUtilException(Throwable cause) {
super(cause);
}

}

public static String serializeExcludingProperties(Object object, String... propertiesToExclude) {
String serialized = JsonUtils.writeValueAsString(object);
Map<String, Object> properties = JsonUtils.readValue(serialized, new TypeReference<Map<String, Object>>() {});
for(String property : propertiesToExclude) {
if(property.contains(".")) {
Map<String, Object> properties = JsonUtils.readValue(serialized, new TypeReference<>() {});
for (String property : propertiesToExclude) {
if (property.contains(".")) {
String[] split = property.split("\\.", 2);
if (properties != null && properties.containsKey(split[0])) {
Object inner = properties.get(split[0]);
Expand Down Expand Up @@ -181,19 +178,19 @@ public static boolean getNodeAsBoolean(JsonNode node, String fieldName, boolean
public static Date getNodeAsDate(JsonNode node, String fieldName) {
JsonNode typeNode = node.get(fieldName);
long date = typeNode == null ? -1 : typeNode.asLong(-1);
if (date==-1) {
if (date == -1) {
return null;
} else {
return new Date(date);
}
}

public static Map<String,Object> getNodeAsMap(JsonNode node) {
public static Map<String, Object> getNodeAsMap(JsonNode node) {
return objectMapper.convertValue(node, Map.class);
}

public static boolean hasLength(CharSequence str) {
return !(str == null || str.length()==0);
return !(str == null || str.length() == 0);
}

public static boolean hasText(CharSequence str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public IdentityProvider<T> setConfig(T config) {
return this;
}

private static String determineType(Class clazz) {
private static String determineType(Class<? extends AbstractIdentityProviderDefinition> clazz) {
if (SamlIdentityProviderDefinition.class.isAssignableFrom(clazz)) {
return SAML;
} else if (UaaIdentityProviderDefinition.class.isAssignableFrom(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static boolean matches(Iterable<Pattern> wildcards, String scope) {
* @param prefix the prefix to strip from key names
* @return a map of String values
*/
public static Map<String, ?> getMapFromProperties(Properties properties, String prefix) {
public static Map<String, Object> getMapFromProperties(Properties properties, String prefix) {
Map<String, Object> result = new HashMap<>();
for (String key : properties.stringPropertyNames()) {
if (key.startsWith(prefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -133,7 +133,7 @@ void containsWildcard() {
@Test
void constructWildcards() {
assertThat(UaaStringUtils.constructWildcards(List.of())).isEmpty();
assertThat(UaaStringUtils.constructWildcards(List.of("any")).contains("any")).isFalse();
assertThat(UaaStringUtils.constructWildcards(List.of("any"))).doesNotContain(Pattern.compile("any"));
}

@Test
Expand Down Expand Up @@ -387,9 +387,9 @@ void isNullOrEmpty_ShouldReturnFalse(final String input) {

@Test
void getMapFromProperties() {
Properties properties = new Properties();
properties.put("pre.key", "value");
Map<String, Object> objectMap = (Map<String, Object>) UaaStringUtils.getMapFromProperties(properties, "pre.");
Properties props = new Properties();
props.put("pre.key", "value");
Map<String, Object> objectMap = UaaStringUtils.getMapFromProperties(props, "pre.");
assertThat(objectMap).containsEntry("key", "value")
.doesNotContainKey("pre.key");
}
Expand All @@ -404,7 +404,8 @@ void getSafeParameterValue() {

@Test
void getArrayDefaultValue() {
assertThat(UaaStringUtils.getValuesOrDefaultValue(Set.of("1", "2"), "1").stream().sorted().collect(Collectors.toList())).isEqualTo(List.of("1", "2").stream().sorted().collect(Collectors.toList()));
assertThat(UaaStringUtils.getValuesOrDefaultValue(Set.of("1", "2"), "1").stream().sorted().toList())
.isEqualTo(Stream.of("1", "2").sorted().toList());
assertThat(UaaStringUtils.getValuesOrDefaultValue(Set.of(), "1")).isEqualTo(List.of("1"));
assertThat(UaaStringUtils.getValuesOrDefaultValue(null, "1")).isEqualTo(List.of("1"));
}
Expand All @@ -415,7 +416,7 @@ void validateInput() {
}

@ParameterizedTest
@ValueSource(strings = { "\0", "", "\t", "\n", "\r" })
@ValueSource(strings = {"\0", "", "\t", "\n", "\r"})
void alertOnInvlidInput(String input) {
assertThatThrownBy(() -> UaaStringUtils.getValidatedString(input))
.isInstanceOf(IllegalArgumentException.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* *****************************************************************************
* Cloud Foundry
* Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
Expand All @@ -14,12 +14,6 @@

package org.cloudfoundry.identity.api.web;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;

import org.cloudfoundry.identity.uaa.test.UaaTestAccounts;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -28,9 +22,12 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.servlet.View;

import java.util.HashMap;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Dave Syer
*
*/
public class ApiControllerTests {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.cloudfoundry.identity.uaa.audit.event.EntityDeletedEvent;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.provider.AbstractIdentityProviderDefinition;
Expand All @@ -34,6 +31,7 @@
import org.cloudfoundry.identity.uaa.util.LdapUtils;
import org.cloudfoundry.identity.uaa.util.UaaMapUtils;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationEventPublisher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private String login(Model model, Principal principal, List<String> excludedProm
Map<String, AbstractIdentityProviderDefinition> loginHintProviders = Map.of();

if (uaaLoginHint != null && (allowedIdentityProviderKeys == null || allowedIdentityProviderKeys.contains(uaaLoginHint.getOrigin()))) {
// Login hint: Only try to read the hinted IdP from database
// Login hint: Only try to read the hinted IdP from the database
if (!(OriginKeys.UAA.equals(uaaLoginHint.getOrigin()) || OriginKeys.LDAP.equals(uaaLoginHint.getOrigin()))) {
try {
IdentityProvider loginHintProvider = externalOAuthProviderConfigurator
Expand Down Expand Up @@ -336,11 +336,9 @@ private String login(Model model, Principal principal, List<String> excludedProm
IdentityProvider uaaIdentityProvider =
providerProvisioning.retrieveByOriginIgnoreActiveFlag(OriginKeys.UAA, IdentityZoneHolder.get().getId());
// ldap and uaa disabled removes username/password input boxes
if (!uaaIdentityProvider.isActive()) {
if (ldapIdentityProvider == null || !ldapIdentityProvider.isActive()) {
fieldUsernameShow = false;
returnLoginPrompts = false;
}
if (!uaaIdentityProvider.isActive() && (ldapIdentityProvider == null || !ldapIdentityProvider.isActive())) {
fieldUsernameShow = false;
returnLoginPrompts = false;
}

// ldap or uaa not part of allowedIdentityProviderKeys
Expand Down Expand Up @@ -482,7 +480,7 @@ private void setJsonInfo(
Map<String, String> idpDefinitionsForJson = new HashMap<>();
if (samlIdentityProviders != null) {
for (SamlIdentityProviderDefinition def : samlIdentityProviders.values()) {
String idpUrl = "%s/saml2/authenticate/%s".formatted(links.get(LOGIN), def.getIdpEntityAlias());
String idpUrl = "%s/saml2/authenticate/%s".formatted(links.get(LOGIN), def.getIdpEntityAlias());
idpDefinitionsForJson.put(def.getIdpEntityAlias(), idpUrl);
}
model.addAttribute(IDP_DEFINITIONS, idpDefinitionsForJson);
Expand Down Expand Up @@ -766,6 +764,7 @@ public String discoverIdentityProvider(@RequestParam String email, @RequestParam
try {
clientDetails = clientDetailsService.loadClientByClientId(clientIds[0], IdentityZoneHolder.get().getId());
} catch (NoSuchClientException ignored) {
// ignore
}
}
if (StringUtils.hasText(loginHint)) {
Expand Down
Loading

0 comments on commit f65c09e

Please sign in to comment.