본문 바로가기

데이터 과학/데이터 과학 실무

elasticsearch 사용자 추가, 인덱스 접근권한 부여

이전 포스트에서는 자세히 다루지 않았지만 현재는 SSL과 사용자인증 기능이 적용된 상태이다.

이 상태에서 공인된 인증서를 사용하지 않는 경우 curl이 먹히지 않을 수 있는데 -k 옵션을 추가하면 해결 가능하다.

 

이전 포스트에 걸려있는 링크의 튜토리얼까지 다 따라왔다면 built-in user의 비밀번호가 설정되어 있을 것이다.

Elasticsearch 에서는 각 프로젝트에 최소한의 권한을 갖는 general user 계정을 만들어 사용할 것을 권장하므로 user 계정을 만들어보도록 하자.

 

role 추가하기

user를 만들 때 parameter로 role이 들어가므로 role 부터 만들어야할 것 같다.

필자는 test_index 라는 인덱스를 미리 만들어두었으므로 아래의 명령어로 test_index에 대한 접근 권한을 가진 role을 만들었다.

curl -k -u elastic:password -X PUT "https://localhost:9200/_security/role/test_index_r?pretty" -H 'Content-Type: application/json' -d'
{
  "cluster": [],
  "indices": [
    {
      "names": [ "test_index" ],
      "privileges": ["read"]
    }
  ]
}
'

-u 옵션 뒤의 user 정보에 password는 elasticsearch-setup-passwords 실행 결과에서 설정된 비밀번호를 넣어준다. 데이터의 names 에는 타겟 인덱스 이름들을 넣어준다. URL의 test_index_r 은 role 이름이다. test_index_r role에는 test_index에 대한 read 권한만 부여할 것이므로 privilegesread만 넣어주었다.

user 추가하기

curl -k -u elastic:password -X POST "https://localhost:9200/_security/user/test_index_user?pretty" -H 'Content-Type: application/json' -d'
{
  "password" : "test_index_user_passwd",
  "roles" : [ "test_index_r" ],
  "full_name" : "Test Index User",
  "email" : "test_index_user@email.com"
}
'

URL의 test_index_user는 username이다. roles에 위에서 만든 test_index_r role을 넣어 test_index_usertest_index_r의 role을 갖게 한다.

접근제어 확인

위와 같이 role, user를 추가하였을 때 기대하는건 생성된 user가 test_index index에만 접근 가능 (read only) 한 것이다.

먼저 elastic user로 elasticsearch에 GET request를 보내서 아래와 같이 결과를 제대로 받아오는 것을 확인하자.

user$ curl -k -u elastic:elastic_password -X GET "https://localhost:9200/?pretty"
{
  "name" : "es01",
  "cluster_name" : "es-docker-cluster",
  "cluster_uuid" : "C9Q8rYXoQ1-aLasbaUyNuQ",
  "version" : {
    "number" : "7.13.4",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "c5f60e894ca0c61cdbae4f5a686d9f08bcefc942",
    "build_date" : "2021-07-14T18:33:36.673943207Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

이번에는 test_index index에 read 권한만 가진 user로 GET request를 보내보자.

user$ curl -k -u test_index_user:test_index_user_passwd -X GET "https://localhost:9200/?pretty"
{
  "error" : {
    "root_cause" : [
      {
        "type" : "security_exception",
        "reason" : "action [cluster:monitor/main] is unauthorized for user [disclosure_suffer] with roles [disclosure_r], this action is granted by the cluster privileges [monitor,manage,all]"
      }
    ],
    "type" : "security_exception",
    "reason" : "action [cluster:monitor/main] is unauthorized for user [disclosure_suffer] with roles [disclosure_r], this action is granted by the cluster privileges [monitor,manage,all]"
  },
  "status" : 403
}

elastic과 달리 결과를 받아오지 못하고 unauthorized for user 메시지를 받아온 것을 볼 수 있다.

 

이번에는 생성한 user로 read 권한을 가진 test_index에 query를 보내보자.

user$ curl -k -u test_index_user:test_index_user_passwd -X GET "https://localhost:9200/test_index/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
   }
}'

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

현재 아무 document도 추가하지 않아 hits에 문서가 나오지는 않지만 search가 잘 되는 것을 확인할 수 있다.

 

다른 setting의 privilege에 대한 정보는 여기서 확인할 수 있다.

'데이터 과학 > 데이터 과학 실무' 카테고리의 다른 글

빠르게 사용해보는 Elasticsearch  (0) 2021.07.23
본 블로그는 쿠팡 파트너스 활동을 포함하고 있으며, 이에 따른 일정액의 수수료를 제공받습니다.