go-elasticsearch
go-elasticsearch copied to clipboard
Add token support for API calls
Previously, I opened #84 with a desire to support tokens in a similar manner manner to the other native authentication methods within elasticsearch. Although I wanted this as a feature before, I now consider this a bug. To my knowledge, all other authentication methods are implemented, so this would ensure consistency with the upstream product.
Hello, sorry for a long delay. We had a discussion about the token authentication internally, and at the moment it seems like it's not a good candidate for some higher-level exposure in the client API — there are various potential usability issues, like the tokens expiring, and so on.
I've added global HTTP request headers support in https://github.com/elastic/go-elasticsearch/commit/3f39d542adc013c69c7818092e6ac0d428e1b9a7, which should solve this problem in a rather unobtrusive way — and also allow setting other global custom headers, eg. Accept
.
I've tested the token authentication locally, by first creating the token:
curl -k -X POST "https://elastic:elastic@localhost:9200/_security/oauth2/token?pretty" -H 'Content-Type: application/json' -d '{ "grant_type" : "client_credentials" }'
Then, I've run a script like this:
hdr := http.Header{}
hdr.Set("Authorization", "Bearer 24ToAx...")
es, _ := elasticsearch.NewClient(
Logger: &estransport.ColorLogger{
Output: os.Stdout,
EnableRequestBody: true,
EnableResponseBody: true,
},
Header: hdr,
},
)
res, _ := es.Info()
log.Println(res)
Does this work for you?