# Retrieve role info in Elasticsearch plugin

**URL:** <https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [December 10, 2018, 3:07pm UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199 "2018-12-10T15:07:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uiidoi12](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@Uiidoi12](https://discuss.elastic.co/u/Uiidoi12)\
**Post date:** [December 10, 2018, 3:07pm UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/1 "2018-12-10T15:07:10Z")

</div>

Hi,

I'm building a custom plugin and need to retrieve, at some point, all info about a specific role by its name.  
How can I do that?  
Do I need to use `org.elasticsearch.client.Client`, is so could you provide a sample please?

Thanks  
Regards

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [December 11, 2018, 5:40am UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/2 "2018-12-11T05:40:41Z")

</div>

Without more information about what type of plugin you have, and where you want to retrieve the roles, it's hard to provide a useful sample.

If you have a `Client`, then you can construct an instance of `org.elasticsearch.xpack.core.security.client.SecurityClient` and call `getRoles` from that.

---

<div class="post-metadata">

**Author:** ![Uiidoi12](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@Uiidoi12](https://discuss.elastic.co/u/Uiidoi12)\
**Post date:** [December 11, 2018, 8:31am UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/3 "2018-12-11T08:31:43Z")

</div>

Hi,

thank you for you answer.  
It's a plugin built upon the one from [this](https://www.elastic.co/blog/how-to-develop-your-own-security-extensions-and-custom-realms-for-elasticsearch) guide.  
The only Elasticsearch dependencies I have are:

```auto
<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>${elastic.version}</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.elasticsearch.plugin</groupId>
  <artifactId>x-pack-core</artifactId>
  <version>${elastic.version}</version>
  <scope>provided</scope>
</dependency>

```

and when I'm in the `buildRole()` method in `WebstoreRolesProvider` class I want to retrieve information from the `Set<String> names` of roles coming from the `lookup()` method.

I saw the `org.elasticsearch.xpack.core.security.client.SecurityClient`, but I'm not sure how to build an `ElasticsearchClient` implementation and pass it to its constructor.  
Do I need to add any specific dependency?

Thanks

**edit** :

I also saw that the future will be using `RestHighLevelClient` so is there any way to use it to retrieve roles info?

Thanks

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [December 12, 2018, 4:45am UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/4 "2018-12-12T04:45:29Z")

</div>

`Client` implements `ElasticsearchClient`, so it you have a `Client` then you can construct a `SecurityClient`.

You need to get the `Client` as part of the `Plugin.createComponents` method, and then retain a reference to that client for use in your `Realm`.

> in `WebstoreRolesProvider` class I want to retrieve information from the `Set<String> names` of roles coming from the `lookup()` method.

That doesn't mean sense to me. Your roles provider needs to create those roles - they will not exist in Elasticsearch. If they did exist in Elasticsearch then your roles provider would not be called.

---

<div class="post-metadata">

**Author:** ![Uiidoi12](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@Uiidoi12](https://discuss.elastic.co/u/Uiidoi12)\
**Post date:** [December 12, 2018, 11:18am UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/5 "2018-12-12T11:18:46Z")

</div>

thanks @TimV I'll try that!

> That doesn't mean sense to me. Your roles provider needs to create those roles - they will not exist in Elasticsearch. If they did exist in Elasticsearch then your roles provider would not be called.

oh I see. I'll review my logic then. Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 9, 2019, 11:18am UTC](https://discuss.elastic.co/t/retrieve-role-info-in-elasticsearch-plugin/160199/6 "2019-01-09T11:18:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
