Skip to content

Commit

Permalink
[Refactor] ApiBase.Authorize
Browse files Browse the repository at this point in the history
[Add] return statements to ExchangeFileImportyApi
[Update] Dockerfile
  • Loading branch information
samatrhea committed Dec 7, 2023
1 parent 1759bad commit 8663c15
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
23 changes: 3 additions & 20 deletions CometServer/Modules/10-25/ApiBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,22 @@ protected ApiBase(IAppConfigService appConfigService, ILoggerFactory loggerFacto
/// </returns>
protected async Task Authorize(IAppConfigService appConfigService, ICredentialsService credentialsService, string username)
{
NpgsqlConnection connection = null;
NpgsqlTransaction transaction = null;

try
{
connection = new NpgsqlConnection(Services.Utils.GetConnectionString(appConfigService.AppConfig.Backtier, appConfigService.AppConfig.Backtier.Database));
await using var connection = new NpgsqlConnection(Services.Utils.GetConnectionString(appConfigService.AppConfig.Backtier, appConfigService.AppConfig.Backtier.Database));

await connection.OpenAsync();

transaction = await connection.BeginTransactionAsync();
await using var transaction = await connection.BeginTransactionAsync();

await credentialsService.ResolveCredentials(transaction, username);
}
catch (Exception)
{
this.logger.LogWarning("Authorization failed for {username}", username);

transaction?.RollbackAsync();

throw;
}
finally
{
if (transaction != null)
{
await transaction.DisposeAsync();
}

if (connection != null)
{
await connection.CloseAsync();
await connection.DisposeAsync();
}
}
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion CometServer/Modules/10-25/ExchangeFileImportyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ internal async Task RestoreDatastore(HttpResponse response, IDataStoreController

response.StatusCode = (int)HttpStatusCode.Forbidden;
await response.AsJson("restore is not allowed");
return;
}

try
Expand Down Expand Up @@ -291,6 +292,7 @@ internal async Task SeedDataStore(HttpRequest request, HttpResponse response, IR

response.StatusCode = (int)HttpStatusCode.Forbidden;
await response.AsJson("seed is not allowed");
return;
}

this.logger.LogInformation("Starting data store seeding");
Expand All @@ -307,6 +309,7 @@ internal async Task SeedDataStore(HttpRequest request, HttpResponse response, IR
{
response.StatusCode = (int)HttpStatusCode.BadRequest;
await response.AsJson("invalid seed file");
return;
}

// Remove the exchange file after processing (saving space)
Expand Down Expand Up @@ -335,7 +338,7 @@ internal async Task SeedDataStore(HttpRequest request, HttpResponse response, IR

this.logger.LogInformation("Finished the data store seed");

response.StatusCode = (int) HttpStatusCode.OK;
response.StatusCode = (int)HttpStatusCode.OK;
await response.AsJson("Datastore seeded");
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion CometServer/Modules/10-25/SiteDirectoryApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public override void AddRoutes(IEndpointRouteBuilder app)
{
try
{
await this.Authorize(this.AppConfigService, credentialsService,req.HttpContext.User.Identity.Name);
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
Expand Down
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ COPY CometServer CometServer

RUN dotnet build CDP4DatabaseAuthentication -c Release
RUN dotnet build CDP4WspDatabaseAuthentication -c Release
RUN dotnet publish CometServer -c Release
RUN dotnet publish -r linux-x64 CometServer -c Release -o /app/CometServer/bin/Release/publish

FROM mcr.microsoft.com/dotnet/aspnet:8.0
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
WORKDIR /app
RUN mkdir /app/logs
RUN mkdir /app/storage
Expand All @@ -21,6 +21,9 @@ RUN mkdir /app/Authentication/CDP4Database
RUN mkdir /app/Authentication/CDP4WspDatabase

COPY --from=build-env /app/CometServer/bin/Release/publish .
RUN rm /app/appsettings.Development.json
RUN mv /app/appsettings.Production.json /app/appsettings.json
RUN rm /app/System.Drawing.Common.dll

# COPY CDP4DatabaseAuthentication plugin
COPY --from=build-env /app/CDP4DatabaseAuthentication/bin/Release/CDP4DatabaseAuthentication.dll /app/Authentication/CDP4Database/CDP4DatabaseAuthentication.dll
Expand All @@ -30,9 +33,7 @@ COPY --from=build-env /app/CDP4DatabaseAuthentication/bin/Release/config.json /a
COPY --from=build-env /app/CDP4WspDatabaseAuthentication/bin/Release/CDP4WspDatabaseAuthentication.dll /app/Authentication/CDP4WspDatabase/CDP4WspDatabaseAuthentication.dll
COPY --from=build-env /app/CDP4WspDatabaseAuthentication/bin/Release/config.json /app/Authentication/CDP4WspDatabase/config.json

# Create a user and give the user access to the working directory
RUN useradd -m cdp4comet
RUN chown -R cdp4comet /app
USER cdp4comet

# set to use the non-root USER here
RUN chown -R $APP_UID /app
USER $APP_UID
CMD ["dotnet", "CometServer.dll"]

0 comments on commit 8663c15

Please sign in to comment.