Skip to content

Commit

Permalink
Minor tidy-up of code (copy doc comments from interface)
Browse files Browse the repository at this point in the history
(#94)
  • Loading branch information
tintoy committed Sep 15, 2019
1 parent f26e02e commit 6e713e1
Showing 1 changed file with 79 additions and 71 deletions.
150 changes: 79 additions & 71 deletions src/KubeClient.Extensions.DataProtection/KubeSecretXmlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace KubeClient.Extensions.DataProtection
public sealed class KubeSecretXmlRepository
: IXmlRepository, IDisposable
{
/// <summary>
/// The default "friendly" name used for top-level elements in the repository when a friendly name is not supplied.
/// </summary>
public static readonly string DefaultElementFriendlyName = "KeyElement";

/// <summary>
/// <see cref="KubeApiClient"/> used to communicate with the Kubernetes API.
/// </summary>
Expand Down Expand Up @@ -95,23 +100,28 @@ public void Dispose()
ILogger Log { get; }

/// <summary>
/// Implement <see cref="IXmlRepository"/>
/// Get all top-level XML elements in the repository.
/// </summary>
/// <returns>
/// A sequence of <see cref="XElement"/>s representing the top-level elements.
/// </returns>
public IReadOnlyCollection<XElement> GetAllElements()
{
return GetAllElementsCore().ToArray();
}

/// <summary>
/// Implement <see cref="IXmlRepository"/>
/// Add a top-level XML element to the repository.
/// </summary>
/// <param name="element">An <see cref="XElement"/> representing the element to add.</param>
/// <param name="friendlyName">An optional name to be associated with the XML element.</param>
public void StoreElement(XElement element, string friendlyName)
{
if (element == null)
throw new ArgumentNullException(nameof(element));

if (String.IsNullOrWhiteSpace(friendlyName))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'friendlyName'.", nameof(friendlyName));
friendlyName = DefaultElementFriendlyName;

// Convert to XML String
string xmlString = element.ToString(SaveOptions.DisableFormatting);
Expand All @@ -122,10 +132,8 @@ public void StoreElement(XElement element, string friendlyName)
);

// Add XML File Extension to allow others File-Mapping
if (String.IsNullOrEmpty(Path.GetExtension(friendlyName)))
{
if (String.IsNullOrWhiteSpace(Path.GetExtension(friendlyName)))
friendlyName += ".xml";
}

// AF: Currently, this implementation is not thread-safe (because the change-notification handler may replace the secret while this code is running).

Expand All @@ -139,71 +147,6 @@ public void StoreElement(XElement element, string friendlyName)
}).GetAwaiter().GetResult();
}

/// <summary>
/// Get all Elements from Repository
/// </summary>
/// <returns>A sequence of <see cref="XElement"/>s.</returns>
public IEnumerable<XElement> GetAllElementsCore()
{
// AF: Currently, this implementation is not thread-safe (because the change-notification handler may replace the secret while this code is running).

foreach (string keyName in _keyManagementSecret.Data.Keys)
{
string encodedKeyXml = _keyManagementSecret.Data[keyName];

// Convert from Base64 to XMLString
string keyXmlText;

try
{
keyXmlText = Encoding.UTF8.GetString(
Convert.FromBase64String(encodedKeyXml)
);
}
catch (ArgumentException cannotDecodeUtf8String)
{
Log.LogError(cannotDecodeUtf8String, "Unable to decode UTF8-encoded data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}
catch (FormatException cannotDecodeBase64String)
{
Log.LogError(cannotDecodeBase64String, "Unable to decode Base64-encoded data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}
catch (Exception unexpectedError)
{
Log.LogError(unexpectedError, "An unexpected error occurred while decoding data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}

XElement keyXml;

try
{
keyXml = XElement.Parse(keyXmlText);
}
catch (XmlException invalidKeyXml)
{
Log.LogError(invalidKeyXml, "Unable to parse XML for key {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}

yield return keyXml;
}
}

/// <summary>
/// Load or Create the Kubernetes Secret
/// </summary>
Expand Down Expand Up @@ -281,5 +224,70 @@ void OnKeyManagementSecretChanged(IResourceEventV1<SecretV1> secretEvent)

// AF: What happens if the secret has been deleted?
}

/// <summary>
/// Get all top-level elements from the repository
/// </summary>
/// <returns>A sequence of <see cref="XElement"/>s.</returns>
IEnumerable<XElement> GetAllElementsCore()
{
// AF: Currently, this implementation is not thread-safe (because the change-notification handler may replace the secret while this code is running).

foreach (string keyName in _keyManagementSecret.Data.Keys)
{
string encodedKeyXml = _keyManagementSecret.Data[keyName];

// Convert from Base64 to XMLString
string keyXmlText;

try
{
keyXmlText = Encoding.UTF8.GetString(
Convert.FromBase64String(encodedKeyXml)
);
}
catch (ArgumentException cannotDecodeUtf8String)
{
Log.LogError(cannotDecodeUtf8String, "Unable to decode UTF8-encoded data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}
catch (FormatException cannotDecodeBase64String)
{
Log.LogError(cannotDecodeBase64String, "Unable to decode Base64-encoded data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}
catch (Exception unexpectedError)
{
Log.LogError(unexpectedError, "An unexpected error occurred while decoding data for key XML {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}

XElement keyXml;

try
{
keyXml = XElement.Parse(keyXmlText);
}
catch (XmlException invalidKeyXml)
{
Log.LogError(invalidKeyXml, "Unable to parse XML for key {KeyName} in secret {SecretName} (namespace {SecretNamespace}); this key will be ignored.",
keyName, _keyManagementSecret.Metadata.Name, _keyManagementSecret.Metadata.Namespace
);

continue;
}

yield return keyXml;
}
}
}
}

0 comments on commit 6e713e1

Please sign in to comment.