Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bassi authored and enricovianello committed Nov 14, 2024
1 parent d5dd04e commit 208f499
Show file tree
Hide file tree
Showing 82 changed files with 421 additions and 419 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class ErrorPageAuthenticationEntryPoint implements AuthenticationEntryPoint {

final String errorPage = "/errors/401";
static final String ERROR_PAGE = "/errors/401";

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
Expand Down Expand Up @@ -73,7 +73,7 @@ public void commence(HttpServletRequest request, HttpServletResponse response,

request.setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, authException);

RequestDispatcher dispatcher = request.getRequestDispatcher(errorPage);
RequestDispatcher dispatcher = request.getRequestDispatcher(ERROR_PAGE);
dispatcher.forward(request, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public PrincipalHelper(ServiceConfigurationProperties config) throws MalformedUR

public String getPrincipalAsString(Authentication authn) {
if (authn == null || authn instanceof AnonymousAuthenticationToken) {
return "anonymous";
return ANONYMOUS;
} else if (authn instanceof OAuth2AuthenticationToken) {
OAuth2AuthenticationToken authToken = (OAuth2AuthenticationToken) authn;
Map<String, Object> attributes = authToken.getPrincipal().getAttributes();
String subjectIssuer = String.format("%s@%s", attributes.get("sub"), attributes.get("iss"));
return subjectIssuer;

return String.format("%s@%s", attributes.get("sub"), attributes.get("iss"));
} else if (authn instanceof PreAuthenticatedAuthenticationToken) {
return authn.getName();
} else if (authn instanceof JwtAuthenticationToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {

public Object extractPrincipal(X509Certificate cert) {

return cert.getSubjectDN().getName();
return cert.getSubjectX500Principal().getName();
}

@Override
public boolean principalChanged(HttpServletRequest request,
Authentication currentAuthentication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected Optional<X509SubjectAuthority> getSubjectAuthority(HttpServletRequest
Optional<X509Certificate[]> chain = Utils.getCertificateChainFromRequest(request);
if (chain.isPresent()) {
return Optional.of(new X509SubjectAuthority(
ProxyUtils.getEndUserCertificate(chain.get()).getSubjectDN().getName()));
ProxyUtils.getEndUserCertificate(chain.get()).getSubjectX500Principal().getName()));
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.URL;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -42,8 +43,8 @@ public class LocalAuthorizationPdp implements PathAuthorizationPdp, TpcUtils {

public static final Logger LOG = LoggerFactory.getLogger(LocalAuthorizationPdp.class);

public static final EnumSet<Permission> READ_PERMS = EnumSet.of(Permission.r, Permission.rw);
public static final EnumSet<Permission> WRITE_PERMS = EnumSet.of(Permission.w, Permission.rw);
private static final Set<Permission> READ_PERMS = EnumSet.of(Permission.r, Permission.rw);
private static final Set<Permission> WRITE_PERMS = EnumSet.of(Permission.w, Permission.rw);

private final URL localAuthzServerIssuer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public boolean hasSubjectAsMember(String subject) {

} finally {
refreshLock.readLock().unlock();
;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static java.util.Collections.emptySet;

import java.io.File;
import java.io.FilenameFilter;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -76,15 +75,7 @@ public VOMapDetailsService build() {
File configDir = new File(serviceConf.getVOMapFilesConfigDir());
directorySanityChecks(configDir);

File[] files = configDir.listFiles(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {

return name.endsWith(VOMAPFILE_SUFFIX);

}
});
File[] files = configDir.listFiles((dir, name) -> name.endsWith(VOMAPFILE_SUFFIX));

if (files.length == 0) {
logger.warn("No mapfiles found in {}. Was looking for files ending in {}", configDir,
Expand All @@ -94,7 +85,7 @@ public boolean accept(File dir, String name) {



Set<VOMembershipProvider> providers = new HashSet<VOMembershipProvider>();
Set<VOMembershipProvider> providers = new HashSet<>();
for (File f : files) {
try {
String voName = FilenameUtils.removeExtension(f.getName());
Expand All @@ -106,7 +97,6 @@ public boolean accept(File dir, String name) {

} catch (Throwable t) {
logger.error("Error parsing mapfile {}: {}", f.getAbsolutePath(), t.getMessage(), t);
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.italiangrid.storm.webdav.tpc.LocalURLService;
import org.italiangrid.storm.webdav.tpc.TransferConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.ConfigAttribute;
Expand All @@ -51,7 +52,7 @@ public int vote(Authentication authentication, FilterInvocation filter,
return ACCESS_ABSTAIN;
}

String destination = filter.getRequest().getHeader(DESTINATION_HEADER);
String destination = filter.getRequest().getHeader(TransferConstants.DESTINATION_HEADER);

if (destination == null) {
return ACCESS_ABSTAIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.italiangrid.storm.webdav.config.StorageAreaInfo;
import org.italiangrid.storm.webdav.server.PathResolver;
import org.italiangrid.storm.webdav.tpc.LocalURLService;
import org.italiangrid.storm.webdav.tpc.TransferConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.ConfigAttribute;
Expand Down Expand Up @@ -57,7 +58,7 @@ public int vote(Authentication authentication, FilterInvocation filter,
return ACCESS_ABSTAIN;
}

String destination = filter.getRequest().getHeader(DESTINATION_HEADER);
String destination = filter.getRequest().getHeader(TransferConstants.DESTINATION_HEADER);

if (destination == null) {
return ACCESS_ABSTAIN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ PathAuthorizationPolicy parsePolicy(FineGrainedAuthzPolicyProperties policy) {
policy.getPrincipals()
.stream()
.map(this::parsePrincipal)
.forEach(builder::withPrincipalMatcher);;
.forEach(builder::withPrincipalMatcher);

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
Expand All @@ -32,12 +31,12 @@

public class SAConfigurationParser implements StorageAreaConfiguration {

private final Set<String> RESERVED_SA_NAMES =
Sets.newHashSet("oauth", ".well-known", "actuator", "assets", "authn-info", "logout", "oidc-login");
private static final Set<String> RESERVED_SA_NAMES = Sets.newHashSet("oauth", ".well-known",
"actuator", "assets", "authn-info", "logout", "oidc-login");

private final ServiceConfiguration serviceConfig;

private String PROPERTIES_FILENAME_SUFFIX = ".properties";
private static final String PROPERTIES_FILENAME_SUFFIX = ".properties";

private List<StorageAreaInfo> saInfos;

Expand All @@ -54,15 +53,11 @@ public SAConfigurationParser(ServiceConfiguration sc) {
File dir = new File(saConfDir);
directorySanityChecks(dir);

File[] saFiles = dir.listFiles(new FilenameFilter() {

@Override
public boolean accept(File file, String name) {
if (RESERVED_SA_NAMES.contains(name) && name.endsWith(PROPERTIES_FILENAME_SUFFIX)) {
log.warn("Skipping {} as it is a reserved storage area name");
}
return (!RESERVED_SA_NAMES.contains(name) && name.endsWith(PROPERTIES_FILENAME_SUFFIX));
File[] saFiles = dir.listFiles((file, name) -> {
if (RESERVED_SA_NAMES.contains(name) && name.endsWith(PROPERTIES_FILENAME_SUFFIX)) {
log.warn("Skipping {} as it is a reserved storage area name", name);
}
return (!RESERVED_SA_NAMES.contains(name) && name.endsWith(PROPERTIES_FILENAME_SUFFIX));
});

if (saFiles.length == 0) {
Expand All @@ -72,7 +67,7 @@ public boolean accept(File file, String name) {
throw new StoRMIntializationError(msg);
}

saInfos = new ArrayList<StorageAreaInfo>();
saInfos = new ArrayList<>();

for (File f : saFiles) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
public class DefaultExtendedFileAttributesHelper implements
ExtendedAttributesHelper {

private static final String USERDEFINEDFILEATTRIBUTEVIEW_NOT_SUPPORTED_MESSAGE =
"UserDefinedFileAttributeView not supported on file ";

public static final String STORM_ADLER32_CHECKSUM_ATTR_NAME = "storm.checksum.adler32";

public DefaultExtendedFileAttributesHelper() {
Expand Down Expand Up @@ -62,8 +65,7 @@ public void setExtendedFileAttribute(File f, String attributeName,

if (faView == null) {
throw new IOException(
"UserDefinedFileAttributeView not supported on file "
+ f.getAbsolutePath());
USERDEFINEDFILEATTRIBUTEVIEW_NOT_SUPPORTED_MESSAGE + f.getAbsolutePath());
}

faView.write(attributeName, StandardCharsets.UTF_8.encode(attributeValue));
Expand All @@ -75,14 +77,13 @@ public String getExtendedFileAttributeValue(File f, String attributeName)

checkNotNull(f);
checkArgument(!isNullOrEmpty(attributeName));

UserDefinedFileAttributeView faView = Files.getFileAttributeView(
f.toPath(), UserDefinedFileAttributeView.class);

if (faView == null) {
throw new IOException(
"UserDefinedFileAttributeView not supported on file "
+ f.getAbsolutePath());
USERDEFINEDFILEATTRIBUTEVIEW_NOT_SUPPORTED_MESSAGE + f.getAbsolutePath());
}

return getAttributeValue(faView, attributeName);
Expand All @@ -93,14 +94,13 @@ public String getExtendedFileAttributeValue(File f, String attributeName)
public List<String> getExtendedFileAttributeNames(File f) throws IOException {

checkNotNull(f);

UserDefinedFileAttributeView faView = Files.getFileAttributeView(
f.toPath(), UserDefinedFileAttributeView.class);

if (faView == null) {
throw new IOException(
"UserDefinedFileAttributeView not supported on file "
+ f.getAbsolutePath());
USERDEFINEDFILEATTRIBUTEVIEW_NOT_SUPPORTED_MESSAGE + f.getAbsolutePath());
}

return faView.list();
Expand All @@ -127,7 +127,7 @@ public String getChecksumAttribute(File f) throws IOException {
public boolean fileSupportsExtendedAttributes(File f) throws IOException {

checkNotNull(f);

UserDefinedFileAttributeView faView = Files.getFileAttributeView(
f.toPath(), UserDefinedFileAttributeView.class);

Expand All @@ -137,7 +137,7 @@ public boolean fileSupportsExtendedAttributes(File f) throws IOException {
@Override
public void setChecksumAttribute(Path p, String checksumValue) throws IOException {
setChecksumAttribute(p.toFile(), checksumValue);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ protected void calculateChecksum() {
@Override
public Object getProperty(QName name) {

if (name.getNamespaceURI().equals(STORM_NAMESPACE_URI)) {
if (name.getLocalPart().equals(PROPERTY_CHECKSUM)) {
try {
return getExtendedAttributesHelper().getChecksumAttribute(getFile());
} catch (IOException e) {
logger.warn("Errror getting checksum value for file: {}", getFile().getAbsolutePath(), e);
return null;
}
if (name.getNamespaceURI().equals(STORM_NAMESPACE_URI)
&& name.getLocalPart().equals(PROPERTY_CHECKSUM)) {
try {
return getExtendedAttributesHelper().getChecksumAttribute(getFile());
} catch (IOException e) {
logger.warn("Errror getting checksum value for file: {}", getFile().getAbsolutePath(), e);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

public class StoRMMiltonRequest extends ServletRequest {

private static final String regex = "(http.*:\\d*)/webdav/(.*)$";
private static final Pattern p = Pattern.compile(regex);
private static final String REGEX = "(http.*:\\d*)/webdav/(.*)$";
private static final Pattern p = Pattern.compile(REGEX);

public StoRMMiltonRequest(HttpServletRequest r, ServletContext servletContext) {

Expand Down Expand Up @@ -60,5 +60,5 @@ public Auth getAuthorization() {
// Always return null as milton is confused by the OAuth2 Bearer scheme
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected Set<GrantedAuthority> extractOauthGroupAuthorities(Jwt jwt) {
String tokenIssuer = jwt.getClaimAsString(JwtClaimNames.ISS);

for (String groupClaim : OAUTH_GROUP_CLAIM_NAMES) {
if (Boolean.TRUE.equals(jwt.containsClaim(groupClaim))) {
if (jwt.hasClaim(groupClaim)) {
jwt.getClaimAsStringList(groupClaim)
.forEach(gc -> groupAuthorities.add(new JwtGroupAuthority(tokenIssuer, gc)));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonInclude(NON_EMPTY)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ErrorResponseDTO {

public static final String UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type";
public static final String INVALID_REQUEST = "invalid_request";
public static final String INVALID_SCOPE = "invalid_scope";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum Permission {
r,
w,
rw
};
}

final String path;
final Permission permission;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonInclude(NON_EMPTY)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class TokenResponseDTO {

String accessToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonInclude(NON_EMPTY)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class AuthzServerMetadata {

private String issuer;
Expand Down
Loading

0 comments on commit 208f499

Please sign in to comment.