You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2023/01/18 07:46:39 UTC
[GitHub] [pulsar] michaeljmarshall opened a new pull request, #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
michaeljmarshall opened a new pull request, #19270:
URL: https://github.com/apache/pulsar/pull/19270
### Motivation
The current `ServerCnx` validates the proxy role on certain protocol messages. This is unnecessary. Instead, we should verify that the `authRole` and the `originalPrincipal` are a valid combination before going to the `Connected` state.
Note that the one "breaking" change is to prevent connections where the `originalPrincipal` is set with an `authRole` that is not a proxy role. This is an invalid state that was allowed to persist before. That state was not a vulnerability because the `isTopicOperationAllowed` validates that both the `originalPrincipal` and the `authRole` have permission to perform an operation.
### Modifications
* Replace `invalidOriginalPrincipal` with `validateRoleAndOriginalPrincipal`.
* Remove calls to `invalidOriginalPrincipal`.
* Add call to `validateRoleAndOriginalPrincipal` when transitioning from `Connecting` to `Connected` state.
### Verifying this change
It's likely that we should add tests for this change. I haven't found a good place to do so yet. The `ServerCnxAuthorizationTest` seems like a good candidate, but it is pretty verbose and relies heavily on mocking. It would be very helpful to add authentication tests that operate similarly to `ServerCnxTest`.
### Documentation
- [x] `doc`
I updated the appropriate Javadocs.
### Matching PR in forked repository
PR in forked repository: https://github.com/michaeljmarshall/pulsar/pull/13
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1516798666
The docs were updated here: https://github.com/apache/pulsar-site/pull/408.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073817281
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
+ String errorMsg = null;
+ if (proxyRoles.contains(authRole)) {
+ if (StringUtils.isBlank(originalPrincipal)) {
+ errorMsg = "originalPrincipal must be provided when connecting with a proxy role.";
+ } else if (proxyRoles.contains(originalPrincipal)) {
+ errorMsg = "originalPrincipal cannot be a proxy role.";
+ }
+ } else if (StringUtils.isNotBlank(originalPrincipal)) {
+ errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
+ }
+ if (errorMsg != null) {
+ service.getPulsarStats().recordConnectionCreateFail();
Review Comment:
I considered this, but I decided not to because throwing an exception is expensive and at this point in the `ServerCnx` lifecycle, I think we should avoid using exceptions as flow control when possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] momo-jun commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "momo-jun (via GitHub)" <gi...@apache.org>.
momo-jun commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1436245444
Hi @michaeljmarshall did you have any plans to update the protocol spec for this cleanup? Just want to make sure it can catch up with the release schedule.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073983457
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ServerCnxTest.java:
##########
@@ -435,6 +435,55 @@ public void testConnectCommandWithAuthenticationPositive() throws Exception {
channel.finish();
}
+ @Test(timeOut = 30000)
+ public void testConnectCommandWithInvalidRoleCombinations() throws Exception {
+ AuthenticationService authenticationService = mock(AuthenticationService.class);
+ AuthenticationProvider authenticationProvider = new BasicCommandAuthenticationProvider();
+ String authMethodName = authenticationProvider.getAuthMethodName();
+
+ when(brokerService.getAuthenticationService()).thenReturn(authenticationService);
+ when(authenticationService.getAuthenticationProvider(authMethodName)).thenReturn(authenticationProvider);
+ svcConfig.setAuthenticationEnabled(true);
+ svcConfig.setAuthorizationEnabled(true);
+ svcConfig.setProxyRoles(Collections.singleton("proxy"));
+
+ resetChannel();
+ assertTrue(channel.isActive());
+ assertEquals(serverCnx.getState(), State.Start);
+
+ // Invalid combinations where authData is proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", "");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", null);
+ // Invalid combinations where original principal is set to a proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "", "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, null, "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "client", "proxy");
+ // Invalid combinations where the original principal is set to a non-proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "", "some_user");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, null, "some_user");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "client", "some_user");
+ }
+
+ private void verifyAuthRoleAndOriginalPrincipalBehavior(String authMethodName, String authData,
+ String originalPrincipal) throws Exception {
+ resetChannel();
+ assertTrue(channel.isActive());
+ assertEquals(serverCnx.getState(), State.Start);
+
+ ByteBuf clientCommand = Commands.newConnect(authMethodName, authData, 1,null,
+ null, originalPrincipal, null, null);
+ channel.writeInbound(clientCommand);
+
+ Object response1 = getResponse();
+ assertTrue(response1 instanceof CommandConnected);
Review Comment:
In order to get this test to pass, I needed to add this verification because we send the response first. It'd be ideal to only send the `CommandError`, if possible.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -965,15 +961,23 @@ protected void handleConnect(CommandConnect connect) {
remoteAddress, originalPrincipal);
}
}
+ if (service.isAuthorizationEnabled()) {
+ validateRoleAndOriginalPrincipal();
+ }
Review Comment:
It's not always correct to verify this here. Specifically, the authentication might not be complete yet in the case of SASL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nicoloboschi commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
nicoloboschi commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073310854
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
I don't see it neither in the master branch and this branch 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1096633025
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ServerCnxTest.java:
##########
@@ -435,6 +435,55 @@ public void testConnectCommandWithAuthenticationPositive() throws Exception {
channel.finish();
}
+ @Test(timeOut = 30000)
+ public void testConnectCommandWithInvalidRoleCombinations() throws Exception {
+ AuthenticationService authenticationService = mock(AuthenticationService.class);
+ AuthenticationProvider authenticationProvider = new BasicCommandAuthenticationProvider();
+ String authMethodName = authenticationProvider.getAuthMethodName();
+
+ when(brokerService.getAuthenticationService()).thenReturn(authenticationService);
+ when(authenticationService.getAuthenticationProvider(authMethodName)).thenReturn(authenticationProvider);
+ svcConfig.setAuthenticationEnabled(true);
+ svcConfig.setAuthorizationEnabled(true);
+ svcConfig.setProxyRoles(Collections.singleton("proxy"));
+
+ resetChannel();
+ assertTrue(channel.isActive());
+ assertEquals(serverCnx.getState(), State.Start);
+
+ // Invalid combinations where authData is proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", "");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "proxy", null);
+ // Invalid combinations where original principal is set to a proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "", "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, null, "proxy");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "client", "proxy");
+ // Invalid combinations where the original principal is set to a non-proxy role
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "", "some_user");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, null, "some_user");
+ verifyAuthRoleAndOriginalPrincipalBehavior(authMethodName, "client", "some_user");
+ }
+
+ private void verifyAuthRoleAndOriginalPrincipalBehavior(String authMethodName, String authData,
+ String originalPrincipal) throws Exception {
+ resetChannel();
+ assertTrue(channel.isActive());
+ assertEquals(serverCnx.getState(), State.Start);
+
+ ByteBuf clientCommand = Commands.newConnect(authMethodName, authData, 1,null,
+ null, originalPrincipal, null, null);
+ channel.writeInbound(clientCommand);
+
+ Object response1 = getResponse();
+ assertTrue(response1 instanceof CommandConnected);
Review Comment:
Fixed by https://github.com/apache/pulsar/pull/19409
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1096632925
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -965,15 +961,23 @@ protected void handleConnect(CommandConnect connect) {
remoteAddress, originalPrincipal);
}
}
+ if (service.isAuthorizationEnabled()) {
+ validateRoleAndOriginalPrincipal();
+ }
} catch (Exception e) {
- service.getPulsarStats().recordConnectionCreateFail();
logAuthException(remoteAddress, "connect", getPrincipal(), Optional.empty(), e);
- String msg = "Unable to authenticate";
- writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg));
- close();
+ closeForAuthenticationError();
}
}
+ private void closeForAuthenticationError() {
+ state = State.Failed;
+ service.getPulsarStats().recordConnectionCreateFail();
+ String msg = "Unable to authenticate";
+ writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg));
+ close();
Review Comment:
This is done in the latest implementation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1096633075
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -965,15 +961,23 @@ protected void handleConnect(CommandConnect connect) {
remoteAddress, originalPrincipal);
}
}
+ if (service.isAuthorizationEnabled()) {
+ validateRoleAndOriginalPrincipal();
+ }
Review Comment:
Fixed by https://github.com/apache/pulsar/pull/19409 because the authentication protocol is serialized to make it easier to ensure we only verify the roles when authentication is completed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1416974106
@lhotari @nicoloboschi PTAL.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073321097
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
In this PR.
Or https://github.com/apache/pulsar/blob/master/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java#L873
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073818425
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
> one note is the previous code perform the checks only if authorization is also enabled, but I think it was wrong since these checks are about authentication
Good point. The code path checks for authentication but not authorization. I'll fix that. (As an aside, I wonder who is running with authentication and without authorization.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073283182
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
+ String errorMsg = null;
+ if (proxyRoles.contains(authRole)) {
+ if (StringUtils.isBlank(originalPrincipal)) {
+ errorMsg = "originalPrincipal must be provided when connecting with a proxy role.";
+ } else if (proxyRoles.contains(originalPrincipal)) {
+ errorMsg = "originalPrincipal cannot be a proxy role.";
+ }
+ } else if (StringUtils.isNotBlank(originalPrincipal)) {
+ errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
+ }
+ if (errorMsg != null) {
+ service.getPulsarStats().recordConnectionCreateFail();
Review Comment:
I agreed with @nicoloboschi.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
See line 837, checked.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1416970736
> Note that the one "breaking" change is to prevent connections where the originalPrincipal is set with an authRole that is not a proxy role. This is an invalid state that was allowed to persist before. That state was not a vulnerability because the isTopicOperationAllowed validates that both the originalPrincipal and the authRole have permission to perform an operation.
I removed this logic from the PR to make it easier to accept. It would be good to submit a follow up PR to propose making the broker's logic stricter.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] lhotari merged pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #19270:
URL: https://github.com/apache/pulsar/pull/19270
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1430236553
I cherry-picked this change to the release branches because https://github.com/apache/pulsar/pull/19455 depends on it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by "nodece (via GitHub)" <gi...@apache.org>.
nodece commented on PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#issuecomment-1418822323
/pulsarbot rerun-failure-checks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nicoloboschi commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
nicoloboschi commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073208937
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
one note is the previous code perform the checks only if authorization is also enabled, but I think it was wrong since these checks are about authentication
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
+ String errorMsg = null;
+ if (proxyRoles.contains(authRole)) {
+ if (StringUtils.isBlank(originalPrincipal)) {
+ errorMsg = "originalPrincipal must be provided when connecting with a proxy role.";
+ } else if (proxyRoles.contains(originalPrincipal)) {
+ errorMsg = "originalPrincipal cannot be a proxy role.";
+ }
+ } else if (StringUtils.isNotBlank(originalPrincipal)) {
+ errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
+ }
+ if (errorMsg != null) {
+ service.getPulsarStats().recordConnectionCreateFail();
Review Comment:
what about throwing an exception in order to let the caller code to catch it?
In this case there's the exactly same code in case of Exception
https://github.com/apache/pulsar/blob/a0e603a3eaf822cb0c41f292ad5845ee54df0ccf/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java#L968-L973
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073824302
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
+ String errorMsg = null;
+ if (proxyRoles.contains(authRole)) {
+ if (StringUtils.isBlank(originalPrincipal)) {
+ errorMsg = "originalPrincipal must be provided when connecting with a proxy role.";
+ } else if (proxyRoles.contains(originalPrincipal)) {
+ errorMsg = "originalPrincipal cannot be a proxy role.";
+ }
+ } else if (StringUtils.isNotBlank(originalPrincipal)) {
+ errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
+ }
+ if (errorMsg != null) {
+ service.getPulsarStats().recordConnectionCreateFail();
Review Comment:
I extracted the code into a shared method so we can avoid an exception and avoid the code duplication.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073817281
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
+ String errorMsg = null;
+ if (proxyRoles.contains(authRole)) {
+ if (StringUtils.isBlank(originalPrincipal)) {
+ errorMsg = "originalPrincipal must be provided when connecting with a proxy role.";
+ } else if (proxyRoles.contains(originalPrincipal)) {
+ errorMsg = "originalPrincipal cannot be a proxy role.";
+ }
+ } else if (StringUtils.isNotBlank(originalPrincipal)) {
+ errorMsg = "cannot specify originalPrincipal when connecting without valid proxy role.";
+ }
+ if (errorMsg != null) {
+ service.getPulsarStats().recordConnectionCreateFail();
Review Comment:
I considered this, but I decided not to because throwing an exception is expensive and at this point in the `ServerCnx` lifecycle, I think we should avoid using exceptions as control flow when possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
michaeljmarshall commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1073818425
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -395,17 +395,32 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
ctx.close();
}
- /*
- * If authentication and authorization is enabled(and not sasl) and
- * if the authRole is one of proxyRoles we want to enforce
- * - the originalPrincipal is given while connecting
- * - originalPrincipal is not blank
- * - originalPrincipal is not a proxy principal
+ /**
+ * When transitioning from Connecting to Connected, this class validates that the {@link #authRole} and the
+ * {@link #originalPrincipal} are a valid combination. Valid combinations fulfill the following rule:
+ * <p>
+ * The {@link #authRole} is in {@link #proxyRoles}, if, and only if, the {@link #originalPrincipal} is set to a role
+ * that is not also in {@link #proxyRoles}.
*/
- private boolean invalidOriginalPrincipal(String originalPrincipal) {
- return (service.isAuthenticationEnabled() && service.isAuthorizationEnabled()
- && proxyRoles.contains(authRole) && (StringUtils.isBlank(originalPrincipal)
- || proxyRoles.contains(originalPrincipal)));
+ private void validateRoleAndOriginalPrincipal() {
Review Comment:
> one note is the previous code perform the checks only if authorization is also enabled, but I think it was wrong since these checks are about authentication
Fair point. The code path checks for authentication but not authorization. I'll fix that. (As an aside, I wonder who is running with authentication and without authorization.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] lhotari commented on a diff in pull request #19270: [cleanup][broker] Validate originalPrincipal earlier in ServerCnx
Posted by GitBox <gi...@apache.org>.
lhotari commented on code in PR #19270:
URL: https://github.com/apache/pulsar/pull/19270#discussion_r1082536276
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java:
##########
@@ -965,15 +961,23 @@ protected void handleConnect(CommandConnect connect) {
remoteAddress, originalPrincipal);
}
}
+ if (service.isAuthorizationEnabled()) {
+ validateRoleAndOriginalPrincipal();
+ }
} catch (Exception e) {
- service.getPulsarStats().recordConnectionCreateFail();
logAuthException(remoteAddress, "connect", getPrincipal(), Optional.empty(), e);
- String msg = "Unable to authenticate";
- writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg));
- close();
+ closeForAuthenticationError();
}
}
+ private void closeForAuthenticationError() {
+ state = State.Failed;
+ service.getPulsarStats().recordConnectionCreateFail();
+ String msg = "Unable to authenticate";
+ writeAndFlush(Commands.newError(-1, ServerError.AuthenticationError, msg));
+ close();
Review Comment:
It's already like this in the original code, but I'd assume that `ctx.writeAndFlush(...).addListener(ChannelFutureListener.CLOSE);` (or `org.apache.pulsar.common.util.netty.NettyChannelUtil.writeAndFlushWithClosePromise`) should be used so that the channel doesn't get closed before the error has been written to the channel.
https://github.com/apache/pulsar/blob/d6fcdb8e1e192e0d9e2fbe6307d273b27ed2930f/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/NettyChannelUtil.java#L59-L61
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org