Skip to content

Commit

Permalink
Low level Linux libsecret operations should throw exceptions (#4497)
Browse files Browse the repository at this point in the history
* Low level Linux libsecret operations should throw exceptions

* pr comment

---------

Co-authored-by: Gladwin Johnson <[email protected]>
  • Loading branch information
bgavrilMS and gladjohn authored Jan 13, 2024
1 parent 4c60cef commit 25a286e
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Runtime.InteropServices;
using Microsoft.Identity.Extensions;

namespace Microsoft.Identity.Client.Extensions.Msal
{
Expand Down Expand Up @@ -103,11 +104,14 @@ public void Clear()
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while clearing secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
throw new InteropException(
$"An error was encountered while clearing secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'",
err.Code);
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during clearing secret in the {nameof(Storage)} ex:'{e}'");
throw new InteropException(
$"An exception was encountered while processing libsecret error information during clearing secret in the {nameof(Storage)} ex:'{e}'", 0, e);
}
}

Expand All @@ -116,8 +120,6 @@ public void Clear()

public byte[] Read()
{
_logger.LogInformation("ReadDataCore");

_logger.LogInformation($"ReadDataCore, Before reading from linux keyring");

byte[] fileData = null;
Expand All @@ -139,16 +141,18 @@ public byte[] Read()
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while reading secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
throw new InteropException(
$"An error was encountered while reading secret from keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'", err.Code);
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during reading in the {nameof(Storage)} ex:'{e}'");
throw new InteropException(
$"An exception was encountered while processing libsecret error information during reading in the {nameof(Storage)} ex:'{e}'", 0, e);
}
}
else if (string.IsNullOrEmpty(secret))
{
_logger.LogError("No matching secret found in the keyring");
_logger.LogWarning("No matching secret found in the keyring");
}
else
{
Expand Down Expand Up @@ -184,11 +188,13 @@ public void Write(byte[] data)
try
{
GError err = (GError)Marshal.PtrToStructure(error, typeof(GError));
_logger.LogError($"An error was encountered while saving secret to keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'");
string message = $"An error was encountered while saving secret to keyring in the {nameof(Storage)} domain:'{err.Domain}' code:'{err.Code}' message:'{err.Message}'";
throw new InteropException(message, err.Code);
}
catch (Exception e)
{
_logger.LogError($"An exception was encountered while processing libsecret error information during saving in the {nameof(Storage)} ex:'{e}'");
throw new InteropException(
$"An exception was encountered while processing libsecret error information during saving in the {nameof(Storage)}", 0, e);
}
}

Expand All @@ -215,7 +221,7 @@ private IntPtr GetLibsecretSchema()

if (_libsecretSchema == IntPtr.Zero)
{
_logger.LogError($"Failed to create libsecret schema from the {nameof(Storage)}");
throw new InteropException("Failed to create libsecret schema from the {nameof(Storage)}", 0);
}

_logger.LogInformation("After creating libsecret schema");
Expand Down

0 comments on commit 25a286e

Please sign in to comment.