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

Allow SP certificates to be OpenSSL::X509::Certificate #726

Open
wants to merge 3 commits into
base: v2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 19 additions & 8 deletions lib/ruby_saml/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,33 @@ def get_all_sp_certs

# Validate certificate, certificate_new, private_key, and sp_cert_multi params.
def validate_sp_certs_params!
multi = sp_cert_multi && !sp_cert_multi.empty?
cert = certificate && !certificate.empty?
cert_new = certificate_new && !certificate_new.empty?
pk = private_key && !private_key.empty?
if multi && (cert || cert_new || pk)
has_multi = sp_cert_multi && !sp_cert_multi.empty?
has_pk = private_key && !private_key.empty?
if has_multi && (cert?(certificate) || cert?(certificate_new) || has_pk)
raise ArgumentError.new("Cannot specify both sp_cert_multi and certificate, certificate_new, private_key parameters")
end
end

# Check if a certificate is present.
def cert?(cert)
return true if cert.is_a?(OpenSSL::X509::Certificate)

cert && !cert.empty?
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

all changes in this file can be rolled back

Copy link
Author

@tobiasamft tobiasamft Oct 8, 2024

Choose a reason for hiding this comment

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

Without it we get the initial NoMethodError again (because validate_sp_certs_params is called before Utils.build_cert_object):

NoMethodError: undefined method `empty?' for an instance of OpenSSL::X509::Certificate
    lib/ruby_saml/settings.rb:377:in `validate_sp_certs_params!'

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK I will check this later, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

In what status is this?


# Get certs from certificate, certificate_new, and private_key parameters.
def get_sp_certs_single
certs = { :signing => [], :encryption => [] }

sp_key = RubySaml::Utils.build_private_key_object(private_key)
cert = RubySaml::Utils.build_cert_object(certificate)
cert = build_cert_object(certificate)
if cert || sp_key
ary = [cert, sp_key].freeze
certs[:signing] << ary
certs[:encryption] << ary
end

cert_new = RubySaml::Utils.build_cert_object(certificate_new)
cert_new = build_cert_object(certificate_new)
if cert_new
ary = [cert_new, sp_key].freeze
certs[:signing] << ary
Expand Down Expand Up @@ -429,7 +434,7 @@ def get_sp_certs_multi
end

certs[type] << [
RubySaml::Utils.build_cert_object(cert),
build_cert_object(cert),
RubySaml::Utils.build_private_key_object(key)
].freeze
end
Expand All @@ -438,5 +443,11 @@ def get_sp_certs_multi
certs.each { |_, ary| ary.freeze }
certs
end

def build_cert_object(cert)
return cert if cert.is_a?(OpenSSL::X509::Certificate)

OneLogin::RubySaml::Utils.build_cert_object(cert)
Copy link
Collaborator

@johnnyshields johnnyshields Oct 8, 2024

Choose a reason for hiding this comment

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

let's just change this method in OneLogin::RubySaml::Utils to return a cert if one is given as an argument (it should be "idempotent")

Same with RubySaml::Utils.build_private_key_object

Copy link
Author

Choose a reason for hiding this comment

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

👍 Your code, your decision 😄 Will change that.

end
end
end
41 changes: 41 additions & 0 deletions test/settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,47 @@ class SettingsTest < Minitest::Test
end
end

it "handles OpenSSL::X509::Certificate objects for single case" do
@settings.certificate = OpenSSL::X509::Certificate.new(cert_text1)
@settings.private_key = key_text1

actual = @settings.get_sp_certs
expected = [[cert_text1, key_text1]]
assert_equal [:signing, :encryption], actual.keys
assert_equal expected, actual[:signing].map {|ary| ary.map(&:to_pem) }
assert_equal expected, actual[:encryption].map {|ary| ary.map(&:to_pem) }
end

it "handles OpenSSL::X509::Certificate objects for single case with new cert" do
@settings.certificate = cert_text1
@settings.certificate_new = OpenSSL::X509::Certificate.new(cert_text2)
@settings.private_key = key_text1

actual = @settings.get_sp_certs
expected = [[cert_text1, key_text1], [cert_text2, key_text1]]
assert_equal [:signing, :encryption], actual.keys
assert_equal expected, actual[:signing].map {|ary| ary.map(&:to_pem) }
assert_equal expected, actual[:encryption].map {|ary| ary.map(&:to_pem) }
end

it "handles OpenSSL::X509::Certificate objects for multi case" do
x509_certificate1 = OpenSSL::X509::Certificate.new(cert_text1)
x509_certificate2 = OpenSSL::X509::Certificate.new(cert_text2)
@settings.sp_cert_multi = {
signing: [{ certificate: x509_certificate1, private_key: key_text1 },
{ certificate: cert_text2, private_key: key_text1 }],
encryption: [{ certificate: x509_certificate2, private_key: key_text1 },
{ certificate: cert_text3, private_key: key_text2 }]
}

actual = @settings.get_sp_certs
expected_signing = [[cert_text1, key_text1], [cert_text2, key_text1]]
expected_encryption = [[cert_text2, key_text1], [cert_text3, key_text2]]
assert_equal [:signing, :encryption], actual.keys
assert_equal expected_signing, actual[:signing].map {|ary| ary.map(&:to_pem) }
assert_equal expected_encryption, actual[:encryption].map {|ary| ary.map(&:to_pem) }
end

it "sp_cert_multi allows sending only signing" do
@settings.sp_cert_multi = {
signing: [{ certificate: cert_text1, private_key: key_text1 },
Expand Down
Loading