Skip to content
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

feat(ci): adjust company identity creation #36

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions charts/dim/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
apiVersion: v2
name: dim
type: application
version: 0.0.2
appVersion: 0.0.2
version: 0.0.3
appVersion: 0.0.3
description: Helm chart for DIM Middle Layer
home: https://github.com/catenax-ng/dim-repo
dependencies:
Expand Down
2 changes: 1 addition & 1 deletion charts/dim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To use the helm chart as a dependency:
dependencies:
- name: dim
repository: https://phil91.github.io/dim-client
version: 0.0.2
version: 0.0.3
```
## Requirements
Expand Down
2 changes: 1 addition & 1 deletion consortia/argocd-app-templates/appsetup-int.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
source:
path: charts/dim
repoURL: 'https://github.com/phil91/dim-client.git'
targetRevision: dim-0.0.2
targetRevision: dim-0.0.3
plugin:
env:
- name: AVP_SECRET
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<Project>
<PropertyGroup>
<VersionPrefix>0.0.2</VersionPrefix>
<VersionPrefix>0.0.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
</Project>
32 changes: 19 additions & 13 deletions src/clients/Dim.Clients/Api/Dim/CreateCompanyIdentityRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,25 @@ public record CreateCompanyIdentityRequest(
);

public record Payload(
[property: JsonPropertyName("hostingUrl")] string HostingUrl,
[property: JsonPropertyName("bootstrap")] Bootstrap Bootstrap,
[property: JsonPropertyName("keys")] IEnumerable<Key> Keys
[property: JsonPropertyName("hostingURL")] string HostingUrl,
[property: JsonPropertyName("network")] Network Network,
[property: JsonPropertyName("services")] IEnumerable<Service> Services,
[property: JsonPropertyName("keys")] IEnumerable<Key> Keys,
[property: JsonPropertyName("name")] string Name
);

public record Service(
[property: JsonPropertyName("id")] string Id,
[property: JsonPropertyName("type")] string Type
);

public record Bootstrap(
[property: JsonPropertyName("description")] string Description,
[property: JsonPropertyName("name")] string Name,
[property: JsonPropertyName("protocols")] IEnumerable<string> Protocols
[property: JsonPropertyName("type")] string Type,
[property: JsonPropertyName("serviceEndpoint")] string ServiceEndpoint
);

public record Key(
public record Network(
[property: JsonPropertyName("didMethod")] string DidMethod,
[property: JsonPropertyName("type")] string Type
);

public record Network(
[property: JsonPropertyName("didMethod")] string DidMethod,
public record Key(
[property: JsonPropertyName("type")] string Type
);

Expand All @@ -56,3 +53,12 @@ public record CreateCompanyIdentityResponse(
[property: JsonPropertyName("companyId")] Guid CompanyId,
[property: JsonPropertyName("downloadURL")] string DownloadUrl
);
//
// public record UpdateCompanyIdentityRequest(
// [property: JsonPropertyName("didDocUpdates")] DidDocUpdates DidDocUpdates
// );
//
// public record DidDocUpdates(
// [property: JsonPropertyName("removeServices")] IEnumerable<string> RemoveServices,
// [property: JsonPropertyName("addServices")] IEnumerable<Service> AddServices
// );
42 changes: 19 additions & 23 deletions src/clients/Dim.Clients/Api/Dim/DimClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,26 @@

namespace Dim.Clients.Api.Dim;

public class DimClient : IDimClient
public class DimClient(IBasicAuthTokenService basicAuthTokenService, IHttpClientFactory clientFactory)
: IDimClient
{
private readonly IBasicAuthTokenService _basicAuthTokenService;
private readonly IHttpClientFactory _clientFactory;

public DimClient(IBasicAuthTokenService basicAuthTokenService, IHttpClientFactory clientFactory)
{
_basicAuthTokenService = basicAuthTokenService;
_clientFactory = clientFactory;
}

public async Task<CreateCompanyIdentityResponse> CreateCompanyIdentity(BasicAuthSettings dimBasicAuth, string hostingUrl, string baseUrl, string tenantName, bool isIssuer, CancellationToken cancellationToken)
public async Task<CreateCompanyIdentityResponse> CreateCompanyIdentity(BasicAuthSettings dimBasicAuth, Guid tenantId, string hostingUrl, string baseUrl, bool isIssuer, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimBasicAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimBasicAuth, cancellationToken).ConfigureAwait(false);
var data = new CreateCompanyIdentityRequest(new Payload(
hostingUrl,
new Bootstrap("Holder with IATP", "Holder IATP", Enumerable.Repeat("IATP", 1)),
new Network("web", "production"),
[new Service($"dim:web:{tenantId}", "CredentialService", "https://dis-agent-prod.eu10.dim.cloud.sap/api/v1.0.0/iatp")],
isIssuer ?
Enumerable.Empty<Key>() :
[
new("SIGNING"),
new("SIGNING_VC")
] :
new Key[]
{
new("SIGNING"),
new("SIGNING_VC"),
}));
new("SIGNING")
},
"holder iatp"));
var result = await client.PostAsJsonAsync($"{baseUrl}/api/v2.0.0/companyIdentities", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("create-company-identity", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
Expand Down Expand Up @@ -77,15 +73,15 @@ public async Task<CreateCompanyIdentityResponse> CreateCompanyIdentity(BasicAuth

public async Task<JsonDocument> GetDidDocument(string url, CancellationToken cancellationToken)
{
var client = _clientFactory.CreateClient("didDocumentDownload");
var client = clientFactory.CreateClient("didDocumentDownload");
using var result = await client.GetStreamAsync(url, cancellationToken).ConfigureAwait(false);
var document = await JsonDocument.ParseAsync(result, cancellationToken: cancellationToken).ConfigureAwait(false);
return document;
}

public async Task<string> CreateApplication(BasicAuthSettings dimAuth, string dimBaseUrl, string tenantName, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var data = new CreateApplicationRequest(new ApplicationPayload(
"catena-x-portal",
$"Catena-X Portal MIW for {tenantName}",
Expand Down Expand Up @@ -117,7 +113,7 @@ public async Task<string> CreateApplication(BasicAuthSettings dimAuth, string di

public async Task<string> GetApplication(BasicAuthSettings dimAuth, string dimBaseUrl, string applicationId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var result = await client.GetAsync($"{dimBaseUrl}/api/v2.0.0/applications/{applicationId}", cancellationToken)
.CatchingIntoServiceExceptionFor("get-application", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
async m =>
Expand Down Expand Up @@ -145,7 +141,7 @@ public async Task<string> GetApplication(BasicAuthSettings dimAuth, string dimBa

public async Task AssignApplicationToCompany(BasicAuthSettings dimAuth, string dimBaseUrl, string applicationKey, Guid companyId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var data = new CompanyIdentityPatch(new ApplicationUpdates(Enumerable.Repeat(applicationKey, 1)));
await client.PatchAsJsonAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("assign-application", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
Expand All @@ -158,7 +154,7 @@ await client.PatchAsJsonAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{compa

public async Task<string> GetStatusList(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var result = await client.GetAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}/revocationLists", cancellationToken);
try
{
Expand All @@ -185,7 +181,7 @@ public async Task<string> GetStatusList(BasicAuthSettings dimAuth, string dimBas

public async Task<string> CreateStatusList(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, CancellationToken cancellationToken)
{
var client = await _basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var client = await basicAuthTokenService.GetBasicAuthorizedClient<DimClient>(dimAuth, cancellationToken).ConfigureAwait(false);
var data = new CreateStatusListRequest(new CreateStatusListPaypload(new CreateStatusList("StatusList2021", DateTimeOffset.UtcNow.ToString("yyyyMMdd"), "New revocation list", 2097152)));
var result = await client.PostAsJsonAsync($"{dimBaseUrl}/api/v2.0.0/companyIdentities/{companyId}/revocationLists", data, JsonSerializerExtensions.Options, cancellationToken)
.CatchingIntoServiceExceptionFor("assign-application", HttpAsyncResponseMessageExtension.RecoverOptions.INFRASTRUCTURE,
Expand Down
3 changes: 1 addition & 2 deletions src/clients/Dim.Clients/Api/Dim/IDimClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ namespace Dim.Clients.Api.Dim;

public interface IDimClient
{
Task<CreateCompanyIdentityResponse> CreateCompanyIdentity(BasicAuthSettings dimBasicAuth, string hostingUrl, string baseUrl, string tenantName, bool isIssuer, CancellationToken cancellationToken);
Task<CreateCompanyIdentityResponse> CreateCompanyIdentity(BasicAuthSettings dimBasicAuth, Guid tenantId, string hostingUrl, string baseUrl, bool isIssuer, CancellationToken cancellationToken);
Task<JsonDocument> GetDidDocument(string url, CancellationToken cancellationToken);
Task<string> CreateApplication(BasicAuthSettings dimAuth, string dimBaseUrl, string tenantName, CancellationToken cancellationToken);

Task<string> GetApplication(BasicAuthSettings dimAuth, string dimBaseUrl, string applicationId, CancellationToken cancellationToken);
Task AssignApplicationToCompany(BasicAuthSettings dimAuth, string dimBaseUrl, string applicationKey, Guid companyId, CancellationToken cancellationToken);
Task<string> GetStatusList(BasicAuthSettings dimAuth, string dimBaseUrl, Guid companyId, CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public interface ITenantRepository
Task<(bool Exists, Guid TechnicalUserId, string CompanyName, string Bpn)> GetTenantDataForTechnicalUserProcessId(Guid processId);
Task<(Guid? spaceId, string technicalUserName)> GetSpaceIdAndTechnicalUserName(Guid technicalUserId);
Task<(Guid ExternalId, string? TokenAddress, string? ClientId, byte[]? ClientSecret, byte[]? InitializationVector, int? EncryptionMode)> GetTechnicalUserCallbackData(Guid technicalUserId);
Task<(Guid? DimInstanceId, Guid? CompanyId)> GetDimInstanceIdAndDid(Guid tenantId);
}
51 changes: 25 additions & 26 deletions src/database/Dim.DbAccess/Repositories/TenantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@

namespace Dim.DbAccess.Repositories;

public class TenantRepository : ITenantRepository
public class TenantRepository(DimDbContext context) : ITenantRepository
{
private readonly DimDbContext _context;

public TenantRepository(DimDbContext context)
{
_context = context;
}

public Tenant CreateTenant(string companyName, string bpn, string didDocumentLocation, bool isIssuer, Guid processId, Guid operatorId) =>
_context.Tenants.Add(new Tenant(Guid.NewGuid(), companyName, bpn, didDocumentLocation, isIssuer, processId, operatorId)).Entity;
context.Tenants.Add(new Tenant(Guid.NewGuid(), companyName, bpn, didDocumentLocation, isIssuer, processId, operatorId)).Entity;

public Task<(bool Exists, Guid TenantId, string CompanyName, string Bpn)> GetTenantDataForProcessId(Guid processId) =>
_context.Tenants
context.Tenants
.Where(x => x.ProcessId == processId)
.Select(x => new ValueTuple<bool, Guid, string, string>(true, x.Id, x.CompanyName, x.Bpn))
.SingleOrDefaultAsync();
Expand All @@ -45,54 +38,54 @@ public void AttachAndModifyTenant(Guid tenantId, Action<Tenant>? initialize, Act
{
var tenant = new Tenant(tenantId, null!, null!, null!, default, Guid.Empty, Guid.Empty);
initialize?.Invoke(tenant);
_context.Tenants.Attach(tenant);
context.Tenants.Attach(tenant);
modify(tenant);
}

public Task<Guid?> GetSubAccountIdByTenantId(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => x.SubAccountId)
.SingleOrDefaultAsync();

public Task<(Guid? SubAccountId, string? ServiceInstanceId)> GetSubAccountAndServiceInstanceIdsByTenantId(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<Guid?, string?>(x.SubAccountId, x.ServiceInstanceId))
.SingleOrDefaultAsync();

public Task<(Guid? SubAccountId, string? ServiceBindingName)> GetSubAccountIdAndServiceBindingNameByTenantId(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<Guid?, string?>(x.SubAccountId, x.ServiceBindingName))
.SingleOrDefaultAsync();

public Task<Guid?> GetSpaceId(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => x.SpaceId)
.SingleOrDefaultAsync();

public Task<Guid?> GetDimInstanceId(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => x.DimInstanceId)
.SingleOrDefaultAsync();

public Task<(string bpn, string? DownloadUrl, string? Did, Guid? DimInstanceId)> GetCallbackData(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<string, string?, string?, Guid?>(x.Bpn, x.DidDownloadUrl, x.Did, x.DimInstanceId))
.SingleOrDefaultAsync();

public Task<(Guid? DimInstanceId, string HostingUrl, bool IsIssuer)> GetDimInstanceIdAndHostingUrl(Guid tenantId)
=> _context.Tenants
=> context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<Guid?, string, bool>(x.DimInstanceId, x.DidDocumentLocation, x.IsIssuer))
.SingleOrDefaultAsync();

public Task<(string? ApplicationId, Guid? CompanyId, Guid? DimInstanceId, bool IsIssuer)> GetApplicationAndCompanyId(Guid tenantId) =>
_context.Tenants
context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<string?, Guid?, Guid?, bool>(
x.ApplicationId,
Expand All @@ -102,40 +95,40 @@ public void AttachAndModifyTenant(Guid tenantId, Action<Tenant>? initialize, Act
.SingleOrDefaultAsync();

public Task<(bool Exists, Guid? CompanyId, Guid? InstanceId)> GetCompanyAndInstanceIdForBpn(string bpn) =>
_context.Tenants.Where(x => x.Bpn == bpn)
context.Tenants.Where(x => x.Bpn == bpn)
.Select(x => new ValueTuple<bool, Guid?, Guid?>(true, x.CompanyId, x.DimInstanceId))
.SingleOrDefaultAsync();

public void CreateTenantTechnicalUser(Guid tenantId, string technicalUserName, Guid externalId, Guid processId) =>
_context.TechnicalUsers.Add(new TechnicalUser(Guid.NewGuid(), tenantId, externalId, technicalUserName, processId));
context.TechnicalUsers.Add(new TechnicalUser(Guid.NewGuid(), tenantId, externalId, technicalUserName, processId));

public void AttachAndModifyTechnicalUser(Guid technicalUserId, Action<TechnicalUser>? initialize, Action<TechnicalUser> modify)
{
var technicalUser = new TechnicalUser(technicalUserId, Guid.Empty, Guid.Empty, null!, Guid.Empty);
initialize?.Invoke(technicalUser);
_context.TechnicalUsers.Attach(technicalUser);
context.TechnicalUsers.Attach(technicalUser);
modify(technicalUser);
}

public Task<(bool Exists, Guid TenantId)> GetTenantForBpn(string bpn) =>
_context.Tenants.Where(x => x.Bpn == bpn)
context.Tenants.Where(x => x.Bpn == bpn)
.Select(x => new ValueTuple<bool, Guid>(true, x.Id))
.SingleOrDefaultAsync();

public Task<(bool Exists, Guid TechnicalUserId, string CompanyName, string Bpn)> GetTenantDataForTechnicalUserProcessId(Guid processId) =>
_context.TechnicalUsers
context.TechnicalUsers
.Where(x => x.ProcessId == processId)
.Select(x => new ValueTuple<bool, Guid, string, string>(true, x.Id, x.Tenant!.CompanyName, x.Tenant.Bpn))
.SingleOrDefaultAsync();

public Task<(Guid? spaceId, string technicalUserName)> GetSpaceIdAndTechnicalUserName(Guid technicalUserId) =>
_context.TechnicalUsers
context.TechnicalUsers
.Where(x => x.Id == technicalUserId)
.Select(x => new ValueTuple<Guid?, string>(x.Tenant!.SpaceId, x.TechnicalUserName))
.SingleOrDefaultAsync();

public Task<(Guid ExternalId, string? TokenAddress, string? ClientId, byte[]? ClientSecret, byte[]? InitializationVector, int? EncryptionMode)> GetTechnicalUserCallbackData(Guid technicalUserId) =>
_context.TechnicalUsers
context.TechnicalUsers
.Where(x => x.Id == technicalUserId)
.Select(x => new ValueTuple<Guid, string?, string?, byte[]?, byte[]?, int?>(
x.ExternalId,
Expand All @@ -145,4 +138,10 @@ public void AttachAndModifyTechnicalUser(Guid technicalUserId, Action<TechnicalU
x.InitializationVector,
x.EncryptionMode))
.SingleOrDefaultAsync();

public Task<(Guid? DimInstanceId, Guid? CompanyId)> GetDimInstanceIdAndDid(Guid tenantId) =>
context.Tenants
.Where(x => x.Id == tenantId)
.Select(x => new ValueTuple<Guid?, Guid?>(x.DimInstanceId, x.CompanyId))
.SingleOrDefaultAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public class DimProcessTypeExecutor(
.ConfigureAwait(false),
ProcessStepTypeId.CREATE_APPLICATION => await dimProcessHandler.CreateApplication(_tenantName, _tenantId, cancellationToken)
.ConfigureAwait(false),
ProcessStepTypeId.CREATE_COMPANY_IDENTITY => await dimProcessHandler.CreateCompanyIdentity(_tenantId, _tenantName, cancellationToken)
ProcessStepTypeId.CREATE_COMPANY_IDENTITY => await dimProcessHandler.CreateCompanyIdentity(_tenantId, cancellationToken)
.ConfigureAwait(false),
ProcessStepTypeId.ASSIGN_COMPANY_APPLICATION => await dimProcessHandler.AssignCompanyApplication(_tenantId, cancellationToken)
.ConfigureAwait(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public record CallbackDataModel(
);

public record AuthenticationDetail(
[property: JsonPropertyName("authenticationServiceUrl")] string AuthenticationServiceUrl,
[property: JsonPropertyName("authenticationServiceUrl")] string AuthenticationServiceUrl,
[property: JsonPropertyName("clientID")] string ClientId,
[property: JsonPropertyName("clientSecret")] string ClientSecret
);
Loading
Loading