public async Task <NodeRegistrationModel?> Get(IWorkContext context)
        {
            if (_cache.TryGetValue(out NodeRegistrationModel model))
            {
                return(model);
            }

            NodeRegistrationModel?subject = await _registerStore.Get(context, ActorKey.VectorKey);

            if (subject == null)
            {
                return(subject);
            }

            _cache.Set(subject);
            return(subject);
        }
Пример #2
0
        public async Task <ArticlePayload?> Get(CancellationToken token)
        {
            if (_cache.TryGetValue(out ArticlePayload? value))
            {
                return(value);
            }

            _logger.LogTrace($"{nameof(Get)}: actorKey={base.ActorKey}");
            ArticlePayload?articlePayload = await _acticleStore.Get((ArticleId)base.ActorKey.Value, token : token);

            if (articlePayload == null)
            {
                return(null);
            }

            _cache.Set(articlePayload);
            return(articlePayload);
        }
Пример #3
0
        public async Task <ArtifactPayload?> Get(CancellationToken token)
        {
            if (_cache.TryGetValue(out ArtifactPayload? value))
            {
                return(value);
            }

            _logger.LogTrace($"{nameof(Get)}: actorKey={ActorKey}");
            ArtifactPayload?articlePayload = await _storage.Get(_artifactId, token : token);

            if (articlePayload == null)
            {
                return(null);
            }

            _cache.Set(articlePayload);
            return(articlePayload);
        }
Пример #4
0
        /// <summary>
        /// Find certificate by thumbprint.  Certificates that have expired will not
        /// be returned and if "throwOnNotFound" is specified, an exception will be
        /// thrown.
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="context">work context</param>
        /// <param name="throwOnNotFound">if true, throw exception if not found</param>
        /// <exception cref="ProgramExitException">Certificate is not found</exception>
        /// <returns>X509 certificate</returns>
        /// <exception cref="CertificateNotFoundException">when certificate valid certificate was not found</exception>
        public X509Certificate2 GetCertificate(IWorkContext context, bool?throwOnNotFound = null)
        {
            context = context.With(_tag);
            X509Certificate2 certificate;

            Exception?saveException = null;

            throwOnNotFound = throwOnNotFound ?? LocalCertificateKey.RequirePrivateKey;

            lock (_lock)
            {
                if (_cachedCertificate.TryGetValue(out certificate))
                {
                    return(certificate);
                }

                using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation))
                {
                    context.Telemetry.Verbose(context, $"Looking for certificate for {this}");

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);
                        X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false);

                        if (certificateList?.Count != 0)
                        {
                            _cachedCertificate.Set(
                                certificateList
                                .OfType <X509Certificate2>()
                                .Where(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey)
                                .Where(x => DateTime.Now <= x.NotAfter)
                                .FirstOrDefault()
                                );
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Telemetry.Warning(context, $"Exception: {ex}");
                        _cachedCertificate.Clear();
                        saveException = ex;
                    }
                }

                context.Telemetry.Verbose(context, $"{(_cachedCertificate != null ? "Found" : "Not found")} certificate for {this}");

                if (!_cachedCertificate !.TryGetValue(out certificate) && throwOnNotFound == true)
                {
                    throw new CertificateNotFoundException($"Certificate not found: {LocalCertificateKey.ToString()}");
                }

                return(certificate);
            }
        }
Пример #5
0
        public async Task Set(MetadataRecord record, CancellationToken token)
        {
            record
            .VerifyNotNull(nameof(record))
            .VerifyAssert(x => x.Id == base.ActorKey.Value, "Id mismatch");

            _logger.LogTrace($"{nameof(Set)}: Writing {record}");
            await _container.Set(record, token);

            _recordCache.Set(record);
        }
Пример #6
0
        public async Task <QueueDefinition?> Get(IWorkContext context)
        {
            if (_cache.TryGetValue(out QueueDefinition model))
            {
                return(model);
            }

            bool exist = await _queueManagement.QueueExists(context, ActorKey.VectorKey);

            if (!exist)
            {
                return(null);
            }

            QueueDefinition subject = await _queueManagement.GetQueue(context, ActorKey.VectorKey);

            _cache.Set(subject);

            return(subject);
        }
Пример #7
0
        public async Task <ArticleDirectory?> Get(CancellationToken token)
        {
            if (_cache.TryGetValue(out ArticleDirectory? value))
            {
                return(value);
            }

            _logger.LogTrace($"{nameof(Get)}: actorKey={ActorKey}");
            ArticleDirectory?acticleDirectory = await _directoryStore.Get(token);

            if (acticleDirectory == null)
            {
                return(null);
            }

            _logger.LogInformation($"{nameof(DirectoryActor)} *** actorKey={base.ActorKey}, articleDirectory={acticleDirectory}");

            _cache.Set(acticleDirectory);
            return(acticleDirectory);
        }
Пример #8
0
        /// <summary>
        /// Find certificate by thumbprint.  Certificates that have expired will not
        /// be returned and if "throwOnNotFound" is specified, an exception will be
        /// thrown.
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="context">work context</param>
        /// <param name="throwOnNotFound">if true, throw exception if not found</param>
        /// <exception cref="ProgramExitException">Certificate is not found</exception>
        /// <returns>X509 certificate</returns>
        /// <exception cref="CertificateNotFoundException">when certificate valid certificate was not found</exception>
        public X509Certificate2?GetCertificate(bool?throwOnNotFound = null)
        {
            throwOnNotFound ??= LocalCertificateKey.RequirePrivateKey;

            lock (_lock)
            {
                if (_cachedCertificate.TryGetValue(out X509Certificate2? certificate))
                {
                    return(certificate);
                }

                using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation))
                {
                    _logger.LogTrace($"Looking for certificate for {this}");

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);
                        X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false);

                        if (certificateList?.Count != 0)
                        {
                            X509Certificate2?cert = certificateList !
                                                    .OfType <X509Certificate2>()
                                                    .Where(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey)
                                                    .Where(x => DateTime.Now <= x.NotAfter)
                                                    .FirstOrDefault();

                            if (cert == null)
                            {
                                _logger.LogTrace($"Certificate Not found for {this}");
                                if (throwOnNotFound == true)
                                {
                                    throw new CertificateNotFoundException($"Certificate not found: {LocalCertificateKey.ToString()}");
                                }
                                return(null);
                            }

                            _cachedCertificate.Set(cert);
                            return(cert);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, $"Exception: {ex}");
                        _cachedCertificate.Clear();
                    }
                }

                _cachedCertificate.Clear();
                return(null);
            }
        }
Пример #9
0
        /// <summary>
        /// find certificate by thumbprint
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="context">work context</param>
        /// <param name="throwOnNotFound">if true, throw exception if not found</param>
        /// <exception cref="ProgramExitException">Certificate is not found</exception>
        /// <returns>X509 certificate</returns>
        public X509Certificate2 GetCertificate(IWorkContext context, bool?throwOnNotFound = null)
        {
            context = context.WithTag(_tag);
            Exception        saveException = null;
            X509Certificate2 certificate   = null;

            throwOnNotFound = throwOnNotFound ?? LocalCertificateKey.RequirePrivateKey;

            lock (_lock)
            {
                if (_cachedCertificate.TryGetValue(out certificate))
                {
                    return(certificate);
                }

                using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation))
                {
                    ToolboxEventSource.Log.Verbose(context, $"Looking for certificate for {this}");

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);
                        X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false);

                        if (certificateList?.Count != 0)
                        {
                            certificate = certificateList
                                          .OfType <X509Certificate2>()
                                          .FirstOrDefault(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey);

                            _cachedCertificate.Set(certificate);
                        }
                    }
                    catch (Exception ex)
                    {
                        ToolboxEventSource.Log.Warning(context, $"Exception: {ex}");
                        _cachedCertificate.Clear();
                        saveException = ex;
                    }
                }

                ToolboxEventSource.Log.Verbose(context, $"{(_cachedCertificate != null ? "Found" : "Not found")} certificate for {this}");
                if (certificate == null && throwOnNotFound == true)
                {
                    throw new ArgumentException($"Cannot find certificate for {this}", saveException);
                }

                return(certificate);
            }
        }
Пример #10
0
        public async Task <HeaderDoc <UserRoleDoc> > Get(IWorkContext context)
        {
            if (_cache.TryGetValue(out HeaderDoc <UserRoleDoc> value))
            {
                return(value);
            }

            HeaderDoc <UserRoleDoc> result = await _roleRepository.Get(context, ActorKey.VectorKey);

            if (result == null)
            {
                return(null);
            }

            _cache.Set(result);
            return(result);
        }
Пример #11
0
        private async Task <ArticleDirectory?> GetDirectory()
        {
            ArticleDirectory?subject;

            if (_cache.TryGetValue(out subject))
            {
                return(subject);
            }

            _logger.LogTrace($"{nameof(GetDirectory)} - fetching directory");

            subject = await _directoryClient.Get();

            _cache.Set(subject);

            return(subject);
        }