diff --git a/src/Enums/LAPSVersion.cs b/src/Enums/LAPSVersion.cs index d04f7ae..ad1af87 100644 --- a/src/Enums/LAPSVersion.cs +++ b/src/Enums/LAPSVersion.cs @@ -1,6 +1,4 @@ -using System.Runtime.Intrinsics; - -namespace LAPS_WebUI.Enums +namespace LAPS_WebUI.Enums { public enum LAPSVersion { diff --git a/src/Models/ADComputer.cs b/src/Models/ADComputer.cs index 877984d..c3c05a5 100644 --- a/src/Models/ADComputer.cs +++ b/src/Models/ADComputer.cs @@ -9,12 +9,12 @@ public ADComputer(string name) public string Name { get; set; } public List? LAPSInformations { get; set; } - + public bool FailedToRetrieveLAPSDetails { get; set; } public bool Loading { get { - if (LAPSInformations is null || !LAPSInformations.Any()) + if (LAPSInformations is null) { return true; } diff --git a/src/Pages/LAPS.razor b/src/Pages/LAPS.razor index 4f16a10..5844fa1 100644 --- a/src/Pages/LAPS.razor +++ b/src/Pages/LAPS.razor @@ -44,43 +44,52 @@ - @if(computer.LAPSInformations != null || !computer.Loading) + @if(!computer.Loading) { - - x.Version == Enums.LAPSVersion.v1 && x.IsCurrent))> - - - x.Version == Enums.LAPSVersion.v2 && x.IsCurrent))> - - - x.Version == Enums.LAPSVersion.v2 && !x.IsCurrent))> - - - - Account - Password - Date set - - - - @foreach(var entry in computer.LAPSInformations!.Where(x => x.IsCurrent == false && x.Version == Enums.LAPSVersion.v2)) - { + @if(!computer.FailedToRetrieveLAPSDetails) + { + + x.Version == Enums.LAPSVersion.v1 && x.IsCurrent))> + + + x.Version == Enums.LAPSVersion.v2 && x.IsCurrent))> + + + x.Version == Enums.LAPSVersion.v2 && !x.IsCurrent))> + + - - @entry.Account - - - @entry.Password - - - @entry.PasswordSetDate - + Account + Password + Date set - } - - - - + + + @foreach (var entry in computer.LAPSInformations!.Where(x => x.IsCurrent == false && x.Version == Enums.LAPSVersion.v2)) + { + + + @entry.Account + + + @entry.Password + + + @entry.PasswordSetDate + + + } + + + + + } + else + { + + No permission to retrieve LAPS Password or no LAPS Password set! + + } } else { diff --git a/src/Pages/LAPS.razor.cs b/src/Pages/LAPS.razor.cs index db74149..0e493cc 100644 --- a/src/Pages/LAPS.razor.cs +++ b/src/Pages/LAPS.razor.cs @@ -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); } @@ -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) diff --git a/src/Services/LDAPService.cs b/src/Services/LDAPService.cs index 6f559ab..959bb8e 100644 --- a/src/Services/LDAPService.cs +++ b/src/Services/LDAPService.cs @@ -88,7 +88,7 @@ public async Task TestCredentialsAsync(LdapCredential ldapCredential) { if (ldapConnection is null) { - throw new Exception("LDAP bin failed!"); + throw new Exception("LDAP bind failed!"); } string? defaultNamingContext = _ldapOptions.Value.SearchBase; @@ -183,16 +183,19 @@ public async Task 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 { @@ -208,7 +211,9 @@ private static async Task 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)); @@ -236,7 +241,7 @@ public async Task> 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;