Skip to content
148 changes: 79 additions & 69 deletions Commander/PAM/PamCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,96 @@

namespace Commander.PAM
{
internal abstract class PamCommandBase
internal abstract class PamCommandBase
{
protected IEnterpriseContext Context { get; }
protected IPamPlugin Plugin { get; private set; }

protected PamCommandBase(IEnterpriseContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
}

protected async Task<bool> EnsurePluginAsync(bool syncIfNeeded = true)
{
protected IEnterpriseContext Context { get; }
protected IPamPlugin Plugin { get; private set; }
Plugin = Context.GetPamPlugin();
if (Plugin == null)
{
Console.WriteLine("PAM plugin is not available. Enterprise admin access is required.");
return false;
}

protected PamCommandBase(IEnterpriseContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
}
if (syncIfNeeded && !Plugin.Controllers.GetAll().Any())
{
Console.WriteLine("Syncing PAM data...");
await Plugin.SyncDownAsync();
}

protected async Task<bool> EnsurePluginAsync(bool syncIfNeeded = true)
{
Plugin = Context.GetPamPlugin();
if (Plugin == null)
{
Console.WriteLine("PAM plugin is not available. Enterprise admin access is required.");
return false;
}
return true;
}

if (syncIfNeeded && !Plugin.Controllers.GetAll().Any())
{
Console.WriteLine("Syncing PAM data...");
await Plugin.SyncDownAsync();
}
protected PamController ResolveGateway(string identifier)
{
if (string.IsNullOrWhiteSpace(identifier))
{
return null;
}

return true;
}
var controllers = Plugin.Controllers.GetAll();
var controller = GatewayUtils.FindGateway(controllers, identifier);
if (controller != null)
{
return controller;
}

protected PamController ResolveGateway(string identifier)
{
if (string.IsNullOrWhiteSpace(identifier))
{
return null;
}
var trimmed = identifier.Trim();
var nameMatches = controllers
.Count(c => string.Equals(c.ControllerName, trimmed, StringComparison.OrdinalIgnoreCase));
if (nameMatches > 1)
{
throw new PamGatewayAmbiguousException(trimmed);
}

var controllers = Plugin.Controllers.GetAll();
var controller = GatewayUtils.FindGateway(controllers, identifier);
if (controller != null)
{
return controller;
}
return null;
}

var trimmed = identifier.Trim();
var nameMatches = controllers
.Count(c => string.Equals(c.ControllerName, trimmed, StringComparison.OrdinalIgnoreCase));
if (nameMatches > 1)
{
throw new PamGatewayAmbiguousException(trimmed);
}
protected static TypedRecord TryResolvePamRecord(
VaultOnline vault,
string identifier,
IEnumerable<string> allowedTypes)
{
try
{
return PamVaultHelpers.ResolveRecord(vault, identifier, allowedTypes);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}

return null;
}
protected VaultContext TryGetVaultContext()
{
if (Context is ConnectedContext connected)
{
return connected._vaultContext;
}

protected static TypedRecord TryResolvePamRecord(
VaultOnline vault,
string identifier,
IEnumerable<string> allowedTypes)
{
try
{
return PamVaultHelpers.ResolveRecord(vault, identifier, allowedTypes);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
return null;
}

protected async Task SyncPamAsync(bool reload = false)
{
if (Plugin == null)
{
Plugin = Context.GetPamPlugin();
}
protected async Task SyncPamAsync(bool reload = false)
{
if (Plugin == null)
{
Plugin = Context.GetPamPlugin();
}

if (Plugin != null)
{
await Plugin.SyncDownAsync(reload);
}
}
if (Plugin != null)
{
await Plugin.SyncDownAsync(reload);
}
}
}
}
Loading