-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(certificates): load webhook certificates from database
- Loading branch information
Showing
10 changed files
with
216 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require_relative 'migration_helper' | ||
|
||
Sequel.migration do | ||
change do | ||
create_table(:certificates, charset: 'utf8') do | ||
primary_key :id | ||
String :uuid, null: false, unique: true, unique_constraint_name: 'uq_certificate_uuid' | ||
String :description, null: true | ||
String :content, null: false, type: PactBroker::MigrationHelper.large_text_type | ||
DateTime :created_at, null: false | ||
DateTime :updated_at, null: false | ||
end | ||
end | ||
end |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module PactBroker | ||
module Certificates | ||
class Certificate < Sequel::Model | ||
end | ||
|
||
Certificate.plugin :timestamps, update_on_create: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'pact_broker/certificates/certificate' | ||
require 'pact_broker/logging' | ||
require 'openssl' | ||
|
||
module PactBroker | ||
module Certificates | ||
module Service | ||
|
||
extend self | ||
extend PactBroker::Logging | ||
|
||
def cert_store | ||
cert_store = OpenSSL::X509::Store.new | ||
cert_store.set_default_paths | ||
find_all_certificates.each do | certificate | | ||
begin | ||
cert_store.add_cert(certificate) | ||
rescue StandardError => e | ||
log_error e, "Error adding certificate object #{certificate.to_s} to store" | ||
end | ||
end | ||
cert_store | ||
end | ||
|
||
def find_all_certificates | ||
Certificate.collect do | certificate | | ||
cert_arr = certificate.content.split(/(-----END [^\-]+-----)/).each_slice(2).map(&:join) | ||
cert_arr.collect do |c| | ||
begin | ||
OpenSSL::X509::Certificate.new(c) | ||
rescue StandardError => e | ||
log_error e, "Error creating certificate object from certificate #{certificate.uuid} '#{certificate.description}'" | ||
nil | ||
end | ||
end | ||
end.flatten.compact | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-----BEGIN CERTIFICATE----- | ||
foo | ||
-----END CERTIFICATE----- | ||
-----BEGIN CERTIFICATE----- | ||
MIIEXDCCA0SgAwIBAgIEOGO5ZjANBgkqhkiG9w0BAQUFADCBtDEUMBIGA1UEChML | ||
RW50cnVzdC5uZXQxQDA+BgNVBAsUN3d3dy5lbnRydXN0Lm5ldC9DUFNfMjA0OCBp | ||
bmNvcnAuIGJ5IHJlZi4gKGxpbWl0cyBsaWFiLikxJTAjBgNVBAsTHChjKSAxOTk5 | ||
IEVudHJ1c3QubmV0IExpbWl0ZWQxMzAxBgNVBAMTKkVudHJ1c3QubmV0IENlcnRp | ||
ZmljYXRpb24gQXV0aG9yaXR5ICgyMDQ4KTAeFw05OTEyMjQxNzUwNTFaFw0xOTEy | ||
MjQxODIwNTFaMIG0MRQwEgYDVQQKEwtFbnRydXN0Lm5ldDFAMD4GA1UECxQ3d3d3 | ||
LmVudHJ1c3QubmV0L0NQU18yMDQ4IGluY29ycC4gYnkgc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5Rof8886ZjXOP/swNlQ8C5LWK5Gb9Auw2DaclVy | ||
vUxFnmG6v4SBkgPR0ml8xQ== | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEhjCCA26gAwIBAgIJAOoR4cViLrYBMA0GCSqGSIb3DQEBBQUAMIGIMQswCQYD | ||
VQQGEwJBVTERMA8GA1UECBMIVmljdG9yaWExEjAQBgNVBAcTCU1lbGJvdXJuZTEY | ||
MBYGA1UEChMPUGFjdCBGb3VuZGF0aW9uMRUwEwYDVQQDEwxCZXRoIFNrdXJyaWUx | ||
ITAfBgkqhkiG9w0BCQEWEmJldGhAYmV0aGVzcXVlLmNvbTAeFw0xNzEyMDcwMDI1 | ||
MjZaFw00NTA0MjQwMDI1MjZaMIGIMQswCQYDVQQGEwJBVTERMA8GA1UECBMIVmlj | ||
dG9yaWExEjAQBgNVBAcTCU1lbGJvdXJuZTEYMBYGA1UEChMPUGFjdCBGb3VuZGF0 | ||
aW9uMRUwEwYDVQQDEwxCZXRoIFNrdXJyaWUxITAfBgkqhkiG9w0BCQEWEmJldGhA | ||
YmV0aGVzcXVlLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKli | ||
EwzIbbZdoDbuJPkMsdGihSYK6KQrLCsvFmNLEgDBKds387E5mQoOjDvlDHLkY+uq | ||
Lm10bI2MSnQAj9B+jqf48FNaoHq2A30iohmK2hPKsrIMi3eaWrXYm9+ZxnNUO/j6 | ||
/WKCsX/InUsZDd0J6F6HO9RZ+/AmfBC6fTuxKFgGENwsZSPIpi2JwBeEV+YCthPf | ||
yOBzgNyljd+BqvZCJK3+r074131TC4AEzWZsV33ipOTpcY3y8XAOa79npWUfnTOX | ||
E3NW1Jqrd9Ozky9+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BFhfGPjK50xA3B20qMo | ||
oPS7mmNz7W3lKtvtFKkrxjYR0CvrB4ul2p5cGZ1WEvVUKcgF7bISKo30Axv/55IQ | ||
h7A6tcOdBTcSo8f0FbnVpDkWm1M6I5HxqIKiaohowXkCIryqptau37AUX7iH0N18 | ||
f3v/rxzP5tsHrV7bhZ3QKw0z2wTR5klAEyt2+z7pnIkPFc4YsIV4IU9rTw76NmfN | ||
B/L/CNDi3tm/Kq+4h4YhPATKt5Rof8886ZjXOP/swNlQ8C5LWK5Gb9Auw2DaclVy | ||
vUxFnmG6v4SBkgPR0ml8xQ== | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'pact_broker/certificates/service' | ||
|
||
module PactBroker | ||
module Certificates | ||
describe Service do | ||
let(:certificate_content) { File.read('spec/fixtures/certificate.pem') } | ||
|
||
describe "#cert_store" do | ||
subject { Service.cert_store } | ||
|
||
it "returns an OpenSSL::X509::Store" do | ||
expect(subject).to be_instance_of(OpenSSL::X509::Store) | ||
end | ||
|
||
context "when there is a duplicate certificate" do | ||
before do | ||
Certificate.create(uuid: '1234', content: certificate_content) | ||
Certificate.create(uuid: '5678', content: certificate_content) | ||
end | ||
|
||
it "logs the error" do | ||
expect(PactBroker.logger).to receive(:error).with(/Error adding certificate/).at_least(1).times | ||
subject | ||
end | ||
|
||
it "returns an OpenSSL::X509::Store" do | ||
expect(subject).to be_instance_of(OpenSSL::X509::Store) | ||
end | ||
end | ||
end | ||
|
||
describe "#find_all_certificates" do | ||
let!(:certificate) do | ||
Certificate.create(uuid: '1234', content: certificate_content) | ||
end | ||
|
||
subject { Service.find_all_certificates } | ||
|
||
context "with a valid certificate file" do | ||
it "returns all the X509 Certificate objects" do | ||
expect(subject.size).to eq 2 | ||
end | ||
end | ||
|
||
context "with an invalid certificate file" do | ||
let(:certificate_content) { File.read('spec/fixtures/certificate-invalid.pem') } | ||
|
||
it "logs an error" do | ||
expect(PactBroker.logger).to receive(:error).with(/Error.*1234/) | ||
subject | ||
end | ||
|
||
it "returns all the valid X509 Certificate objects" do | ||
expect(subject.size).to eq 1 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters