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

SoupAPI Action password Encryption #4038

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
using Amdocs.Ginger.Common;
using GingerCore;
using GingerCore.GeneralLib;
using System;
using System.Windows;
using System.Windows.Controls;
using CheckBox = System.Windows.Controls.CheckBox;
Expand Down Expand Up @@ -139,7 +140,14 @@ private void xSMTPPassTextBox_LostFocus(object sender, RoutedEventArgs e)
/// </summary>
private void CertificatePasswordUCValueExpression_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
EncryptPasswordIfNeeded();
try
{
EncryptPasswordIfNeeded();
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to encrypt certificate password", ex);
}
}

/// <summary>
Expand All @@ -148,18 +156,29 @@ private void CertificatePasswordUCValueExpression_LostKeyboardFocus(object sende
/// <returns>True if it is a value expression, otherwise false.</returns>
private bool IsPasswordValueExpression()
{
return ValueExpression.IsThisAValueExpression(CertificatePasswordUCValueExpression.ValueTextBox.Text);
return CertificatePasswordUCValueExpression?.ValueTextBox?.Text != null && ValueExpression.IsThisAValueExpression(CertificatePasswordUCValueExpression.ValueTextBox.Text);
}

/// <summary>
/// Encrypts the password in CertificatePasswordUCValueExpression if it is not already encrypted.
/// </summary>
private void EncryptPasswordIfNeeded()
{
string password = CertificatePasswordUCValueExpression.ValueTextBox.Text;
if (!string.IsNullOrEmpty(password) && !IsPasswordValueExpression() && !EncryptionHandler.IsStringEncrypted(password))
try
{
if (string.IsNullOrEmpty(CertificatePasswordUCValueExpression.ValueTextBox.Text))
{
return;
}
string password = CertificatePasswordUCValueExpression.ValueTextBox.Text;
if (!string.IsNullOrEmpty(password) && !IsPasswordValueExpression() && !EncryptionHandler.IsStringEncrypted(password))
{
CertificatePasswordUCValueExpression.ValueTextBox.Text = EncryptionHandler.EncryptwithKey(password);
}
}
catch (Exception ex)
{
CertificatePasswordUCValueExpression.ValueTextBox.Text = EncryptionHandler.EncryptwithKey(password);
Reporter.ToLog(eLogLevel.ERROR, "Failed to encrypt certificate password", ex);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions Ginger/GingerCoreCommon/Actions/Act.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,5 +2119,32 @@ public bool AreEqual(object obj)
return AreEqual(obj as Act);
}

/// <summary>
/// Decrypts the given password. If the password is a value expression, it evaluates the expression before decryption.
/// </summary>
/// <param name="password">The password to decrypt.</param>
/// <param name="isPasswordValueExpression">Indicates if the password is a value expression.</param>
/// <param name="act">The Act instance containing the value expression.</param>
/// <returns>The decrypted password.</returns>
public static string DecryptPassword(string password, bool isPasswordValueExpression, Act act)
Copy link
Contributor

Choose a reason for hiding this comment

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

Move this method to GingerCoreNet proejct - > GeneralLib/General.cs class

{
if (password == null)
{
return null;
}

string decryptedPassword = string.Empty;
string evaluatedValue = password;

if (isPasswordValueExpression)
{
act.ValueExpression.Value = password;
evaluatedValue = act.ValueExpression.ValueCalculated;
}

decryptedPassword = EncryptionHandler.IsStringEncrypted(evaluatedValue) ? EncryptionHandler.DecryptwithKey(evaluatedValue) : evaluatedValue;

return decryptedPassword;
}
}
}
23 changes: 0 additions & 23 deletions Ginger/GingerCoreCommon/Actions/Webservices/ActSoapUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,6 @@ public bool isActionExecuted
return (System.IO.Directory.Exists(LastExecutionFolderPath));
}
}
/// <summary>
/// Decrypts the given password. If the password is a value expression, it evaluates the expression first.
/// </summary>
/// <param name="password">The password to decrypt.</param>
/// <param name="isPasswordValueExpression">Indicates if the password is a value expression.</param>
/// <returns>The decrypted password.</returns>
public string DecryptPassword(string password, bool isPasswordValueExpression)
{
string decryptedPassword = string.Empty;

if (isPasswordValueExpression)
{
this.ValueExpression.Value = password;
string evaluatedValue = this.ValueExpression.ValueCalculated;

decryptedPassword = EncryptionHandler.IsStringEncrypted(evaluatedValue) ? EncryptionHandler.DecryptwithKey(evaluatedValue) : evaluatedValue;
}
else
{
decryptedPassword = EncryptionHandler.IsStringEncrypted(password) ? EncryptionHandler.DecryptwithKey(password) : password;
}

return decryptedPassword;
}
}
}
23 changes: 0 additions & 23 deletions Ginger/GingerCoreNET/ActionsLib/Webservices/ActWebAPIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,29 +405,6 @@ public void ParseOutput()
ParseNodesToReturnParams(this, ResponseMessage);
}

/// <summary>
/// Decrypts the given password. If the password is an expression, it evaluates the expression first.
/// </summary>
/// <param name="password">The password to decrypt.</param>
/// <param name="isPasswordValueExpression">Indicates if the password is an expression that needs to be evaluated.</param>
/// <returns>The decrypted password.</returns>
public string DecryptPassword(string password, bool isPasswordValueExpression)
{
string decryptedPassword = string.Empty;

if (isPasswordValueExpression)
{
this.ValueExpression.Value = password;
string evaluatedValue = this.ValueExpression.ValueCalculated;

decryptedPassword = EncryptionHandler.IsStringEncrypted(evaluatedValue) ? EncryptionHandler.DecryptwithKey(evaluatedValue) : evaluatedValue;
}
else
{
decryptedPassword = EncryptionHandler.IsStringEncrypted(password) ? EncryptionHandler.DecryptwithKey(password) : password;
}

return decryptedPassword;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private bool SetCertificates(HttpClientHandler handler)
{
string certificateKey = mAct.GetInputParamCalculatedValue(ActWebAPIBase.Fields.CertificatePassword);

certificateKey = mAct.DecryptPassword(certificateKey, ValueExpression.IsThisAValueExpression(certificateKey));
certificateKey = Act.DecryptPassword(certificateKey, ValueExpression.IsThisAValueExpression(certificateKey), mAct);
if (!string.IsNullOrEmpty(path))
{
if (string.IsNullOrEmpty(certificateKey))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public bool Command(ref string commandParam)
string password = mAct.GetInputParamCalculatedValue(ActSoapUI.Fields.Password);
if (!string.IsNullOrEmpty(password))
{
var decryptedPassword = mAct.DecryptPassword(password, ValueExpression.IsThisAValueExpression(password));
var decryptedPassword = Act.DecryptPassword(password, ValueExpression.IsThisAValueExpression(password), mAct);
commandParam = commandParam + " -p" + Quotationmark + decryptedPassword + Quotationmark;
}

Expand Down
2 changes: 1 addition & 1 deletion Ginger/GingerCoreNET/GeneralLib/EmailOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public bool Send_SMTP()
string CertificateKey = Email.CertificatePasswordUCValueExpression;
if (!string.IsNullOrEmpty(CertificateKey))
{
var decryptedPassword = DecryptPassword(CertificateKey, ValueExpression.IsThisAValueExpression(CertificateKey));
CertificateKey = DecryptPassword(CertificateKey, ValueExpression.IsThisAValueExpression(CertificateKey));
}
string targetPath = System.IO.Path.Combine(WorkSpace.Instance.Solution.Folder, @"Documents\EmailCertificates");//certificate is present in this folder //used this since relative path cannot be used during execution
string Certificatepath = Path.Combine(targetPath, CertificateName);
Expand Down
Loading