Skip to content

Commit a852c44

Browse files
authored
Merge pull request #576 from jlledom/THREESCALE-12122-test-ssl
THREESCALE 12122: Fix tests suite with SSL
2 parents 9f5f160 + 0050367 commit a852c44

2 files changed

Lines changed: 126 additions & 27 deletions

File tree

.circleci/config.yml

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
11
version: 2.1
2+
3+
commands:
4+
bundle_install:
5+
steps:
6+
- run:
7+
name: bundle install
8+
command: |
9+
bundle config set --local force_ruby_platform true
10+
bundle config set --local deployment 'true'
11+
bundle config set --local path 'vendor/bundle'
12+
bundle install --jobs $(grep -c processor /proc/cpuinfo) --retry 3
13+
14+
boot_zync:
15+
steps:
16+
- run:
17+
name: boot zync
18+
command: |
19+
BUNDLE_WITHOUT=development:test bundle exec bin/rails runner --environment=production 'puts Rails.env'
20+
21+
setup_db:
22+
steps:
23+
- run:
24+
name: Set up the DB
25+
command: |
26+
bundle exec bin/rails db:wait db:setup
27+
28+
run_tests:
29+
steps:
30+
- run:
31+
name: rails test
32+
command: |
33+
circleci tests glob "test/**/*_test.rb" | circleci tests run --command="xargs bundle exec rake test TESTOPTS='-v'" --verbose --split-by=timings
34+
35+
run_license_finder:
36+
steps:
37+
- run:
38+
name: license_finder
39+
command: |
40+
bundle exec license_finder
41+
242
jobs:
343
docker-build:
444
resource_class: small
@@ -37,50 +77,98 @@ jobs:
3777
RAILS_ENV: test
3878
DISABLE_SPRING: 1 # we can't really run spring as it hangs on local circleci build
3979
DATABASE_URL: postgres://postgres:@localhost/circle_test
80+
SECRET_KEY_BASE: test
4081
steps:
4182
- checkout
42-
43-
# Restore bundle cache
4483
- restore_cache:
4584
keys:
4685
- zync-bundle-v2-{{ .Environment.CACHE_VERSION }}-{{ arch }}-{{ checksum "Gemfile.lock" }}
47-
48-
- run:
49-
name: bundle install
50-
command: |
51-
bundle config --local force_ruby_platform true
52-
bundle config set --local deployment 'true'
53-
bundle config set --local path 'vendor/bundle'
54-
bundle install --jobs $(grep -c processor /proc/cpuinfo) --retry 3
55-
- run:
56-
name: boot zync
57-
command: SECRET_KEY_BASE=test BUNDLE_WITHOUT=development:test bundle exec bin/rails runner --environment=production 'puts Rails.env'
58-
86+
- bundle_install
5987
- save_cache:
6088
key: zync-bundle-v2-{{ .Environment.CACHE_VERSION }}-{{ arch }}-{{ checksum "Gemfile.lock" }}
6189
paths:
6290
- vendor/bundle
91+
- boot_zync
92+
- setup_db
93+
- run_tests
94+
- run_license_finder
95+
- store_test_results:
96+
path: test/reports
6397

98+
build_ssl:
99+
parameters:
100+
postgresql_image:
101+
type: string
102+
working_directory: /opt/app-root/src
103+
docker:
104+
- image: quay.io/3scale/zync:ci-builder-ruby-3.3
105+
- image: << parameters.postgresql_image >>
106+
command:
107+
- bash
108+
- -c
109+
- |
110+
openssl req -nodes -new -x509 -subj "/CN=localhost" -keyout /server.key -out /server.crt -days 365 2>/dev/null
111+
chown postgres:postgres /server.key /server.crt
112+
chmod 600 /server.key
113+
# Configure pg_hba.conf to only allow SSL connections (hostssl instead of host)
114+
# local rule is needed for postgres internal initialization via unix socket
115+
echo "local all all trust" > /tmp/pg_hba.conf
116+
echo "hostssl all all all trust" >> /tmp/pg_hba.conf
117+
exec docker-entrypoint.sh postgres -c ssl=on -c ssl_cert_file=/server.crt -c ssl_key_file=/server.key -c hba_file=/tmp/pg_hba.conf
118+
environment:
119+
POSTGRES_HOST_AUTH_METHOD: trust
120+
POSTGRES_DB: circle_test
121+
environment:
122+
RAILS_ENV: test
123+
DISABLE_SPRING: 1
124+
DATABASE_URL: postgres://postgres:@localhost/circle_test
125+
DATABASE_SSL_MODE: verify-full
126+
SECRET_KEY_BASE: test
127+
steps:
128+
- checkout
64129
- run:
65-
name: Set up the DB
66-
command: bundle exec bin/rails db:wait db:setup
67-
130+
name: Wait for PostgreSQL to be ready
131+
command: |
132+
for i in $(seq 1 30); do
133+
if openssl s_client -starttls postgres -connect localhost:5432 </dev/null 2>&1 | grep -q "SSL handshake"; then
134+
echo "PostgreSQL SSL is ready"
135+
exit 0
136+
fi
137+
echo "Waiting for PostgreSQL SSL... ($i/30)"
138+
sleep 1
139+
done
140+
echo "PostgreSQL failed to start with SSL"
141+
exit 1
68142
- run:
69-
name: rails test
143+
name: Extract server CA certificate and generate client certificate
70144
command: |
71-
circleci tests glob "test/**/*_test.rb" | circleci tests run --command="xargs bundle exec rake test TESTOPTS='-v'" --verbose --split-by=timings
145+
mkdir -p .ssl
146+
# Extract the server certificate (CA) using openssl s_client
147+
openssl s_client -starttls postgres -connect localhost:5432 -showcerts </dev/null 2>/dev/null \
148+
| openssl x509 -outform PEM > .ssl/ca.crt
149+
# Generate client key and certificate (self-signed, PostgreSQL doesn't verify client certs by default)
150+
openssl req -nodes -new -x509 -subj "/CN=postgres" -keyout .ssl/client.key -out .ssl/client.crt -days 365 2>/dev/null
151+
chmod 600 .ssl/client.key
72152
- run:
73-
name: license_finder
153+
name: Set SSL environment variables
74154
command: |
75-
bundle exec license_finder
76-
77-
- store_test_results:
78-
path: test/reports
79-
155+
echo 'export DATABASE_SSL_CA=/opt/app-root/src/.ssl/ca.crt' >> "$BASH_ENV"
156+
echo 'export DATABASE_SSL_CERT=/opt/app-root/src/.ssl/client.crt' >> "$BASH_ENV"
157+
echo 'export DATABASE_SSL_KEY=/opt/app-root/src/.ssl/client.key' >> "$BASH_ENV"
158+
- restore_cache:
159+
keys:
160+
- zync-bundle-v2-{{ .Environment.CACHE_VERSION }}-{{ arch }}-{{ checksum "Gemfile.lock" }}
161+
- bundle_install
80162
- save_cache:
81-
key: zync-branch-v2-{{ arch }}-{{ .Branch }}
163+
key: zync-bundle-v2-{{ .Environment.CACHE_VERSION }}-{{ arch }}-{{ checksum "Gemfile.lock" }}
82164
paths:
83165
- vendor/bundle
166+
- boot_zync
167+
- setup_db
168+
- run_tests
169+
- run_license_finder
170+
- store_test_results:
171+
path: test/reports
84172

85173
workflows:
86174
version: 2.1
@@ -90,4 +178,8 @@ workflows:
90178
matrix:
91179
parameters:
92180
postgresql_image: [ "cimg/postgres:14.19", "cimg/postgres:15.14", "cimg/postgres:16.10", "cimg/postgres:17.6", "cimg/postgres:18.0" ]
181+
- build_ssl:
182+
matrix:
183+
parameters:
184+
postgresql_image: [ "cimg/postgres:14.19", "cimg/postgres:15.14", "cimg/postgres:16.10", "cimg/postgres:17.6", "cimg/postgres:18.0" ]
93185
- docker-build

config/initializers/que.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515

1616
def Que.start!
1717
require 'que/locker'
18+
require 'que/db_connection_url'
1819

1920
# Workaround for https://github.com/chanks/que/pull/192
2021
require 'active_record/base'
21-
Que.locker = Que::Locker.new(**Rails.application.config.x.que)
22+
23+
# Build connection URL with SSL parameters from database config
24+
# Workaround for https://github.com/que-rb/que/issues/442
25+
db_config = ActiveRecord::Base.connection_db_config.configuration_hash
26+
connection_url = Que::DBConnectionURL.build_connection_url(db_config)
27+
28+
Que.locker = Que::Locker.new(connection_url: connection_url, **Rails.application.config.x.que)
2229
end
2330

2431
def Que.stop!

0 commit comments

Comments
 (0)