# Indexing data with document-level security - is it necessary to use 'run\_as'?

**URL:** https://discuss.elastic.co/t/indexing-data-with-document-level-security-is-it-necessary-to-use-run-as/69350
**Category:** Elasticsearch
**Created:** [December 17, 2016, 9:40pm UTC](https://discuss.elastic.co/t/indexing-data-with-document-level-security-is-it-necessary-to-use-run-as/69350 "2016-12-17T21:40:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![baden0x1](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@baden0x1](https://discuss.elastic.co/u/baden0x1)
#### Post date: [December 17, 2016, 9:40pm UTC](https://discuss.elastic.co/t/indexing-data-with-document-level-security-is-it-necessary-to-use-run-as/69350/1 "2016-12-17T21:40:08Z")

</div>

In reading through the X-Pack security-related documentation, I see that you can impersonate users while executing queries.

First what's best in setting up document-level security? Just a \_user.metadata field (hash holding custom metadata of the current authenticated user)? Can I read this hash from the user's index later, to add it to PUT requests?

In my case, a cron job will eventually be writing data to an index. While inserting data for a specific user, does this have to use the 'run\_as' mechanism or is it sufficient to populate the document's \_user.metadata field with this, to uniquely identify the user?

---

<div class="post-metadata">

### Author: ![jaymode](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaymode/32/50103_2.png) [@jaymode](https://discuss.elastic.co/u/jaymode)
#### Post date: [January 3, 2017, 3:19pm UTC](https://discuss.elastic.co/t/indexing-data-with-document-level-security-is-it-necessary-to-use-run-as/69350/2 "2017-01-03T15:19:38Z")

</div>

Hi @baden0x1,

First, I would like to apologize for your question being unanswered for so long.

In you use case, you may only need to add a field to the documents you are indexing that contains the username of the user or some other unique identifier. Once you have that, you can use a [templated role query](https://www.elastic.co/guide/en/x-pack/current/field-and-document-access-control.html#templating-role-query) for your users and limit their access to the documents that match their information.

> While inserting data for a specific user, does this have to use the 'run\_as' mechanism

The `run_as` mechanism does not need to be used. Populating the document with a field is all that is needed.

---

<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 31, 2017, 3:19pm UTC](https://discuss.elastic.co/t/indexing-data-with-document-level-security-is-it-necessary-to-use-run-as/69350/3 "2017-01-31T15:19:38Z")

</div>

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