diff --git a/EchoServiceApi/Program.cs b/EchoServiceApi/Program.cs index a1d3849..5a26878 100644 --- a/EchoServiceApi/Program.cs +++ b/EchoServiceApi/Program.cs @@ -62,10 +62,7 @@ logger.LogInformation("Environment: {environmentName}; DeveloperMode:{isDevelopment}", app.Environment.EnvironmentName, app.Environment.IsDevelopment()); app.UseHttpOverrides(); - -#pragma warning disable S3923 // All branches in a conditional structure should not have exactly the same implementation if (app.Environment.IsDevelopment()) -#pragma warning restore S3923 // All branches in a conditional structure should not have exactly the same implementation { // app.UseDeveloperExceptionPage() } diff --git a/EchoServiceApi/Verifiers/BaseVerifier.cs b/EchoServiceApi/Verifiers/BaseVerifier.cs index d4d2254..c7ce647 100644 --- a/EchoServiceApi/Verifiers/BaseVerifier.cs +++ b/EchoServiceApi/Verifiers/BaseVerifier.cs @@ -27,12 +27,14 @@ protected BaseVerifier(IServiceProvider serviceProvider) protected ProviderConnectionString GetConnection(string name) { if (string.IsNullOrEmpty(name)) + { throw new ArgumentNullException(nameof(name)); + } IConnectionStringManager connectionStringManager = new ConnectionStringManager(Configuration); var connectionObj = connectionStringManager[name] ?? throw new Exception($"Connection string '{name}' not found"); return connectionObj; } - protected IDisposable LoggerBeginScopeDiagnostic() => Logger.BeginScope(DiagnosticInfo.LoggingScopeState); + protected IDisposable LoggerBeginScopeDiagnostic() => Logger.BeginScope(DiagnosticInfo.LoggingScopeState) ?? throw new Exception("Logger.BeginScope null"); } diff --git a/EchoServiceApi/Verifiers/CosmosCacheVerifier.cs b/EchoServiceApi/Verifiers/CosmosCacheVerifier.cs index ff23ac7..f955c34 100644 --- a/EchoServiceApi/Verifiers/CosmosCacheVerifier.cs +++ b/EchoServiceApi/Verifiers/CosmosCacheVerifier.cs @@ -17,12 +17,12 @@ public CosmosCacheVerifier(IServiceProvider serviceProvider) : base(serviceProvi public async Task VerifyAsync(string name) { var connectionObj = GetConnection(name); - var cosmosCacheInfo = connectionObj.Get(); + var cosmosCacheInfo = connectionObj.Get() ?? throw new Exception("CosmosCacheInfo is required"); var databaseName = cosmosCacheInfo.DatabaseName; var containerName = cosmosCacheInfo.ContainerName; - using var cosmosclient = await CreateClientAsync(connectionObj, cosmosCacheInfo); + using var cosmosClient = await CreateClientAsync(connectionObj, cosmosCacheInfo); using var scope = LoggerBeginScopeDiagnostic(); @@ -31,7 +31,7 @@ public async Task VerifyAsync(string name) var cosmosCacheOptions = new CosmosCacheOptions { - CosmosClient = cosmosclient, + CosmosClient = cosmosClient, ContainerName = containerName, DatabaseName = databaseName, CreateIfNotExists = false, @@ -39,6 +39,6 @@ public async Task VerifyAsync(string name) var cache = new CosmosCache(Options.Create(cosmosCacheOptions)); _ = await cache.GetStringAsync("CosmosCacheVerifier"); - return VerifyResult.Successed("CosmosCache", connectionObj); + return VerifyResult.Succeed("CosmosCache", connectionObj); } } diff --git a/EchoServiceApi/Verifiers/CosmosVerifier.cs b/EchoServiceApi/Verifiers/CosmosVerifier.cs index 14a0a6d..851e562 100644 --- a/EchoServiceApi/Verifiers/CosmosVerifier.cs +++ b/EchoServiceApi/Verifiers/CosmosVerifier.cs @@ -1,7 +1,6 @@ using Microsoft.Azure.Cosmos; using NetLah.Extensions.Configuration; -#pragma warning disable S4457 // Parameter validation in "async"/"await" methods should be wrapped namespace EchoServiceApi.Verifiers; public class CosmosVerifier : BaseCosmosVerifier @@ -11,26 +10,27 @@ public CosmosVerifier(IServiceProvider serviceProvider) : base(serviceProvider) public async Task VerifyAsync(string name, string key) { if (string.IsNullOrEmpty(key)) + { throw new ArgumentNullException(nameof(key)); + } var connectionObj = GetConnection(name); - var cosmosInfo = connectionObj.Get(); + var cosmosInfo = connectionObj.Get() ?? throw new Exception("CosmosContainerInfo is required"); var containerName = cosmosInfo.ContainerName; var databaseName = cosmosInfo.DatabaseName; - using var cosmosclient = await CreateClientAsync(connectionObj, cosmosInfo); + using var cosmosClient = (await CreateClientAsync(connectionObj, cosmosInfo)) ?? throw new Exception("CosmosContainerInfo is required"); using var scope = LoggerBeginScopeDiagnostic(); Logger.LogInformation("CosmosVerifier db:{databaseName} container:{containerName} name={query_name} key={query_key}", databaseName, containerName, name, key); - var container = cosmosclient.GetContainer(databaseName, containerName); + var container = cosmosClient.GetContainer(databaseName, containerName); _ = await container.ReadContainerAsync().ConfigureAwait(false); var itemResponse = await container.ReadItemAsync>(key, new PartitionKey(key)); return VerifyResult.SuccessObject("Cosmos", connectionObj, itemResponse.Resource); } -} -#pragma warning restore S4457 // Parameter validation in "async"/"await" methods should be wrapped +} \ No newline at end of file diff --git a/EchoServiceApi/Verifiers/DirVerifier.cs b/EchoServiceApi/Verifiers/DirVerifier.cs index 5f33747..89d03df 100644 --- a/EchoServiceApi/Verifiers/DirVerifier.cs +++ b/EchoServiceApi/Verifiers/DirVerifier.cs @@ -7,7 +7,9 @@ public DirVerifier(IServiceProvider serviceProvider) : base(serviceProvider) { } public Task VerifyAsync(string path) { if (string.IsNullOrEmpty(path)) + { throw new ArgumentException("Path is required"); + } var isDir = false; var isFile = File.Exists(path); diff --git a/EchoServiceApi/Verifiers/HttpVerifier.cs b/EchoServiceApi/Verifiers/HttpVerifier.cs index dea8dd8..46c845e 100644 --- a/EchoServiceApi/Verifiers/HttpVerifier.cs +++ b/EchoServiceApi/Verifiers/HttpVerifier.cs @@ -13,7 +13,9 @@ public HttpVerifier(HttpClient httpClient, IServiceProvider serviceProvider) public async Task VerifyAsync(Uri url, string? host) { if (url == null) + { throw new ArgumentException("Url is required"); + } if (!string.IsNullOrEmpty(host)) { diff --git a/EchoServiceApi/Verifiers/KeyVaultCertificateVerifier.cs b/EchoServiceApi/Verifiers/KeyVaultCertificateVerifier.cs index 45d15ea..c1c0891 100644 --- a/EchoServiceApi/Verifiers/KeyVaultCertificateVerifier.cs +++ b/EchoServiceApi/Verifiers/KeyVaultCertificateVerifier.cs @@ -33,8 +33,11 @@ public async Task VerifyAsync(string name, bool privateKey, bool a var certificateName = locationParts[2]; var versions = new List(); var version = locationParts.Length >= 4 ? locationParts[3] : null; - if (string.IsNullOrEmpty(version)) version = null; - + if (string.IsNullOrEmpty(version)) + { + version = null; + } + versions.Add(version); if (privateKey) diff --git a/EchoServiceApi/Verifiers/KeyVaultKeyVerifier.cs b/EchoServiceApi/Verifiers/KeyVaultKeyVerifier.cs index a4ca887..a468232 100644 --- a/EchoServiceApi/Verifiers/KeyVaultKeyVerifier.cs +++ b/EchoServiceApi/Verifiers/KeyVaultKeyVerifier.cs @@ -28,7 +28,9 @@ public async Task VerifyAsync(string name) var keyName = locationParts[2]; var version = locationParts.Length >= 4 ? locationParts[3] : null; if (string.IsNullOrEmpty(version)) + { version = null; + } var keyClient = new KeyClient(keyIdentifier, tokenCredential); var response = await keyClient.GetKeyAsync(keyName, version); @@ -51,7 +53,7 @@ public async Task VerifyAsync(string name) var detail = $"KeyType={keyVaultKey.KeyType}; Text={text}; Signature={signed}; Ciphertext={ciphertext}; Plaintext={decryptText}; ValidSignature={validSignature}"; - return VerifyResult.Successed("KeyVaultKey", connectionObj, detail: detail); + return VerifyResult.Succeed("KeyVaultKey", connectionObj, detail: detail); } return new VerifyResult diff --git a/EchoServiceApi/Verifiers/PosgreSqlVerifier.cs b/EchoServiceApi/Verifiers/PosgreSqlVerifier.cs index 0051bb9..f8a40eb 100644 --- a/EchoServiceApi/Verifiers/PosgreSqlVerifier.cs +++ b/EchoServiceApi/Verifiers/PosgreSqlVerifier.cs @@ -1,5 +1,4 @@ -#pragma warning disable S4457 // Parameter validation in "async"/"await" methods should be wrapped -namespace EchoServiceApi.Verifiers; +namespace EchoServiceApi.Verifiers; public class PosgreSqlVerifier : BaseVerifier { @@ -8,7 +7,9 @@ public PosgreSqlVerifier(IServiceProvider serviceProvider) : base(serviceProvide public async Task VerifyAsync(string name, string tableName) { if (string.IsNullOrEmpty(tableName)) + { throw new ArgumentNullException(nameof(tableName)); + } var connectionObj = GetConnection(name); var connectionString = connectionObj.Value; @@ -22,7 +23,6 @@ public async Task VerifyAsync(string name, string tableName) command.CommandType = System.Data.CommandType.Text; command.ExecuteNonQuery(); - return VerifyResult.Successed("PosgreSql", connectionObj, detail: query); + return VerifyResult.Succeed("PosgreSql", connectionObj, detail: query); } -} -#pragma warning restore S4457 // Parameter validation in "async"/"await" methods should be wrapped +} \ No newline at end of file diff --git a/EchoServiceApi/Verifiers/ProviderConnectionStringExtensions.cs b/EchoServiceApi/Verifiers/ProviderConnectionStringExtensions.cs index cbd0740..621392f 100644 --- a/EchoServiceApi/Verifiers/ProviderConnectionStringExtensions.cs +++ b/EchoServiceApi/Verifiers/ProviderConnectionStringExtensions.cs @@ -37,12 +37,12 @@ public static TConnectionCredentialInfo TryGet(this P return new TConnectionCredentialInfo { Value = connectionString.Value }; } - return connectionString.Get(); + return connectionString.Get() ?? throw new Exception("ConnectionCredentialValue is required"); } public static ConnectionCredentialBag Load(this ProviderConnectionString connectionString) { - var result = connectionString.Get>(); + var result = connectionString.Get>() ?? throw new Exception("ConnectionCredentialBag is required"); result.Value = connectionString.Get(); return result; } diff --git a/EchoServiceApi/Verifiers/ServiceBusVerifier.cs b/EchoServiceApi/Verifiers/ServiceBusVerifier.cs index affbd05..21222ba 100644 --- a/EchoServiceApi/Verifiers/ServiceBusVerifier.cs +++ b/EchoServiceApi/Verifiers/ServiceBusVerifier.cs @@ -25,7 +25,7 @@ public async Task VerifyAsync(string name, bool send, bool receive { if (string.IsNullOrEmpty(queueName)) { - queueName = connectionObj.Get().QueueName ?? throw new Exception("QueueName is required"); + queueName = connectionObj.Get()?.QueueName ?? throw new Exception("QueueName is required"); } client = new ServiceBusClient(connectionString: connectionObj.Value); } @@ -57,7 +57,7 @@ public async Task VerifyAsync(string name, bool send, bool receive await receiver.CompleteMessageAsync(message); } var detail1 = $"Received={isExist}; queueName={queueName}; fqns={receiver.FullyQualifiedNamespace}; messageId={message?.MessageId}; messageBody={message?.Body.ToString()}"; - return VerifyResult.Successed("ServiceBus", connectionObj, detail1); + return VerifyResult.Succeed("ServiceBus", connectionObj, detail1); } else if (send) { @@ -65,21 +65,25 @@ public async Task VerifyAsync(string name, bool send, bool receive await sender.SendMessageAsync(new ServiceBusMessage($"{queueName}-{DateTimeOffset.Now}")); var detail1 = $"Status=Sent; queueName={queueName}; fqns={sender.FullyQualifiedNamespace};"; - return VerifyResult.Successed("ServiceBus", connectionObj, detail1); + return VerifyResult.Succeed("ServiceBus", connectionObj, detail1); } sender = client.CreateSender(queueName); receiver = client.CreateReceiver(queueName); var detail = $"queueName={queueName}; fqns={sender.FullyQualifiedNamespace};"; - return VerifyResult.Successed("ServiceBus", connectionObj, detail); + return VerifyResult.Succeed("ServiceBus", connectionObj, detail); } finally { if (receiver != null) + { await receiver.DisposeAsync(); + } if (sender != null) + { await sender.DisposeAsync(); + } await client.DisposeAsync(); diff --git a/EchoServiceApi/Verifiers/TokenCredentialFactory.cs b/EchoServiceApi/Verifiers/TokenCredentialFactory.cs index 7aef825..d78501d 100644 --- a/EchoServiceApi/Verifiers/TokenCredentialFactory.cs +++ b/EchoServiceApi/Verifiers/TokenCredentialFactory.cs @@ -38,15 +38,12 @@ private async Task GetDefaultTokenCredentialAsync() return result; } - public async Task GetTokenCredentialAsync(AzureCredentialInfo? options) => -#pragma warning disable S3358 // Ternary operators should not be nested - options != null && options.ClientId is { } clientId - ? options.TenantId is { } tenantId + public async Task GetTokenCredentialAsync(AzureCredentialInfo? options) => options != null && options.ClientId is { } clientId + ? options.TenantId is { } tenantId && options.ClientSecret is { } clientSecret ? await GetClientSecretCredentialAsync(tenantId, clientId, clientSecret) : await GetManagedIdentityClientIdAsync(clientId) : null; -#pragma warning restore S3358 // Ternary operators should not be nested public string? Redact(string? secret) { diff --git a/EchoServiceApi/Verifiers/VerifyResult.cs b/EchoServiceApi/Verifiers/VerifyResult.cs index 34e8273..29c8d1b 100644 --- a/EchoServiceApi/Verifiers/VerifyResult.cs +++ b/EchoServiceApi/Verifiers/VerifyResult.cs @@ -18,7 +18,7 @@ public static VerifyResult Failed(Exception ex) { Success = false, Error = $"{ex.GetType().FullName}: {cosmosException.Message}", - Disagnostics = cosmosException.Diagnostics?.ToString(), + Diagnostics = cosmosException.Diagnostics?.ToString(), StackTrace = cosmosException.StackTrace, DetailError = cosmosException.ToString(), } @@ -31,7 +31,7 @@ public static VerifyResult Failed(Exception ex) }; } - public static VerifyResult Successed(string serviceName, ProviderConnectionString connectionObj, string? detail = null) + public static VerifyResult Succeed(string serviceName, ProviderConnectionString connectionObj, string? detail = null) { return new VerifySuccessMessage { @@ -57,7 +57,7 @@ public class VerifyFailed : VerifyResult public string? Error { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public object? Disagnostics { get; set; } + public object? Diagnostics { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? StackTrace { get; internal set; }