Table of Contents

Class SecureElasticRepositoryBase<TEntity>

Namespace
Excalibur.Data.ElasticSearch
Assembly
Excalibur.Data.ElasticSearch.dll

Security-enhanced base class for Elasticsearch repositories with comprehensive data protection, access controls, audit logging, and threat detection capabilities.

public abstract class SecureElasticRepositoryBase<TEntity> : ElasticRepositoryBase<TEntity>, IInitializeElasticIndex, IElasticRepositoryBase<TEntity>, IElasticRepositoryBaseQuery<TEntity> where TEntity : class

Type Parameters

TEntity

The type of entity managed by this repository.

Inheritance
SecureElasticRepositoryBase<TEntity>
Implements
Inherited Members

Remarks

Initializes a new instance of the SecureElasticRepositoryBase<TEntity> class.

Constructors

SecureElasticRepositoryBase(ElasticsearchClient, IElasticsearchFieldEncryptor, IElasticsearchSecurityAuditor, IElasticsearchSecurityMonitor, IOptions<ElasticsearchSecurityOptions>, ILogger<SecureElasticRepositoryBase<TEntity>>)

Security-enhanced base class for Elasticsearch repositories with comprehensive data protection, access controls, audit logging, and threat detection capabilities.

protected SecureElasticRepositoryBase(ElasticsearchClient client, IElasticsearchFieldEncryptor fieldEncryptor, IElasticsearchSecurityAuditor securityAuditor, IElasticsearchSecurityMonitor securityMonitor, IOptions<ElasticsearchSecurityOptions> securityOptions, ILogger<SecureElasticRepositoryBase<TEntity>> logger)

Parameters

client ElasticsearchClient

The Elasticsearch client for data operations.

fieldEncryptor IElasticsearchFieldEncryptor

The field encryption service for sensitive data protection.

securityAuditor IElasticsearchSecurityAuditor

The security auditing service for compliance and monitoring.

securityMonitor IElasticsearchSecurityMonitor

The security monitoring service for threat detection.

securityOptions IOptions<ElasticsearchSecurityOptions>

The security configuration options.

logger ILogger<SecureElasticRepositoryBase<TEntity>>

The logger for security and operational events.

Remarks

Initializes a new instance of the SecureElasticRepositoryBase<TEntity> class.

Exceptions

ArgumentNullException

Thrown when required dependencies are null.

Methods

GetCurrentSourceIpAddress()

Gets the current source IP address from the request context.

protected abstract string? GetCurrentSourceIpAddress()

Returns

string

The current source IP address, or null if not available.

Remarks

Implement this method to provide the source IP from your application's HTTP context (e.g., IHttpContextAccessor.HttpContext.Connection.RemoteIpAddress).

Exceptions

ArgumentNullException

Thrown when required dependencies are null.

GetCurrentUserId()

Gets the current user identifier from the security context.

protected abstract string? GetCurrentUserId()

Returns

string

The current user identifier, or null if not available.

Remarks

Implement this method to provide user identity from your application's authentication context (e.g., ClaimsPrincipal, IHttpContextAccessor, or a custom identity provider).

Exceptions

ArgumentNullException

Thrown when required dependencies are null.

IsIpInRange(string, string)

Checks whether an IP address falls within a configured range or matches it exactly.

protected virtual bool IsIpInRange(string ipAddress, string rangeOrIp)

Parameters

ipAddress string

The IP address to check.

rangeOrIp string

The CIDR range or literal address to match against.

Returns

bool

true if the address matches; otherwise, false.

Remarks

The default handles both forms used in the whitelist and blacklist: CIDR notation (10.0.0.0/24, 2001:db8::/32) and a single literal address, for IPv4 and IPv6 alike. Addresses are compared parsed rather than as text, so equivalent spellings of the same address match. Override only for a matching rule the framework cannot express — a correct override is harder than it looks, and this predicate decides whether a caller is blocked.

An entry that cannot be parsed does not match, and is logged as a warning: a malformed blacklist entry would otherwise silently fail to block, with nothing to show for it.

Only called when NetworkSecurity is enabled.

Exceptions

ArgumentNullException

Thrown when required dependencies are null.

SecureDeleteAsync(string, CancellationToken)

Securely deletes a document with comprehensive audit logging.

public Task<DeleteResponse> SecureDeleteAsync(string id, CancellationToken cancellationToken)

Parameters

id string

The document identifier to delete.

cancellationToken CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Task<DeleteResponse>

A task representing the asynchronous deletion operation with security enhancements applied.

Exceptions

SecurityException

Thrown when security validation fails or access is denied.

ArgumentException

SecureGetByIdAsync(string, CancellationToken)

Securely retrieves a document by ID with decryption and access logging.

[RequiresUnreferencedCode("Field-level encryption decrypts the stored document, which deserializes it reflectively over its members; trimming may remove them.")]
[RequiresDynamicCode("Field-level encryption decrypts the stored document, which deserializes it reflectively, generating serialization code for its type at run time.")]
public Task<TEntity?> SecureGetByIdAsync(string id, CancellationToken cancellationToken)

Parameters

id string

The document identifier to retrieve.

cancellationToken CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Task<TEntity>

The decrypted document if found and accessible, null otherwise.

Exceptions

SecurityException

Thrown when security validation fails or access is denied.

ArgumentException

SecureIndexAsync(TEntity, CancellationToken)

Securely indexes a document with field-level encryption and comprehensive audit logging.

[RequiresUnreferencedCode("Field-level encryption encrypts the document's fields, which serializes it reflectively over its members; trimming may remove them.")]
[RequiresDynamicCode("Field-level encryption encrypts the document's fields, which serializes it reflectively, generating serialization code for its type at run time.")]
public Task<IndexResponse> SecureIndexAsync(TEntity document, CancellationToken cancellationToken)

Parameters

document TEntity

The document to index with security processing.

cancellationToken CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Task<IndexResponse>

A task representing the asynchronous indexing operation with security enhancements applied.

Exceptions

SecurityException

Thrown when security validation fails or access is denied.

SecureSearchAsync(Action<SearchRequestDescriptor<TEntity>>, CancellationToken)

Performs a secure search operation with result decryption and access logging.

[RequiresUnreferencedCode("Field-level encryption decrypts each hit, which deserializes it reflectively over its members; trimming may remove them.")]
[RequiresDynamicCode("Field-level encryption decrypts each hit, which deserializes it reflectively, generating serialization code for its type at run time.")]
public Task<SearchResponse<TEntity>> SecureSearchAsync(Action<SearchRequestDescriptor<TEntity>> searchRequest, CancellationToken cancellationToken)

Parameters

searchRequest Action<SearchRequestDescriptor<TEntity>>

The search request configuration.

cancellationToken CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Task<SearchResponse<TEntity>>

The search results with decrypted documents if encryption is enabled.

Exceptions

SecurityException

Thrown when security validation fails or access is denied.

SecureUpdateAsync(string, TEntity, CancellationToken)

Securely updates a document with encryption and comprehensive audit logging.

[RequiresUnreferencedCode("Field-level encryption encrypts the document's fields, which serializes it reflectively over its members; trimming may remove them.")]
[RequiresDynamicCode("Field-level encryption encrypts the document's fields, which serializes it reflectively, generating serialization code for its type at run time.")]
public Task<UpdateResponse<TEntity>> SecureUpdateAsync(string id, TEntity document, CancellationToken cancellationToken)

Parameters

id string

The document identifier to update.

document TEntity

The updated document data.

cancellationToken CancellationToken

The cancellation token to monitor for cancellation requests.

Returns

Task<UpdateResponse<TEntity>>

A task representing the asynchronous update operation with security enhancements applied.

Exceptions

SecurityException

Thrown when security validation fails or access is denied.

ArgumentException