serilog-sinks-elasticsearch icon indicating copy to clipboard operation
serilog-sinks-elasticsearch copied to clipboard

Configure authentication like api key from appsettings.json

Open snebjorn opened this issue 3 years ago • 5 comments
trafficstars

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

  • [ ] Bug
  • [x] New Feature

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.

  • [ ] netCore 2.0
  • [ ] netCore 1.0
  • [ ] 4.7
  • [ ] 4.6.x
  • [ ] 4.5.x

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"
        }
      }
    }
  }
}

snebjorn avatar Oct 10 '22 09:10 snebjorn

Hi,

you can use like this

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

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

ricardodemauro avatar Dec 20 '22 20:12 ricardodemauro

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

snebjorn avatar Dec 21 '22 01:12 snebjorn

when is this going to be implemented?

TiemenSET avatar May 24 '23 10:05 TiemenSET

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.

mivano avatar May 24 '23 21:05 mivano

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

eharshosh avatar Jan 23 '24 13:01 eharshosh