How do I use Core WCF in Kubernetes?

172 Views Asked by At

I can use my core wcf project on my local(windows 11) and I can test it with using SOapUi. However when I try core wcf on Kubernetes I encounter a lot of errors.


│ warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]                                                │

│       Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protect │

│ warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]                                                         │

│       No XML encryptor configured. Key {7358a31c-fdd8-4661-a7fb-d75731bb7627} may be persisted to storage in unencrypted form.    │

│ Unhandled exception. System.InvalidOperationException: Could not find a base address that matches scheme https for the endpoint w │

│    at CoreWCF.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollection baseAddresses) │

│    at CoreWCF.Description.DispatcherBuilder.BuildDispatcher[TService](ServiceConfiguration`1 serviceConfig, IServiceProvider serv │

│    at CoreWCF.Configuration.ServiceConfiguration`1.GetDispatchers()                                                               │

│    at CoreWCF.Configuration.DispatcherBuilderImpl.BuildDispatchers(Type serviceType)                                              │

│    at CoreWCF.Channels.ServiceModelHttpMiddleware.BuildBranch()                                                                   │

│    at CoreWCF.Channels.ServiceModelHttpMiddleware.EnsureBranchBuilt()                                                             │

│    at CoreWCF.Channels.ServiceModelHttpMiddleware.ServiceBuilderOpenedCallback(Object sender, EventArgs e)                        │

│    at CoreWCF.Channels.CommunicationObject.OnOpened()                                                                             │

│ --- End of stack trace from previous location ---                                                                                 │

│    at CoreWCF.Configuration.WrappingIServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationTo │

│    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)                          │

│    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)                                  │

│    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)                │

│    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)                │

│    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)                                              │

│    at Microsoft.AspNetCore.Builder.WebApplication.Run(String url)                                                                 │

│    at Program.<Main>$(String[] args) in /src/eGovernment.Wcf/Program.cs:line 17

The best article about data protection is https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1 I researched about thing but I could not find anything for kubernetes.

We tried off the key generation like that builder.Services.AddDataProtection().DisableAutomaticKeyGeneration(); although we encounter problems.

1

There are 1 best solutions below

2
Anton Tykhyy On

You can implement a Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository that stores keys in an appropriately scoped Kubernetes object (a secret or a configmap). Correct scoping (pod, replicaset, deployment etc.) depends on how your application is set up and whether data protection keys should be shared across multiple pods. You will have to name the object appropriately, and give the service account your code is running under appropriate permissions to change the object(s). It may be possible to label this object as a dependent of the pod/replicaset/etc. so that Kubernetes automatically cleans it up when the pod/replicaset/etc. is deleted. Use FileSystemXmlRepository from ASP.NET Core source as a base for your implementation. You will need methods to store and retrieve data from Kubernetes. Storing an XML element is a bit tricky because you need to ensure you're not writing stale data to Kubernetes. Supply the object's resource version with the ReplaceXxx operation to make it conditional. If the conditional fails, you have to fetch back fresh data, re-add the XML element being stored, and retry. IXmlRepository methods for storing and retrieving data are unfortunately not async, so you will have to wrap code using Kubernetes API calls in Task.Run(...).GetAwaiter().GetResult().