Skip to content

Commit f962361

Browse files
authored
fix: Add handling in case of PassTicketException (#1810)
* Add handling in case of PassTicketException Signed-off-by: at670475 <andrea.tabone@broadcom.com> * Fix unit test Signed-off-by: at670475 <andrea.tabone@broadcom.com> * Fix integration test * Import Signed-off-by: at670475 <andrea.tabone@broadcom.com> * import Signed-off-by: at670475 <andrea.tabone@broadcom.com> * Fix * Add acceptance test Signed-off-by: at670475 <andrea.tabone@broadcom.com> * Add license Signed-off-by: at670475 <andrea.tabone@broadcom.com>
1 parent e006531 commit f962361

File tree

8 files changed

+125
-4
lines changed

8 files changed

+125
-4
lines changed

common-service-core/src/main/java/org/zowe/apiml/passticket/AbstractIRRPassTicketException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public enum ErrorCode {
5454
ERR_8_8_4(8, 8, 4, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Parameter list error."),
5555
ERR_8_8_8(8, 8, 8, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An internal error was encountered."),
5656
ERR_8_8_12(8, 8, 12, HttpStatus.SC_INTERNAL_SERVER_ERROR, "A recovery environment could not be established."),
57-
ERR_8_8_16(8, 8, 16, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Not authorized to use this service."),
57+
ERR_8_8_16(8, 8, 16, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Not authorized to use this service. Verify that the user and the application name are valid, and check that corresponding permissions have been set up."),
5858
ERR_8_8_20(8, 8, 20, HttpStatus.SC_INTERNAL_SERVER_ERROR, "High order bit was not set to indicate last parameter."),
5959
ERR_8_12_8(8, 12, 8, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invocation of the Security Server Network Authentication Service Program Call (PC) interface failed with a 'parameter buffer overflow' return code. This indicates an internal error in IRRSPK00."),
6060
ERR_8_12_12(8, 12, 12, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invocation of the Security Server Network Authentication Service Program Call (PC) interface failed with an 'unable to allocate storage' return code. The region size for the Security Server Network Authentication Service started task (SKRBKDC) should be increased."),

common-service-core/src/test/java/org/zowe/apiml/passticket/PassTicketServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void testDefaultPassTicketImpl_GenerateUnknownUser() {
156156
assertEquals(16, e.getRacfRsn());
157157
assertNotNull(e.getErrorCode());
158158
assertEquals(AbstractIRRPassTicketException.ErrorCode.ERR_8_8_16, e.getErrorCode());
159-
assertEquals("Error on generation of PassTicket: Not authorized to use this service.", e.getMessage());
159+
assertEquals("Error on generation of PassTicket: Not authorized to use this service. Verify that the user and the application name are valid, and check that corresponding permissions have been set up.", e.getMessage());
160160
}
161161

162162
@Test

gateway-service/src/main/java/org/zowe/apiml/gateway/error/check/SecurityErrorCheck.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.security.authentication.BadCredentialsException;
1818
import org.springframework.security.core.AuthenticationException;
1919
import org.zowe.apiml.gateway.error.ErrorUtils;
20+
import org.zowe.apiml.gateway.security.service.PassTicketException;
2021
import org.zowe.apiml.message.api.ApiMessageView;
2122
import org.zowe.apiml.message.core.MessageService;
2223
import org.zowe.apiml.security.common.token.TokenExpireException;
@@ -51,6 +52,11 @@ public ResponseEntity<ApiMessageView> checkError(HttpServletRequest request, Thr
5152
messageView = messageService.createMessage("org.zowe.apiml.security.login.invalidCredentials",
5253
ErrorUtils.getGatewayUri(request)
5354
).mapToView();
55+
} else if (cause instanceof PassTicketException) {
56+
messageView = messageService.createMessage("org.zowe.apiml.security.ticket.generateFailed",
57+
cause.getMessage() + ". " + cause.getCause()
58+
).mapToView();
59+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).body(messageView);
5460
}
5561
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).contentType(MediaType.APPLICATION_JSON).body(messageView);
5662
}

gateway-service/src/main/java/org/zowe/apiml/gateway/ribbon/http/ServiceAuthenticationDecorator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public class ServiceAuthenticationDecorator {
3131
private final ServiceAuthenticationService serviceAuthenticationService;
3232
private final AuthenticationService authenticationService;
3333

34-
private static final String INVALID_JWT_MESSAGE = "Invalid JWT token";
35-
3634
/**
3735
* If a service requires authentication,
3836
* verify that the specific instance was selected upfront

gateway-service/src/main/java/org/zowe/apiml/gateway/security/service/PassTicketException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ public PassTicketException(String message, Throwable cause) {
1919
super(message, cause);
2020
}
2121

22+
public PassTicketException(String message) {
23+
super(message);
24+
}
2225
}

gateway-service/src/main/java/org/zowe/apiml/gateway/security/ticket/SuccessfulTicketHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.zowe.apiml.gateway.security.ticket;
1111

12+
import lombok.extern.slf4j.Slf4j;
1213
import org.zowe.apiml.security.common.ticket.TicketRequest;
1314
import org.zowe.apiml.security.common.ticket.TicketResponse;
1415
import org.zowe.apiml.security.common.token.TokenAuthentication;
@@ -35,6 +36,7 @@
3536
*/
3637
@Component
3738
@RequiredArgsConstructor
39+
@Slf4j
3840
public class SuccessfulTicketHandler implements AuthenticationSuccessHandler {
3941
private final ObjectMapper mapper;
4042
private final PassTicketService passTicketService;
@@ -64,6 +66,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
6466
ApiMessageView messageView = messageService.createMessage("org.zowe.apiml.security.ticket.generateFailed",
6567
e.getErrorCode().getMessage()).mapToView();
6668
mapper.writeValue(response.getWriter(), messageView);
69+
log.debug("The generation of the PassTicket failed. Please supply a valid user and application name, and check that corresponding permissions have been set up.");
6770
}
6871

6972
response.getWriter().flush();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
package org.zowe.apiml.acceptance;
11+
12+
import io.restassured.http.Cookie;
13+
import org.apache.http.HttpStatus;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Nested;
16+
import org.junit.jupiter.api.Test;
17+
import org.springframework.context.annotation.Import;
18+
import org.zowe.apiml.acceptance.common.AcceptanceTest;
19+
import org.zowe.apiml.acceptance.common.AcceptanceTestWithTwoServices;
20+
import org.zowe.apiml.acceptance.config.PassTicketFailureConfig;
21+
22+
import static io.restassured.RestAssured.given;
23+
import static org.hamcrest.core.Is.is;
24+
25+
/**
26+
* This test verifies that a REST message is returned in case of failure during generation of PassTicket.
27+
*/
28+
@AcceptanceTest
29+
@Import(PassTicketFailureConfig.class)
30+
public class PassTicketSchemeTest extends AcceptanceTestWithTwoServices {
31+
32+
@Nested
33+
class GivenValidJwtToken {
34+
Cookie validJwtToken;
35+
36+
@BeforeEach
37+
void setUp() {
38+
validJwtToken = securityRequests.validJwtToken();
39+
}
40+
41+
@Nested
42+
class WhenPassTicketRequestedByService {
43+
44+
@BeforeEach
45+
void prepareService() {
46+
applicationRegistry.setCurrentApplication(serviceWithDefaultConfiguration.getId());
47+
}
48+
49+
@Test
50+
void thenPassTicketErrorMessageIsReturned() {
51+
52+
given()
53+
.log().all()
54+
.cookie(validJwtToken)
55+
.when()
56+
.get(basePath + serviceWithDefaultConfiguration.getPath())
57+
.then()
58+
.log().all()
59+
.statusCode(is(HttpStatus.SC_INTERNAL_SERVER_ERROR)).body("messages[0].messageKey",is("org.zowe.apiml.security.ticket.generateFailed"));
60+
}
61+
}
62+
}
63+
}
64+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*/
10+
package org.zowe.apiml.acceptance.config;
11+
12+
import com.netflix.zuul.ZuulFilter;
13+
import org.springframework.context.annotation.Bean;
14+
import org.zowe.apiml.gateway.security.service.PassTicketException;
15+
16+
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
17+
18+
/**
19+
* Configuration that creates a Zuul filter which throws a PassTicketException to test the error handler.
20+
*/
21+
public class PassTicketFailureConfig {
22+
23+
@Bean
24+
public ZuulFilter zuulFilter() {
25+
return new ZuulFilter() {
26+
@Override
27+
public String filterType() {
28+
return PRE_TYPE;
29+
}
30+
31+
@Override
32+
public int filterOrder() {
33+
return 0;
34+
}
35+
36+
@Override
37+
public boolean shouldFilter() {
38+
return true;
39+
}
40+
41+
@Override
42+
public Object run() {
43+
throw new PassTicketException("problem");
44+
}
45+
};
46+
}
47+
}

0 commit comments

Comments
 (0)