Skip to content

Commit

Permalink
fix: issues from sonar analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Mar 25, 2024
1 parent 25592e6 commit 90a26c6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.IntFunction;

import org.apache.commons.io.IOUtils;

import fr.insee.arc.core.service.kubernetes.bo.JsonFileParameter;
import fr.insee.arc.core.service.p0initialisation.ApiInitialisationService;
import fr.insee.arc.utils.consumer.ThrowingFunction;
import fr.insee.arc.utils.database.Delimiters;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import fr.insee.arc.utils.kubernetes.provider.KubernetesServiceLayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import java.util.List;

import org.springframework.stereotype.Component;

import fr.insee.arc.core.model.TraitementPhase;
import fr.insee.arc.core.service.global.ApiService;
import fr.insee.arc.core.service.p6export.parquet.ParquetDao;
import fr.insee.arc.core.service.p6export.parquet.ParquetEncryptionKey;
import fr.insee.arc.utils.database.TableToRetrieve;
import fr.insee.arc.utils.exception.ArcException;

@Component
public class ApiExportService extends ApiService {

public ApiExportService() {
Expand Down
9 changes: 0 additions & 9 deletions arc-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
</properties>

<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>prod/*.properties</exclude>
</excludes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
31 changes: 19 additions & 12 deletions arc-web/src/main/java/fr/insee/arc/web/Oauth2ClientForKeycloak.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.Set;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand All @@ -17,13 +15,10 @@
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;

import fr.insee.arc.core.util.StaticLoggerDispatcher;
import fr.insee.arc.utils.webutils.WebAttributesName;

public class Oauth2ClientForKeycloak {

private static final Logger LOGGER = LogManager.getLogger(Oauth2ClientForKeycloak.class);

@Value(WebAttributesName.KEYCLOAK_ATTRIBUTE_REALM)
protected String keycloakRealm;

Expand All @@ -36,6 +31,18 @@ public class Oauth2ClientForKeycloak {
@Value(WebAttributesName.KEYCLOAK_ATTRIBUTE_CREDENTIALS)
private String keycloakCredential;


private static final String SCOPE_OPENID = "openid";
private static final String SCOPE_PROFILE = "profile";
private static final String SCOPE_EMAIL = "email";
private static final String SCOPE_ROLES = "roles";
private static final String[] SCOPES = new String[] {SCOPE_OPENID, SCOPE_PROFILE, SCOPE_EMAIL, SCOPE_ROLES};

private static final String CLAIM_ROLES = "roles";
private static final String CLAIM_GROUPS = "groups";
private static final String CLAIM_REALM_ACCESS = "realm_access";


protected ClientRegistration keycloakClientRegistration(ClientAuthenticationMethod method) {

String realmUri = keycloakServer + "/realms/" + keycloakRealm;
Expand All @@ -49,7 +56,7 @@ protected ClientRegistration keycloakClientRegistration(ClientAuthenticationMeth
.clientAuthenticationMethod(method) //
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) //
.issuerUri(realmUri) //
.scope("openid", "profile", "email", "roles").authorizationUri(realmUri + openIdConnect + "/auth") //
.scope(SCOPES).authorizationUri(realmUri + openIdConnect + "/auth") //
.tokenUri(realmUri + openIdConnect + "/token") //
.userInfoUri(realmUri + openIdConnect + "/userinfo") //
.jwkSetUri(realmUri + openIdConnect + "/certs") //
Expand All @@ -62,18 +69,18 @@ protected GrantedAuthoritiesMapper userAuthoritiesMapper() {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();

authorities.forEach(authority -> {
if (authority instanceof OidcUserAuthority) {
OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority;
if (authority instanceof OidcUserAuthority oidcUserAuthority) {

OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();

@SuppressWarnings("unchecked")
List<String> roles = (List<String>) ObjectUtils.firstNonNull(
userInfo.getClaimAsStringList("roles"),
userInfo.getClaimAsStringList("groups"),
userInfo.getClaimAsMap("realm_access")==null ? null : userInfo.getClaimAsMap("realm_access").get("roles"));
userInfo.getClaimAsStringList(CLAIM_ROLES),
userInfo.getClaimAsStringList(CLAIM_GROUPS),
userInfo.getClaimAsMap(CLAIM_REALM_ACCESS)==null ? null : userInfo.getClaimAsMap(CLAIM_REALM_ACCESS).get(CLAIM_ROLES));

List<SimpleGrantedAuthority> groupAuthorities = roles.stream()
.map(g -> new SimpleGrantedAuthority(g)).toList();
.map(SimpleGrantedAuthority::new).toList();
mappedAuthorities.addAll(groupAuthorities);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ private List<String> eraseInputFields(List<String> headersDLabel, Map<String, St
*/
public boolean insert(VObject currentData, AttributeValue... attributeValues) {

if (currentData.getInputFields()==null || currentData.getInputFields().size()==0)
if (currentData.getInputFields()==null || currentData.getInputFields().isEmpty())
{
return false;
}
Expand Down

0 comments on commit 90a26c6

Please sign in to comment.