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

Cassandra Connection String Changes #3585

Merged
merged 18 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 82 additions & 15 deletions Ginger/GingerCoreNET/Database/NoSqlBase/GingerCassandra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ limitations under the License.
#endregion

using Amdocs.Ginger.Common;
using Amdocs.Ginger.CoreNET.RunLib.CLILib;
using Cassandra;
using GingerCore.Actions;
using GingerCore.NoSqlBase.DataAccess;
using HBaseNet.Const;
using Microsoft.Graph;
using Microsoft.Graph.SecurityNamespace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Authentication;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand All @@ -37,32 +42,72 @@ public class GingerCassandra : NoSqlBase
dynamic myclass = null;
string mUDTName = null;



public override bool Connect()
{
try
{
string queryTimeoutString = "querytimeout=";
const string queryTimeoutString = "querytimeout=";
const string sslString = "ssl=";
int queryTimeout = 20000;//default timeout (20 seconds).
if (Db.DatabaseOperations.TNSCalculated.ToLower().Contains(queryTimeoutString.ToLower()))
string[] queryArray = Db.DatabaseOperations.TNSCalculated.ToLower().Split(';');
SSLOptions sslOptions = null;
for (int i = 1; i < queryArray.Length; i++)
{
string queryTimeoutValue = Db.DatabaseOperations.TNSCalculated.Substring(Db.DatabaseOperations.TNSCalculated.ToLower().IndexOf(queryTimeoutString.ToLower()) + queryTimeoutString.Length);
queryTimeout = Convert.ToInt32(queryTimeoutValue) * 1000;
switch (queryArray[i])
{
case var str when str.Contains(queryTimeoutString):
string queryTimeoutValue = str.Substring(str.IndexOf(queryTimeoutString) + queryTimeoutString.Length);
if (!int.TryParse(queryTimeoutValue, out int timeout))
{
throw new ArgumentException("Query timeout value is not a valid integer.");
}
queryTimeout = Convert.ToInt32(queryTimeoutValue) * 1000;
break;

case var str when str.Contains(sslString):
string sslValue = str.Substring(str.IndexOf(sslString) + sslString.Length);
sslOptions = SetupSslOptions(sslValue);
break;
default:
throw new ArgumentException("Please check connection string.");
}
}

string[] HostKeySpace = Db.DatabaseOperations.TNSCalculated.Split('/');
string[] HostKeySpace = queryArray[0].ToLower().Replace("http://", "").Replace("https://", "").Split('/');
string[] HostPort = HostKeySpace[0].Split(':');

if (HostPort.Length == 2)
{
if (string.IsNullOrEmpty(Db.Pass) && string.IsNullOrEmpty(Db.User))
{
cluster = Cluster.Builder().AddContactPoint(HostPort[0]).WithPort(Int32.Parse(HostPort[1])).WithQueryTimeout(queryTimeout).Build();
if (sslOptions ==null)
{
cluster = Cluster.Builder().AddContactPoint(HostPort[0]).WithPort(Int32.Parse(HostPort[1])).WithQueryTimeout(queryTimeout).Build();
}
else
{
cluster = Cluster.Builder()
.AddContactPoint(HostPort[0])
.WithPort(Int32.Parse(HostPort[1]))
.WithSSL(sslOptions)
.WithQueryTimeout(queryTimeout)
.Build();
}
}
else
{
cluster = Cluster.Builder().WithCredentials(Db.User.ToString(), Db.Pass.ToString()).AddContactPoint(HostPort[0]).WithPort(Int32.Parse(HostPort[1])).WithQueryTimeout(queryTimeout).Build();
if (sslOptions == null) {
cluster = Cluster.Builder().WithCredentials(Db.User.ToString(), Db.Pass.ToString()).AddContactPoint(HostPort[0]).WithPort(Int32.Parse(HostPort[1])).WithQueryTimeout(queryTimeout).Build();
}
else
{
cluster = Cluster.Builder()
.AddContactPoint(HostPort[0])
.WithPort(Int32.Parse(HostPort[1]))
.WithAuthProvider(new PlainTextAuthProvider(Db.User, Db.Pass))
.WithSSL(sslOptions)
.WithQueryTimeout(queryTimeout)
.Build();
}
}
}
else
Expand All @@ -85,16 +130,16 @@ public override bool Connect()
}
catch (Exception e)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to connect to Cassandra DB", e);
throw (e);
Reporter.ToLog(eLogLevel.ERROR, "Failed to connect to Cassandra DB. Please check Connection String", e);
throw;
}
}

public override bool MakeSureConnectionIsOpen()
{
try
{
if (session != null)
if (session != null && !session.IsDisposed)
aditydha marked this conversation as resolved.
Show resolved Hide resolved
{
Metadata m = cluster.Metadata;
ICollection<string> Keyspaces = m.GetKeyspaces();
Expand Down Expand Up @@ -182,8 +227,15 @@ public override Task<List<string>> GetColumnList(string tablename)

private void Disconnect()
{
session.Dispose();
cluster.Dispose();
try
{
session.Dispose();
cluster.Dispose();
}
finally
{
session = null;
}
}

public Type TypeConverter(RowSet RS, string Type1)
Expand Down Expand Up @@ -789,5 +841,20 @@ void UpdateOutValues(RowSet RS, Type t = null)
i++;
}
}

public static SSLOptions SetupSslOptions(string sslParamValue)
{
try
{
var sslProtocol = Enum.Parse<SslProtocols>(sslParamValue, ignoreCase: true);
return new SSLOptions(sslProtocol, false, (_, _, _, _) => true);
}
catch (ArgumentException e)
{
Reporter.ToLog(eLogLevel.ERROR, "SSL value not correct. Please enter SSL value like Default, None, Ssl2, Ssl3, Tls, Tls11, Tls12, Tls13", e);
Reporter.ToLog(eLogLevel.ERROR, "Note - Also try removing ssl= parameter from connection string", e);
throw new ArgumentException("SSL value not correct. Please enter SSL value like Default, None, Ssl2, Ssl3, Tls, Tls11, Tls12, Tls13.");
}
}
}
}
6 changes: 4 additions & 2 deletions Ginger/GingerCoreNET/Database/NoSqlBase/GingerCouchbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ private string GetBucketName(string inputSQL)
{
string bucketName = string.Empty;
string[] bucketNameArray;
const string strUpdate = "update ";
const string strFrom = " from ";
if (Action == ActDBValidation.eDBValidationType.RecordCount)
{
bucketName = inputSQL.Replace("`", "");
Expand All @@ -164,11 +166,11 @@ private string GetBucketName(string inputSQL)
{
if (Action == ActDBValidation.eDBValidationType.UpdateDB)
{
bucketName = inputSQL.Substring(inputSQL.ToLower().IndexOf("Update ") + 7);
bucketName = inputSQL.Substring(inputSQL.IndexOf(strUpdate, StringComparison.OrdinalIgnoreCase) + strUpdate.Length);
}
else
{
bucketName = inputSQL.Substring(inputSQL.ToLower().IndexOf(" from ") + 6);
bucketName = inputSQL.Substring(inputSQL.IndexOf(strFrom, StringComparison.OrdinalIgnoreCase) + strFrom.Length);
}
bucketNameArray = bucketName.Split('.');
bucketName = bucketNameArray[0].Trim();
Expand Down
Loading