Skip to content

Parse SslMode from connection string for MySQL#4488

Merged
ravirk91 merged 2 commits into
masterfrom
Defect/61207-SSL
Apr 29, 2026
Merged

Parse SslMode from connection string for MySQL#4488
ravirk91 merged 2 commits into
masterfrom
Defect/61207-SSL

Conversation

@tanushahande2003
Copy link
Copy Markdown
Contributor

@tanushahande2003 tanushahande2003 commented Apr 29, 2026

Add logic to extract and set the SslMode property from the calculated connection string if present. Only assign Database.ConnectionString if the calculated string is empty. This improves MySQL connection configuration flexibility.

Description

Type of Change

  • Bug fix - [ ] New feature - [ ] Breaking change - [ ] Plugin update

Checklist

  • PR description clearly describes the changes
  • Target branch is correct (master for features, Releases/* for fixes)
  • Latest code from target branch merged
  • No commented/junk code included
  • No new build warnings or errors
  • All existing unit tests pass
  • New unit tests added for new functionality
  • Cross-platform compatibility verified (Windows/Linux/macOS)
  • CI/CD pipeline passes
  • Code follows project conventions (Act{Platform}{Type}, {Platform}Driver)
  • Repository objects use [IsSerializedForLocalRepository] where needed
  • Error handling uses Reporter.ToLog() pattern
  • Documentation updated for user-facing changes
  • Self-review completed and code review comments addressed

Summary by CodeRabbit

  • Bug Fixes
    • Improved MySQL database connection handling to properly respect SSL/TLS configuration settings, ensuring more reliable and secure encrypted database connections.

Add logic to extract and set the SslMode property from the calculated connection string if present. Only assign Database.ConnectionString if the calculated string is empty. This improves MySQL connection configuration flexibility.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2026

Walkthrough

MySQL connection handling in the Connect method now conditionally parses SslMode from the decrypted connection string instead of unconditionally overwriting it. The logic extracts SslMode values via Enum.TryParse and applies them selectively when appropriate.

Changes

Cohort / File(s) Summary
MySQL Connection String Handling
Ginger/GingerCoreNET/Database/DatabaseOperations.cs
Refactored MySQL connection setup to conditionally derive and apply SslMode from ConnectionStringCalculated. Replaced unconditional connection string assignment with fallback logic that parses SslMode enum values (case-insensitive) and applies overrides only when needed, preserving original computed connection strings.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A whisker-twitch of MySQL care,
Connection strings parsed with flair!
SslMode dances, enum-wise,
No overwrites—a clever prize.
Fallback logic hops just right,
Encryption settings shining bright! 🔐✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description lacks critical details: no type of change is selected, and most checklist items remain unchecked with no verification evidence provided. Select the appropriate type of change, complete checklist items with verification details, and confirm unit tests and cross-platform compatibility.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: parsing SslMode from the connection string for MySQL connections.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch Defect/61207-SSL

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs`:
- Around line 492-519: The Connect() path currently parses SslMode from
ConnectionStringCalculated but never updates Database.ConnectionString when
ConnectionStringCalculated is non-empty, leaving Database.ConnectionString out
of sync; after you finish parsing/applying values from
ConnectionStringCalculated (i.e. after the loop where you handle SslMode in
DatabaseOperations.Connect or equivalent), set Database.ConnectionString =
my.ConnectionString so Database.ConnectionString always reflects the effective
connection string used by the MySql connection (ensure this is done both when
ConnectionStringCalculated is empty and when it is non-empty so
GetConnectionString()/Connect() invariants hold).
- Around line 505-509: The parsing of SslMode using Enum.TryParse silently
ignores invalid values so a typo downgrades the config; update the block around
Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode) (the code that
assigns my.SslMode) to fail fast: if TryParse returns false, throw an
informative exception (e.g., ArgumentException or FormatException) that includes
the invalid value and expected MySqlSslMode enum names; otherwise assign
my.SslMode as before. This ensures misconfigured SslMode is surfaced
immediately.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: fb62419b-b83a-4884-ac58-09c2464a12ed

📥 Commits

Reviewing files that changed from the base of the PR and between bccf642 and 6cb4c89.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/Database/DatabaseOperations.cs

Comment on lines +492 to +519
if (!string.IsNullOrEmpty(ConnectionStringCalculated))
{
foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}

string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}

break;
}
}
}

if (string.IsNullOrEmpty(ConnectionStringCalculated))
{
Database.ConnectionString = my.ConnectionString;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Keep Database.ConnectionString aligned with the effective MySQL connection.

This now leaves Database.ConnectionString untouched whenever ConnectionStringCalculated is non-empty. For inputs like SslMode=Required, Connect() succeeds with my.ConnectionString, but ConnectionStringCalculated/GetConnectionString() still resolve to that partial fragment afterward instead of the real connection string. That is a regression from the previous invariant and can break callers that inspect the effective value after Connect().

Suggested fix
-                        if (!string.IsNullOrEmpty(ConnectionStringCalculated))
+                        string calculatedConnectionString = ConnectionStringCalculated;
+                        if (!string.IsNullOrEmpty(calculatedConnectionString))
                         {
-                            foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
+                            foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
                             {
                                 int eq = segment.IndexOf('=');
                                 if (eq <= 0)
                                 {
                                     continue;
@@
                                 }
                             }
                         }
-
-                        if (string.IsNullOrEmpty(ConnectionStringCalculated))
-                        {
-                            Database.ConnectionString = my.ConnectionString;
-                        }
+                        Database.ConnectionString = my.ConnectionString;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!string.IsNullOrEmpty(ConnectionStringCalculated))
{
foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}
string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
break;
}
}
}
if (string.IsNullOrEmpty(ConnectionStringCalculated))
{
Database.ConnectionString = my.ConnectionString;
}
string calculatedConnectionString = ConnectionStringCalculated;
if (!string.IsNullOrEmpty(calculatedConnectionString))
{
foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
}
string key = segment.Substring(0, eq).Trim();
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
break;
}
}
}
Database.ConnectionString = my.ConnectionString;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 492 - 519,
The Connect() path currently parses SslMode from ConnectionStringCalculated but
never updates Database.ConnectionString when ConnectionStringCalculated is
non-empty, leaving Database.ConnectionString out of sync; after you finish
parsing/applying values from ConnectionStringCalculated (i.e. after the loop
where you handle SslMode in DatabaseOperations.Connect or equivalent), set
Database.ConnectionString = my.ConnectionString so Database.ConnectionString
always reflects the effective connection string used by the MySql connection
(ensure this is done both when ConnectionStringCalculated is empty and when it
is non-empty so GetConnectionString()/Connect() invariants hold).

Comment on lines +505 to +509
string value = segment.Substring(eq + 1).Trim();
if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
{
my.SslMode = sslMode;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fail fast on invalid SslMode values.

If Enum.TryParse fails here, the code silently keeps the builder default and still opens the connection. A typo in SslMode then becomes a hidden config downgrade instead of an actionable error.

Suggested fix
                                 if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
                                 {
                                     string value = segment.Substring(eq + 1).Trim();
-                                    if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
-                                    {
-                                        my.SslMode = sslMode;
-                                    }
+                                    if (!Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
+                                    {
+                                        throw new ArgumentException($"Invalid MySQL SslMode '{value}'.");
+                                    }
+
+                                    my.SslMode = sslMode;
 
                                     break;
                                 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 505 - 509,
The parsing of SslMode using Enum.TryParse silently ignores invalid values so a
typo downgrades the config; update the block around Enum.TryParse(value,
ignoreCase: true, out MySqlSslMode sslMode) (the code that assigns my.SslMode)
to fail fast: if TryParse returns false, throw an informative exception (e.g.,
ArgumentException or FormatException) that includes the invalid value and
expected MySqlSslMode enum names; otherwise assign my.SslMode as before. This
ensures misconfigured SslMode is surfaced immediately.

@ravirk91 ravirk91 merged commit 29052ac into master Apr 29, 2026
5 of 6 checks passed
@ravirk91 ravirk91 deleted the Defect/61207-SSL branch April 29, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants