Skip to content

Commit

Permalink
Merge pull request #18 from HammerheadShark666/setup-sql-server-manag…
Browse files Browse the repository at this point in the history
…ed-identity

Add DatabaseConnectionStringNotFound exception and refactor sql provi…
  • Loading branch information
HammerheadShark666 authored Sep 15, 2024
2 parents 8a959dc + 7d6cadf commit ba31991
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microservice.Customer.Api.Data.Repository;
using Microservice.Customer.Api.Data.Repository.Interfaces;
using Microservice.Customer.Api.Helpers;
using Microservice.Customer.Api.Helpers.Exceptions;
using Microservice.Customer.Api.Helpers.Interfaces;
using Microservice.Customer.Api.Helpers.Providers;
using Microservice.Customer.Api.Helpers.Swagger;
Expand Down Expand Up @@ -46,36 +47,21 @@ public static void ConfigureAutoMapper(this IServiceCollection services)
services.AddAutoMapper(Assembly.GetAssembly(typeof(GetCustomerMapper)));
}

//public static void ConfigureDatabaseContext(this IServiceCollection services, ConfigurationManager configuration)
//{
// services.AddDbContextFactory<CustomerDbContext>(options =>
// options.UseSqlServer(configuration.GetConnectionString(Helpers.Constants.DatabaseConnectionString),
// options => options.EnableRetryOnFailure()));
//}

public static void ConfigureSqlServer(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment)
{
if (environment.IsProduction())
{
services.AddDbContextFactory<CustomerDbContext>(options =>
{
SqlAuthenticationProvider.SetProvider(
SqlAuthenticationMethod.ActiveDirectoryManagedIdentity,
new ProductionAzureSQLProvider());
var sqlConnection = new SqlConnection(configuration.GetConnectionString(Constants.AzureDatabaseConnectionString));
options.UseSqlServer(sqlConnection);
});
var connectionString = configuration.GetConnectionString(Constants.AzureDatabaseConnectionString)
?? throw new DatabaseConnectionStringNotFound("Production database connection string not found.");

AddDbContextFactory(services, SqlAuthenticationMethod.ActiveDirectoryManagedIdentity, new ProductionAzureSQLProvider(), connectionString);
}
else if (environment.IsDevelopment())
{
services.AddDbContextFactory<CustomerDbContext>(options =>
{
SqlAuthenticationProvider.SetProvider(
SqlAuthenticationMethod.ActiveDirectoryServicePrincipal,
new DevelopmentAzureSQLProvider());
var sqlConnection = new SqlConnection(configuration.GetConnectionString(Constants.LocalDatabaseConnectionString));
options.UseSqlServer(sqlConnection);
});
var connectionString = configuration.GetConnectionString(Constants.LocalDatabaseConnectionString)
?? throw new DatabaseConnectionStringNotFound("Development database connection string not found.");

AddDbContextFactory(services, SqlAuthenticationMethod.ActiveDirectoryServicePrincipal, new DevelopmentAzureSQLProvider(), connectionString);
}
}

Expand Down Expand Up @@ -112,4 +98,17 @@ public static void ConfigureSwagger(this IServiceCollection services)
options.SupportNonNullableReferenceTypes();
});
}

private static void AddDbContextFactory(IServiceCollection services, SqlAuthenticationMethod sqlAuthenticationMethod, SqlAuthenticationProvider sqlAuthenticationProvider, string connectionString)
{
services.AddDbContextFactory<CustomerDbContext>(options =>
{
SqlAuthenticationProvider.SetProvider(
sqlAuthenticationMethod,
sqlAuthenticationProvider);
var sqlConnection = new SqlConnection(connectionString);
options.UseSqlServer(sqlConnection);
});
}

}
2 changes: 0 additions & 2 deletions Microservice.Customer.Api/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ public class Constants
public const string JwtAudience = "JWT_AUDIENCE";
public const string JwtSymmetricSecurityKey = "JWT_SYMMETRIC_SECURITY_KEY";

//public const string DatabaseConnectionString = "SQLAZURECONNSTR_CUSTOMER";

public const string AzureUserAssignedManagedIdentityClientId = "AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID";
public const string AzureDatabaseConnectionString = "AZURE_MANAGED_IDENTITY_SQL_CONNECTION";

Expand Down
3 changes: 0 additions & 3 deletions Microservice.Customer.Api/Helpers/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ public class EnvironmentVariables
public static string JwtIssuer => GetEnvironmentVariable(Constants.JwtIssuer);
public static string JwtAudience => GetEnvironmentVariable(Constants.JwtAudience);
public static string JwtSymmetricSecurityKey => GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey);
// public static string AzureDatabaseConnectionString => GetEnvironmentVariable(Constants.AzureDatabaseConnectionString);
public static string AzureUserAssignedManagedIdentityClientId => GetEnvironmentVariable(Constants.AzureUserAssignedManagedIdentityClientId);
//public static string LocalDatabaseConnectionString => GetEnvironmentVariable(Constants.LocalDatabaseConnectionString);
public static string LocalDevelopmentClientId => GetEnvironmentVariable(Constants.AzureLocalDevelopmentClientId);
public static string LocalDevelopmentClientSecret => GetEnvironmentVariable(Constants.AzureLocalDevelopmentClientSecret);
public static string LocalDevelopmentTenantId => GetEnvironmentVariable(Constants.AzureLocalDevelopmentTenantId);


public static string GetEnvironmentVariable(string name)
{
var variable = Environment.GetEnvironmentVariable(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Microservice.Customer.Api.Helpers.Exceptions;

public class DatabaseConnectionStringNotFound : Exception
{
public DatabaseConnectionStringNotFound()
{
}

public DatabaseConnectionStringNotFound(string message)
: base(message)
{
}

public DatabaseConnectionStringNotFound(string message, Exception inner)
: base(message, inner)
{
}
}

0 comments on commit ba31991

Please sign in to comment.