Skip to content

Commit

Permalink
correctly exit non zero when the SAN doesn't match
Browse files Browse the repository at this point in the history
(cherry picked from commit 03655d0)
  • Loading branch information
evgeni authored and ekohl committed Sep 17, 2024
1 parent 3a55f80 commit b1eaefa
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
10 changes: 2 additions & 8 deletions bin/katello-certs-check
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,9 @@ function check-cert-san () {
return
fi
done
error
echo "The $CERT_FILE does not have a Subject Alt Name matching the Subject CN"
error 11 "The $CERT_FILE does not have a Subject Alt Name matching the Subject CN"
else
error
cat <<Explanation
$CERT_FILE does not contain a Subject Alt Name. Common Name is deprecated, use Subject Alt Name instead.
See: https://tools.ietf.org/html/rfc2818#section-3.1
Explanation
error 11 "The $CERT_FILE does not contain a Subject Alt Name. Common Name is deprecated, use Subject Alt Name instead. See: https://tools.ietf.org/html/rfc2818#section-3.1"
fi
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDXjCCAkagAwIBAgIUHYl6NxLcZpBuyTUI4LaWJOaM8iAwDQYJKoZIhvcNAQEL
BQAwHjEcMBoGA1UEAwwTVGVzdCBTZWxmLVNpZ25lZCBDQTAeFw0yNDA5MTYxMjU2
NTBaFw0zNDA5MTQxMjU2NTBaMCYxJDAiBgNVBAMMG2ZvcmVtYW4tYmFkLXNhbi5l
eGFtcGxlLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALfDETt1
UPBqx5dr7cVywZ2wz5s8SyLLtbcXEqXxcS38kJCeFEh2WsSYrNp9MfA6ORZcp0AG
ZiAnXNPLSNteBCiGkk3TNiq0VY1M8iph75uDPrJBu+whlMhYrJQ00jLxh5lAD8Xx
a4s6AnwKniQTXBaX4pybRQn4BE2PuXey+oBn6vPlYjGBn2LvUeq9uR2IahRScKGM
DkkOEX+h251EGSxAdMpciOpXJullMHU55RP6WWubaRosh6ZZbIqg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Ij9tV33GLg2JZ0SKXVKSCEEkCg3hUJ3/Eze5RJlcqT4sNMtg4XmhZ3
OIscMZU55ezoNBnnaIHzKtzRtyy8obGE7RhLgq+6JQKBgQDQ2xskB8dob6R4esLy
s2E3kf2OmVswllK15H36u6FkS1h/AxiaRTUTkbmvpiobdqDAAKGlZGxF8hck4wmq
+3uZWUcdbO67ld/P0xiP3RNwSOpQ7WZsv7zKGnCaungxGPvqNp5Dm7RTqR+kQtKV
A/FM9fRgYNuJPmgpee84o2Dk4Q==
-----END PRIVATE KEY-----
10 changes: 10 additions & 0 deletions spec/fixtures/katello-certs-check/create_cert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ else
echo "CA certificate bundle with trust rules exists. Skipping."
fi

CERT_NAME=foreman-bad-san.example.com
if [[ ! -f "$CERTS_DIR/$CERT_NAME.key" || ! -f "$CERTS_DIR/$CERT_NAME.crt" ]]; then
echo "Generate server certificate"
openssl genrsa -out $CERTS_DIR/$CERT_NAME.key 2048
openssl req -new -key $CERTS_DIR/$CERT_NAME.key -out $CERTS_DIR/$CERT_NAME.csr -subj "/CN=${CERT_NAME}"
openssl x509 -req -in $CERTS_DIR/$CERT_NAME.csr -CA $CERTS_DIR/$CA_CERT_NAME.crt -CAkey $CERTS_DIR/$CA_CERT_NAME.key -CAcreateserial -out $CERTS_DIR/$CERT_NAME.crt -days 3650 -sha256 -extfile extensions.txt -extensions extensions
else
echo "Server certificate with bad SAN exists. Skipping."
fi

CERT_NAME=foreman.example.com
if [[ ! -f "$CERTS_DIR/$CERT_NAME.key" || ! -f "$CERTS_DIR/$CERT_NAME.crt" ]]; then
echo "Generate server certificate"
Expand Down
14 changes: 13 additions & 1 deletion spec/katello_certs_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ def fixture(filename)
command_with_certs = "#{command} -b #{ca} -k #{key} -c #{cert}"
_stdout, stderr, status = Open3.capture3(command_with_certs)
expect(stderr).to include 'does not verify'
expect(status.exitstatus).to eq 4
expect(status.exitstatus).to eq 15 # the code for invalid is 4, but the cert is also failing the SAN check, making it 15
end
end

context 'with invalid SAN server certificates' do
let(:key) { File.join(certs_directory, 'foreman-bad-san.example.com.key') }
let(:cert) { File.join(certs_directory, 'foreman-bad-san.example.com.crt') }

it 'fails if purpose is not sslserver' do
command_with_certs = "#{command} -b #{ca} -k #{key} -c #{cert}"
_stdout, stderr, status = Open3.capture3(command_with_certs)
expect(stderr).to include 'does not have a Subject Alt Name matching the Subject CN'
expect(status.exitstatus).to eq 11
end
end

Expand Down

0 comments on commit b1eaefa

Please sign in to comment.