{"id":10854,"date":"2024-04-04T20:53:00","date_gmt":"2024-04-05T01:53:00","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=10854"},"modified":"2024-04-03T21:27:31","modified_gmt":"2024-04-04T02:27:31","slug":"elasticsearch-to-opensearch-migration-map-users-to-roles","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=10854","title":{"rendered":"ElasticSearch to OpenSearch Migration: Map Users to Roles"},"content":{"rendered":"\n<p>After the roles are created, I need to map users into the roles &#8212; using the ElasticSearch API to list all roles and add each user to the corresponding OpenSearch role. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport requests\nfrom requests.auth import HTTPBasicAuth\n\ndef addUserToRole(strRole, strUID):\n        jsonAddUser = &#x5B;\n        {               &quot;op&quot;: &quot;add&quot;,            &quot;path&quot;: f&quot;\/{strRole}&quot;,          &quot;value&quot;: {&quot;users&quot;: strUID} }]\n        print(f&quot;{strRole}\\t{jsonAddUser}&quot;)\n        r2 = requests.patch(f&quot;https:\/\/opensearch.example.com:9200\/_plugins\/_security\/api\/rolesmapping&quot;, json=jsonAddUser, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n        print(r2.text)\n        print(r2.status_code)\n\nlistIgnoredGroups = &#x5B;&#039;security_rest_api_access&#039;, &#039;logstash_role&#039;, &#039;elastalert_role&#039;, &#039;kibana_server&#039;, &#039;wsadmin_role&#039;, &#039;mgmt_role&#039;, &#039;logstash&#039;, &#039;manage_snapshots&#039;, &#039;readall&#039;, &#039;all_access&#039;, &#039;own_index&#039;, &#039;kibana_user&#039;, ]\n\n# Get all roles from prod &amp; list users in those roles\n#GET _opendistro\/_security\/api\/rolesmapping\/\nr = requests.get(f&quot;https:\/\/elasticsearch.example.com:9200\/_opendistro\/_security\/api\/rolesmapping\/&quot;, auth = HTTPBasicAuth(&#039;something&#039;, &#039;something&#039;), verify=False)\n\ndictAllRoles = r.json()\n\n# For each role, list out each user and add that user to that role in OS\nfor item in dictAllRoles.items():\n        if item&#x5B;0] not in listIgnoredGroups:\n                for strUID in item&#x5B;1].get(&#039;users&#039;):\n                        addUserToRole(item&#x5B;0], item&#x5B;1].get(&#039;users&#039;))\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>After the roles are created, I need to map users into the roles &#8212; using the ElasticSearch API to list all roles and add each user to the corresponding OpenSearch role.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1588],"tags":[1590,2001,1740],"class_list":["post-10854","post","type-post","status-publish","format-standard","hentry","category-elk","tag-elasticsearch","tag-elasticsearch-to-opensearch-migration","tag-opensearch"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10854"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10854\/revisions"}],"predecessor-version":[{"id":10855,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10854\/revisions\/10855"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}