Skip to content

Commit

Permalink
fix review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
ntruchsess committed Aug 2, 2024
1 parent 61ecbc8 commit c9d9a56
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Configuration;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Encryption;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories;
Expand All @@ -52,7 +51,7 @@ public class ServiceAccountBusinessLogic(
{
private readonly IIdentityData _identityData = identityService.IdentityData;
private readonly ServiceAccountSettings _settings = options.Value;
private readonly TimeSpan lockExpiryTime = new(options.Value.LockExpirySeconds * 10000000L);
private readonly TimeSpan _lockExpiryTime = new(options.Value.LockExpirySeconds * 10000000L);

private const string CompanyId = "companyId";

Expand Down Expand Up @@ -103,11 +102,8 @@ public async Task<int> DeleteOwnCompanyServiceAccountAsync(Guid serviceAccountId
{
var serviceAccountRepository = portalRepositories.GetInstance<IServiceAccountRepository>();
var companyId = _identityData.CompanyId;
var result = await serviceAccountRepository.GetOwnCompanyServiceAccountWithIamServiceAccountRolesAsync(serviceAccountId, companyId).ConfigureAwait(ConfigureAwaitOptions.None);
if (result == null)
{
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_NOT_CONFLICT, [new("serviceAccountId", serviceAccountId.ToString()), new(CompanyId, companyId.ToString())]);
}
var result = await serviceAccountRepository.GetOwnCompanyServiceAccountWithIamServiceAccountRolesAsync(serviceAccountId, companyId).ConfigureAwait(ConfigureAwaitOptions.None)
?? throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_NOT_CONFLICT, [new("serviceAccountId", serviceAccountId.ToString()), new(CompanyId, companyId.ToString())]);

if (result.StatusId is ConnectorStatusId.ACTIVE or ConnectorStatusId.PENDING)
{
Expand All @@ -119,14 +115,13 @@ public async Task<int> DeleteOwnCompanyServiceAccountAsync(Guid serviceAccountId
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_USERID_ACTIVATION_ACTIVE_CONFLICT);
}

if (!result.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(lockExpiryTime)))
if (!result.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(_lockExpiryTime)))
{
throw UnexpectedConditionException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", serviceAccountId.ToString())]);
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", serviceAccountId.ToString())]);
}

// save the lock of the service account here to make sure no process overwrites it
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
portalRepositories.Clear();

// serviceAccount
if (!string.IsNullOrWhiteSpace(result.ClientClientId) && !result.IsDimServiceAccount)
Expand Down Expand Up @@ -277,14 +272,13 @@ public async Task<ServiceAccountDetails> UpdateOwnCompanyServiceAccountDetailsAs
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_CLIENTID_NOT_NULL_CONFLICT, [new("serviceAccountId", serviceAccountId.ToString())]);
}

if (!result.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(lockExpiryTime)))
if (!result.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(_lockExpiryTime)))
{
throw UnexpectedConditionException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", serviceAccountId.ToString())]);
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", serviceAccountId.ToString())]);
}

// save the lock of the service account here to make sure no process overwrites it
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
portalRepositories.Clear();

ClientAuthData? authData;
if (result.ServiceAccount.CompanyServiceAccountKindId == CompanyServiceAccountKindId.INTERNAL)
Expand All @@ -303,18 +297,8 @@ public async Task<ServiceAccountDetails> UpdateOwnCompanyServiceAccountDetailsAs
authData = null;
}

serviceAccountRepository.AttachAndModifyCompanyServiceAccount(
serviceAccountId,
sa =>
{
sa.Name = result.ServiceAccount.Name;
sa.Description = result.ServiceAccount.Description;
},
sa =>
{
sa.Name = serviceAccountDetails.Name;
sa.Description = serviceAccountDetails.Description;
});
result.ServiceAccount.Name = serviceAccountDetails.Name;
result.ServiceAccount.Description = serviceAccountDetails.Description;

result.ServiceAccount.ReleaseLock();
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
Expand Down Expand Up @@ -420,20 +404,20 @@ public async Task HandleServiceAccountDeletionCallback(Guid processId)
throw new ConflictException($"ServiceAccountId must be set for process {processId}");
}

if (!processData.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(lockExpiryTime)))
if (!processData.ServiceAccount.TryLock(dateTimeProvider.OffsetNow.Add(_lockExpiryTime)))
{
throw UnexpectedConditionException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", processData.ServiceAccount.Id.ToString())]);
throw ConflictException.Create(AdministrationServiceAccountErrors.SERVICE_ACCOUNT_LOCKED, [new("serviceAccountId", processData.ServiceAccount.Id.ToString())]);
}

// save the lock of the service account here to make sure no process overwrites it
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
portalRepositories.Clear();

portalRepositories.GetInstance<IUserRepository>().AttachAndModifyIdentity(processData.ServiceAccount.Id, null, i =>
{
i.UserStatusId = UserStatusId.INACTIVE;
});

processData.ServiceAccount.ReleaseLock();
context.FinalizeProcessStep();
await portalRepositories.SaveAsync().ConfigureAwait(ConfigureAwaitOptions.None);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Enums;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public IAsyncEnumerable<Process> GetActiveProcesses(IEnumerable<ProcessTypeId> p

public Task<(ProcessTypeId ProcessTypeId, VerifyProcessData ProcessData, CompanyServiceAccount? ServiceAccount)> GetProcessDataForServiceAccountDeletionCallback(Guid processId, IEnumerable<ProcessStepTypeId> processStepTypeIds) =>
_context.Processes
.AsNoTracking()
.Where(x => x.Id == processId && x.ProcessTypeId == ProcessTypeId.DIM_TECHNICAL_USER)
.Select(x => new ValueTuple<ProcessTypeId, VerifyProcessData, CompanyServiceAccount?>(
x.ProcessTypeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void AttachAndModifyCompanyServiceAccount(

public Task<CompanyServiceAccountWithRoleDataClientId?> GetOwnCompanyServiceAccountWithIamClientIdAsync(Guid serviceAccountId, Guid userCompanyId) =>
portalDbContext.CompanyServiceAccounts
.AsNoTracking()
.Where(serviceAccount =>
serviceAccount.Id == serviceAccountId
&& serviceAccount.Identity!.UserStatusId == UserStatusId.ACTIVE
Expand All @@ -89,7 +88,6 @@ public void AttachAndModifyCompanyServiceAccount(

public Task<OwnServiceAccountData?> GetOwnCompanyServiceAccountWithIamServiceAccountRolesAsync(Guid serviceAccountId, Guid companyId) =>
portalDbContext.CompanyServiceAccounts
.AsNoTracking()
.Where(serviceAccount =>
serviceAccount.Id == serviceAccountId &&
serviceAccount.Identity!.UserStatusId == UserStatusId.ACTIVE &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,28 @@ public class DimUserProcessTypeExecutor(
IPortalRepositories portalRepositories,
IDimUserProcessService dimUserProcessService) : IProcessTypeExecutor
{
private readonly IEnumerable<ProcessStepTypeId> _executableProcessSteps =
[ProcessStepTypeId.CREATE_DIM_TECHNICAL_USER, ProcessStepTypeId.DELETE_DIM_TECHNICAL_USER];

private static readonly IEnumerable<int> RecoverableStatusCodes =
[
(int)HttpStatusCode.BadGateway,
(int)HttpStatusCode.ServiceUnavailable,
(int)HttpStatusCode.GatewayTimeout
];

private static readonly IEnumerable<ProcessStepTypeId> ExecutableProcessSteps =
[
ProcessStepTypeId.CREATE_DIM_TECHNICAL_USER,
ProcessStepTypeId.DELETE_DIM_TECHNICAL_USER
];

private Guid _dimServiceAccountId;
private Guid _processId;

public ProcessTypeId GetProcessTypeId() => ProcessTypeId.DIM_TECHNICAL_USER;

public bool IsExecutableStepTypeId(ProcessStepTypeId processStepTypeId) =>
_executableProcessSteps.Contains(processStepTypeId);
ExecutableProcessSteps.Contains(processStepTypeId);

public IEnumerable<ProcessStepTypeId> GetExecutableStepTypeIds() => _executableProcessSteps;
public IEnumerable<ProcessStepTypeId> GetExecutableStepTypeIds() => ExecutableProcessSteps;

public async ValueTask<IProcessTypeExecutor.InitializationResult> InitializeProcess(Guid processId, IEnumerable<ProcessStepTypeId> processStepTypeIds)
{
Expand Down Expand Up @@ -96,20 +99,30 @@ public bool IsExecutableStepTypeId(ProcessStepTypeId processStepTypeId) =>
}
catch (Exception ex) when (ex is not SystemException)
{
(stepStatusId, processMessage, nextStepTypeIds) = ProcessError(ex);
(stepStatusId, processMessage, nextStepTypeIds) = ProcessError(ex, processStepTypeId);
modified = true;
}

return new IProcessTypeExecutor.StepExecutionResult(modified, stepStatusId, nextStepTypeIds, null, processMessage);
}

private (ProcessStepStatusId StatusId, string? ProcessMessage, IEnumerable<ProcessStepTypeId>? nextSteps) ProcessError(Exception ex) =>
private static (ProcessStepStatusId StatusId, string? ProcessMessage, IEnumerable<ProcessStepTypeId>? nextSteps) ProcessError(Exception ex, ProcessStepTypeId processStepTypeId) =>
ex switch
{
ServiceException { IsRecoverable: true } => (ProcessStepStatusId.TODO, ex.Message, null),
FlurlHttpException { StatusCode: not null } flurlHttpException when
RecoverableStatusCodes.Contains(flurlHttpException.StatusCode.Value) => (ProcessStepStatusId.TODO,
ex.Message, null),
_ => (ProcessStepStatusId.FAILED, ex.Message, Enumerable.Repeat(GetProcessTypeId() == ProcessTypeId.DIM_TECHNICAL_USER ? ProcessStepTypeId.RETRIGGER_CREATE_DIM_TECHNICAL_USER : ProcessStepTypeId.RETRIGGER_DELETE_DIM_TECHNICAL_USER, 1))
_ => (
ProcessStepStatusId.FAILED,
ex.Message,
processStepTypeId switch
{
ProcessStepTypeId.CREATE_DIM_TECHNICAL_USER => [ProcessStepTypeId.RETRIGGER_CREATE_DIM_TECHNICAL_USER],
ProcessStepTypeId.DELETE_DIM_TECHNICAL_USER => [ProcessStepTypeId.RETRIGGER_DELETE_DIM_TECHNICAL_USER],
_ => throw new UnexpectedConditionException(
$"Execution for {processStepTypeId} is currently not supported.")
}
)
};
}

0 comments on commit c9d9a56

Please sign in to comment.