Skip to content

Commit

Permalink
Decouple HttpTrigger from discovery logic and ActivityService/History (
Browse files Browse the repository at this point in the history
…#317)

* Added ActivityHistory
refactored some classes into their own files
removed old DAL/DALResolver

* added collection/queue for activity support

* Shell for classifier logic

* changes to support ActivityHistory for long running activity (graph queries)

* Changes to support redirect on request.

* moved interface definitions to Interfaces
added some file headers
  • Loading branch information
jkeane authored Nov 10, 2020
1 parent f39cafb commit 75caccc
Show file tree
Hide file tree
Showing 50 changed files with 1,456 additions and 1,143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GraphDeltaProcessorFunctionsTests
private readonly ISecretClient _secretClient;
private readonly IGraphHelper<ServicePrincipal> _graphHelper;
private readonly IServicePrincipalProcessor _processor;
private readonly IActivityService _activityService;
IServiceProvider _serviceProvider;
ILogger<GraphDeltaProcessor> _logger;

Expand All @@ -31,7 +32,7 @@ public GraphDeltaProcessorFunctionsTests()
_serviceProvider = Substitute.For<IServiceProvider>();
_logger = Substitute.For<ILogger<GraphDeltaProcessor>>();

_subject = new GraphDeltaProcessor(_serviceProvider, _processor, _logger);
_subject = new GraphDeltaProcessor(_serviceProvider, _activityService, _processor, _logger);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public async Task GetDeltaGraphObjects_GetAll()
var config = GetConfiguration();
var service = serviceScope.ServiceProvider.GetService<IGraphHelper<ServicePrincipal>>();

var results = await service.GetDeltaGraphObjects(new ActivityContext(), config);
var results = await service.GetDeltaGraphObjects(new ActivityContext(null), config);

}
Assert.True(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using CSE.Automation.Graph;
using Microsoft.Graph;
using System.Threading.Tasks;
using Status = CSE.Automation.Model.Status;

namespace CSE.Automation.Tests.FunctionsUnitTests
{
Expand All @@ -25,7 +24,7 @@ class MockUserGraphHelper : IGraphHelper<User>
throw new NotImplementedException();
}

public Task<User> GetGraphObject(string id)
public Task<User> GetGraphObjectWithOwners(string id)
{
return Task.FromResult(new User());
}
Expand Down Expand Up @@ -68,7 +67,6 @@ public void ServicePrinciapalModelValidate_ReturnsTrueIfValid()
Deleted = new DateTime(2001, 1, 1),
LastUpdated = new DateTime(2002, 1, 1),
ObjectType = ObjectType.ServicePrincipal,
Status = Status.Remediated
};

var results = servicePrincipalValidator.Validate(servicePrincipal);
Expand All @@ -91,7 +89,7 @@ public void AuditEntryModelValidate_ReturnsValidationFailuresIfInvalid()
[Fact]
public void AuditEntryModelValidate_ReturnsTrueIfValid()
{
var context = new ActivityContext();
var context = new ActivityContext(null);

var auditItem = new AuditEntry()
{
Expand Down
97 changes: 50 additions & 47 deletions src/Automation/CSE.Automation/Base/KeyVaultBase.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CSE.Automation.Base
{
#pragma warning disable CA1052 // Static holder types should be Static or NotInheritable
public class KeyVaultBase
#pragma warning restore CA1052 // Static holder types should be Static or NotInheritable
{
#pragma warning disable CA1034 // Nested types should not be visible
public static class KeyVaultHelper
#pragma warning restore CA1034 // Nested types should not be visible
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSE.Automation.Base
{
#pragma warning disable CA1052 // Static holder types should be Static or NotInheritable
public class KeyVaultBase
#pragma warning restore CA1052 // Static holder types should be Static or NotInheritable
{
#pragma warning disable CA1034 // Nested types should not be visible
public static class KeyVaultHelper
#pragma warning restore CA1034 // Nested types should not be visible
{

/// <summary>
/// Build the Key Vault URL from the name
/// </summary>
/// <param name="name">Key Vault Name</param>
/// <returns>URL to Key Vault</returns>
public static bool BuildKeyVaultConnectionString(string keyVaultName, out string keyvaultConnection)
{
// name is required
if (string.IsNullOrWhiteSpace(keyVaultName))
{
throw new ArgumentNullException(nameof(keyVaultName));
}

/// <summary>
/// Build the Key Vault URL from the name
/// </summary>
/// <param name="name">Key Vault Name</param>
/// <returns>URL to Key Vault</returns>
public static bool BuildKeyVaultConnectionString(string keyVaultName, out string keyvaultConnection)
{
// name is required
if (string.IsNullOrWhiteSpace(keyVaultName))
{
throw new ArgumentNullException(nameof(keyVaultName));
}

var uriBuilder = new UriBuilder
{
Scheme = Uri.UriSchemeHttps,
Expand All @@ -34,24 +37,24 @@ public static bool BuildKeyVaultConnectionString(string keyVaultName, out string

keyvaultConnection = uriBuilder.Uri.AbsoluteUri;

return true;
}

/// <summary>
/// Validate the keyvault name
/// </summary>
/// <param name="name">string</param>
/// <returns>bool</returns>
public static bool ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();

return name.Length >= 3 && name.Length <= 24;
}
}
}
}
return true;
}

/// <summary>
/// Validate the keyvault name
/// </summary>
/// <param name="name">string</param>
/// <returns>bool</returns>
public static bool ValidateName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();

return name.Length >= 3 && name.Length <= 24;
}
}
}
}
4 changes: 0 additions & 4 deletions src/Automation/CSE.Automation/CSE.Automation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="DataAccess\DAL.cs" />
<Compile Remove="DataAccess\DALResolver.cs" />
<Compile Remove="Model\SecretSetting.cs" />
<Compile Remove="Processors\EvaluateProcessor.cs" />
<Compile Remove="Processors\ProcessorResolver.cs" />
Expand All @@ -40,8 +38,6 @@
</Content>
</ItemGroup>
<ItemGroup>
<None Include="DataAccess\DAL.cs" />
<None Include="DataAccess\DALResolver.cs" />
<None Include="Model\SecretSetting.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/Automation/CSE.Automation/Config/CosmosConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.Cosmos;
Expand Down
7 changes: 6 additions & 1 deletion src/Automation/CSE.Automation/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace CSE.Automation
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace CSE.Automation
{
public sealed class Constants
{
Expand All @@ -21,9 +24,11 @@ public sealed class Constants
public const string CosmosDBConfigCollectionName = "SPConfigurationCollection";
public const string CosmosDBAuditCollectionName = "SPAuditCollection";
public const string CosmosDBObjectTrackingCollectionName = "SPObjectTrackingCollection";
public const string CosmosDBActivityHistoryCollectionName = "SPActivityHistoryCollection";

// Azure Storage Queue Constants
public const string SPStorageConnectionString = "SPStorageConnectionString";
public const string DiscoverQueueAppSetting = "%SPDiscoverQueue%";
public const string EvaluateQueueAppSetting = "%SPEvaluateQueue%";
public const string UpdateQueueAppSetting = "%SPUpdateQueue%";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using CSE.Automation.Interfaces;
using CSE.Automation.Model;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;

namespace CSE.Automation.DataAccess
{
internal class ActivityHistoryRepository : CosmosDBRepository<ActivityHistory>, IActivityHistoryRepository
{
private readonly ActivityHistoryRepositorySettings settings;
public ActivityHistoryRepository(ActivityHistoryRepositorySettings settings, ILogger<AuditRepository> logger)
: base(settings, logger)
{
this.settings = settings;
}

public override string GenerateId(ActivityHistory entity)
{
if (string.IsNullOrWhiteSpace(entity.Id))
{
entity.Id = Guid.NewGuid().ToString();
}

return entity.Id;
}

public async Task<IEnumerable<ActivityHistory>> GetCorrelated(string correlationId)
{
return await InternalCosmosDBSqlQuery($"select * from c where c.correlationId = '{correlationId}' order by c.created").ConfigureAwait(false);
}

public override string CollectionName => settings.CollectionName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using CSE.Automation.Interfaces;
using CSE.Automation.Model;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;

namespace CSE.Automation.DataAccess
{
internal class ActivityHistoryRepositorySettings : CosmosDBSettings
{
public ActivityHistoryRepositorySettings(ISecretClient secretClient)
: base(secretClient)
{
}

public string CollectionName { get; set; }
public override void Validate()
{
base.Validate();
if (string.IsNullOrEmpty(this.CollectionName))
{
throw new ConfigurationErrorsException($"{this.GetType().Name}: CollectionName is invalid");
}
}
}
}
23 changes: 6 additions & 17 deletions src/Automation/CSE.Automation/DataAccess/AuditRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,13 @@

namespace CSE.Automation.DataAccess
{
internal class AuditRespositorySettings : CosmosDBSettings
{
public AuditRespositorySettings(ISecretClient secretClient) : base(secretClient) { }

public string CollectionName { get; set; }
public override void Validate()
{
base.Validate();
if (string.IsNullOrEmpty(this.CollectionName)) throw new ConfigurationErrorsException($"{this.GetType().Name}: CollectionName is invalid");
}
}

internal interface IAuditRepository : ICosmosDBRepository<AuditEntry> { }
internal class AuditRepository : CosmosDBRepository<AuditEntry>, IAuditRepository
{
private readonly AuditRespositorySettings _settings;
public AuditRepository(AuditRespositorySettings settings, ILogger<AuditRepository> logger) : base(settings, logger)
private readonly AuditRepositorySettings settings;
public AuditRepository(AuditRepositorySettings settings, ILogger<AuditRepository> logger)
: base(settings, logger)
{
_settings = settings;
this.settings = settings;
}

public override string GenerateId(AuditEntry entity)
Expand All @@ -38,9 +26,10 @@ public override string GenerateId(AuditEntry entity)
{
entity.Id = Guid.NewGuid().ToString();
}

return entity.Id;
}

public override string CollectionName => _settings.CollectionName;
public override string CollectionName => settings.CollectionName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using CSE.Automation.Interfaces;
using CSE.Automation.Model;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;

namespace CSE.Automation.DataAccess
{
internal class AuditRepositorySettings : CosmosDBSettings
{
public AuditRepositorySettings(ISecretClient secretClient)
: base(secretClient)
{
}

public string CollectionName { get; set; }
public override void Validate()
{
base.Validate();
if (string.IsNullOrEmpty(this.CollectionName))
{
throw new ConfigurationErrorsException($"{this.GetType().Name}: CollectionName is invalid");
}
}
}
}
Loading

0 comments on commit 75caccc

Please sign in to comment.