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

Secret feature #11

Open
wants to merge 2 commits 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
@@ -0,0 +1 @@
.env
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
source "https://rubygems.org"

ruby '2.0.0'
ruby '2.2.3'

gem 'sinatra'
gem 'httparty'

group :development do
gem 'shotgun'
gem 'pry'
gem 'pry-byebug'
gem 'dotenv'
end
21 changes: 15 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
byebug (9.0.5)
coderay (1.1.1)
dotenv (2.1.1)
httparty (0.12.0)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.1)
method_source (0.8.2)
multi_xml (0.5.5)
pry (0.9.12.4)
coderay (~> 1.0)
method_source (~> 0.8)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.4.0)
byebug (~> 9.0)
pry (~> 0.10)
rack (1.5.2)
rack-protection (1.5.1)
rack
Expand All @@ -21,14 +26,18 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.4.7)
slop (3.6.0)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
dotenv
httparty
pry
pry-byebug
shotgun
sinatra

BUNDLED WITH
1.11.2
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ heroku apps:create ${COMPANY_NAME}-deploy-hook-forker
heroku addons:create deployhooks:http --url=https://${COMPANY_NAME}-deploy-hook-forker.herokuapp.com
```

### Securing with a secret

As deployed, anyone on the internet can POST to your deploy-hook-forker instance
and trick you into thinking your project has been deployed.

To avoid this, you can optionally require a secret be present when your apps POST to your
deploy-hook-forker instance. To do so, add a secret to your app:

```
➔ heroku config:set secret=`hexdump /dev/random | head | md5` --app ${COMPANY_NAME}-deploy-hook-forker
Setting secret and restarting ⬢ company-name-deploy-hook-forker... done, v7
secret: e4b9c3c27ad5a3b6f0c9b0291eeccc28
```

Now, when pointing an app's deploy hook to your deploy hook forker, use the secret in the url:

```sh
heroku addons:create deployhooks:http --url=https://${COMPANY_NAME}-deploy-hook-forker.herokuapp.com?secret=e4b9c3c27ad5a3b6f0c9b0291eeccc28
```


## Development

```ruby
bundle
bundle exec rackup

# in another terminal...
curl http://localhost:9292
curl -d ... http://localhost:9292
```

## References

Expand Down
31 changes: 28 additions & 3 deletions server.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
ENV['RACK_ENV'] ||= 'development'

require 'bundler'
Bundler.require

require 'sinatra'
require 'yaml'
require 'uri'
Expand All @@ -6,28 +11,48 @@

use Rack::Logger

set :config_path, File.expand_path('../config.yml', __FILE__)
set :config, ->{ YAML.load(ERB.new(File.read(config_path)).result) }
configure :development do
require 'dotenv'
Dotenv.load
end

configure do
set :config_path, File.expand_path('../config.yml', __FILE__)
set :config, ->{ YAML.load(ERB.new(File.read(config_path)).result) }
set :secret, ENV["SECRET"]
puts "SECRET=#{ENV["SECRET"]}"
puts settings.config.inspect
end


helpers do
def logger
request.logger
end

def config
settings.config[params['app']] || {}
end
end

before do
if settings.secret && params["secret"] != settings.secret
halt 401, "Unauthorized"
end
end

get '/' do
content_type :json
status 200
"ok"
JSON.pretty_generate(settings.config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking if the user didn't secure their app then we don't want to dump this

if we don't have that logic, then we should probably just require a secret. which seems reasonable.

end

post '/' do
logger.info "RECIEVED POST: #{params.inspect}"
forwardable_params = params.dup
forwardable_params.delete('splat')
forwardable_params.delete('captures')
forwardable_params.delete('secret')

config.values.each do |url|
uri = URI.parse(url)
Expand Down