Skip to content

Commit

Permalink
P4ADEV-320-refactoring-accordingly-handbook
Browse files Browse the repository at this point in the history
  • Loading branch information
LarissaASLeite committed May 23, 2024
1 parent 213fd32 commit d4230d0
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 63 deletions.
9 changes: 9 additions & 0 deletions .devops/deploy-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ stages:
echo "##vso[task.setvariable variable=appVersion;isOutput=true]$version"
failOnStderr: true

- task: Bash@3
displayName: Lock Gradle Dependencies
condition: and(succeeded(), eq(variables.canDeploy, true))
inputs:
targetType: 'inline'
script: |
./gradlew dependencies --write-locks
failOnStderr: true

- task: Docker@2
condition: and(succeeded(), ${{ parameters.executeBuild }})
displayName: 'Build and publish $(imageRepository) image'
Expand Down
66 changes: 66 additions & 0 deletions gradle.lockfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
ch.qos.logback:logback-classic:1.4.14=compileClasspath
ch.qos.logback:logback-core:1.4.14=compileClasspath
com.auth0:java-jwt:4.4.0=compileClasspath
com.auth0:jwks-rsa:0.22.1=compileClasspath
com.fasterxml.jackson.core:jackson-annotations:2.15.4=compileClasspath
com.fasterxml.jackson.core:jackson-core:2.15.4=compileClasspath
com.fasterxml.jackson.core:jackson-databind:2.15.4=compileClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.4=compileClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.4=compileClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.4=compileClasspath
com.fasterxml.jackson.module:jackson-module-parameter-names:2.15.4=compileClasspath
com.fasterxml.jackson:jackson-bom:2.15.4=compileClasspath
com.google.code.findbugs:jsr305:3.0.2=compileClasspath
com.nimbusds:nimbus-jose-jwt:9.38-rc5=compileClasspath
io.jsonwebtoken:jjwt-api:0.12.5=compileClasspath
io.jsonwebtoken:jjwt:0.12.5=compileClasspath
io.micrometer:micrometer-commons:1.12.5=compileClasspath
io.micrometer:micrometer-core:1.12.5=compileClasspath
io.micrometer:micrometer-jakarta9:1.12.5=compileClasspath
io.micrometer:micrometer-observation:1.12.5=compileClasspath
io.swagger.core.v3:swagger-annotations-jakarta:2.2.21=compileClasspath
io.swagger.core.v3:swagger-core-jakarta:2.2.21=compileClasspath
io.swagger.core.v3:swagger-models-jakarta:2.2.21=compileClasspath
jakarta.activation:jakarta.activation-api:2.1.3=compileClasspath
jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath
jakarta.validation:jakarta.validation-api:3.0.2=compileClasspath
jakarta.xml.bind:jakarta.xml.bind-api:4.0.2=compileClasspath
org.apache.commons:commons-lang3:3.13.0=compileClasspath
org.apache.logging.log4j:log4j-api:2.21.1=compileClasspath
org.apache.logging.log4j:log4j-to-slf4j:2.21.1=compileClasspath
org.apache.tomcat.embed:tomcat-embed-core:10.1.20=compileClasspath
org.apache.tomcat.embed:tomcat-embed-el:10.1.20=compileClasspath
org.apache.tomcat.embed:tomcat-embed-websocket:10.1.20=compileClasspath
org.codehaus.janino:commons-compiler:3.1.12=compileClasspath
org.codehaus.janino:janino:3.1.12=compileClasspath
org.openapitools:jackson-databind-nullable:0.2.6=compileClasspath
org.projectlombok:lombok:1.18.32=compileClasspath
org.slf4j:jul-to-slf4j:2.0.13=compileClasspath
org.slf4j:slf4j-api:2.0.13=compileClasspath
org.springdoc:springdoc-openapi-starter-common:2.5.0=compileClasspath
org.springdoc:springdoc-openapi-starter-webmvc-api:2.5.0=compileClasspath
org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0=compileClasspath
org.springframework.boot:spring-boot-actuator-autoconfigure:3.2.5=compileClasspath
org.springframework.boot:spring-boot-actuator:3.2.5=compileClasspath
org.springframework.boot:spring-boot-autoconfigure:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter-actuator:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter-json:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter-logging:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter-tomcat:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter-web:3.2.5=compileClasspath
org.springframework.boot:spring-boot-starter:3.2.5=compileClasspath
org.springframework.boot:spring-boot:3.2.5=compileClasspath
org.springframework:spring-aop:6.1.6=compileClasspath
org.springframework:spring-beans:6.1.6=compileClasspath
org.springframework:spring-context:6.1.6=compileClasspath
org.springframework:spring-core:6.1.6=compileClasspath
org.springframework:spring-expression:6.1.6=compileClasspath
org.springframework:spring-jcl:6.1.6=compileClasspath
org.springframework:spring-web:6.1.6=compileClasspath
org.springframework:spring-webmvc:6.1.6=compileClasspath
org.webjars:swagger-ui:5.13.0=compileClasspath
org.yaml:snakeyaml:2.0=compileClasspath
empty=
10 changes: 0 additions & 10 deletions openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,8 @@ components:
- AUTH_GENERIC_ERROR
- AUTH_INVALID_TOKEN
- AUTH_INVALID_REQUEST
description: >-
"ENG: Error code: AUTH_TOKEN_EXPIRED_DATE: Token date expired,
AUTH_INVALID_TOKEN: The token is not valid
AUTH_INVALID_REQUEST: Something went wrong handling request,
AUTH_GENERIC_ERROR: Application Error - IT: Codice di errore:
AUTH_TOKEN_EXPIRED_DATE: Token scaduto,
AUTH_INVALID_TOKEN: Il Token non è valido
AUTH_INVALID_REQUEST: Qualcosa è andato storto durante l'invio della richiesta,
AUTH_GENERIC_ERROR: Errore generico"
message:
type: string
description: "ENG: Error message - IT: Messaggio di errore"
security:
- apiKeyHeader: []
- apiKeyQuery: []
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,14 @@ public class AuthExceptionHandler {
@ExceptionHandler({InvalidTokenException.class, TokenExpiredException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public AuthErrorDTO handleInvalidTokenException(ServiceException ex, HttpServletRequest request){
logException(ex, request);
String message = ex.getMessage();

log.info("A {} occurred handling request {}: HttpStatus 401 - {}",
ex.getClass(),
getRequestDetails(request), message);

return new AuthErrorDTO(ex.getCode(), message);
}

public static void logException(ServiceException error, HttpServletRequest request) {
if(error.isPrintStackTrace()){
log.info("A {} occurred handling request {} at {}",
error.getClass().getSimpleName() ,
getRequestDetails(request),
error.getStackTrace().length > 0 ? error.getStackTrace()[0] : "UNKNOWN");
}else {
log.info("A {} occurred handling request {}: HttpStatus 401 - {}",
error.getClass(),
getRequestDetails(request), error.getMessage());
}
}

public static String getRequestDetails(HttpServletRequest request) {
return "%s %s".formatted(request.getMethod(), request.getRequestURI());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@

@Getter
public class ServiceException extends RuntimeException {

private final AuthErrorDTO.CodeEnum code;
private final boolean printStackTrace;

public ServiceException(AuthErrorDTO.CodeEnum code, String message) {
this(code, message, false, null);
}

public ServiceException(AuthErrorDTO.CodeEnum code, String message, boolean printStackTrace, Throwable ex) {
super(message, ex);
super(message);
this.code = code;
this.printStackTrace = printStackTrace;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public InvalidTokenException(String message) {
}

public InvalidTokenException(AuthErrorDTO.CodeEnum code, String message) {
this(code, message, false, null);
}

public InvalidTokenException(AuthErrorDTO.CodeEnum code, String message, boolean printStackTrace, Throwable ex) {
super(code, message, printStackTrace, ex);
super(code, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ public TokenExpiredException(String message) {
}

public TokenExpiredException(AuthErrorDTO.CodeEnum code, String message) {
this(code, message, false, null);
}

public TokenExpiredException(AuthErrorDTO.CodeEnum code, String message, boolean printStackTrace, Throwable ex) {
super(code, message, printStackTrace, ex);
super(code, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.auth0.jwt.interfaces.DecodedJWT;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.TokenExpiredException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import org.springframework.stereotype.Component;

import java.security.interfaces.RSAPublicKey;
Expand Down Expand Up @@ -54,7 +53,7 @@ public Map<String, String> validate(String token, String urlJwkProvider) {
} catch (com.auth0.jwt.exceptions.TokenExpiredException e){
throw new TokenExpiredException(e.getMessage());
} catch (JwkException | JWTVerificationException ex) {
throw new InvalidTokenException(AuthErrorDTO.CodeEnum.INVALID_TOKEN, "The token is not valid", true, ex);
throw new InvalidTokenException("The token is not valid");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package it.gov.pagopa.payhub.auth.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.TokenExpiredException;
import it.gov.pagopa.payhub.model.generated.AuthErrorDTO;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -33,9 +31,6 @@ class AuthExceptionHandlerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;

@SpyBean
private TestController testControllerSpy;

Expand All @@ -62,20 +57,6 @@ void handleInvalidTokenException() throws Exception {

}

@Test
void handleInvalidTokenExceptionWithStackTrace() throws Exception {
doThrow(new InvalidTokenException(AuthErrorDTO.CodeEnum.INVALID_TOKEN, "Error", true, new Throwable()))
.when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("AUTH_INVALID_TOKEN"))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error"));

}

@Test
void handleTokenExpiredException() throws Exception {
doThrow(new TokenExpiredException("Error")).when(testControllerSpy).testEndpoint();
Expand Down

0 comments on commit d4230d0

Please sign in to comment.