Skip to content

Commit 7f05a48

Browse files
author
Ken Johnson
authored
Merge pull request #376 from relotnek/openshift
Added Openshift Support
2 parents 89af66d + 77a223b commit 7f05a48

File tree

10 files changed

+189
-2
lines changed

10 files changed

+189
-2
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ ADD Gemfile /myapp/Gemfile
66
ADD Gemfile.lock /myapp/Gemfile.lock
77
RUN bundle install
88
ADD . /myapp
9-

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ group :development, :test, :mysql do
5656
gem "test-unit"
5757
end
5858

59+
group :openshift do
60+
gem "pg"
61+
end
62+
5963
group :mysql do
6064
gem "mysql2"
6165
end

config/database.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#
44
# Ensure the SQLite 3 gem is defined in your Gemfile
55
# gem 'sqlite3'
6+
<% user = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? "root" : ENV["POSTGRESQL_USER"] %>
7+
<% password = ENV.key?("POSTGRESQL_ADMIN_PASSWORD") ? ENV["POSTGRESQL_ADMIN_PASSWORD"] : ENV["POSTGRESQL_PASSWORD"] %>
8+
<% db_service = ENV.fetch("DATABASE_SERVICE_NAME","").upcase %>
9+
610
development:
711
adapter: sqlite3
812
database: db/development.sqlite3
@@ -32,3 +36,15 @@ production:
3236
database: db/production.sqlite3
3337
pool: 5
3438
timeout: 5000
39+
40+
openshift:
41+
adapter: postgresql
42+
encoding: unicode
43+
# For details on connection pooling, see rails configuration guide
44+
# http://guides.rubyonrails.org/configuring.html#database-pooling
45+
pool: <%= ENV["POSTGRESQL_MAX_CONNECTIONS"] || 5 %>
46+
username: <%= user %>
47+
password: <%= password %>
48+
host: <%= ENV["#{db_service}_SERVICE_HOST"] %>
49+
port: <%= ENV["#{db_service}_SERVICE_PORT"] %>
50+
database: <%= ENV["POSTGRESQL_DATABASE"] %>

config/environments/development.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
Railsgoat::Application.configure do
33
# Settings specified here will take precedence over those in config/application.rb
4-
4+
config.hosts << '.svc.cluster.local'
55
# In the development environment your application's code is reloaded on
66
# every request. This slows down response time but is perfect for development
77
# since you don't have to restart the web server when you make code changes.

config/environments/openshift.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
Railsgoat::Application.configure do
3+
# Settings specified here will take precedence over those in config/application.rb
4+
config.hosts << '.svc.cluster.local'
5+
# In the development environment your application's code is reloaded on
6+
# every request. This slows down response time but is perfect for development
7+
# since you don't have to restart the web server when you make code changes.
8+
config.cache_classes = false
9+
10+
# Show full error reports and disable caching
11+
config.consider_all_requests_local = true
12+
config.action_controller.perform_caching = false
13+
14+
# Don't care if the mailer can't send
15+
config.action_mailer.raise_delivery_errors = false
16+
17+
# Print deprecation notices to the Rails logger
18+
config.active_support.deprecation = :log
19+
20+
# Only use best-standards-support built into browsers
21+
config.action_dispatch.best_standards_support = :builtin
22+
23+
# Tired of caching causing issues
24+
config.middleware.delete Rack::ETag
25+
26+
# Do not compress assets
27+
config.assets.compress = false
28+
29+
# Expands the lines which load the assets
30+
config.assets.debug = true
31+
32+
# ActionMailer settings for email support
33+
config.action_mailer.delivery_method = :smtp
34+
config.action_mailer.smtp_settings = { address: "127.0.0.1", port: 1025 }
35+
config.action_mailer.default_url_options = { host: "127.0.0.1:3000" }
36+
37+
# config.middleware.insert_before(
38+
# Rack::Lock, Rack::LiveReload,
39+
# :min_delay => 500,
40+
# :max_delay => 1000,
41+
# :port => 35727,
42+
# :host => 'railsgoat.dev',
43+
# :ignore => [ %r{dont/modify\.html$} ]
44+
# )
45+
46+
# For Rails 4.0+
47+
# Do not eager load code on boot. This avoids loading your whole application
48+
# just for the purpose of running a single test. If you are using a tool that
49+
# preloads Rails for running tests, you may have to set it to true.
50+
config.eager_load = false
51+
end

config/secrets.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$ANSIBLE_VAULT;1.1;AES256
2+
63303430303835393535616235383138383365363438363234316163373866616539353065343530
3+
3138626232306637323534343936383734363439376437620a383165366162623936613764336363
4+
61393232626465306139333339643531396338363631653361363562346237366534306539373938
5+
3135343835633239660a353038623433353364636264636633356538646431303234326437366138
6+
63336262376136386463653033643064616432623763626132353062646431333032353137393833
7+
61303832323139633831333932616565313762356233646663316636363663323166633466373066
8+
36663631393339633163643364613261326530363238333761306163363463623736333363323937
9+
30363865623132653730396235383862303439613762313163376365373464383364366333663637
10+
3066

config/secrets2.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$ANSIBLE_VAULT;1.1;AES256
2+
35626437333463353464663839363636313435336332376561623031666263616263613638303031
3+
3064343638663837633934636537353736323335313663350a313338383464353231346661616563
4+
39353235353135323330653437333062653232366330316237656339383465653130636166346537
5+
6663373338366133610a383365303662306266303832326561646263363031666237303464663062
6+
3435

docs/openshift.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Openshift Deployment
2+
To deploy Railsgoat with Openshift you must first have a working [Openshift Cluster](https://docs.okd.io/latest/welcome/index.html) installed. The Railsgoat Openshift deployment currently supports Postgres on the native Openshift Postgres imagestream.
3+
4+
```bash
5+
$ oc new-project railsgoat --description="Railsgoat Openshift Deployment" --display-name="Railsgoat"
6+
```
7+
8+
## Edit the Build Strategy
9+
Since Railsgoat supports both normal Docker deployment and openshift deployment, the Openshift deployment is located in the `openshift-configs` directory. Find the railsgoat build in your openshift deployment and edit the dockerStrategy so that it reads something like:
10+
11+
```
12+
strategy:
13+
dockerStrategy:
14+
dockerfilePath: openshift-configs/Dockerfile
15+
from:
16+
kind: ImageStreamTag
17+
name: 'ruby:2.6.5'
18+
namespace: railsgoat
19+
type: Docker
20+
```
21+
22+
### Creating a Database Service
23+
24+
Although Railsgoat in Openshift can be used with the development SQL Lite database, it also takes advantage of the PostgreSQL database image in Openshift for more creative demonstrations and an expansion of SQL attacks. To create the database service you will use the `oc new-app` command and will need to pass some environment variables. You can change these to anything you want.
25+
26+
```bash
27+
$ oc new-app postgresql -e POSTGRESQL_DATABASE=db_name -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password
28+
```
29+
30+
You can also set the password for the database admin by appending the previous command with:
31+
32+
```bash
33+
-e POSTGRESQL_ADMIN_PASSWORD=admin_pw
34+
```
35+
36+
To deploy Railsgoat alongside the postgres database, you will need to run:
37+
```bash
38+
$ oc new-app https://github.com/OWASP/railsgoat.git --name=railsgoat -e POSTGRESQL_USER=username -e POSTGRESQL_PASSWORD=password -e POSTGRESQL_DATABASE=db_name -e DATABASE_SERVICE_NAME=postgresql -e RAILS_ENV=openshift
39+
```
40+
41+
If you changed the database environment variables, make sure they match up.
42+
43+
Enter the deployments of your new Railsgoat project and locate the railsgoat deployment config. Add the following to the config under the container object.
44+
45+
```
46+
command:
47+
- /bin/bash
48+
- '-c'
49+
args:
50+
- rails server -p 3000 -b 0.0.0.0
51+
ports:
52+
- containerPort: 3000
53+
protocol: TCP
54+
```
55+
56+
From the openshift console run the following where `<RAILSGOAT POD ID>` is the active pod (for example, `railsgoat-2-dkalf`):
57+
58+
```bash
59+
$ oc rsh <RAILSGOAT POD ID>
60+
```
61+
62+
From the terminal:
63+
64+
```bash
65+
$ rails db:migrate
66+
```
67+
68+
Create the railsgoat service using the template in the openshift-configs folder by either creating it in the openshift terminal window or pulling it directly from git.
69+
70+
```bash
71+
$ oc create -f https://raw.githubusercontent.com/relotnek/railsgoat/master/openshift-configs/railsgoatservice.yaml
72+
```
73+
74+
Expose the service
75+
76+
```bash
77+
$ oc expose service railsgoat
78+
```
79+
80+
Once the railgoat deployment is running navigate to the indicated route

openshift-configs/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM ruby:2.6.5
2+
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
3+
RUN mkdir /myapp
4+
WORKDIR /myapp
5+
ADD Gemfile /myapp/Gemfile
6+
ADD Gemfile.lock /myapp/Gemfile.lock
7+
RUN bundle install --with=openshift
8+
ADD . /myapp
9+
RUN chgrp -R 0 /myapp \
10+
&& chmod -R g+rwX /myapp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: railsgoat
5+
spec:
6+
selector:
7+
app: railsgoat
8+
ports:
9+
- protocol: TCP
10+
port: 80
11+
targetPort: 3000

0 commit comments

Comments
 (0)