Skip to content

Commit

Permalink
Improved KDS Root Key selection algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGrafnetter committed Oct 2, 2023
1 parent e01c342 commit f6deab9
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Src/DSInternals.DataStore/DirectoryAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public IEnumerable<GroupManagedServiceAccount> GetGroupManagedServiceAccounts(Da
{
// Fetch all KDS root keys first.
var rootKeys = new Dictionary<Guid, KdsRootKey>();
KdsRootKey latestRootKey = null;

foreach (var rootKey in this.GetKdsRootKeys())
{
Expand All @@ -148,6 +149,12 @@ public IEnumerable<GroupManagedServiceAccount> GetGroupManagedServiceAccounts(Da
{
// Allow the key to be found by ID
rootKeys.Add(rootKey.KeyId, rootKey);

// Check if this key is the newest found yet
if(rootKey.EffectiveTime <= effectiveTime && (latestRootKey == null || latestRootKey.CreationTime < rootKey.CreationTime))
{
latestRootKey = rootKey;
}
}
}

Expand All @@ -159,13 +166,23 @@ public IEnumerable<GroupManagedServiceAccount> GetGroupManagedServiceAccounts(Da

if (gmsa.ManagedPasswordId != null)
{
// Find the proper key by Guid
Guid associateRootKeyId = gmsa.ManagedPasswordId.RootKeyId;
bool keyFound = rootKeys.TryGetValue(associateRootKeyId, out var associatedRootKey);
DateTime nextPasswordChange = gmsa.PasswordLastSet.Value.AddDays(gmsa.ManagedPasswordInterval.Value);
KdsRootKey rootKeyToUse;
if (nextPasswordChange <= effectiveTime)
{
// The existing password has already expired, so generate the managed password based on the latest Root Key
rootKeyToUse = latestRootKey;
}
else
{
// Generate the managed password based on the Root Key currently associated with it
Guid associateRootKeyId = gmsa.ManagedPasswordId.RootKeyId;
rootKeys.TryGetValue(associateRootKeyId, out rootKeyToUse);
}

if (keyFound)
if (rootKeyToUse != null)
{
gmsa.CalculatePassword(associatedRootKey, effectiveTime);
gmsa.CalculatePassword(rootKeyToUse, effectiveTime);
}
}

Expand Down

0 comments on commit f6deab9

Please sign in to comment.