From 8a306b5c1ccc9220f788ed055ba4859381412fbd Mon Sep 17 00:00:00 2001 From: vman Date: Wed, 30 Nov 2022 14:59:42 -0500 Subject: Project cleanup + analyzer updates --- .../ClientCacheConfiguration.cs | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 VNLib.Data.Caching.Extensions/ClientCacheConfiguration.cs (limited to 'VNLib.Data.Caching.Extensions/ClientCacheConfiguration.cs') diff --git a/VNLib.Data.Caching.Extensions/ClientCacheConfiguration.cs b/VNLib.Data.Caching.Extensions/ClientCacheConfiguration.cs new file mode 100644 index 0000000..96f54a7 --- /dev/null +++ b/VNLib.Data.Caching.Extensions/ClientCacheConfiguration.cs @@ -0,0 +1,122 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: ClientCacheConfiguration.cs +* +* ClientCacheConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.Data.Caching.Extensions is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Data.Caching.Extensions. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Security.Cryptography; + +using VNLib.Hashing; +using VNLib.Net.Messaging.FBM.Client; + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// A fluent api configuration object for configuring a + /// to connect to cache servers. + /// + public sealed class ClientCacheConfiguration + { + internal ECDsa SigningKey { get; init; } + internal ECDsa VerificationKey { get; init; } + internal string ServerChallenge { get; init; } + internal string? NodeId { get; set; } + internal Uri? BrokerAddress { get; set; } + internal bool UseTls { get; set; } + internal ActiveServer[]? CacheServers { get; set; } + + public ClientCacheConfiguration() + { + //Init the algorithms + SigningKey = ECDsa.Create(FBMDataCacheExtensions.CacheCurve); + VerificationKey = ECDsa.Create(FBMDataCacheExtensions.CacheCurve); + ServerChallenge = RandomHash.GetRandomBase32(24); + } + + /// + /// Imports the private key used to sign messages + /// + /// The pkcs8 encoded private key to sign messages + /// Chainable fluent object + /// + /// + public ClientCacheConfiguration ImportSigningKey(ReadOnlySpan pkcs8PrivKey) + { + SigningKey.ImportPkcs8PrivateKey(pkcs8PrivKey, out _); + return this; + } + + /// + /// Imports the public key used to verify messages from the remote server + /// + /// The subject-public-key-info formatted cache public key + /// Chainable fluent object + /// + /// + public ClientCacheConfiguration ImportVerificationKey(ReadOnlySpan spkiPublicKey) + { + VerificationKey.ImportSubjectPublicKeyInfo(spkiPublicKey, out _); + return this; + } + + /// + /// Specifies if all connections should be using TLS + /// + /// A value that indicates if connections should use TLS + /// Chainable fluent object + public ClientCacheConfiguration WithTls(bool useTls) + { + UseTls = useTls; + return this; + } + /// + /// Specifies the broker address to discover cache nodes from + /// + /// The address of the server broker + /// Chainable fluent object + /// + public ClientCacheConfiguration WithBroker(Uri brokerAddress) + { + this.BrokerAddress = brokerAddress ?? throw new ArgumentNullException(nameof(brokerAddress)); + return this; + } + + /// + /// Specifies the current server's cluster node id. If this + /// is a server connection attempting to listen for changes on the + /// remote server, this id must be set and unique + /// + /// The cluster node id of the current server + /// Chainable fluent object + /// + public ClientCacheConfiguration WithNodeId(string nodeId) + { + this.NodeId = nodeId ?? throw new ArgumentNullException(nameof(nodeId)); + return this; + } + + ~ClientCacheConfiguration() + { + SigningKey.Clear(); + VerificationKey.Clear(); + } + } +} -- cgit