Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #17 from eonu/master
Browse files Browse the repository at this point in the history
Remove IP whitelisting + rework static file serving
  • Loading branch information
eonu authored Jan 1, 2019
2 parents 695bc37 + cd03270 commit 8c5a44e
Show file tree
Hide file tree
Showing 24 changed files with 87 additions and 538 deletions.
21 changes: 18 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# 0.5.2

#### Major changes

- Remove IP whitelisting feature (remove `Eucalypt::Whitelist` and `eucalypt/whitelist.rb`)
- (**`config/assets.rb`**): Change `assets.rb` to automatically append the paths of any directories under `app/assets` to the `Sprockets::Environment` object
- Rework `static` method for serving static files
- Revert static data accessor to be configured to `app/static` rather than the value of `settings.public_folder`
- Change `settings.public_folder` to `app/static/public` (to differentiate between static files which are public, and static files which should remain internal to the application unless exposed at other endpoints)

#### Minor changes

- (**`Gemfile`**): Remove `:production` symbol from what should be the test environment for requiring the `rspec`, `rack-test` and `should-matchers` gems
- (**`README.md`**): Remove `README.md` code highlighting, replace with screenshot of CLI

# 0.5.1

#### Major changes

- Move policy role models from `app/models` to `app/models/roles` to avoid cluttering the `models` directory, preserving it for proper models such as `User`
- Add `lib/eucalypt/security/permissions.rb` to dynamically define permission methods for each policy in `app/policies` to avoid cluttering the policy file.
- Add `lib/eucalypt/security/permissions.rb` to dynamically define permission methods for each policy in `app/policies` to avoid cluttering the policy file

#### Minor changes

Expand All @@ -13,7 +28,7 @@

#### Major changes

- Introduced IP whitelisting with the `Eucalypt::Whitelist` class, and IP-protected routes with the `ip_check` helper method
- Introduce IP whitelisting with the `Eucalypt::Whitelist` class, and IP-protected routes with the `ip_check` helper method
- Rework maintenance mode to be defined with a special route in the `ApplicationController`:

```ruby
Expand Down Expand Up @@ -41,7 +56,7 @@

#### Minor changes

- Fix a minor whitespace issue in `config/assets.rb` when blogging environment is setup
- Fix a minor whitespace issue in `config/asset_pipeline.rb` when blogging environment is setup

# 0.4.1

Expand Down
20 changes: 1 addition & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,7 @@ $ eucalypt init my-new-app

Move into your new application's directory and run the top-level `eucalypt` command to display a list of all available commands:

```bash
$ eucalypt

Commands:
eucalypt init [NAME] ·› Sets up your application
eucalypt launch [ENV] ·› Launches your application
eucalypt console ·› Interactive console with all files loaded
eucalypt test ·› Run all application tests
eucalypt version ·› Display installed Eucalypt version
eucalypt rake ·› Run all database migrations
eucalypt blog [COMMAND] ·› Manage static blog environment
eucalypt generate [COMMAND] ·› Generate individual MVC files or scaffolds
eucalypt destroy [COMMAND] ·› Destroy individual MVC files or scaffolds
eucalypt security [COMMAND] ·› Manage authentication and authorization
eucalypt migration [COMMAND] ·› Generate ActiveRecord migrations

For more information about a specific command, use eucalypt -H.
Example: eucalypt -H generate scaffold
```
<p align="center"><img width="70%" src="gfx/cli.png"></p>

## Documentation

Expand Down
3 changes: 1 addition & 2 deletions eucalypt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
spec.license = "MIT"
spec.files = Dir.glob('lib/**/*', File::FNM_DOTMATCH) + %w[Gemfile LICENSE README.md Rakefile eucalypt.gemspec bin/eucalypt]
spec.bindir = "bin"
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f)}
spec.executables = "eucalypt"
spec.require_paths = ["lib"]

spec.required_ruby_version = "~> 2.5"
Expand All @@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "string-builder", "~> 2.3"
spec.add_runtime_dependency "activesupport", "~> 5.2"
spec.add_runtime_dependency "activerecord", "~> 5.2"
spec.add_runtime_dependency "bcrypt", "~> 3.1"
spec.add_runtime_dependency "front_matter_parser", "0.2.0"
spec.add_runtime_dependency "thor", "~> 0.20"
spec.add_runtime_dependency "sinatra", "~> 2.0", ">= 2.0.4"
Expand Down
Binary file added gfx/cli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions lib/eucalypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
require 'eucalypt/list'
require 'eucalypt/core/helpers/manifest'
require 'eucalypt/core/helpers/partial'
require 'eucalypt/whitelist'
require 'eucalypt/core/helpers/static'

Eucalypt::CLI.extend Eucalypt::List

Expand Down
12 changes: 0 additions & 12 deletions lib/eucalypt/blog/namespaces/blog/cli/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ def setup
generator.helper
generator.controller(route: options[:route])
generator.views

assets_file = File.join(directory, 'config', 'assets.rb')

File.open(assets_file) do |f|
return if f.read.include? "assets.append_path Eucalypt.path 'app', 'assets', 'blog'"
end

insert_into_file(
assets_file,
" assets.append_path Eucalypt.path 'app', 'assets', 'blog'\n",
after: "set :assets, Sprockets::Environment.new\n"
)
else
Eucalypt::Error.wrong_directory
end
Expand Down
6 changes: 3 additions & 3 deletions lib/eucalypt/core/helpers/maintenance.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'sinatra'
require 'bcrypt'
require 'securerandom'
class ApplicationController < Sinatra::Base
if settings.methods(false).include?(:maintenance)
if settings.maintenance
Expand All @@ -14,9 +14,9 @@ class ApplicationController < Sinatra::Base
unlink '*', &block
end
else
define_singleton_method(:maintenance) {|&block| get "/#{BCrypt::Password.create(?1)}", &block}
define_singleton_method(:maintenance) {|&block| get "/#{SecureRandom.random_bytes 16}", &block}
end
else
define_singleton_method(:maintenance) {|&block| get "/#{BCrypt::Password.create(?1)}", &block}
define_singleton_method(:maintenance) {|&block| get "/#{SecureRandom.random_bytes 16}", &block}
end
end
54 changes: 44 additions & 10 deletions lib/eucalypt/core/helpers/static.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
require 'sinatra'
class ApplicationController < Sinatra::Base
def static(uri, *args)
if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET'
status 303
else
status 302
module Eucalypt
class Static::Router
attr_reader :routes

def initialize(public_folder)
@routes = []
@public_folder = public_folder
end

# According to RFC 2616 section 14.30, "the field value consists of a
# single absolute URI"
response['Location'] = uri(uri.to_s, settings.absolute_redirects?, settings.prefixed_redirects?)
halt(*args)
def route(file, aliases: [])
raise ArgumentError.new("Invalid argument #{file} for 'file' - Expected string (file path with preceding /)") unless file.is_a?(String) && file.start_with?('/')
location = File.join @public_folder, file.sub('/', '')
raise ArgumentError.new("Invalid argument #{file} for 'file' - File \"#{location}\" doesn't exist") unless File.file? location
raise ArgumentError.new("Invalid keyword argument #{aliases} for 'aliases' - Expected Array of String") unless aliases.is_a?(Array) && aliases.all?{|a| a.is_a? String}
raise ArgumentError.new("Invalid keyword argument #{aliases} for 'aliases' - Expected Array of route names (preceded by /)") unless aliases.all?{|a| a.start_with? '/'}
@routes << {file: file, aliases: aliases}
end

alias_method :<<, :route
end
end

class ApplicationController < Sinatra::Base
set :static_router, ->{ Eucalypt::Static::Router.new settings.public_folder }

def self.static(file = nil, aliases: [])
if settings.static_router.is_a? Eucalypt::Static::Router
if block_given?
yield settings.static_router
settings.static_router.routes.each do |route|
route.values.flatten.each do |path|
get(path) { send_file File.join(settings.public_folder, route[:file].sub('/','')) }
end
end
else
if file && aliases
raise ArgumentError.new("Invalid argument #{file} for 'file' - Expected string (file path with preceding /)") unless file.is_a?(String) && file.start_with?('/')
location = File.join settings.public_folder, file.sub('/', '')
raise ArgumentError.new("Invalid argument #{file} for 'file' - File \"#{location}\" doesn't exist") unless File.file? location
raise ArgumentError.new("Invalid keyword argument #{aliases} for 'aliases' - Expected Array of String") unless aliases.is_a?(Array) && aliases.all?{|a| a.is_a? String}
raise ArgumentError.new("Invalid keyword argument #{aliases} for 'aliases' - Expected Array of route names (preceded by /)") unless aliases.all?{|a| a.start_with? '/'}
routes = [file] + aliases
routes.each {|path| get(path){send_file location} }
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/eucalypt/core/templates/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'rake', '~> 12.3'
gem 'thin', '~> 1.7'

# Test environment
group :test, :production do
group :test do
gem 'rack-test', '~> 1.0', require: 'rack/test'
gem 'rspec', '~> 3.7'
gem 'shoulda-matchers', '~> 3.1'
Expand Down
10 changes: 3 additions & 7 deletions lib/eucalypt/core/templates/eucalypt/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ class ApplicationController < Sinatra::Base
set :app_file, __FILE__

# Set public folder for static files
set :public_folder, Eucalypt.path('app', 'static')
set :public_folder, Eucalypt.path('app', 'static', 'public')

# Allow static files to be served
set :static, true
::Static = Eucalypt::Static.new(settings.public_folder, symbolize: true).freeze
# Set static data accessor
::Static = Eucalypt::Static.new(Eucalypt.path('app', 'static'), symbolize: true).freeze

# Set views directory
set :views, Eucalypt.path('app', 'views')

# Set default ERB template
set :erb, layout: :'layouts/main'

# Set IP whitelist
set :whitelist, Eucalypt::Whitelist.new(Eucalypt.path 'config', 'whitelist')

# Toggle maintenance mode
disable :maintenance

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class ApplicationController < Sinatra::Base
helpers ApplicationHelper if defined? ApplicationHelper

static '/maintenance.html', aliases: %w[/maintenance]

maintenance do
static '/maintenance.html'
redirect '/maintenance'
end

get '/' do
Expand Down
8 changes: 4 additions & 4 deletions lib/eucalypt/core/templates/eucalypt/config/assets.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class ApplicationController < Sinatra::Base
set :assets, Sprockets::Environment.new
assets.append_path Eucalypt.path 'app', 'assets', 'stylesheets'
assets.append_path Eucalypt.path 'app', 'assets', 'scripts'
assets.append_path Eucalypt.path 'app', 'assets', 'images'
assets.append_path Eucalypt.path 'app', 'assets', 'fonts'

assets.css_compressor = :scss
assets.js_compressor = :uglify

Eucalypt.glob 'app', 'assets', '*' do |item|
assets.append_path item if File.directory? item
end

get '/assets/*' do
env["PATH_INFO"].sub! '/assets', ''
settings.assets.call env
Expand Down
12 changes: 0 additions & 12 deletions lib/eucalypt/exceptions.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/eucalypt/guard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApplicationController < Sinatra::Base
helpers do
def guard
settings.guard
end
end
end
1 change: 1 addition & 0 deletions lib/eucalypt/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Eucalypt.require 'config', 'initializers', '*.rb'

require 'eucalypt/core/helpers/maintenance'
require 'eucalypt/core/helpers/static'

Eucalypt.require 'app', 'helpers', '{application_helper.rb}'
Eucalypt.require 'app', 'controllers', 'application_controller.rb'
Expand Down
2 changes: 1 addition & 1 deletion lib/eucalypt/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Eucalypt
VERSION = '0.5.1'
VERSION = '0.5.2'
end
70 changes: 0 additions & 70 deletions lib/eucalypt/whitelist.rb

This file was deleted.

Empty file.
1 change: 0 additions & 1 deletion spec/eucalypt/whitelist/resources/newline

This file was deleted.

4 changes: 0 additions & 4 deletions spec/eucalypt/whitelist/resources/newlines

This file was deleted.

1 change: 0 additions & 1 deletion spec/eucalypt/whitelist/resources/unexpected

This file was deleted.

3 changes: 0 additions & 3 deletions spec/eucalypt/whitelist/resources/unexpecteds

This file was deleted.

Loading

0 comments on commit 8c5a44e

Please sign in to comment.