-
Notifications
You must be signed in to change notification settings - Fork 226
add support for env credentials or loading creds from json file #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.X
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,10 +100,28 @@ await options.WithParsedAsync(async options => | |
|
|
||
| if (options.DomainController != null) ldapOptions.Server = options.DomainController; | ||
|
|
||
| if (options.LDAPUsername != null) | ||
| { | ||
| if (options.LDAPPassword == null) | ||
| { | ||
| if (options.LDAPCredentialJsonPath != null) { | ||
| string json = File.ReadAllText(options.LDAPCredentialJsonPath); | ||
| Credentials ldapCreds = JsonConvert.DeserializeObject<Credentials>(json); | ||
| ldapOptions.Username = ldapCreds.Username; | ||
| ldapOptions.Password = ldapCreds.Password; | ||
| } | ||
|
Comment on lines
+103
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing using statements and error handling for JSON credential loading. The code references Apply this diff to add the required using statements at the top of the file: using System;
using System.DirectoryServices.Protocols;
+using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
+using Newtonsoft.Json;
using Sharphound.Client;
using SharpHoundCommonLib;
using SharpHoundCommonLib.Enums;Then apply this diff to add error handling and validation: if (options.LDAPCredentialJsonPath != null) {
- string json = File.ReadAllText(options.LDAPCredentialJsonPath);
- Credentials ldapCreds = JsonConvert.DeserializeObject<Credentials>(json);
- ldapOptions.Username = ldapCreds.Username;
- ldapOptions.Password = ldapCreds.Password;
+ try {
+ string json = File.ReadAllText(options.LDAPCredentialJsonPath);
+ Credentials ldapCreds = JsonConvert.DeserializeObject<Credentials>(json);
+
+ if (ldapCreds == null || string.IsNullOrEmpty(ldapCreds.Username) || string.IsNullOrEmpty(ldapCreds.Password)) {
+ logger.LogError("Invalid credentials in JSON file. Both Username and Password must be provided.");
+ return;
+ }
+
+ ldapOptions.Username = ldapCreds.Username;
+ ldapOptions.Password = ldapCreds.Password;
+ } catch (FileNotFoundException) {
+ logger.LogError("LDAP credentials JSON file not found at path: {Path}", options.LDAPCredentialJsonPath);
+ return;
+ } catch (JsonException ex) {
+ logger.LogError("Failed to parse LDAP credentials JSON file: {Message}", ex.Message);
+ return;
+ } catch (Exception ex) {
+ logger.LogError("Error reading LDAP credentials from JSON file: {Message}", ex.Message);
+ return;
+ }
}
🤖 Prompt for AI Agents |
||
|
|
||
| if (options.LDAPEnvUsername != null) { | ||
| if (options.LDAPEnvPassword == null) { | ||
| logger.LogError("You must specify LDAPEnvPassword if using the LDAPEnvUsername options"); | ||
| return; | ||
| } | ||
|
|
||
| string EnvUsername = Environment.GetEnvironmentVariable(options.LDAPEnvUsername); | ||
| string EnvPassword = Environment.GetEnvironmentVariable(options.LDAPEnvPassword); | ||
|
|
||
| ldapOptions.Username = EnvUsername; | ||
| ldapOptions.Password = EnvPassword; | ||
| } | ||
|
Comment on lines
+110
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add validation for environment variable credential values. The code retrieves credentials from environment variables but doesn't validate that the variables exist or contain non-empty values. If an environment variable doesn't exist, Apply this diff: if (options.LDAPEnvUsername != null) {
if (options.LDAPEnvPassword == null) {
logger.LogError("You must specify LDAPEnvPassword if using the LDAPEnvUsername options");
return;
}
string EnvUsername = Environment.GetEnvironmentVariable(options.LDAPEnvUsername);
string EnvPassword = Environment.GetEnvironmentVariable(options.LDAPEnvPassword);
+
+ if (string.IsNullOrEmpty(EnvUsername)) {
+ logger.LogError("Environment variable {VarName} not found or empty", options.LDAPEnvUsername);
+ return;
+ }
+
+ if (string.IsNullOrEmpty(EnvPassword)) {
+ logger.LogError("Environment variable {VarName} not found or empty", options.LDAPEnvPassword);
+ return;
+ }
ldapOptions.Username = EnvUsername;
ldapOptions.Password = EnvPassword;
}🤖 Prompt for AI Agents |
||
|
|
||
| if (options.LDAPUsername != null) { | ||
| if (options.LDAPPassword == null) { | ||
| logger.LogError("You must specify LDAPPassword if using the LDAPUsername options"); | ||
| return; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo: "Enviroment" → "Environment".
The help text contains a spelling error that should be corrected for consistency and professionalism.
Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents