Skip to content

Commit

Permalink
Fixed #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Seji64 committed Jul 25, 2023
1 parent 19e09d0 commit 3319cee
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 52 deletions.
4 changes: 1 addition & 3 deletions src/Enums/LAPSVersion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.Intrinsics;

namespace LAPS_WebUI.Enums
namespace LAPS_WebUI.Enums
{
public enum LAPSVersion
{
Expand Down
4 changes: 2 additions & 2 deletions src/Models/ADComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public ADComputer(string name)

public string Name { get; set; }
public List<LapsInformation>? LAPSInformations { get; set; }

public bool FailedToRetrieveLAPSDetails { get; set; }
public bool Loading
{
get
{
if (LAPSInformations is null || !LAPSInformations.Any())
if (LAPSInformations is null)
{
return true;
}
Expand Down
77 changes: 43 additions & 34 deletions src/Pages/LAPS.razor
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,52 @@
</CardHeaderActions>
</MudCardHeader>
<MudCardContent>
@if(computer.LAPSInformations != null || !computer.Loading)
@if(!computer.Loading)
{
<MudTabs Position="Position.Top" Rounded="true" Border="false" ApplyEffectsToContainer="true" @ref="_tabs" PanelClass="pa-4">
<MudTabPanel Icon="@Icons.Material.Outlined.Filter1" ID="@("v1")" Text="v1" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v1 && x.IsCurrent))>
<LapsInformationDetail LapsInfo="computer.LAPSInformations!.Single(x => x.Version == Enums.LAPSVersion.v1)" />
</MudTabPanel>
<MudTabPanel Icon="@Icons.Material.Outlined.Filter2" ID="@("v2")" Text="v2" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v2 && x.IsCurrent))>
<LapsInformationDetail LapsInfo="computer.LAPSInformations!.Single(x => x.Version == Enums.LAPSVersion.v2 && x.IsCurrent)" />
</MudTabPanel>
<MudTabPanel Icon="@Icons.Material.Outlined.History" ID="@("history")" Text="History" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v2 && !x.IsCurrent))>
<MudSimpleTable Style="overflow-x: auto;" Dense="true" Hover="true" Striped="true">
<thead>
<tr>
<th>Account</th>
<th>Password</th>
<th>Date set</th>
</tr>
</thead>
<tbody>
@foreach(var entry in computer.LAPSInformations!.Where(x => x.IsCurrent == false && x.Version == Enums.LAPSVersion.v2))
{
@if(!computer.FailedToRetrieveLAPSDetails)
{
<MudTabs Position="Position.Top" Rounded="true" Border="false" ApplyEffectsToContainer="true" @ref="_tabs" PanelClass="pa-4">
<MudTabPanel Icon="@Icons.Material.Outlined.Filter1" ID="@("v1")" Text="v1" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v1 && x.IsCurrent))>
<LapsInformationDetail LapsInfo="computer.LAPSInformations!.Single(x => x.Version == Enums.LAPSVersion.v1)" />
</MudTabPanel>
<MudTabPanel Icon="@Icons.Material.Outlined.Filter2" ID="@("v2")" Text="v2" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v2 && x.IsCurrent))>
<LapsInformationDetail LapsInfo="computer.LAPSInformations!.Single(x => x.Version == Enums.LAPSVersion.v2 && x.IsCurrent)" />
</MudTabPanel>
<MudTabPanel Icon="@Icons.Material.Outlined.History" ID="@("history")" Text="History" Disabled=@(!computer.LAPSInformations!.Any(x => x.Version == Enums.LAPSVersion.v2 && !x.IsCurrent))>
<MudSimpleTable Style="overflow-x: auto;" Dense="true" Hover="true" Striped="true">
<thead>
<tr>
<td>
@entry.Account
</td>
<td>
@entry.Password
</td>
<td>
@entry.PasswordSetDate
</td>
<th>Account</th>
<th>Password</th>
<th>Date set</th>
</tr>
}
</tbody>
</MudSimpleTable>
</MudTabPanel>
</MudTabs>
</thead>
<tbody>
@foreach (var entry in computer.LAPSInformations!.Where(x => x.IsCurrent == false && x.Version == Enums.LAPSVersion.v2))
{
<tr>
<td>
@entry.Account
</td>
<td>
@entry.Password
</td>
<td>
@entry.PasswordSetDate
</td>
</tr>
}
</tbody>
</MudSimpleTable>
</MudTabPanel>
</MudTabs>
}
else
{
<MudAlert Icon="@Icons.Material.Outlined.Warning" ShowCloseIcon="false" Variant="Variant.Outlined" Severity="Severity.Warning">
<MudText Typo="Typo.inherit">No permission to retrieve LAPS Password or no LAPS Password set!</MudText>
</MudAlert>
}
}
else
{
Expand Down
10 changes: 6 additions & 4 deletions src/Pages/LAPS.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

private async Task OnSelectedItemChangedAsync(ADComputer value)
{
if (value != null && !string.IsNullOrEmpty(value.Name) && !SelectedComputers.Any(x => x.Name == value.Name))
if (value != null && !string.IsNullOrEmpty(value.Name) && !SelectedComputers.Exists(x => x.Name == value.Name))
{
await FetchComputerDetailsAsync(value.Name);
}
Expand Down Expand Up @@ -127,11 +127,13 @@ private async Task FetchComputerDetailsAsync(string computerName)
SelectedComputers.Add(placeHolder);
await InvokeAsync(StateHasChanged);

var tmp = await LDAPService.GetADComputerAsync(await sessionManager.GetLdapCredentialsAsync(), computerName);
var AdComputerObject = await LDAPService.GetADComputerAsync(await sessionManager.GetLdapCredentialsAsync(), computerName);
var selectedComputer = SelectedComputers.SingleOrDefault(x => x.Name == computerName);

if (tmp != null)
if (AdComputerObject != null && selectedComputer != null)
{
SelectedComputers.Single(x => x.Name == computerName).LAPSInformations = tmp.LAPSInformations;
selectedComputer.LAPSInformations = AdComputerObject.LAPSInformations;
selectedComputer.FailedToRetrieveLAPSDetails = AdComputerObject.FailedToRetrieveLAPSDetails;
}
}
catch (Exception ex)
Expand Down
23 changes: 14 additions & 9 deletions src/Services/LDAPService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<bool> TestCredentialsAsync(LdapCredential ldapCredential)
{
if (ldapConnection is null)
{
throw new Exception("LDAP bin failed!");
throw new Exception("LDAP bind failed!");
}

string? defaultNamingContext = _ldapOptions.Value.SearchBase;
Expand Down Expand Up @@ -183,16 +183,19 @@ public async Task<bool> TestCredentialsAsync(LdapCredential ldapCredential)
}
}
}
}

if (ADComputer.LAPSInformations is null || !ADComputer.LAPSInformations.Any())
{
throw new Exception("No permission to retrieve LAPS Password or no LAPS Password set!");
}
#endregion

if (ADComputer.LAPSInformations is null || !ADComputer.LAPSInformations.Any())
{
ADComputer.FailedToRetrieveLAPSDetails = true;
}
else
{
ADComputer.LAPSInformations = ADComputer.LAPSInformations.OrderBy(x => x.PasswordExpireDate).ToList();
}

#endregion

}
else
{
Expand All @@ -208,7 +211,9 @@ private static async Task<string> DecryptLAPSPayload(byte[] value, LdapCredentia
StringBuilder pythonScriptResult = new();
string pythonDecryptScriptPath = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory)!, "scripts", "DecryptEncryptedLAPSPassword.py");

var pythonCmd = Cli.Wrap("python3")
string pythonBin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "python" : "python3";

var pythonCmd = Cli.Wrap(pythonBin)
.WithArguments($"\"{pythonDecryptScriptPath}\" --user {ldapCredential.UserName} --password {ldapCredential.Password} --data {Convert.ToBase64String(value)})")
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(pythonScriptResult));

Expand Down Expand Up @@ -236,7 +241,7 @@ public async Task<List<ADComputer>> SearchADComputersAsync(LdapCredential ldapCr

using (LdapConnection? ldapConnection = await CreateBindAsync(ldapCredential.UserName, ldapCredential.Password))
{
var filter = $"(&(objectCategory=computer)(name={query}{(query.EndsWith("*") ? "" : "*")}))";
string filter = $"(&(objectCategory=computer)(name={query}{(query.EndsWith('*') ? string.Empty : '*')}))";
var PropertiesToLoad = new string[] { "cn" };
string? defaultNamingContext = _ldapOptions.Value.SearchBase;

Expand Down

0 comments on commit 3319cee

Please sign in to comment.