PromucFlow_constructor/app/server/appsmith-plugins
shanid544 6fc632795b
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>
2024-11-15 12:32:41 +05:30
..
amazons3Plugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
anthropicPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
appsmithAiPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
arangoDBPlugin chore: Query Editor full width option (#37138) 2024-10-31 15:20:48 +05:30
awsLambdaPlugin fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00
databricksPlugin fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00
dynamoPlugin fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00
elasticSearchPlugin fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00
firestorePlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
googleAiPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
googleSheetsPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
graphqlPlugin chore: Upgrading spring to 3.3.3 to resolve vulnerable dependencies (#36266) 2024-10-01 22:12:56 +05:30
jsPlugin chore: Applied Spotless formatter (#25173) 2023-07-07 00:43:11 +05:30
mongoPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
mssqlPlugin chore: add spotless for sql files for postgres (#37016) 2024-11-09 11:21:30 +05:30
mysqlPlugin chore: add spotless for sql files for postgres (#37016) 2024-11-09 11:21:30 +05:30
openAiPlugin chore: Updating the copy in the action settings pane (#37260) 2024-11-07 00:20:52 +05:30
oraclePlugin chore: add spotless for sql files for postgres (#37016) 2024-11-09 11:21:30 +05:30
postgresPlugin chore: add spotless for sql files for postgres (#37016) 2024-11-09 11:21:30 +05:30
redisPlugin chore: Query Editor full width option (#37138) 2024-10-31 15:20:48 +05:30
redshiftPlugin chore: add spotless for sql files for postgres (#37016) 2024-11-09 11:21:30 +05:30
restApiPlugin chore: Upgrading spring to 3.3.3 to resolve vulnerable dependencies (#36266) 2024-10-01 22:12:56 +05:30
saasPlugin fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00
smtpPlugin fix: SMTP datasource should work without username and password (#37319) 2024-11-15 12:32:41 +05:30
snowflakePlugin chore(deps): bump net.snowflake:snowflake-jdbc from 3.13.29 to 3.20.0 in /app/server/appsmith-plugins/snowflakePlugin (#37175) 2024-11-15 07:29:14 +05:30
pom.xml fix: logger-not-working-in-plugins (#36231) 2024-09-17 10:47:58 +01:00