2021-05-28 13:48:19 -04:00
---
layout: default
2021-09-21 15:34:59 -04:00
title: Cross-cluster search
parent: Access control
2023-01-27 13:12:47 -05:00
nav_order: 105
2023-05-08 11:07:01 -04:00
redirect_from:
- /security/access-control/cross-cluster-search/
2023-05-26 17:48:31 -04:00
- /security-plugin/access-control/cross-cluster-search/
2021-05-28 13:48:19 -04:00
---
# Cross-cluster search
2023-05-04 11:11:54 -04:00
Cross-cluster search is exactly what it sounds like: it lets any node in a cluster execute search requests against other clusters. The Security plugin supports cross-cluster search out of the box.
2021-05-28 13:48:19 -04:00
---
#### Table of contents
1. TOC
{:toc}
---
## Authentication flow
When accessing a *remote cluster* from a *coordinating cluster* using cross-cluster search:
2023-05-04 11:11:54 -04:00
1. The Security plugin authenticates the user on the coordinating cluster.
1. The Security plugin fetches the user's backend roles on the coordinating cluster.
2021-05-28 13:48:19 -04:00
1. The call, including the authenticated user, is forwarded to the remote cluster.
1. The user's permissions are evaluated on the remote cluster.
You can have different authentication and authorization configurations on the remote and coordinating cluster, but we recommend using the same settings on both.
## Permissions
2022-09-01 11:48:42 -04:00
To query indexes on remote clusters, users need to have `READ` or `SEARCH` permissions. Furthermore, when the search request includes the query parameter `ccs_minimize_roundtrips=false` – which tells OpenSearch not to minimize outgoing and ingoing requests to remote clusters – users need to have the following additional permission for the index:
2021-05-28 13:48:19 -04:00
```
indices:admin/shards/search_shards
```
2022-10-27 12:50:39 -04:00
For more information about the `ccs_minimize_roundtrips` parameter, see the list of [URL Parameters ]({{site.url}}{{site.baseurl}}/api-reference/search/#url-parameters ) for the Search API.
2021-05-28 13:48:19 -04:00
#### Sample roles.yml configuration
```yml
humanresources:
cluster:
- CLUSTER_COMPOSITE_OPS_RO
indices:
'humanresources':
'*':
- READ
2022-09-01 11:48:42 -04:00
- indices:admin/shards/search_shards # needed when the search request includes parameter setting 'ccs_minimize_roundtrips=false'.
2021-05-28 13:48:19 -04:00
```
#### Sample role in OpenSearch Dashboards
2021-06-09 22:15:41 -04:00
![OpenSearch Dashboards UI for creating a cross-cluster search role ]({{site.url}}{{site.baseurl}}/images/security-ccs.png )
2021-05-28 13:48:19 -04:00
## Walkthrough
Save this file as `docker-compose.yml` and run `docker-compose up` to start two single-node clusters on the same network:
```yml
version: '3'
services:
2021-09-24 16:49:48 -04:00
opensearch-ccs-node1:
2021-05-28 13:48:19 -04:00
image: opensearchproject/opensearch:{{site.opensearch_version}}
2021-09-24 16:49:48 -04:00
container_name: opensearch-ccs-node1
2021-05-28 13:48:19 -04:00
environment:
2021-09-24 16:49:48 -04:00
- cluster.name=opensearch-ccs-cluster1
2021-05-28 13:48:19 -04:00
- discovery.type=single-node
- bootstrap.memory_lock=true # along with the memlock settings below, disables swapping
- "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" # minimum and maximum Java heap size, recommend setting both to 50% of system RAM
2024-02-01 14:01:58 -05:00
- "OPENSEARCH_INITIAL_ADMIN_PASSWORD=< custom-admin-password > " # The initial admin password used by the demo configuration
2021-05-28 13:48:19 -04:00
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- opensearch-data1:/usr/share/opensearch/data
ports:
- 9200:9200
- 9600:9600 # required for Performance Analyzer
networks:
- opensearch-net
2021-09-24 16:49:48 -04:00
opensearch-ccs-node2:
2021-05-28 13:48:19 -04:00
image: opensearchproject/opensearch:{{site.opensearch_version}}
2021-09-24 16:49:48 -04:00
container_name: opensearch-ccs-node2
2021-05-28 13:48:19 -04:00
environment:
2021-09-24 16:49:48 -04:00
- cluster.name=opensearch-ccs-cluster2
2021-05-28 13:48:19 -04:00
- discovery.type=single-node
- bootstrap.memory_lock=true # along with the memlock settings below, disables swapping
- "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" # minimum and maximum Java heap size, recommend setting both to 50% of system RAM
2024-02-01 14:01:58 -05:00
- "OPENSEARCH_INITIAL_ADMIN_PASSWORD=< custom-admin-password > " # The initial admin password used by the demo configuration
2021-05-28 13:48:19 -04:00
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- opensearch-data2:/usr/share/opensearch/data
ports:
- 9250:9200
- 9700:9600 # required for Performance Analyzer
networks:
- opensearch-net
volumes:
opensearch-data1:
opensearch-data2:
networks:
opensearch-net:
```
After the clusters start, verify the names of each:
```json
2024-02-01 14:01:58 -05:00
curl -XGET -u 'admin:< custom-admin-password > ' -k 'https://localhost:9200'
2021-05-28 13:48:19 -04:00
{
2021-09-24 16:49:48 -04:00
"cluster_name" : "opensearch-ccs-cluster1",
2021-05-28 13:48:19 -04:00
...
}
2024-02-01 14:01:58 -05:00
curl -XGET -u 'admin:< custom-admin-password > ' -k 'https://localhost:9250'
2021-05-28 13:48:19 -04:00
{
2021-09-24 16:49:48 -04:00
"cluster_name" : "opensearch-ccs-cluster2",
2021-05-28 13:48:19 -04:00
...
}
```
2021-09-24 16:49:48 -04:00
Both clusters run on `localhost` , so the important identifier is the port number. In this case, use port 9200 (`opensearch-ccs-node1`) as the remote cluster, and port 9250 (`opensearch-ccs-node2`) as the coordinating cluster.
2021-05-28 13:48:19 -04:00
To get the IP address for the remote cluster, first identify its container ID:
```bash
docker ps
CONTAINER ID IMAGE PORTS NAMES
2021-09-24 16:49:48 -04:00
6fe89ebc5a8e opensearchproject/opensearch:{{site.opensearch_version}} 0.0.0.0:9200->9200/tcp, 0.0.0.0:9600->9600/tcp, 9300/tcp opensearch-ccs-node1
2da08b6c54d8 opensearchproject/opensearch:{{site.opensearch_version}} 9300/tcp, 0.0.0.0:9250->9200/tcp, 0.0.0.0:9700->9600/tcp opensearch-ccs-node2
2021-05-28 13:48:19 -04:00
```
Then get that container's IP address:
```bash
docker inspect --format='{% raw %}{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}{% endraw %}' 6fe89ebc5a8e
172.31.0.3
```
On the coordinating cluster, add the remote cluster name and the IP address (with port 9300) for each "seed node." In this case, you only have one seed node:
```json
2024-02-01 14:01:58 -05:00
curl -k -XPUT -H 'Content-Type: application/json' -u 'admin:< custom-admin-password > ' 'https://localhost:9250/_cluster/settings' -d '
2021-05-28 13:48:19 -04:00
{
"persistent": {
2022-06-22 13:57:09 -04:00
"cluster.remote": {
2021-09-24 16:49:48 -04:00
"opensearch-ccs-cluster1": {
2021-05-28 13:48:19 -04:00
"seeds": ["172.31.0.3:9300"]
}
}
}
}'
```
On the remote cluster, index a document:
```bash
2024-02-01 14:01:58 -05:00
curl -XPUT -k -H 'Content-Type: application/json' -u 'admin:< custom-admin-password > ' 'https://localhost:9200/books/_doc/1' -d '{"Dracula": "Bram Stoker"}'
2021-05-28 13:48:19 -04:00
```
At this point, cross-cluster search works. You can test it using the `admin` user:
```bash
2024-02-01 14:01:58 -05:00
curl -XGET -k -u 'admin:< custom-admin-password > ' 'https://localhost:9250/opensearch-ccs-cluster1:books/_search?pretty'
2021-05-28 13:48:19 -04:00
{
...
"hits": [{
2021-09-24 16:49:48 -04:00
"_index": "opensearch-ccs-cluster1:books",
2021-05-28 13:48:19 -04:00
"_id": "1",
"_score": 1.0,
"_source": {
"Dracula": "Bram Stoker"
}
}]
}
```
To continue testing, create a new user on both clusters:
```bash
2024-02-01 14:01:58 -05:00
curl -XPUT -k -u 'admin:< custom-admin-password > ' 'https://localhost:9200/_plugins/_security/api/internalusers/booksuser' -H 'Content-Type: application/json' -d '{"password":"password"}'
curl -XPUT -k -u 'admin:< custom-admin-password > ' 'https://localhost:9250/_plugins/_security/api/internalusers/booksuser' -H 'Content-Type: application/json' -d '{"password":"password"}'
2021-05-28 13:48:19 -04:00
```
Then run the same search as before with `booksuser` :
```json
2021-09-24 16:49:48 -04:00
curl -XGET -k -u booksuser:password 'https://localhost:9250/opensearch-ccs-cluster1:books/_search?pretty'
2021-05-28 13:48:19 -04:00
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "no permissions for [indices:admin/shards/search_shards, indices:data/read/search] and User [name=booksuser, roles=[], requestedTenant=null]"
}
],
"type" : "security_exception",
"reason" : "no permissions for [indices:admin/shards/search_shards, indices:data/read/search] and User [name=booksuser, roles=[], requestedTenant=null]"
},
"status" : 403
}
```
Note the permissions error. On the remote cluster, create a role with the appropriate permissions, and map `booksuser` to that role:
```bash
2024-02-01 14:01:58 -05:00
curl -XPUT -k -u 'admin:< custom-admin-password > ' -H 'Content-Type: application/json' 'https://localhost:9200/_plugins/_security/api/roles/booksrole' -d '{"index_permissions":[{"index_patterns":["books"],"allowed_actions":["indices:admin/shards/search_shards","indices:data/read/search"]}]}'
curl -XPUT -k -u 'admin:< custom-admin-password > ' -H 'Content-Type: application/json' 'https://localhost:9200/_plugins/_security/api/rolesmapping/booksrole' -d '{"users" : ["booksuser"]}'
2021-05-28 13:48:19 -04:00
```
Both clusters must have the user, but only the remote cluster needs the role and mapping; in this case, the coordinating cluster handles authentication (i.e. "Does this request include valid user credentials?"), and the remote cluster handles authorization (i.e. "Can this user access this data?").
{: .tip }
Finally, repeat the search:
```bash
2021-09-24 16:49:48 -04:00
curl -XGET -k -u booksuser:password 'https://localhost:9250/opensearch-ccs-cluster1:books/_search?pretty'
2021-05-28 13:48:19 -04:00
{
...
"hits": [{
2021-09-24 16:49:48 -04:00
"_index": "opensearch-ccs-cluster1:books",
2021-05-28 13:48:19 -04:00
"_id": "1",
"_score": 1.0,
"_source": {
"Dracula": "Bram Stoker"
}
}]
}
```