Closed sneha122 closed 21 hours ago
@sneha122 Shall I pick this?
@shanid544 Sure you can pick this up, could you please let me know what test case are you planning to add here?
@sneha122 Sure, I will do.
@sneha122 I used - docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog, able to reproduce the issue & fixed the issue, now we can configure SMTP with & without credentials. will test one more time & attach screenshots
Thanks @shanid544 that's super helpful, can you please add relevant test cases along with the fix if possible?
@sneha122 , Will update the existing test case
@sneha122 case 1: Suppose user adding username alone/password alone as the part of conf, test should pass, or custom error saying "Username and password must both be provided"?
@shanid544 Is it even possible to configure SMTP datasource with just the username or just the password? I don't think it is. But if it is then we need the test to pass in such cases
Hi @shanid544 Were you able to figure out the test cases for this issue? If yes are you planning to raise a PR for this anytime soon?
@sneha122 will raise soon
Could you please review this
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());
}
manual test: Manual Testing
Environment: Configured SMTP datasource using MailHog, set up via Docker. Steps: Configured the SMTP datasource in Appsmith without authentication (no username or password). Created a query to send an email. Result: Email was successfully sent without requiring authentication credentials.
Environment: Configured SMTP datasource using Gmail's SMTP server without providing authentication credentials. Steps: Set up the Gmail SMTP server in Appsmith without entering a username or password. Created a query to send an email. Result: Appsmith was able to connect to the Gmail SMTP server, and the email was successfully sent without authentication credentials. Expected Result: Error should be thrown due to missing authentication credentials. Gmail requires authentication for sending emails, so the system should reject connections without credentials. Go back and edit the datasource - add username and password, Will be able to send the mail.
Environment: Configured SMTP datasource using Gmail's SMTP server with valid username and password. Steps: Entered Gmail SMTP server details along with valid authentication credentials in Appsmith. Created a query to send an email. Result: Email was successfully sent using the authenticated configuration.
Thank you @shanid544 for the PR, I will review this
Is there an existing issue for this?
Description
When configuring SMTP datasource without username and password, always throws error
Invalid authentication credentials. Please check datasource configuration.
Ideally we should be able to configure SMTP datasource without username and password too. More specifically we need to remove the manual validations added on SMTP code base as it is possible to configure smtp service without password as well
Steps To Reproduce
Public Sample App
No response
Environment
Production
Severity
Medium (Frustrating UX)
Issue video log
No response
Version
v1.47