Camel
  1. Camel
  2. CAMEL-2796

camel-spring-security: provide reference to the policy when authorization fails

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.0
    • Fix Version/s: 2.5.0
    • Component/s: None
    • Labels:
      None

      Description

      While it is possible to set up different policies to enforce authorization rules on Camel routes with the new camel-spring-security plugin, there is currently no way to tell which policy was being enforced when a org.springframework.security.SpringSecurityException is thrown. It would be very helpful to have a reference to the policy ID in the CamelAuthorizationException or in the headers so we can use it in the onException handlers.

      For example, let's say you have two policies:

        <authorizationPolicy id="user" access="ROLE_USER" authenticationManager="authenticationManager" accessDecisionManager="accessDecisionManager"
          xmlns="http://camel.apache.org/schema/spring-security" />
      
        <authorizationPolicy id="admin" access="ROLE_ADMIN" authenticationManager="authenticationManager" accessDecisionManager="accessDecisionManager"
          xmlns="http://camel.apache.org/schema/spring-security" />
      

      You also have two routes which use these policies:

          <camelContext id="myCamelContext" xmlns="http://camel.apache.org/schema/spring">
                <onException useOriginalMessage="true">
                  <exception>org.springframework.security.BadCredentialsException</exception>
                  <handled><constant>true</constant></handled>
                  <to uri="log:auth_error" />
              </onException>
              <route>
                  <from uri="direct:adminStart"/>
                  <policy ref="admin">
                      <to uri="mock:end"/>
                  </policy>
              </route>
              <route>
                  <from uri="direct:userStart"/>
                  <policy ref="admin">
                      <to uri="mock:end"/>
                  </policy>
              </route>
          </camelContext>
      

      Both of these routes will fail with an AccessDeniedException if the user doesn't have the proper role, but it would be desirable to handle exceptions for the ROLE_ADMIN failure differently than the ROLE_USER failure, maybe by using a <choice> element in the <onException> element.

      I know we have the CamelFailureEndpoint header, but this isn't very useful when more than one route uses the same authorization policy.

      1. CAMEL-2796.patch
        2 kB
        Paul Mietz Egli

        Activity

        Hide
        Willem Jiang added a comment -

        Hi Pual

        I already committed a patch into trunk , and you can get the PolicyId from the CamelAuthorizationException.
        If you want to use <choice> on the Exception, you can use simple expression "$exception.policyId" to check the PolicyId.

        Please check out the latest camel 2.4-SNAPSHOT to verify the fixing.

        Show
        Willem Jiang added a comment - Hi Pual I already committed a patch into trunk , and you can get the PolicyId from the CamelAuthorizationException. If you want to use <choice> on the Exception, you can use simple expression "$exception.policyId" to check the PolicyId. Please check out the latest camel 2.4-SNAPSHOT to verify the fixing.
        Hide
        Paul Mietz Egli added a comment - - edited

        Willem, thanks for the update. There is still an issue with this change, however. If the SpringSecurityAuthorizationPolicy.authenticateIfRequired() method throws an exception (for example, a BadCredentialsException in the case where the username or password is incorrect), the policy ID header will not be set, which means that it won't be available for the thrown CamelAuthorizationException. The fix is pretty easy – we just need to add the line which sets the policy ID header to the outer catch block. Working on a patch now.

        Show
        Paul Mietz Egli added a comment - - edited Willem, thanks for the update. There is still an issue with this change, however. If the SpringSecurityAuthorizationPolicy.authenticateIfRequired() method throws an exception (for example, a BadCredentialsException in the case where the username or password is incorrect), the policy ID header will not be set, which means that it won't be available for the thrown CamelAuthorizationException. The fix is pretty easy – we just need to add the line which sets the policy ID header to the outer catch block. Working on a patch now.
        Hide
        Paul Mietz Egli added a comment -

        patch to SpringSecurityAuthorizationPolicy that adds the policyId prior to throwing CamelAuthorizationException in the event of a RuntimeException, unit test to reproduce the issue/check for regression.

        Show
        Paul Mietz Egli added a comment - patch to SpringSecurityAuthorizationPolicy that adds the policyId prior to throwing CamelAuthorizationException in the event of a RuntimeException, unit test to reproduce the issue/check for regression.
        Hide
        Willem Jiang added a comment -

        Applied patch with thanks to Paul.

        Show
        Willem Jiang added a comment - Applied patch with thanks to Paul.
        Hide
        Claus Ibsen added a comment -

        Closing all resolved tickets from 2010 or older

        Show
        Claus Ibsen added a comment - Closing all resolved tickets from 2010 or older

          People

          • Assignee:
            Willem Jiang
            Reporter:
            Paul Mietz Egli
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development