Document-level security or field-level security

Now that we know how to create a new user, create a new role, and assign roles to a user, let's explore how security can be imposed on documents and fields for a given index/document.

The sample data that we imported previously, at the beginning of this chapter, contained two indexes: employee and department. Let's use these indexes and understand the document-level security with two use cases.

Use case 1: When a user searches for employee details, the user should not be able to find the salary/address details contained in the documents belonging to the employee index.

This is where field-level security helps. Let's create a new role (employee_read) with read index privileges on the employee index. To restrict the fields, type the actual field names that are allowed to be accessed by the user in the Granted Fields section, as shown in the following screenshot, and click the Create role button:

When creating a role, you can specify the same set of privileges on multiple indexes by adding one or more index names to the Indices field, or you can specify different privileges for different indexes by clicking on the Add index privilege button that's found in the Index privileges section.

Assign the newly created role to user2:

Now, let's search in the employee index and check what fields were returned in the response. As we can see in the following response, we have successfully restricted the user from accessing salary and address details:

curl -u user2:password "http://localhost:9200/employee/_search?pretty"
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "employee",
"_type" : "_doc",
"_id" : "xsTc2GoBlyaBuhcfU42x",
"_score" : 1.0,
"_source" : {
"gender" : "M",
"state" : "NE",
"email" : "[email protected]"
}
},
{
"_index" : "employee",
"_type" : "_doc",
"_id" : "x8Tc2GoBlyaBuhcfU42x",
"_score" : 1.0,
"_source" : {
"gender" : "F",
"state" : "OR",
"email" : "[email protected]"
}
},
{
"_index" : "employee",
"_type" : "_doc",
"_id" : "yMTc2GoBlyaBuhcfU42x",
"_score" : 1.0,
"_source" : {
"gender" : "F",
"state" : "CA",
"email" : "[email protected]"
}
}
]
}
}

Use case 2: We want to have a multi-tenant index and restrict certain documents to certain users. For example, user1 should be able to search in the department index and retrieve only documents belonging to the IT department.

Let's create a role, department_IT_role, and provide the read privilege for the department index. To restrict the documents, specify the query in the Granted Documents Query section. The query should be in the Elasticsearch Query DSL format:

Associate the newly created role with user1:

Let's verify that it is working as expected by executing a search against the department index using the user1 credentials:

curl -u user1:password "http://localhost:9200/department/_search?pretty"
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "department",
"_type" : "department",
"_id" : "ycTc2GoBlyaBuhcfU42x",
"_score" : 1.0,
"_source" : {
"name" : "IT",
"employees" : 50
}
}
]
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.115.121