Skip to content

Commit

Permalink
Add Token Auth Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
leefine02 authored and leefine02 committed Oct 12, 2023
1 parent 4a911bf commit 7a9886e
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Bundle/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override JobResult ProcessJob(DiscoveryJobConfiguration config, SubmitDis
LogHandlerCommon.Debug(logger, certificateStore, "Getting partitions");
SetPAMSecrets(config.ServerUsername, config.ServerPassword, logger);

F5Client f5 = new F5Client(certificateStore, ServerUserName, ServerPassword, config.UseSSL, string.Empty, true, new List<PreviousInventoryItem>());
F5Client f5 = new F5Client(certificateStore, ServerUserName, ServerPassword, config.UseSSL, string.Empty, true, false, new List<PreviousInventoryItem>());
List<string> partitions = f5.GetPartitions().Select(p => p.name).ToList();

LogHandlerCommon.Trace(logger, certificateStore, $"Found {partitions?.Count} partitions");
Expand Down
2 changes: 1 addition & 1 deletion Bundle/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override JobResult ProcessJob(InventoryJobConfiguration config, SubmitInv
{
base.ParseJobProperties();
SetPAMSecrets(config.ServerUsername, config.ServerPassword, logger);
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, config.LastInventory) { F5Version = base.F5Version };
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, UseTokenAuth, config.LastInventory) { F5Version = base.F5Version };

LogHandlerCommon.Debug(logger, JobConfig.CertificateStoreDetails, $"Getting inventory for CA Bundle '{config.CertificateStoreDetails.StorePath}'");
inventory = f5.GetCABundleInventory();
Expand Down
2 changes: 1 addition & 1 deletion Bundle/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override JobResult ProcessJob(ManagementJobConfiguration config)
base.ParseJobProperties();
base.PrimaryNodeActive();

F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, config.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, config.LastInventory)
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, config.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, UseTokenAuth, config.LastInventory)
{
PrimaryNode = base.PrimaryNode,
F5Version = base.F5Version
Expand Down
8 changes: 6 additions & 2 deletions F5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal class F5Client
public string PrimaryNode { get; set; }
public string F5Version { get; set; }
public bool IgnoreSSLWarning { get; set; }
public bool UseTokenAuth { get; set; }
private RESTHandler REST { get; set; }
private F5Transaction Transaction { get; set; }

Expand All @@ -53,14 +54,15 @@ internal class F5Client

#region Constructors

public F5Client(CertificateStore certificateStore, string serverUserName, string serverPassword, bool useSSL, string pfxPassword, bool ignoreSSLWarning, IEnumerable<PreviousInventoryItem> inventory)
public F5Client(CertificateStore certificateStore, string serverUserName, string serverPassword, bool useSSL, string pfxPassword, bool ignoreSSLWarning, bool useTokenAuth, IEnumerable<PreviousInventoryItem> inventory)
{
CertificateStore = certificateStore;
ServerUserName = serverUserName;
ServerPassword = serverPassword;
UseSSL = useSSL;
PFXPassword = pfxPassword;
IgnoreSSLWarning = ignoreSSLWarning;
UseTokenAuth = UseTokenAuth;
Inventory = inventory;

if (logger == null)
Expand All @@ -69,7 +71,9 @@ public F5Client(CertificateStore certificateStore, string serverUserName, string
}

REST = new RESTHandler(certificateStore.ClientMachine, serverUserName, serverPassword, useSSL, IgnoreSSLWarning);
REST.Token = GetToken(serverUserName, serverPassword);

if (UseTokenAuth)
REST.Token = GetToken(serverUserName, serverPassword);
}

// Constructors
Expand Down
2 changes: 2 additions & 0 deletions InventoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class InventoryBase : F5JobBase, IInventoryJobExtension

protected string F5Version { get; set; }
protected bool IgnoreSSLWarning { get; set; }
protected bool UseTokenAuth { get; set; }

public string ExtensionName => string.Empty;

Expand All @@ -38,6 +39,7 @@ protected void ParseJobProperties()
LogHandlerCommon.Trace(logger, JobConfig.CertificateStoreDetails, $"F5 version '{F5Version}'");

IgnoreSSLWarning = properties.IgnoreSSLWarning == null || string.IsNullOrEmpty(properties.IgnoreSSLWarning.Value) ? false : bool.Parse(properties.IgnoreSSLWarning.Value);
UseTokenAuth = properties.UseTokenAuth == null || string.IsNullOrEmpty(properties.UseTokenAuth.Value) ? false : bool.Parse(properties.UseTokenAuth.Value);
LogHandlerCommon.Trace(logger, JobConfig.CertificateStoreDetails, $"Ignore SSL Warnings '{IgnoreSSLWarning.ToString()}'");
}
}
Expand Down
4 changes: 3 additions & 1 deletion ManagementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public abstract class ManagementBase : F5JobBase, IManagementJobExtension
protected int _primaryNodeRetryCount = 0;
protected string F5Version { get; set; }
protected bool IgnoreSSLWarning { get; set; }
protected bool UseTokenAuth { get; set; }

public string ExtensionName => string.Empty;

Expand Down Expand Up @@ -82,6 +83,7 @@ protected void ParseJobProperties()
LogHandlerCommon.Trace(logger, JobConfig.CertificateStoreDetails, $"F5 version '{F5Version}'");

IgnoreSSLWarning = properties.IgnoreSSLWarning == null || string.IsNullOrEmpty(properties.IgnoreSSLWarning.Value) ? false : bool.Parse(properties.IgnoreSSLWarning.Value);
UseTokenAuth = properties.UseTokenAuth == null || string.IsNullOrEmpty(properties.UseTokenAuth.Value) ? false : bool.Parse(properties.UseTokenAuth.Value);
LogHandlerCommon.Trace(logger, JobConfig.CertificateStoreDetails, $"Ignore SSL Warnings '{IgnoreSSLWarning.ToString()}'");
}

Expand All @@ -91,7 +93,7 @@ protected void PrimaryNodeActive()

if (PrimaryNodeOnlineRequired)
{
F5Client f5 = new F5Client(JobConfig.CertificateStoreDetails, ServerUserName, ServerPassword, JobConfig.UseSSL, JobConfig.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, JobConfig.LastInventory)
F5Client f5 = new F5Client(JobConfig.CertificateStoreDetails, ServerUserName, ServerPassword, JobConfig.UseSSL, JobConfig.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, UseTokenAuth, JobConfig.LastInventory)
{ PrimaryNode = this.PrimaryNode };
if (!f5.PrimaryNodeActive())
{
Expand Down
10 changes: 8 additions & 2 deletions RESTHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,14 @@ public void UploadFile(string filename, byte[] fileBytes)
webClient.Headers.Add("ServerHost", $"{GetProtocol()}{Host}/mgmt/shared/file-transfer/uploads/{filename}");
webClient.Headers.Add("Content-Type", "application/octet-stream");
webClient.Headers.Add("Content-Range", $"0-{fileBytes.Length - 1}/{fileBytes.Length}");
webClient.Headers.Add("X-F5-Auth-Token", Token);
//webClient.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.ASCII.GetBytes($"{User}:{Password}"))}");
if (Token == null)
{
webClient.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.ASCII.GetBytes($"{User}:{Password}"))}");
}
else
{
webClient.Headers.Add("X-F5-Auth-Token", Token);
}

webClient.UploadData($"{GetProtocol()}{Host}/mgmt/shared/file-transfer/uploads/{filename}", fileBytes);
}
Expand Down
2 changes: 1 addition & 1 deletion SSLProfile/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override JobResult ProcessJob(DiscoveryJobConfiguration config, SubmitDis

SetPAMSecrets(config.ServerUsername, config.ServerPassword, logger);

F5Client f5 = new F5Client(certificateStore, ServerUserName, ServerPassword, config.UseSSL, string.Empty, true, new List<PreviousInventoryItem>());
F5Client f5 = new F5Client(certificateStore, ServerUserName, ServerPassword, config.UseSSL, string.Empty, true, false, new List<PreviousInventoryItem>());
List<string> locations = f5.GetPartitions().Select(p => p.name).ToList();

LogHandlerCommon.Debug(logger, certificateStore, $"Submitting {locations?.Count} partitions");
Expand Down
2 changes: 1 addition & 1 deletion SSLProfile/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override JobResult ProcessJob(InventoryJobConfiguration config, SubmitInv
{
base.ParseJobProperties();
SetPAMSecrets(config.ServerUsername, config.ServerPassword, logger);
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, config.LastInventory) { F5Version = base.F5Version };
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, UseTokenAuth, config.LastInventory) { F5Version = base.F5Version };

LogHandlerCommon.Debug(logger, JobConfig.CertificateStoreDetails, $"Getting inventory from '{config.CertificateStoreDetails.StorePath}'");
inventory = f5.GetSSLProfiles(20);
Expand Down
2 changes: 1 addition & 1 deletion SSLProfile/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override JobResult ProcessJob(ManagementJobConfiguration config)
base.ParseJobProperties();
base.PrimaryNodeActive();

F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, config.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, config.LastInventory)
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, config.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, UseTokenAuth, config.LastInventory)
{
PrimaryNode = base.PrimaryNode,
F5Version = base.F5Version
Expand Down
2 changes: 1 addition & 1 deletion WebServer/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override JobResult ProcessJob(InventoryJobConfiguration config, SubmitInv
base.ParseJobProperties();
SetPAMSecrets(config.ServerUsername, config.ServerPassword, logger);

F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, config.LastInventory);
F5Client f5 = new F5Client(config.CertificateStoreDetails, ServerUserName, ServerPassword, config.UseSSL, null, IgnoreSSLWarning, UseTokenAuth, config.LastInventory);

LogHandlerCommon.Debug(logger, JobConfig.CertificateStoreDetails, "Getting the F5 web server device inventory");
inventory = f5.GetWebServerInventory();
Expand Down
2 changes: 1 addition & 1 deletion WebServer/Management.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override JobResult ProcessJob(ManagementJobConfiguration config)
base.ParseJobProperties();
base.PrimaryNodeActive();

F5Client f5 = new F5Client(JobConfig.CertificateStoreDetails, ServerUserName, ServerPassword, JobConfig.UseSSL, JobConfig.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, JobConfig.LastInventory)
F5Client f5 = new F5Client(JobConfig.CertificateStoreDetails, ServerUserName, ServerPassword, JobConfig.UseSSL, JobConfig.JobCertificate.PrivateKeyPassword, IgnoreSSLWarning, UseTokenAuth, JobConfig.LastInventory)
{
PrimaryNode = base.PrimaryNode
};
Expand Down

0 comments on commit 7a9886e

Please sign in to comment.