Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: SMTP datasource should work without username and password (#37319)
## Description ### Bug Description Issue: When configuring an SMTP datasource without a username and password, the system throws an error: “Invalid authentication credentials. Please check datasource configuration.” Expected Behavior: The system should allow SMTP datasource configuration without requiring authentication credentials, as it is possible to connect to some SMTP servers without a username or password. **Steps To Reproduce** Use any email service for configuring SMTP datasource without setting up username and password Create SMTP datasource with host port, keeping username and password blank Test the configuration **Root Cause** Manual validation in the codebase was enforcing that both username and password fields are mandatory for SMTP configuration. This validation prevented the successful configuration of SMTP services that do not require authentication. **Solution Details** _To fix this, the following changes were implemented: Updated Validation Method (validateDatasource)_ Before: Enforced mandatory username and password validation. After: Removed the strict validation check for authentication fields, allowing for configurations without credentials. ``` if (authentication == null || !StringUtils.hasText(authentication.getUsername()) || !StringUtils.hasText(authentication.getPassword())) { invalids.add(new AppsmithPluginException(AppsmithPluginError.PLUGIN_AUTHENTICATION_ERROR).getMessage()); } ``` _Modified the SMTP Session Creation (datasourceCreate)_ Before: Always initialized the SMTP session with authentication, assuming credentials were required. After: Updated the session creation to support both authenticated and unauthenticated configurations. ``` Properties prop = new Properties(); prop.put("mail.smtp.auth", "false"); // Default to no authentication if (authentication != null && StringUtils.hasText(authentication.getUsername()) && StringUtils.hasText(authentication.getPassword())) { prop.put("mail.smtp.auth", "true"); session = Session.getInstance(prop, new Authenticator() { @OverRide protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); } else { session = Session.getInstance(prop); } ``` _Enhanced Testing for Authentication Scenarios (testDatasource)_ Before: Errors were logged if authentication failed, even for servers where authentication wasn’t required. After: Introduced a flag to detect if authentication was required based on the session configuration, and adjusted error handling accordingly. ``` boolean isAuthRequired = "true".equals(connection.getProperty("mail.smtp.auth")); if (isAuthRequired && transport != null) { try { transport.connect(); } catch (AuthenticationFailedException e) { invalids.add(SMTPErrorMessages.DS_AUTHENTICATION_FAILED_ERROR_MSG); } } ``` **Testing and Verification** **Unit Tests** Without Authentication: _Updated testNullAuthentication test case to ensure no errors are returned when authentication is absent._ ``` @test public void testNullAuthentication() { DatasourceConfiguration invalidDatasourceConfiguration = createDatasourceConfiguration(); invalidDatasourceConfiguration.setAuthentication(null); assertEquals(0, pluginExecutor.validateDatasource(invalidDatasourceConfiguration).size()); } ``` Fixes #37271 ## Automation /ok-to-test tags="" updated testNullAuthentication() from SmtpPluginTest class <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced SMTP plugin now supports conditional authentication during session creation. - Improved error handling for authentication failures, providing clearer validation results. - Added support for testing SMTP connections without authentication. - **Bug Fixes** - Streamlined validation logic for datasource configurations, particularly for authentication scenarios. - **Documentation** - Updated test methods to clarify expected error messages for invalid configurations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: muhammed.shanid@zemosolabs.com <muhammed.shanid@zemosolabs.com>
- Loading branch information