Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Basic Authentication and Elasticsearch authentication #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*DS_STORE
history.yml
.env
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ gem 'smashing'
group :test, :development do
gem 'rake'
gem 'rubocop', '= 0.35.1'

end

gem 'faraday'
gem 'dotenv'
11 changes: 10 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -85,6 +92,8 @@ PLATFORMS
ruby

DEPENDENCIES
dotenv
faraday
rake
rubocop (= 0.35.1)
smashing
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions config.ru
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions jobs/counts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions jobs/elasticsearch_client.rb
Original file line number Diff line number Diff line change
@@ -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']
8 changes: 4 additions & 4 deletions jobs/es_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions jobs/include.rb
Original file line number Diff line number Diff line change
@@ -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}/"
Expand All @@ -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)
Expand Down