Skip to content

Commit

Permalink
Fix auth desugar panic path
Browse files Browse the repository at this point in the history
  • Loading branch information
chamil321 committed Sep 1, 2022
1 parent 2cd4b90 commit f68cb63
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 40 deletions.
79 changes: 79 additions & 0 deletions ballerina-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ dependencies = [
{org = "ballerina", name = "log"},
{org = "ballerina", name = "regex"}
]
modules = [
{org = "ballerina", packageName = "auth", moduleName = "auth"}
]

[[package]]
org = "ballerina"
Expand All @@ -39,6 +42,9 @@ scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "constraint", moduleName = "constraint"}
]

[[package]]
org = "ballerina"
Expand All @@ -49,6 +55,9 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "crypto", moduleName = "crypto"}
]

[[package]]
org = "ballerina"
Expand All @@ -63,6 +72,9 @@ dependencies = [
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "file", moduleName = "file"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -101,9 +113,22 @@ org = "ballerina"
name = "http_tests"
version = "2.4.0"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "file"},
{org = "ballerina", name = "http"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "jwt"},
{org = "ballerina", name = "lang.boolean"},
{org = "ballerina", name = "lang.float"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "lang.xml"},
{org = "ballerina", name = "mime"},
{org = "ballerina", name = "oauth2"},
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "url"}
Expand All @@ -121,12 +146,18 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
]
modules = [
{org = "ballerina", packageName = "io", moduleName = "io"}
]

[[package]]
org = "ballerina"
name = "jballerina.java"
version = "0.0.0"
scope = "testOnly"
modules = [
{org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"}
]

[[package]]
org = "ballerina"
Expand All @@ -143,6 +174,9 @@ dependencies = [
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "jwt", moduleName = "jwt"}
]

[[package]]
org = "ballerina"
Expand All @@ -164,6 +198,18 @@ dependencies = [
{org = "ballerina", name = "lang.__internal"}
]

[[package]]
org = "ballerina"
name = "lang.boolean"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.boolean", moduleName = "lang.boolean"}
]

[[package]]
org = "ballerina"
name = "lang.decimal"
Expand All @@ -173,6 +219,18 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.float"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.float", moduleName = "lang.float"}
]

[[package]]
org = "ballerina"
name = "lang.int"
Expand All @@ -181,6 +239,9 @@ scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.int", moduleName = "lang.int"}
]

[[package]]
org = "ballerina"
Expand All @@ -196,6 +257,9 @@ scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"}
]

[[package]]
org = "ballerina"
Expand All @@ -218,6 +282,18 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.xml"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.xml", moduleName = "lang.xml"}
]

[[package]]
org = "ballerina"
name = "log"
Expand Down Expand Up @@ -256,6 +332,9 @@ dependencies = [
{org = "ballerina", name = "log"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerina", packageName = "oauth2", moduleName = "oauth2"}
]

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function testAuthzError() returns error? {
}
);
http:Response res = check clientEP->get("/auth");
test:assertEquals(res.statusCode, 403);
test:assertEquals(res.statusCode, 401);
assertHeaderValue(check res.getHeader("last-interceptor"), "default-response-error-interceptor");
assertHeaderValue(check res.getHeader("default-response-error-interceptor"), "true");
assertHeaderValue(check res.getHeader("last-response-interceptor"), "true");
Expand Down
6 changes: 3 additions & 3 deletions ballerina/auth_desugar.bal
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public isolated function authenticateResource(Service serviceRef, string methodN
if header is string {
Unauthorized|Forbidden? result = tryAuthenticate(<ListenerAuthConfig[]>authConfig, header);
if result is Unauthorized {
panic error ListenerAuthnError("");
panic error InternalListenerAuthnError("");
} else if result is Forbidden {
panic error ListenerAuthzError("");
panic error InternalListenerAuthzError("");
}
} else {
panic error ListenerAuthnError("");
panic error InternalListenerAuthnError("");
}
}

Expand Down
6 changes: 6 additions & 0 deletions ballerina/http_errors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public type ListenerAuthnError distinct ListenerAuthError;
# Defines the authorization error types that returned from listener.
public type ListenerAuthzError distinct ListenerAuthError;

# Defined for internal use when panicing from the auth_desugar
type InternalListenerAuthnError distinct ListenerAuthError;

# Defined for internal use when panicing from the auth_desugar
type InternalListenerAuthzError distinct ListenerAuthError;

# Defines the client error types that returned while sending outbound request.
public type OutboundRequestError distinct ClientError;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void returnErrorResponse(BError error) {
invokeBalMethod(paramFeed, "returnErrorResponse");
}

private void invokeBalMethod(Object[] paramFeed, String methodName) {
public void invokeBalMethod(Object[] paramFeed, String methodName) {
Callback returnCallback = new Callback() {
@Override
public void notifySuccess(Object result) {
Expand Down Expand Up @@ -153,15 +153,16 @@ private void stopObserverContext() {
public void notifyFailure(BError error) { // handles panic and check_panic
cleanupRequestMessage();
// This check is added to update the status code with respect to the auth errors.
if (error.getType().getName().equals(HttpErrorType.LISTENER_AUTHN_ERROR.getErrorName())) {
if (error.getType().getName().equals(HttpErrorType.INTERNAL_LISTENER_AUTHN_ERROR.getErrorName())) {
requestMessage.setHttpStatusCode(401);
} else if (error.getType().getName().equals(HttpErrorType.LISTENER_AUTHZ_ERROR.getErrorName())) {
} else if (error.getType().getName().equals(HttpErrorType.INTERNAL_LISTENER_AUTHZ_ERROR.getErrorName())) {
requestMessage.setHttpStatusCode(403);
} else {
requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR, true);
}
if (alreadyResponded(error)) {
return;
}
requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR, true);
invokeErrorInterceptors(error, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public enum HttpErrorType {
REQ_DISPATCHING_ERROR("RequestDispatchingError"),
SERVICE_DISPATCHING_ERROR("ServiceDispatchingError"),
RESOURCE_DISPATCHING_ERROR("ResourceDispatchingError"),
LISTENER_AUTHZ_ERROR("ListenerAuthzError"),
LISTENER_AUTHN_ERROR("ListenerAuthnError");
INTERNAL_LISTENER_AUTHZ_ERROR("InternalListenerAuthzError"),
INTERNAL_LISTENER_AUTHN_ERROR("InternalListenerAuthnError");

private final String errorName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,19 @@
package io.ballerina.stdlib.http.api;

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.Runtime;
import io.ballerina.runtime.api.async.Callback;
import io.ballerina.runtime.api.types.ServiceType;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.stdlib.http.api.nativeimpl.ModuleUtils;
import io.ballerina.stdlib.http.api.nativeimpl.connection.Respond;
import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage;

import java.util.Objects;

/**
* {@code HttpResponseInterceptorUnitCallback} is the responsible for acting on notifications received from Ballerina
* side when a response interceptor service is invoked.
*/
public class HttpResponseInterceptorUnitCallback implements Callback {
public class HttpResponseInterceptorUnitCallback extends HttpCallableUnitCallback {
private static final String ILLEGAL_FUNCTION_INVOKED = "illegal return: response has already been sent";

private final HttpCarbonMessage requestMessage;
Expand All @@ -45,18 +40,17 @@ public class HttpResponseInterceptorUnitCallback implements Callback {
private final Environment environment;
private final BObject requestCtx;
private final DataContext dataContext;
private final Runtime runtime;


public HttpResponseInterceptorUnitCallback(HttpCarbonMessage requestMessage, BObject caller, BObject response,
Environment env, DataContext dataContext, Runtime runtime) {
super(requestMessage, runtime);
this.requestMessage = requestMessage;
this.requestCtx = (BObject) requestMessage.getProperty(HttpConstants.REQUEST_CONTEXT);
this.caller = caller;
this.response = response;
this.environment = env;
this.dataContext = dataContext;
this.runtime = runtime;
}

@Override
Expand All @@ -74,7 +68,7 @@ public void notifyFailure(BError error) { // handles panic and check_panic
invokeErrorInterceptors(error, false);
}

private void invokeErrorInterceptors(BError error, boolean printError) {
public void invokeErrorInterceptors(BError error, boolean printError) {
requestMessage.setProperty(HttpConstants.INTERCEPTOR_SERVICE_ERROR, error);
if (printError) {
error.printStackTrace();
Expand Down Expand Up @@ -177,25 +171,4 @@ public void returnErrorResponse(BError error) {

invokeBalMethod(paramFeed, "returnErrorResponse");
}

private void invokeBalMethod(Object[] paramFeed, String methodName) {
Callback returnCallback = new Callback() {
@Override
public void notifySuccess(Object result) {
printStacktraceIfError(result);
Object isPanic = requestMessage.getProperty(HttpConstants.INTERCEPTOR_SERVICE_PANIC_ERROR);
if (Objects.nonNull(isPanic) && (boolean) isPanic) {
System.exit(0);
}
}

@Override
public void notifyFailure(BError result) {
sendFailureResponse(result);
}
};
runtime.invokeMethodAsyncSequentially(
caller, methodName, null, ModuleUtils.getNotifySuccessMetaData(),
returnCallback, null, PredefinedTypes.TYPE_NULL, paramFeed);
}
}

0 comments on commit f68cb63

Please sign in to comment.