Skip to content

Upgrade Ruby and Rails versions, improve Docker and general repo maintenance #2

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

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a426ff6
Remove docker build and deploy scripts as they are not that helpful
mowsec Aug 1, 2023
e0be30b
Move GitHub related files to .github/
mowsec Aug 1, 2023
4702e91
Move terraform and jenkins related files to thier own directories
mowsec Aug 1, 2023
fb43086
Remove unused app readme
mowsec Aug 1, 2023
d372ced
New Dockerfile based on Alpine and using multi-stage builds
mowsec Aug 1, 2023
37ecbcd
Upgrade Ruby to 2.7.7 and Rails from <5 => 5.1
mowsec Aug 1, 2023
c55f477
Run rails app:update to implement version changes
mowsec Aug 1, 2023
6d292f6
One more change from update script
mowsec Aug 1, 2023
6197bb1
Updating capybara testing to use cuprite instead of poltergeist as pe…
mowsec Aug 1, 2023
6876f51
Upgrade Rails from 5.1 => 5.2
mowsec Aug 1, 2023
067aefa
Run app:update to implement version changes (5.1 -> 5.2)
mowsec Aug 1, 2023
d424f9a
Upgrade Rails from 5.2 => 6.0 and run app:upgrade script
mowsec Aug 1, 2023
440fd9f
Upgrade Rails from 5.2 => 6.0 and run app:upgrade script
mowsec Aug 1, 2023
a032d3c
Upgrade Ruby from 2.7.7 => 3.0.6
mowsec Aug 1, 2023
e5532a6
Upgrade Ruby from 3.0 => 3.1.4, Rails from 6.0 => 6.1.7
mowsec Aug 3, 2023
1f9a808
Run app:update to implement version changes (6.0 -> 6.1.7)
mowsec Aug 3, 2023
34bcd89
Fixing javascript loading issue by removing turbolinks
mowsec Aug 3, 2023
aba7bec
Fixing graph loading issues from upstream:
mowsec Aug 3, 2023
e32d1a3
Fixing SQL Injection test spec
mowsec Aug 3, 2023
aa1ffd3
Add to .gitignore
mowsec Aug 3, 2023
9daf99a
Create .dockerignore to stop unnecessary files in build
mowsec Aug 3, 2023
cd6fbad
rename .gitkeep to .keep for convention
mowsec Aug 3, 2023
9643daf
Adding sample .env file
mowsec Aug 3, 2023
89c7b20
Adding dev and prod services to docker compose
mowsec Aug 3, 2023
36d6029
Check agent configuration on container start and fail
mowsec Aug 3, 2023
ed7a720
Rewriting README.md and adding supporting documentation to the docs/ …
mowsec Aug 3, 2023
fa1c3ec
Updating terraform config
davidaustinarcher Aug 4, 2023
a29e252
Adding path context for terraform
davidaustinarcher Aug 4, 2023
36ae544
Fixed block order in Jenkinsfile
davidaustinarcher Aug 4, 2023
9556a11
Removed checkout from Jenkinsfile
davidaustinarcher Aug 4, 2023
7fce2cc
Force committing images used in README.md
mowsec Aug 4, 2023
1795a5c
Updated Jenkinsfile to use standard commit hash
davidaustinarcher Aug 8, 2023
b6dea8b
adding .env to git commit
mowsec Jul 9, 2024
1c87c40
renamed .env to .env.example
mowsec Jul 9, 2024
0290c66
added .env to dockerignore for builds
mowsec Jul 9, 2024
b1430e8
added contrast_security.yaml to gitignore for all directories
mowsec Jul 9, 2024
51e8ed8
added contrast_security.yaml to gitignore for all directories
mowsec Jul 9, 2024
7c06291
updated yaml.safeload in parseyaml.py
mowsec Jul 9, 2024
3e068d9
improving startup scripts
mowsec Jul 9, 2024
a1178e5
Update Gemfile
mowsec Jul 9, 2024
d37565d
Updating documentation
mowsec Jul 9, 2024
164170e
Updating docker-compose
mowsec Jul 9, 2024
5db205c
Merge branch 'upgrade-rails-and-repo-maintainence' of github.com:Cont…
mowsec Jul 9, 2024
bce91c3
fixing location for contrast_security.yaml during Jenkins pipeline runs
mowsec Jul 9, 2024
48c664a
fixing location for contrast_security.yaml during Jenkins pipeline runs
mowsec Jul 9, 2024
9f9b4f1
updating terraform to use a unique resourse group to avoid conflicts
mowsec Jul 9, 2024
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
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Ignore root files that are not needed in docker image
.github
.jenkins
.terraform
.gitignore
docker-compose.yml
Dockerfile
LICENSE.md
README.md

# Ignore directories that are not needed in docker image
docs/
tmp/
log/*
public/data/*

# Ignore the dockerenv .env file during docker builds so that credentials are
# not accedentally built into container images.
.env
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Contrast Agent Authentication Keys
# CONTRAST__API__URL=https://eval.contrastsecurity.com/Contrast
# CONTRAST__API__API_KEY=XXXXXX
# CONTRAST__API__SERVICE_KEY=XXXXXX
# CONTRAST__API__USER_NAME=XXXXXX

# Override the default application name and code set in config/contrast_security.yml
# CONTRAST__APPLICATION__NAME=OWASP RailsGoat
# CONTRAST__APPLICATION__CODE=demo-railsgoat

# Override the default server name and environment set in config/contrast_security.yml
# CONTRAST__SERVER__NAME=railsgoat-docker
# CONTRAST__SERVER__ENVIRONMENT=development

# See https://docs.contrastsecurity.com/user-vulnerableapps.html#session
# CONTRAST__APPLICATION__SESSION_METADATA=""
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.bundle
/bin
/db/*.sqlite3
/log/*.log
/tmp
Expand All @@ -14,4 +13,8 @@ coverage
/vendor/ruby
run.sh
test.sh
contrast_security.yaml

.env
**/contrast_security.[yaml, yml]
contrast_connection.json
contrast.log
94 changes: 49 additions & 45 deletions Jenkinsfile → .jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ pipeline {
tools {
terraform 'terraform'
}
environment {
terraformDir = '.terraform'
}

stages {
stage('dependencies') {
steps {
script {
withCredentials([file(credentialsId: env.contrast_yaml, variable: 'path')]) {
def contents = readFile(env.path)
writeFile file: 'contrast_security.yaml', text: "$contents"
dir("$terraformDir") {
script {
withCredentials([file(credentialsId: env.contrast_yaml, variable: 'path')]) {
def contents = readFile(env.path)
writeFile file: 'contrast_security.yaml', text: "$contents"
}
}
sh '''
terraform init -upgrade
'''
}
sh '''
terraform init -upgrade
'''
}
}
stage('provision') {
steps {
script {
env.GIT_SHORT_COMMIT = checkout(scm).GIT_COMMIT.take(7)
env.GIT_BRANCH = checkout(scm).GIT_BRANCH

withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
dir("$terraformDir") {
script {
withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
export ARM_CLIENT_ID=$AZURE_CLIENT_ID
export ARM_CLIENT_SECRET=$AZURE_CLIENT_SECRET
export ARM_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID
Expand All @@ -36,14 +39,15 @@ pipeline {
-var 'initials=$initials' \
-var 'environment=qa' \
-var 'servername=jenkins' \
-var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_SHORT_COMMIT},version=1.0' \
-var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_COMMIT},version=1.0' \
-var 'run_automated_tests=true'
"""
} catch (Exception e) {
echo "Terraform refresh failed, deleting state"
sh "rm -rf terraform.tfstate"
currentBuild.result = "FAILURE"
error("Aborting the build.")
echo 'Terraform refresh failed, deleting state'
sh 'rm -rf terraform.tfstate'
currentBuild.result = 'FAILURE'
error('Aborting the build.')
}
}
}
}
Expand All @@ -56,13 +60,11 @@ pipeline {
}
stage('provision - dev') {
steps {
script {
env.GIT_SHORT_COMMIT = checkout(scm).GIT_COMMIT.take(7)
env.GIT_BRANCH = checkout(scm).GIT_BRANCH

withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
dir("$terraformDir") {
script {
withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
export ARM_CLIENT_ID=$AZURE_CLIENT_ID
export ARM_CLIENT_SECRET=$AZURE_CLIENT_SECRET
export ARM_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID
Expand All @@ -72,14 +74,15 @@ pipeline {
-var 'initials=$initials' \
-var 'environment=development' \
-var 'servername=Macbook-Pro' \
-var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_SHORT_COMMIT},version=1.0' \
-var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_COMMIT},version=1.0' \
-var 'run_automated_tests=true'
"""
} catch (Exception e) {
echo "Terraform refresh failed, deleting state"
sh "rm -rf terraform.tfstate"
currentBuild.result = "FAILURE"
error("Aborting the build.")
echo 'Terraform refresh failed, deleting state'
sh 'rm -rf terraform.tfstate'
currentBuild.result = 'FAILURE'
error('Aborting the build.')
}
}
}
}
Expand All @@ -92,25 +95,24 @@ pipeline {
}
stage('provision - prod') {
steps {
script {
env.GIT_SHORT_COMMIT = checkout(scm).GIT_COMMIT.take(7)
env.GIT_BRANCH = checkout(scm).GIT_BRANCH

withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
dir("$terraformDir") {
script {
withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
try {
sh """
export ARM_CLIENT_ID=$AZURE_CLIENT_ID
export ARM_CLIENT_SECRET=$AZURE_CLIENT_SECRET
export ARM_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID
export ARM_TENANT_ID=$AZURE_TENANT_ID

terraform apply -auto-approve -var 'location=$location' -var 'initials=$initials' -var 'environment=production' -var 'servername=Prod-01' -var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_SHORT_COMMIT},version=1.0' -var 'run_automated_tests=true'
terraform apply -auto-approve -var 'location=$location' -var 'initials=$initials' -var 'environment=production' -var 'servername=Prod-01' -var 'session_metadata=branchName=${env.GIT_BRANCH},buildNumber=${BUILD_NUMBER},commitHash=${env.GIT_COMMIT},version=1.0' -var 'run_automated_tests=true'
"""
} catch (Exception e) {
echo "Terraform refresh failed, deleting state"
sh "rm -rf terraform.tfstate"
currentBuild.result = "FAILURE"
error("Aborting the build.")
echo 'Terraform refresh failed, deleting state'
sh 'rm -rf terraform.tfstate'
currentBuild.result = 'FAILURE'
error('Aborting the build.')
}
}
}
}
Expand All @@ -123,15 +125,17 @@ pipeline {
}
stage('destroy') {
steps {
withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
sh """
dir("$terraformDir") {
withCredentials([azureServicePrincipal('ContrastAzureSponsored')]) {
sh """
export ARM_CLIENT_ID=\$AZURE_CLIENT_ID
export ARM_CLIENT_SECRET=\$AZURE_CLIENT_SECRET
export ARM_SUBSCRIPTION_ID=\$AZURE_SUBSCRIPTION_ID
export ARM_TENANT_ID=\$AZURE_TENANT_ID
terraform destroy --auto-approve \
-var 'location=$location'
"""
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.6.2
3.1.4
4 changes: 2 additions & 2 deletions main.tf → .terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data "external" "yaml" {

#Set up a personal resource group for the SE local to them
resource "azurerm_resource_group" "personal" {
name = "Sales-Engineer-${var.initials}"
name = "Sales-Engineer-Jenkins-${var.initials}"
location = var.location
}

Expand All @@ -26,7 +26,7 @@ resource "azurerm_container_group" "app" {

container {
name = "web"
image = "contrastsecuritydemo/railsgoat:1.0"
image = "contrastsecuritydemo/railsgoat:6.1.7"
cpu = "1"
memory = "1.5"
ports {
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions .terraform/parseyaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import yaml, json
with open('../contrast_security.yaml') as f:
config = yaml.safe_load(f)
print(json.dumps(config['api']))
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions 1-Build-Docker-Image.sh

This file was deleted.

6 changes: 0 additions & 6 deletions 2-Deploy-Docker-Image-To-Docker-Hub.sh

This file was deleted.

117 changes: 61 additions & 56 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
# TODO: change to slim or alpine
FROM ruby:2.6.2
# Multistage docker build which first builds and bundles all Ruby gems before
# creating build targets for the development and production images.

ARG username
ARG service_key
# Default Ruby version for this project.
ARG RUBY_VERSION=3.1.4

# Add build and runtime dependencies
# TODO: separate build & runtime and purge build dependencies at the end
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
# BASE STAGE
# Create a base ruby-alpine stage with common configuration
# that can be used in all other stages.
FROM ruby:$RUBY_VERSION-alpine as ruby-alpine

# Install phantomjs dependencies
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
bzip2 \
libfontconfig \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Set environment variables to be shared across all stages.
ENV GEM_HOME=/usr/local/bundle
ENV BUNDLE_PATH=$GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH
ENV RAILS_ENV development
ENV RACK_ENV development

# Install phantomjs & clean up
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
&& mkdir /tmp/phantomjs \
&& curl -L https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 \
| tar -xj --strip-components=1 -C /tmp/phantomjs \
&& cd /tmp/phantomjs \
&& mv bin/phantomjs /usr/local/bin \
&& cd \
&& apt-get purge --auto-remove -y \
curl \
&& apt-get clean \
&& rm -rf /tmp/* /var/lib/apt/lists/*
# Add basic packages that are shared across all stages
RUN apk add --no-cache \

Check warning on line 20 in Dockerfile

View check run for this annotation

Wiz Inc. (187287b22a) / Wiz IaC Scanner

Unpinned Package Version in Apk Add

Rule ID: f0b1168a-9724-44e2-9ecc-bf0d0f7bb7a9 Severity: Medium Resource: FROM={{ruby:$RUBY_VERSION-alpine as ruby-alpine}}.{{RUN apk add --no-cache nodejs tzdata}} Package version pinning reduces the range of versions that can be installed, reducing the chances of failure due to unanticipated changes
Raw output
Expected: RUN instruction with 'apk add <package>' should use package pinning form 'apk add <package>=<version>'
Found: RUN instruction apk add --no-cache     nodejs     tzdata does not use package pinning form
nodejs \
tzdata

# Build and package the app
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
# BUILDER STAGE
# Build all gems and dependencies in a builder stage,
# whch can then be copied to other stages.
FROM ruby-alpine as builder

# Add Contrast agent
RUN bundle add contrast-agent
# Add packages for required for building
RUN apk add --no-cache \

Check warning on line 30 in Dockerfile

View check run for this annotation

Wiz Inc. (187287b22a) / Wiz IaC Scanner

Unpinned Package Version in Apk Add

Rule ID: f0b1168a-9724-44e2-9ecc-bf0d0f7bb7a9 Severity: Medium Resource: FROM={{ruby-alpine as builder}}.{{RUN apk add --no-cache autoconf build-base libpq-dev mariadb-dev}} Package version pinning reduces the range of versions that can be installed, reducing the chances of failure due to unanticipated changes
Raw output
Expected: RUN instruction with 'apk add <package>' should use package pinning form 'apk add <package>=<version>'
Found: RUN instruction apk add --no-cache     autoconf     build-base     libpq-dev     mariadb-dev does not use package pinning form
autoconf \
build-base \
libpq-dev \
mariadb-dev

RUN bundle install
# Set the working directory for the app.
WORKDIR /app

ADD ./app /myapp/app
ADD ./config /myapp/config
ADD ./db /myapp/db
ADD ./doc /myapp/doc
ADD ./lib /myapp/lib
ADD ./log /myapp/log
ADD ./public /myapp/public
ADD ./script /myapp/script
ADD ./spec /myapp/spec
RUN mkdir /myapp/tmp
ADD ./vendor /myapp/vendor
ADD ./config.ru /myapp/config.ru
ADD ./entrypoint.sh /myapp/entrypoint.sh
ADD ./Rakefile /myapp/Rakefile
# Copy the Gemfile and Gemfile.lock files to the current directory.
COPY Gemfile* .

#Setup the database
RUN rails db:setup
# Install gems and remove any unnecessary build artifacts.
RUN bundle config force_ruby_platform true \
&& bundle install --jobs 4 --retry 3 \
&& rm -rf $BUNDLE_PATH/cache/*.gem \
&& rm -rf $BUNDLE_PATH/ruby/*/cache

# Make port 3000 available
# RUNNER STAGE
# Copy the needed gems and dependenies from the builder stage to
# create the final minimal image for running the app.
FROM ruby-alpine as runner

Check failure on line 51 in Dockerfile

View check run for this annotation

Wiz Inc. (187287b22a) / Wiz IaC Scanner

Missing User Instruction

Rule ID: bc86b2b4-bcfa-498f-90f0-f9a1354d6e01 Severity: High Resource: FROM={{ruby-alpine as runner}} A user should be specified in the dockerfile, otherwise the image will run as root
Raw output
Expected: The 'Dockerfile' should contain the 'USER' instruction
Found: The 'Dockerfile' does not contain any 'USER' instruction

Check notice on line 51 in Dockerfile

View check run for this annotation

Wiz Inc. (187287b22a) / Wiz IaC Scanner

Healthcheck Instruction Missing

Rule ID: 52476cb9-4a0d-4d31-8767-e9e8d440bb84 Severity: Low Resource: FROM={{ruby-alpine as runner}} Ensure that HEALTHCHECK is being used. The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working
Raw output
Expected: Dockerfile should contain instruction 'HEALTHCHECK'
Found: Dockerfile doesn't contain instruction 'HEALTHCHECK'

# Add packages required for running the app
RUN apk add --no-cache \

Check warning on line 54 in Dockerfile

View check run for this annotation

Wiz Inc. (187287b22a) / Wiz IaC Scanner

Unpinned Package Version in Apk Add

Rule ID: f0b1168a-9724-44e2-9ecc-bf0d0f7bb7a9 Severity: Medium Resource: FROM={{ruby-alpine as runner}}.{{RUN apk add --no-cache chromium chromium-chromedriver libpq mariadb}} Package version pinning reduces the range of versions that can be installed, reducing the chances of failure due to unanticipated changes
Raw output
Expected: RUN instruction with 'apk add <package>' should use package pinning form 'apk add <package>=<version>'
Found: RUN instruction apk add --no-cache     chromium     chromium-chromedriver     libpq     mariadb does not use package pinning form
chromium \
chromium-chromedriver \
libpq \
mariadb

# Set the working directory for the app.
WORKDIR /app

# Copy the bundle directory from the "builder" image
# and copy all other files to the current directory.
COPY --from=builder $BUNDLE_PATH $BUNDLE_PATH
COPY . .

# Expose port 3000 for the application.
EXPOSE 3000

# Start the app server
ENTRYPOINT [ "/bin/bash", "-c", "/myapp/entrypoint.sh"]
# Run the command to start the Rails server.
ENTRYPOINT ["/bin/sh"]
CMD ["/app/entrypoint.sh"]
Loading