-
Notifications
You must be signed in to change notification settings - Fork 12
Support adding database managed identity policies #161
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7fee3c0
Initial plan
Copilot 08bbc1d
Add managed identity policy KQL generation for Azure Data Explorer da…
Copilot c87b416
Add KustoManagedIdentityPolicyLoader and fix combined script generation
alex-slynko d87ca96
Restructure to nested policies object in database YAML
alex-slynko d49a0f9
Fix Policy column name in managed identity loader
alex-slynko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
KustoSchemaTools.Tests/Model/ManagedIdentityPolicyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| using KustoSchemaTools.Model; | ||
| using KustoSchemaTools.Changes; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
|
|
||
| namespace KustoSchemaTools.Tests.ManagedIdentity | ||
| { | ||
| public class ManagedIdentityPolicyTests | ||
| { | ||
| [Fact] | ||
| public void CreateCombinedScript_SinglePolicy_GeneratesCorrectKql() | ||
| { | ||
| // Arrange | ||
| var policies = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var script = ManagedIdentityPolicy.CreateCombinedScript("MyDatabase", policies); | ||
|
|
||
| // Assert | ||
| Assert.Equal("ManagedIdentityPolicy", script.Kind); | ||
| Assert.Equal(80, script.Script.Order); | ||
| Assert.Contains(".alter-merge database MyDatabase policy managed_identity", script.Script.Text); | ||
| Assert.Contains("\"ObjectId\": \"12345678-1234-1234-1234-123456789abc\"", script.Script.Text); | ||
| Assert.Contains("\"AllowedUsages\": \"NativeIngestion\"", script.Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateCombinedScript_MultipleUsages_JoinsWithCommaAlphabetically() | ||
| { | ||
| // Arrange | ||
| var policies = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion", "AutomatedFlows", "ExternalTable" } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var script = ManagedIdentityPolicy.CreateCombinedScript("MyDatabase", policies); | ||
|
|
||
| // Assert | ||
| Assert.Contains("\"AllowedUsages\": \"AutomatedFlows, ExternalTable, NativeIngestion\"", script.Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateCombinedScript_MultiplePolicies_SortsByObjectId() | ||
| { | ||
| // Arrange | ||
| var policies = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz", | ||
| AllowedUsages = new List<string> { "ExternalTable" } | ||
| }, | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var script = ManagedIdentityPolicy.CreateCombinedScript("MyDatabase", policies); | ||
|
|
||
| // Assert - both identities in a single script, sorted by ObjectId | ||
| Assert.Equal("ManagedIdentityPolicy", script.Kind); | ||
| var aIdx = script.Script.Text.IndexOf("aaaaaaaa"); | ||
| var zIdx = script.Script.Text.IndexOf("zzzzzzzz"); | ||
| Assert.True(aIdx < zIdx, "Policies should be sorted by ObjectId"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateCombinedScript_DatabaseNameUsedInKql() | ||
| { | ||
| // Arrange | ||
| var policies = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", | ||
| AllowedUsages = new List<string> { "ExternalTable" } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var script = ManagedIdentityPolicy.CreateCombinedScript("TargetDatabase", policies); | ||
|
|
||
| // Assert | ||
| Assert.StartsWith(".alter-merge database TargetDatabase policy managed_identity", script.Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateCombinedScript_WrapsJsonInBackticks() | ||
| { | ||
| // Arrange | ||
| var policies = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var script = ManagedIdentityPolicy.CreateCombinedScript("MyDatabase", policies); | ||
|
|
||
| // Assert | ||
| Assert.Contains("```", script.Script.Text); | ||
| Assert.EndsWith("```", script.Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DatabaseChanges_WithManagedIdentityPolicies_GeneratesScript() | ||
| { | ||
| // Arrange | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var oldState = new Database { Name = "TestDb" }; | ||
| var newState = new Database | ||
| { | ||
| Name = "TestDb", | ||
| Policies = new DatabasePolicies | ||
| { | ||
| ManagedIdentity = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var changes = DatabaseChanges.GenerateChanges(oldState, newState, "TestDb", loggerMock.Object); | ||
|
|
||
| // Assert | ||
| Assert.NotEmpty(changes); | ||
| var scripts = changes.SelectMany(c => c.Scripts).ToList(); | ||
| Assert.NotEmpty(scripts); | ||
| var managedIdentityScript = scripts.FirstOrDefault(s => s.Kind == "ManagedIdentityPolicy"); | ||
| Assert.NotNull(managedIdentityScript); | ||
| Assert.Contains(".alter-merge database TestDb policy managed_identity", managedIdentityScript.Script.Text); | ||
| Assert.Contains("12345678-1234-1234-1234-123456789abc", managedIdentityScript.Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DatabaseChanges_WithMultipleManagedIdentityPolicies_GeneratesSingleScript() | ||
| { | ||
| // Arrange | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var oldState = new Database { Name = "TestDb" }; | ||
| var newState = new Database | ||
| { | ||
| Name = "TestDb", | ||
| Policies = new DatabasePolicies | ||
| { | ||
| ManagedIdentity = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "aaaaaaaa-1111-2222-3333-444444444444", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| }, | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "bbbbbbbb-1111-2222-3333-444444444444", | ||
| AllowedUsages = new List<string> { "ExternalTable" } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var changes = DatabaseChanges.GenerateChanges(oldState, newState, "TestDb", loggerMock.Object); | ||
|
|
||
| // Assert - should generate exactly one ManagedIdentityPolicy script (combined) | ||
| var managedIdentityScripts = changes | ||
| .SelectMany(c => c.Scripts) | ||
| .Where(s => s.Kind == "ManagedIdentityPolicy") | ||
| .ToList(); | ||
| Assert.Single(managedIdentityScripts); | ||
| Assert.Contains("aaaaaaaa-1111-2222-3333-444444444444", managedIdentityScripts[0].Script.Text); | ||
| Assert.Contains("bbbbbbbb-1111-2222-3333-444444444444", managedIdentityScripts[0].Script.Text); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DatabaseChanges_WithUnchangedManagedIdentityPolicies_GeneratesNoChanges() | ||
| { | ||
| // Arrange | ||
| var loggerMock = new Mock<ILogger>(); | ||
| var policy = new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| }; | ||
| var oldState = new Database | ||
| { | ||
| Name = "TestDb", | ||
| Policies = new DatabasePolicies | ||
| { | ||
| ManagedIdentity = new List<ManagedIdentityPolicy> { policy } | ||
| } | ||
| }; | ||
| var newState = new Database | ||
| { | ||
| Name = "TestDb", | ||
| Policies = new DatabasePolicies | ||
| { | ||
| ManagedIdentity = new List<ManagedIdentityPolicy> | ||
| { | ||
| new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = "12345678-1234-1234-1234-123456789abc", | ||
| AllowedUsages = new List<string> { "NativeIngestion" } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Act | ||
| var changes = DatabaseChanges.GenerateChanges(oldState, newState, "TestDb", loggerMock.Object); | ||
|
|
||
| // Assert - no database-level changes since policies are identical | ||
| var databaseScriptChanges = changes | ||
| .SelectMany(c => c.Scripts) | ||
| .Where(s => s.Kind == "ManagedIdentityPolicy") | ||
| .ToList(); | ||
| Assert.Empty(databaseScriptChanges); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace KustoSchemaTools.Model | ||
| { | ||
| public class DatabasePolicies | ||
| { | ||
| public List<ManagedIdentityPolicy> ManagedIdentity { get; set; } = new List<ManagedIdentityPolicy>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using KustoSchemaTools.Changes; | ||
| using Newtonsoft.Json; | ||
| using KustoSchemaTools.Helpers; | ||
| using KustoSchemaTools.Parser; | ||
|
|
||
| namespace KustoSchemaTools.Model | ||
| { | ||
| public class ManagedIdentityPolicy | ||
| { | ||
| public string ObjectId { get; set; } | ||
| public List<string> AllowedUsages { get; set; } = new List<string>(); | ||
|
|
||
| /// <summary> | ||
| /// Creates a single script that sets managed identity policy for all provided identities. | ||
| /// Uses one combined command to avoid duplicate Kind keys in the diff pipeline. | ||
| /// </summary> | ||
| public static DatabaseScriptContainer CreateCombinedScript(string databaseName, List<ManagedIdentityPolicy> policies) | ||
| { | ||
| var policyObjects = policies | ||
| .OrderBy(p => p.ObjectId) | ||
| .Select(p => new { p.ObjectId, AllowedUsages = string.Join(", ", p.AllowedUsages.OrderBy(u => u)) }) | ||
| .ToArray(); | ||
| var json = JsonConvert.SerializeObject(policyObjects, Serialization.JsonPascalCase); | ||
| return new DatabaseScriptContainer("ManagedIdentityPolicy", 80, $".alter-merge database {databaseName.BracketIfIdentifier()} policy managed_identity ```{json}```"); | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
KustoSchemaTools/Parser/KustoLoader/KustoManagedIdentityPolicyLoader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using Kusto.Data.Common; | ||
| using KustoSchemaTools.Model; | ||
| using KustoSchemaTools.Plugins; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace KustoSchemaTools.Parser.KustoLoader | ||
| { | ||
| public class KustoManagedIdentityPolicyLoader : IKustoBulkEntitiesLoader | ||
| { | ||
| const string script = @" | ||
| .show database policy managed_identity | ||
| | project Policies = parse_json(Policy) | ||
| | mv-expand Policy = Policies | ||
| | project ObjectId = tostring(Policy.ObjectId), AllowedUsages = tostring(Policy.AllowedUsages)"; | ||
|
|
||
| public async Task Load(Database database, string databaseName, KustoClient kusto) | ||
| { | ||
| var response = await kusto.Client.ExecuteQueryAsync(databaseName, script, new ClientRequestProperties()); | ||
| var rows = response.As<ManagedIdentityRow>(); | ||
| if (database.Policies == null) | ||
| database.Policies = new DatabasePolicies(); | ||
| database.Policies.ManagedIdentity = rows | ||
| .Select(r => new ManagedIdentityPolicy | ||
| { | ||
| ObjectId = r.ObjectId, | ||
| AllowedUsages = r.AllowedUsages | ||
| .Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) | ||
| .OrderBy(u => u) | ||
| .ToList() | ||
| }) | ||
| .OrderBy(p => p.ObjectId) | ||
| .ToList(); | ||
| } | ||
|
|
||
| private class ManagedIdentityRow | ||
| { | ||
| public string ObjectId { get; set; } | ||
| public string AllowedUsages { get; set; } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove the unused
using Newtonsoft.Json;directive (it’s not referenced in this loader). Keeping it adds noise and may introduce build warnings if the repo ever enables warnings-as-errors.