diff --git a/.gitignore b/.gitignore index e1be11e..a159c62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *DS_STORE history.yml +.env \ No newline at end of file diff --git a/Gemfile b/Gemfile index e9709aa..3e796a3 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,8 @@ gem 'smashing' group :test, :development do gem 'rake' gem 'rubocop', '= 0.35.1' + end + +gem 'faraday' +gem 'dotenv' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 6f50c18..46e1d51 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,15 +11,22 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.1.7) daemons (1.3.1) + dotenv (2.8.1) et-orbi (1.2.4) tzinfo eventmachine (1.2.7) execjs (2.7.0) - ffi (1.13.1) + faraday (1.3.0) + faraday-net_http (~> 1.0) + multipart-post (>= 1.2, < 3) + ruby2_keywords + faraday-net_http (1.0.1) + ffi (1.15.5) fugit (1.3.9) et-orbi (~> 1.1, >= 1.1.8) raabro (~> 1.3) multi_json (1.15.0) + multipart-post (2.3.0) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) parser (2.6.3.0) @@ -85,6 +92,8 @@ PLATFORMS ruby DEPENDENCIES + dotenv + faraday rake rubocop (= 0.35.1) smashing diff --git a/README.md b/README.md index 60659f3..177cf6c 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,15 @@ bundle install ES_ENDPOINT=http://your_es_hostname_or_ip:9200/ ES_INDEX=pelias smashing start ``` +Add Elasticsearch username and password as needed +``` +ES_ENDPOINT=http://your_es_hostname_or_ip:9200/ ES_USERNAME=elastic ES_PASSWORD=password ES_INDEX=pelias smashing start +``` + +Add Username and password to enable Basic Auth +``` +ES_ENDPOINT=http://your_es_hostname_or_ip:9200/ USERNAME=username PASSWORD=password ES_INDEX=pelias smashing start +``` * navigate to http://localhost:3030 in your browser Docker diff --git a/config.ru b/config.ru index 8654964..b1ad181 100644 --- a/config.ru +++ b/config.ru @@ -1,10 +1,20 @@ +require 'dotenv/load' require 'dashing' + configure do - helpers do + helpers do def protected! - # Put any authentication code you want in here. - # This method is run before accessing any resource. + unless authorized? + response['WWW-Authenticate'] = %(Basic realm="Restricted Area") + throw(:halt, [401, "Not authorized\n"]) + end + end + + def authorized? + return true unless ENV['USERNAME'] || ENV['PASSWORD'] + @auth ||= Rack::Auth::Basic::Request.new(request.env) + @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [ENV['USERNAME'], ENV['PASSWORD']] end end end diff --git a/jobs/counts.rb b/jobs/counts.rb index 8005fe9..31af457 100644 --- a/jobs/counts.rb +++ b/jobs/counts.rb @@ -32,7 +32,7 @@ # get total count total_url = URI.parse "#{@es_endpoint}#{@es_index}/_count" - total_response = Net::HTTP.post(total_url, '', { "Content-Type" => "application/json" }) + total_response = @es_client.post(total_url, '', { "Content-Type" => "application/json" }) total_response_body = JSON.parse total_response.body @@ -54,7 +54,7 @@ # get layer counts by aggregation url = URI.parse "#{@es_endpoint}#{@es_index}/_search?request_cache=true" - response = Net::HTTP.post(url, query.to_json, { "Content-Type" => "application/json" }) + response = @es_client.post(url, query.to_json, { "Content-Type" => "application/json" }) response_body = JSON.parse response.body diff --git a/jobs/elasticsearch_client.rb b/jobs/elasticsearch_client.rb new file mode 100644 index 0000000..0059c1c --- /dev/null +++ b/jobs/elasticsearch_client.rb @@ -0,0 +1,8 @@ +require 'faraday' + +# Allow specification of an elasticsearch endpoint via env var +# Should take the form of "http://{ip|hostname}:{port}/" +@es_endpoint = ENV['ES_ENDPOINT'] || 'http://localhost:9200/' + +@es_client = Faraday.new(@es_endpoint) +@es_client.basic_auth ENV['ES_USERNAME'], ENV['ES_PASSWORD'] if ENV['ES_USERNAME'] || ENV['ES_PASSWORD'] \ No newline at end of file diff --git a/jobs/es_metrics.rb b/jobs/es_metrics.rb index 9d73806..17531f9 100644 --- a/jobs/es_metrics.rb +++ b/jobs/es_metrics.rb @@ -7,7 +7,7 @@ SCHEDULER.every '1m' do url = URI.parse "#{@es_endpoint}#{@es_index}/_stats/docs" - response = JSON.parse Net::HTTP.get_response(url).body + response = JSON.parse @es_client.get(url).body indexed = response['indices'][@es_index]['primaries']['docs']['count'] percent_complete = ((indexed.to_f / @expected_doc_count.to_f) * 100).to_i @@ -20,7 +20,7 @@ # es metrics SCHEDULER.every '1m' do url = URI.parse "#{@es_endpoint}#{@es_index}/_stats?human" - response = JSON.parse Net::HTTP.get_response(url).body + response = JSON.parse @es_client.get(url).body store_size = response['indices'][@es_index]['primaries']['store']['size'] send_event('es-store-size', text: store_size) @@ -34,7 +34,7 @@ count << { rate: 0, indexed: false } SCHEDULER.every '10s' do url = URI.parse "#{@es_endpoint}#{@es_index}/_stats/indexing?human" - response = JSON.parse Net::HTTP.get_response(url).body + response = JSON.parse @es_client.get(url).body indexed = response['indices'][@es_index]['primaries']['indexing']['index_total'] # avoid huge spike with first data point @@ -56,7 +56,7 @@ port.nil? ? port = 80 : port version_url = URI.parse "http://#{host}:#{port}/" - response = JSON.parse Net::HTTP.get_response(version_url).body + response = JSON.parse @es_client.get(version_url).body version = response['version']['number'] send_event('es-version', text: version) diff --git a/jobs/include.rb b/jobs/include.rb index f8299a0..1f03801 100644 --- a/jobs/include.rb +++ b/jobs/include.rb @@ -1,3 +1,4 @@ +require_relative 'elasticsearch_client.rb' # Allow specification of an elasticsearch endpoint via env var # Should take the form of "http://{ip|hostname}:{port}/" @@ -6,11 +7,11 @@ # determine if a given index name is actually an alias # and if so, return the true index name def resolve_alias(index_name) - alias_response = Net::HTTP.get_response(URI.parse("#{@es_endpoint}_alias/#{index_name}")) + alias_response = @es_client.get "_alias/#{index_name}" puts alias_response.body - puts alias_response.code + puts alias_response.status - if alias_response.code != "200" + if alias_response.status != "200" index_name else parsed_response = JSON.parse(alias_response.body)