serilog-contrib / serilog-sinks-elasticsearch

A Serilog sink that writes events to Elasticsearch
Apache License 2.0
434 stars 196 forks source link

Configure authentication like api key from appsettings.json #476

Open snebjorn opened 2 years ago

snebjorn commented 2 years ago

Does this issue relate to a new feature or an existing bug?

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package. serilog.sinks.elasticsearch v8.4.1

What is the target framework and operating system? See target frameworks & net standard matrix.

Please describe the current behavior?

Not possible to set api keys from appsettings.json. Today it has to be done from code

.UseSerilog(
    (context, services, configuration) =>
    {
        configuration.WriteTo.Elasticsearch(
            new ElasticsearchSinkOptions(new Uri("..."))
            {
                ModifyConnectionSettings = (x) =>
                    x.ApiKeyAuthentication(new ApiKeyAuthenticationCredentials("my api key"))
            }
        );
    }
)

Please describe the expected behavior?

It would be handy if we could configure authentication like api keys from appsettings.json

Something like

{
  "Serilog": {
    "Using": ["Serilog.Sinks.Elasticsearch"],
    "WriteTo": {
      "ElasticsearchSink": {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "...",
          "apiKey": "my api key"
        }
      }
    }
  }
}
ricardodemauro commented 1 year ago

Hi,

you can use like this

{
    "Name": "Elasticsearch",
    "Args": {
        "nodeUris": "https://.............../;",
        "connectionGlobalHeaders": "Authorization=Basic {XXX}",
    }
}

where {XXX} is {username}:{password} in base64 format

snebjorn commented 1 year ago

Thanks for the suggestion. However {username}:{password} is different from an API key.

TiemenSET commented 1 year ago

when is this going to be implemented?

mivano commented 1 year ago

When somebody submits a PR for this functionality. Alternatively, look at the sink Elastic itself offers if this is a better match. You can find a link in the readme.

eharshosh commented 9 months ago

Thanks for the suggestion. However {username}:{password} is different from an API key.

Didn't test it, but from looking at the official API key documentation, it looks like this might work:

        "connectionGlobalHeaders": "Authorization=ApiKey {XXX}",

Where XXX is the base64 encoded API key. Source: https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html