/// <inheritdoc/> public async Task RenewCertificateAsync(string groupId, CancellationToken ct) { if (string.IsNullOrEmpty(groupId)) { throw new ArgumentNullException(nameof(groupId)); } var group = await _groups.FindAsync(groupId, ct); if (group == null) { throw new ResourceNotFoundException("Group not found"); } TrustGroupRegistrationModel parent = null; if (!string.IsNullOrEmpty(group.Group.ParentId)) { parent = await _groups.FindAsync(group.Group.ParentId, ct); if (parent == null) { throw new ResourceNotFoundException("Parent not found"); } } var certificate = await RenewGroupCertificateAsync(group, parent, ct); _logger.Information("Group {groupId} certificate renewed.", groupId); }
/// <summary> /// Create registration model /// </summary> /// <param name="model"></param> public static TrustGroupRegistrationApiModel ToApiModel( this TrustGroupRegistrationModel model) { if (model == null) { return(null); } return(new TrustGroupRegistrationApiModel { Id = model.Id, Group = model.Group.ToApiModel() }); }
/// <summary> /// Renew group certificate /// </summary> /// <param name="group"></param> /// <param name="parent"></param> /// <param name="ct"></param> /// <returns></returns> private async Task <Certificate> RenewGroupCertificateAsync( TrustGroupRegistrationModel group, TrustGroupRegistrationModel parent, CancellationToken ct) { var now = DateTime.UtcNow.AddDays(-1); var notBefore = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Utc); if (parent == null) { return(await RenewGroupRootAsync(group, notBefore, ct)); } return(await RenewGroupFromParentAsync(group, parent, notBefore, ct)); }
public void ItFetchesACertificateGroupConfigurationFromTheServiceLayer() { var id = "Default"; var configuration = new TrustGroupRegistrationModel { Id = id }; // Moq Quickstart: https://github.com/Moq/moq4/wiki/Quickstart // Arrange _groups.Setup(x => x.GetGroupAsync(id, CancellationToken.None)).ReturnsAsync(configuration); // Act var result = _target.GetGroupAsync(id).Result; // Verify _groups.Verify(x => x.GetGroupAsync( It.Is <string>(s => s == id), CancellationToken.None), Times.Once); }
/// <summary> /// Renew group /// </summary> /// <param name="group"></param> /// <param name="notBefore"></param> /// <param name="ct"></param> /// <returns></returns> private async Task <Certificate> RenewGroupRootAsync(TrustGroupRegistrationModel group, DateTime notBefore, CancellationToken ct) { // create new CA cert return(await _issuer.NewRootCertificateAsync( group.Id, new X500DistinguishedName(group.Group.SubjectName), notBefore, group.Group.Lifetime, new CreateKeyParams { KeySize = group.Group.KeySize, Type = KeyType.RSA }, new IssuerPolicies { IssuedLifetime = group.Group.IssuedLifetime, SignatureType = group.Group.IssuedSignatureAlgorithm .ToSignatureType() }, null, ct)); }
/// <inheritdoc/> public async Task <TrustGroupRegistrationModel> AddAsync( TrustGroupRegistrationModel group, CancellationToken ct) { if (group == null) { throw new ArgumentNullException(nameof(group)); } while (true) { group.Id = "grp" + Guid.NewGuid(); try { var result = await _groups.AddAsync(group.ToDocumentModel(), ct); return(result.Value.ToServiceModel()); } catch (ConflictingResourceException) { continue; } } }
/// <summary> /// Create registration model /// </summary> /// <param name="model"></param> public TrustGroupRegistrationApiModel(TrustGroupRegistrationModel model) { Id = model.Id; Group = model.Group == null ? null : new TrustGroupApiModel(model.Group); }