Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor messageContext initialization for enhanced scope availability in SamlAssertionConsumerFunction #5622

Merged
merged 4 commits into from May 20, 2024

Conversation

yeojin-dev
Copy link
Contributor

Motivation:

This PR addresses a code enhancement in the SamlAssertionConsumerFunction by initializing the messageContext variable at the beginning of the method. This change is intended to streamline the assignment and handling of messageContext, ensuring that it can be consistently used throughout the method, particularly in exception handling scenarios.

Modifications:

Declared messageContext at the method start to allow its use across the entire method scope, including within try and catch blocks.

Result:

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one nit but looks solid. Thanks @yeojin-dev ! 🙇 👍 🙇

@@ -56,14 +56,14 @@ attach it to your <type://Server>.
```java
// Specify information about your keystore.
// The keystore contains two key pairs, which are identified as 'signing' and 'encryption'.
KeyStoreCredentialResolver credentialResolver =
CredentialResolver credentialResolver =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is a mistake since CredentialResolver doesn't exist

Suggested change
CredentialResolver credentialResolver =
KeyStoreCredentialResolver credentialResolver =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think we need to change this one too. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops nevermind, I was looking at the wrong part of the code. The fix looks good 👍

Comment on lines +65 to +66
.keyPassword("signing", "N5^X[hvG")
.keyPassword("encryption", "N5^X[hvG")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍

@jrhee17 jrhee17 added the defect label Apr 19, 2024
@jrhee17 jrhee17 added this to the 1.29.0 milestone Apr 19, 2024
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @yeojin-dev!

@@ -116,7 +117,7 @@ public HttpResponse serve(ServiceRequestContext ctx, AggregatedHttpRequest req,

return ssoHandler.loginSucceeded(ctx, req, messageContext, sessionIndex, relayState);
} catch (SamlException e) {
return ssoHandler.loginFailed(ctx, req, null, e);
return ssoHandler.loginFailed(ctx, req, messageContext, e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this messageContext to the log message?


Could you also add a simple test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll. 🚀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating test cases for the use of messageContext is challenging due to the internal use of static methods (HttpRedirectBindingUtil and HttpPostBindingUtil) in the serve() function. While mocking static functions is possible, it is not a best practice and is not utilized by Armeria. To improve the design for easier testing, I propose the creation of SamlBindingProcessorFactory and SamlBindingProcessor interfaces. This structure allows for more flexible and maintainable code. Below is a brief example of how these interfaces could be implemented:

public interface SamlBindingProcessor {
    MessageContext<Response> toSamlObject(AggregatedHttpRequest request, String responseType);
}

public interface SamlBindingProcessorFactory {
    SamlBindingProcessor create(SamlBindingProtocol bindingProtocol);
}

public class DefaultSamlBindingProcessorFactory implements SamlBindingProcessorFactory {
    @Override
    public SamlBindingProcessor create(SamlBindingProtocol bindingProtocol) {
        switch (bindingProtocol) {
            case HTTP_REDIRECT:
                return new HttpRedirectBindingProcessor();
            case HTTP_POST:
                return new HttpPostBindingProcessor();
            default:
                throw new IllegalArgumentException("Unsupported binding protocol");
        }
    }
}

public class SamlAssertionConsumerFunction {
    private SamlBindingProcessor processor;

    public SamlAssertionConsumerFunction(SamlAssertionConsumerConfig cfg, String entityId,
                                         SamlBindingProcessorFactory processorFactory) {
        this.processor = processorFactory.create(cfg.endpoint().bindingProtocol());
        // Use the processor like this:
        // MessageContext<Response> messageContext = processor.toSamlObject(req, SAML_RESPONSE);
    }
}

These changes necessitate more extensive modifications than what is currently proposed in the PR. Please review and let me know if there are any misunderstandings or further adjustments needed. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I thought we could just use the existing test.
For example, we could probably capture the messageContext here and use it in the test where the login fails.
https://github.com/minwoox/armeria/blob/main/saml/src/test/java/com/linecorp/armeria/server/saml/SamlServiceProviderTest.java#L274

SamlBindingProcessorFactory and SamlBindingProcessor seems well-designed, but I'm uncertain whether customization is necessary. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yeojin-dev, I've added a test. 😉
8f87ec2

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once @minwoox's comment (add tests) is addressed. Thanks, @yeojin-dev!

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, @yeojin-dev! 😄

@minwoox minwoox merged commit 806556e into line:main May 20, 2024
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide debug hook point to trace original saml callback response
5 participants