Skip to content

Commit 95ebb7f

Browse files
author
Jurgens du Toit
committed
refac: Use sinatra instead of Roda. Add more specs
1 parent e19a86b commit 95ebb7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+973
-454
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rubygems'
22
require 'bundler/gem_tasks'
33
require 'rspec/core/rake_task'
4+
require 'proxes/rake_tasks'
45

56
RSpec::Core::RakeTask.new(:spec)
67

Vagrantfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# vi: set ft=ruby :
33

44
Vagrant.configure(2) do |config|
5-
config.vm.box = "ubuntu/trusty64"
5+
config.vm.box = "ubuntu/xenial64"
66

77
config.vm.network :private_network, ip: '172.16.248.110'
88

@@ -16,7 +16,7 @@ Vagrant.configure(2) do |config|
1616
sudo apt-get install -y screen curl git build-essential libssl-dev
1717
1818
# Ruby
19-
sudo apt-get install ruby2.0
19+
sudo apt-get install -y ruby2.3 ruby2.3-dev
2020
# if [ ! -f /home/vagrant/.rvm/scripts/rvm ]
2121
# then
2222
# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
@@ -27,7 +27,7 @@ Vagrant.configure(2) do |config|
2727
# Ruby and it's Gems
2828
cd /vagrant
2929
# rvm use $(cat .ruby-version) --install
30-
gem install bundler --no-rdoc --no-ri
30+
sudo gem install bundler --no-rdoc --no-ri
3131
bundle install
3232
3333
# Node

config.ru

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,61 @@
22
libdir = File.expand_path(File.dirname(__FILE__) + '/lib')
33
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
44

5+
raise 'Unconfigured' unless ENV['ELASTICSEARCH_URL']
6+
57
require 'proxes'
68
require 'proxes/db'
79

8-
raise 'Unconfigured' unless ENV['ELASTICSEARCH_URL']
9-
1010
use Rack::Static, urls: ['/assets'], root: 'public'
11-
12-
use Rack::Session::Pool
13-
# use Rack::Session::Cookie,
14-
# :key => '_ProxES_session',
15-
# #:secure=>!TEST_MODE, # Uncomment if only allowing https:// access
16-
# :secret=>File.read('.session_secret')
11+
use Rack::MethodOverride
12+
use Rack::Session::Cookie,
13+
:key => '_ProxES_session',
14+
#:secure=>!TEST_MODE, # Uncomment if only allowing https:// access
15+
:secret=>File.read('.session_secret')
1716

1817
require 'omniauth'
1918
require 'omniauth-identity'
19+
require 'proxes/controllers/auth_identity'
2020
# OmniAuth.config.test_mode = true
21-
2221
use OmniAuth::Builder do
2322
# The identity provider is used by the App.
2423
provider :identity,
2524
fields: [:username],
25+
callback_path: '/_proxes/auth/identity/callback',
2626
model: ProxES::Identity,
27-
on_login: ProxES::Security,
28-
on_registration: ProxES::Security,
27+
on_login: ProxES::AuthIdentity,
28+
on_registration: ProxES::AuthIdentity,
2929
locate_conditions: lambda{|req| {username: req['username']} }
3030
end
31-
32-
OmniAuth.config.on_failure = Proc.new { |env|
33-
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
34-
}
31+
OmniAuth.config.on_failure = ProxES::AuthIdentity
3532

3633
require 'warden'
3734
require 'proxes/strategies/jwt_token'
3835
use Warden::Manager do |manager|
3936
manager.default_strategies :jwt_token
4037
manager.scope_defaults :default, action: '_proxes/unauthenticated'
41-
manager.failure_app = ProxES::Security
38+
manager.failure_app = ProxES::App
4239
end
43-
4440
Warden::Manager.serialize_into_session { |user| user.id }
4541
Warden::Manager.serialize_from_session { |id| ProxES::User[id] }
4642

43+
# Management App
44+
require 'proxes/controllers'
45+
46+
map '/_proxes' do
47+
{
48+
'/users' => ProxES::Users,
49+
'/user-roles' => ProxES::UserRoles,
50+
}.each do |route, app|
51+
map route do
52+
run app
53+
end
54+
end
55+
56+
run ProxES::App
57+
end
58+
59+
4760
# Proxy all Elasticsearch requests
4861
map '/' do
4962
# Security
@@ -52,8 +65,3 @@ map '/' do
5265
# Forward requests to ES
5366
run Rack::Proxy.new(backend: ENV['ELASTICSEARCH_URL'])
5467
end
55-
56-
# Management App
57-
map '/_proxes' do
58-
run ProxES::App
59-
end

lib/proxes.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
require 'proxes/version'
2-
require 'proxes/base'
32
require 'proxes/app'
43
require 'proxes/security'
5-
require 'proxes/es_request'

lib/proxes/app.rb

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
1-
require 'proxes/base'
2-
require 'proxes/routes'
1+
require 'proxes/controllers/application'
32

43
module ProxES
54
# Manage your Elasticsearch cluster, user and user sessions
6-
class App < ProxES::Base
7-
plugin :multi_route
5+
class App < Application
6+
get '/' do
7+
authenticate!
8+
haml :index
9+
end
810

9-
def logger
10-
require 'logger'
11-
@logger ||= Logger.new($stdout)
11+
['/unauthenticated', '/_proxes/unauthenticated'].each do |path|
12+
get path do
13+
redirect '/auth/identity'
14+
end
1215
end
1316

14-
def root_url
15-
@root_url = opts[:root_url] || '/_proxes'
17+
post '/auth/identity/new' do
18+
identity = Identity.new(params['identity'])
19+
if identity.valid? && identity.save
20+
flash[:info] = 'Successfully Registered. Please log in'
21+
redirect '/auth/identity'
22+
else
23+
flash.now[:warning] = 'Could not complete the registration. Please try again.'
24+
view 'security/register', locals: { identity: identity }
25+
end
1626
end
1727

18-
route do |r|
19-
r.multi_route
28+
post '/auth/identity/callback' do
29+
user = User.find_or_create(email: env['omniauth.auth']['info']['email'])
30+
user.add_user_role role: 'user' unless user.has_role? 'user'
31+
user.add_user_role(role: 'super_admin') if (user.id == 1 && user.has_role?('super_admin') == false)
2032

21-
r.get do
22-
authenticate!
33+
identity = Identity.find(username: user.email)
34+
user.add_identity identity unless identity.user == user
2335

24-
view 'index'
25-
end
36+
set_user user
37+
flash[:success] = 'Logged In'
38+
redirect '/_proxes'
39+
end
40+
41+
delete '/auth/identity' do
42+
logout
43+
44+
flash[:info] = 'Logged Out'
45+
46+
redirect '/_proxes'
2647
end
2748
end
2849
end

lib/proxes/base.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

lib/proxes/controllers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'proxes/controllers/users'
2+
require 'proxes/controllers/user_roles'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'sinatra/base'
2+
require 'sinatra/flash'
3+
require 'proxes/helpers/views'
4+
require 'proxes/helpers/pundit'
5+
require 'proxes/helpers/authentication'
6+
7+
module ProxES
8+
class Application < Sinatra::Base
9+
set :root, ::File.expand_path(::File.dirname(__FILE__) + '/../../../')
10+
register Sinatra::Flash
11+
helpers ProxES::Helpers::Pundit, ProxES::Helpers::Views, ProxES::Helpers::Authentication
12+
13+
configure :production do
14+
disable :show_exceptions
15+
end
16+
17+
configure :development do
18+
set :show_exceptions, :after_handler
19+
end
20+
21+
configure :production, :development do
22+
enable :logging
23+
end
24+
25+
not_found do
26+
haml :'404', locals: { title: '4 oh 4' }
27+
end
28+
29+
error do
30+
error = env['sinatra.error']
31+
haml :error, locals: { title: 'Something went wrong', message: error }
32+
end
33+
34+
error ::Pundit::NotAuthorizedError do
35+
flash[:warning] = 'Please log in first.'
36+
redirect '/auth/identity'
37+
end
38+
end
39+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'proxes/controllers/application'
2+
3+
module ProxES
4+
class AuthIdentity < Application
5+
get '/auth/identity' do
6+
haml :'identity/login', locals: { title: 'Log In' }
7+
end
8+
9+
# Failed Login
10+
post '/_proxes/auth/identity/callback' do
11+
flash[:warning] = 'Invalid credentials. Please try again.'
12+
redirect '/auth/identity'
13+
end
14+
15+
get '/auth/identity/register' do
16+
identity = Identity.new
17+
haml :'identity/register', locals: { title: 'Register', identity: identity }
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)