{"id":470,"date":"2020-03-25T08:42:08","date_gmt":"2020-03-25T08:42:08","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=470"},"modified":"2020-03-25T08:42:08","modified_gmt":"2020-03-25T08:42:08","slug":"securing-elasticsearch","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/03\/25\/securing-elasticsearch\/","title":{"rendered":"Securing Elasticsearch"},"content":{"rendered":"<p>In this post I will try to enable security for the elasticsearch cluster. It would be simply penned as a sequence of steps you need to follow to enable security as part of the cluster.<\/p>\n<h2>Helpful links<\/h2>\n<ul>\n<li>https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/security-api.html#security-role-apis<\/li>\n<li>https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/certutil.html<\/li>\n<li>https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/security-api-put-user.html<\/li>\n<\/ul>\n<pre>[elastic@samarthya ~]$ .\/elasticsearch\/bin\/elasticsearch -V                                                                                                                                        \nfuture versions of Elasticsearch will require Java 11; your Java version from [\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64\/jre] does not meet this requirement                  \nVersion: 7.3.1, Build: default\/tar\/4749ba6\/2019-08-19T20:19:25.651794Z, JVM: 1.8.0_201<\/pre>\n<h2>Step 1<\/h2>\n<p class=\"highlight\"><code>xpack.security.enabled: true<\/code><\/p>\n<p>You need to open &#8216;config\/elasticsearch.yml&#8217; file and add the line shown above. In the console you can look at<\/p>\n<pre>[2020-03-25T07:24:21,280][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [node1] Active license is now [BASIC]; Security is enabled  \n\n<\/pre>\n<h3>CURL &#8211; _cat\/nodes?pretty<\/h3>\n<p>Issuing a basic curl command to get the nodes you can see the following output.<\/p>\n<pre>[elastic@samarthya ~]$ curl 'myserver:9200\/_cat\/nodes?pretty' \n{ \n\"error\" : { \n\"root_cause\" : [ \n{ \n\"type\" : \"security_exception\", \n\"reason\" : \"missing authentication credentials for REST request [\/_cat\/nodes?pretty]\", \n\"header\" : { \n\"WWW-Authenticate\" : \"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\" \n} \n} \n], \n\"type\" : \"security_exception\", \n\"reason\" : \"missing authentication credentials for REST request [\/_cat\/nodes?pretty]\", \n\"header\" : { \n\"WWW-Authenticate\" : \"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\" \n} \n}, \n\"status\" : 401 \n}<\/pre>\n<h2>Step 2 &#8211; Internal password<\/h2>\n<p>`elasticsearch-setup-passwords` allows you to set password.<\/p>\n<pre>Commands \n-------- \nauto - Uses randomly generated passwords \ninteractive - Uses passwords entered by a user \n\nNon-option arguments: \ncommand \n\nOption Description \n------ ----------- \n-h, --help show help \n-s, --silent show minimal output \n-v, --verbose show verbose output<\/pre>\n<p>\u00a0<\/p>\n<pre>[elastic@samarthya ~]$ .\/elasticsearch\/bin\/elasticsearch-setup-passwords interactive                                                                                                               \nfuture versions of Elasticsearch will require Java 11; your Java version from [\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64\/jre] does not meet this requirement                  \nInitiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.                                                              \nYou will be prompted to enter passwords as the process progresses.                                                                                                                               \nPlease confirm that you would like to continue [y\/N]y                                                                                                                                            \n                                                                                                                                                                                                 \n                                                                                                                                                                                                 \nEnter password for [elastic]:                                                                                                                                                                    \nReenter password for [elastic]:                                                                                                                                                                  \nEnter password for [apm_system]:                                                                                                                                                                 \nReenter password for [apm_system]:                                                                                                                                                               \nEnter password for [kibana]:                                                                                                                                                                     \nReenter password for [kibana]:                                                                                                                                                                   \nEnter password for [logstash_system]:                                                                                                                                                            \nReenter password for [logstash_system]:                                                                                                                                                          \nEnter password for [beats_system]:                                                                                                                                                               \nReenter password for [beats_system]:                                                                                                                                                             \nEnter password for [remote_monitoring_user]:                                                                                                                                                     \nReenter password for [remote_monitoring_user]:                                                                                                                                                   \nPasswords do not match.                                                                                                                                                                          \nTry again.                                                                                                                                                                                       \nEnter password for [remote_monitoring_user]:                                                                                                                                                     \nReenter password for [remote_monitoring_user]:                                                                                                                                                   \nPasswords do not match.                                                                                                                                                                          \nTry again.                                                                                                                                                                                       \nEnter password for [remote_monitoring_user]:                                                                                                                                                     \nReenter password for [remote_monitoring_user]:                                                                                                                                                   \nPasswords do not match.                                                                                                                                                                          \nTry again.                                                                                                                                                                                       \nEnter password for [remote_monitoring_user]:                                                                                                                                                     \nReenter password for [remote_monitoring_user]:                                                                                                                                                   \nPasswords do not match.                                                                                                                                                                          \nTry again.                                                                                                                                                                                       \nEnter password for [remote_monitoring_user]:                                                                                                                                                     \nReenter password for [remote_monitoring_user]:                                                                                                                                                   \nChanged password for user [apm_system]                                                                                                                                                           \nChanged password for user [kibana]                                                                                                                                                               \nChanged password for user [logstash_system]                                                                                                                                                      \nChanged password for user [beats_system]                                                                                                                                                         \nChanged password for user [remote_monitoring_user]                                                                                                                                               \nChanged password for user [elastic]<\/pre>\n<h3>Check the CURL again<\/h3>\n<p>This time we will specify the user elastic for which we set the password above<\/p>\n<pre>[elastic@server1 ~]$ curl -u elastic 'myserver:9200\/_cat\/nodes?pretty' \nEnter host password for user 'elastic': \n100.10.10.200 26 25 1 0.00 0.03 0.06 dim * node1<\/pre>\n<p>\u00a0<\/p>\n<h2>Step &#8211; 3 Setup Kibana security<\/h2>\n<p>When you start Kibana without configuring the security, you will get an error like below.<\/p>\n<pre><span class=\" xterm-color-7 \">\u00a0\u00a0log\u00a0\u00a0<\/span>\u00a0[07:36:28.860]\u00a0[<span class=\" xterm-color-1 \">warning<\/span>][task_manager] PollError [security_exception] missing authentication credentials for REST request [\/_template\/.kibana_task_manager?filter_path=*.version], with { header={ WWW-Authenticate=\"Basic realm=\\\"security\\\" charset=\\\"UTF-8\\\"\" } }<\/pre>\n<h3>kibana-keystore<\/h3>\n<p>We will be using the Keystore to create information placeholders to keep the value safe and not accessible to everyone except the intended process.<\/p>\n<h4>HELP<\/h4>\n<pre>[elastic@samarthya ~]$ .\/kibana\/bin\/kibana-keystore --help \nUsage: bin\/kibana-keystore [options] [command] \n\nA tool for managing settings stored in the Kibana keystore \n\nOptions: \n-V, --version output the version number \n-h, --help output usage information \n\nCommands: \ncreate [options] Creates a new Kibana keystore \nlist [options] List entries in the keystore \nadd [options] &lt;key&gt; Add a string setting to the keystore \nremove [options] &lt;key&gt; Remove a setting from the keystore<\/pre>\n<h4>Add Username and Password to the keystore<\/h4>\n<pre>[elastic@samarthya ~]$ .\/kibana\/bin\/kibana-keystore create                                                                                                                                         \nCreated Kibana keystore in \/home\/elastic\/kibana\/data\/kibana.keystore                                                                                                                             \n[elastic@samarthya ~]$ .\/kibana\/bin\/kibana-keystore add elasticsearch.username                                                                                                                     \nEnter value for elasticsearch.username: ******                                                                                                                                                   \n[elastic@samarthya ~]$ .\/kibana\/bin\/kibana-keystore add elasticsearch.password                                                                                                                     \nEnter value for elasticsearch.password: ********<\/pre>\n<p>Now when we restart Kibana and launch the URL to access the app you will be prompted for password.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-medium wp-image-475\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-294x300.png\" alt=\"\" width=\"294\" height=\"300\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-294x300.png 294w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-1003x1024.png 1003w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-768x784.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-300x306.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM-850x867.png 850w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.15.07-PM.png 1264w\" sizes=\"(max-width: 294px) 100vw, 294px\" \/><\/p>\n<p>\u00a0<\/p>\n<h2>Kibana<\/h2>\n<p>Once we specify the user elastic for which we set the password and go to the management we can see the Users and roles section.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-medium wp-image-476\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.16.22-PM-284x300.png\" alt=\"\" width=\"284\" height=\"300\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.16.22-PM-284x300.png 284w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.16.22-PM-300x317.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.16.22-PM.png 448w\" sizes=\"(max-width: 284px) 100vw, 284px\" \/><\/p>\n<p>\u00a0<\/p>\n<h3>Users<\/h3>\n<p>You can look at the users available for default.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-478 size-medium\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-300x119.png\" alt=\"\" width=\"300\" height=\"119\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-300x119.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-1024x408.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-768x306.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-1536x612.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-2048x815.png 2048w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2020\/03\/Screenshot-2020-03-25-at-1.18.28-PM-850x338.png 850w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2>Role<\/h2>\n<p>Creating a new role is as simple as invoking an API as under<\/p>\n<pre>POST _security\/role\/read_only_role_a<br \/>{<br \/>\"cluster\": [],<br \/>\"indices\": [{<br \/>   \"names\": [ \"profile_record\" ],<br \/>   \"privileges\": [\"read\", \"view_index_metadata\"]<br \/>  }]<br \/>}<\/pre>\n<p>It should return a response like below<\/p>\n<pre>{<br \/>  \"role\" : {<br \/>  \"created\" : true<br \/>  }<br \/>}<\/pre>\n<h2>User<\/h2>\n<p>Creating a new user and assign the role<\/p>\n<pre>POST \/_security\/user\/samarthya<br \/>{<br \/>\"password\" : \"password\",<br \/>\"roles\" : [ \"read_only_role_a\" ],<br \/>\"full_name\" : \"Samarthya Saurabh\",<br \/>\"email\" : \"saurabh@samarthya.com\",<br \/>\"metadata\" : {<br \/>\"intelligence\" : 21,<br \/>\"country\": \"India\"<br \/>}<br \/>}<\/pre>\n<p>If the payload is all formatted it should return a response as under<\/p>\n<pre>{<br \/>\"created\" : true<br \/>}<\/pre>\n<p>Every node that wants to join a secured cluster needs a certificate from the established CA. In the process below I will lay out the required steps to establish the same.<\/p>\n<h2>Certificate Authority<\/h2>\n<p>Time to use certificates to secure the communication<\/p>\n<h3>elasticsearch-certutil<\/h3>\n<p>We will be using the builtin certutil to generate the certificate<\/p>\n<pre>Commands <br \/>-------- <br \/>csr - generate certificate signing requests <br \/>cert - generate X.509 certificates and keys <br \/>ca - generate a new local certificate authority <br \/><br \/>Non-option arguments: <br \/>command <br \/><br \/>Option Description <br \/>------ ----------- <br \/>-h, --help show help <br \/>-s, --silent show minimal output <br \/>-v, --verbose show verbose output <br \/><br \/>Will stick to the default name<\/pre>\n<pre>[elastic@samarthya ~]$ .\/elasticsearch\/bin\/elasticsearch-certutil ca --pem                                                                                                                         <br \/>future versions of Elasticsearch will require Java 11; your Java version from [\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64\/jre] does not meet this requirement                  <br \/>This tool assists you in the generation of X.509 certificates and certificate                                                                                                                    <br \/>signing requests for use with SSL\/TLS in the Elastic stack.                                                                                                                                      <br \/>                                                                                                                                                                                                 <br \/>The 'ca' mode generates a new 'certificate authority'                                                                                                                                            <br \/>This will create a new X.509 certificate and private key that can be used                                                                                                                        <br \/>to sign certificate when running in 'cert' mode.                                                                                                                                                 <br \/>                                                                                                                                                                                                 <br \/>Use the 'ca-dn' option if you wish to configure the 'distinguished name'                                                                                                                         <br \/>of the certificate authority                                                                                                                                                                     <br \/>                                                                                                                                                                                                 <br \/>By default the 'ca' mode produces a single PKCS#12 output file which holds:                                                                                                                      <br \/>    * The CA certificate                                                                                                                                                                         <br \/>    * The CA's private key                                                                                                                                                                       <br \/>                                                                                                                                                                                                 <br \/>If you elect to generate PEM format certificates (the -pem option), then the output will                                                                                                         <br \/>be a zip file containing individual files for the CA certificate and private key                                                                                                                 <br \/>                                                                                                                                                                                                 <br \/>Please enter the desired output file [elastic-stack-ca.zip]:  <\/pre>\n<p>Unzipping the elastic-stack-ca.zip will give you two files<\/p>\n<pre>[elastic@samarthya ca]$ ls <br \/>ca.crt ca.key<\/pre>\n\n\n<p>The CA key and certificate are now available to generate node certificates.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>.\/elasticsearch\/bin\/elasticsearch-certutil cert --ca-cert \/mycacertificate\/ca\/ca.crt --ca-key \/mycacertificate\/ca\/ca.key<\/code><\/pre>\n\n\n\n<p>This command has to be used on each node, to generate the certificate. It will prompt for a password which you SHOULD REMEMBER as it will be utilised in subsequent steps.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Truststore and Keystore<\/h3>\n\n\n\n<p>Keystore and Truststore are both important and essential for communication with an <strong>SSL Certificate<\/strong>. Both are very similar in terms of construct and structure.<\/p>\n\n\n\n<p>Truststore is used for the storage of certificates from the trusted CA, which is used in the <span style=\"text-decoration: underline;\"><strong>verification of the certificate<\/strong><\/span> provided by the server in an <a href=\"https:\/\/dzone.com\/articles\/ssl-in-java\">SSL connection<\/a>. On the other hand, a Keystore is used to store the private key and own identity certificate to be identified for verification.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In an SSL handshake, the work of Truststore is to verify the credentials, whereas the work of Keystore is to provide those credentials.<\/p><\/blockquote>\n\n\n\n<p>Commands to add the kesytore and trustore password (the one specified in the step above)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>.\/elasticsearch\/bin\/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password<\/code><\/li><li><code>.\/elasticsearch\/bin\/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password<\/code><\/li><\/ul>\n\n\n\n<p>Adding certificate information to the node<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>xpack.security.transport.ssl.enabled: true\nxpack.security.transport.ssl.verification_mode: certificate\nxpack.security.transport.ssl.keystore.path: elastic-certificates.p12\nxpack.security.transport.ssl.truststore.path: elastic-certificates.p12<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I will try to enable security for the elasticsearch cluster. It would be simply penned as a sequence of steps you need to follow to enable security as part of the cluster. Helpful links https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/security-api.html#security-role-apis https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/certutil.html https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/security-api-put-user.html [elastic@samarthya ~]$ .\/elasticsearch\/bin\/elasticsearch -V future versions of Elasticsearch will require Java 11; your Java version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":471,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[53,16],"class_list":["post-470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-elasticsearch","tag-security"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/471"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}