Files
dokku/plugins/certs/report_test.go
T
Jose Diaz-Gonzalez 326607e356 feat: add fingerprint and serial to certs:report
The report described an installed certificate without carrying anything that identified it, so the only exact way to tell whether an app was serving a given certificate was to pull the full PEM back with `certs:show` and compare bytes. That moves certificate material off the server on every check, and the same command with `key` returns the private key. The new `--ssl-fingerprint` and `--ssl-serial` flags answer the same question in a single report read that discloses nothing sensitive. The sample output in the ssl documentation has also been corrected to match what the command actually prints.
2026-09-02 03:56:02 -04:00

142 lines
4.3 KiB
Go

package certs
import (
"reflect"
"testing"
)
func TestSubjectCommonName(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{"rfc2253 cn only", "subject=CN=dokku.me", "dokku.me"},
{"rfc2253 multi rdn reversed", "subject=CN=node-js-app.dokku.me,OU=Operations,O=Expa,L=San Francisco,ST=California,C=US", "node-js-app.dokku.me"},
{"rfc2253 wildcard", "subject=CN=*.dokku.me", "*.dokku.me"},
{"openssl 3.x default spaced", "subject=CN = cn-only.example.com", "cn-only.example.com"},
{"openssl 3.x default multi rdn", "subject=C=US, ST=California, L=San Francisco, O=Expa, OU=Operations, CN=node-js-app.dokku.me", "node-js-app.dokku.me"},
{"legacy compat slash prefix", "subject=/CN=dokku.me", "dokku.me"},
{"no subject prefix", "CN=dokku.me", "dokku.me"},
{"no common name", "subject=OU=Operations,O=Expa,C=US", ""},
{"empty", "", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := subjectCommonName(tc.in); got != tc.want {
t.Errorf("subjectCommonName(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
const sanCertText = `Certificate:
Data:
X509v3 extensions:
X509v3 Subject Alternative Name:
DNS:www.test.dokku.me, DNS:www.test.app.dokku.me
`
func TestSSLHostnames(t *testing.T) {
cases := []struct {
name string
subject string
certText string
want []string
}{
{
name: "cn only no san",
subject: "subject=CN=dokku.me",
want: []string{"dokku.me"},
},
{
name: "cn plus sans sorted",
subject: "subject=CN=test.dokku.me",
certText: sanCertText,
want: []string{"test.dokku.me", "www.test.app.dokku.me", "www.test.dokku.me"},
},
{
name: "sans only no cn",
subject: "subject=OU=Operations",
certText: sanCertText,
want: []string{"www.test.app.dokku.me", "www.test.dokku.me"},
},
{
name: "dedupes cn present in san",
subject: "subject=CN=www.test.dokku.me",
certText: sanCertText,
want: []string{"www.test.app.dokku.me", "www.test.dokku.me"},
},
{
name: "no cn no san",
subject: "subject=OU=Operations",
want: []string{},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := sslHostnames(tc.subject, tc.certText); !reflect.DeepEqual(got, tc.want) {
t.Errorf("sslHostnames(%q, ...) = %#v, want %#v", tc.subject, got, tc.want)
}
})
}
}
func TestFormatSSLSubject(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{"compat single", "subject=/CN=dokku.me", "CN=dokku.me"},
{
"compat multi rdn",
"subject=/C=US/ST=California/L=San Francisco/O=Expa/OU=Operations/CN=node-js-app.dokku.me",
"C=US; ST=California; L=San Francisco; O=Expa; OU=Operations; CN=node-js-app.dokku.me",
},
{
"compat wildcard multi rdn",
"subject=/OU=Domain Control Validated/OU=PositiveSSL Wildcard/CN=*.dokku.me",
"OU=Domain Control Validated; OU=PositiveSSL Wildcard; CN=*.dokku.me",
},
{"legacy spaced prefix", "subject= /CN=dokku.me", "CN=dokku.me"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := formatSSLSubject(tc.in); got != tc.want {
t.Errorf("formatSSLSubject(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestFormatSSLHexField(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{"fingerprint openssl 1.x label", "SHA256 Fingerprint=B7:DF:D5:84:C6:2E:27:BF", "B7:DF:D5:84:C6:2E:27:BF"},
{"fingerprint openssl 3.x label", "sha256 Fingerprint=B7:DF:D5:84:C6:2E:27:BF", "B7:DF:D5:84:C6:2E:27:BF"},
{"fingerprint lowercase digest", "sha256 Fingerprint=b7:df:d5:84:c6:2e:27:bf", "B7:DF:D5:84:C6:2E:27:BF"},
{"serial", "serial=322844AD8CD6D4FF76B05C50833AB91DEEDA2AD1", "322844AD8CD6D4FF76B05C50833AB91DEEDA2AD1"},
{"serial shorter", "serial=C46823E5D7C09FC9", "C46823E5D7C09FC9"},
{"serial lowercase", "serial=c46823e5d7c09fc9", "C46823E5D7C09FC9"},
{"serial negative", "serial=-1F2E", "-1F2E"},
{"surrounding whitespace", " serial=C46823E5D7C09FC9 \n", "C46823E5D7C09FC9"},
{"no separator", "unable to load certificate", ""},
{"empty", "", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := formatSSLHexField(tc.in); got != tc.want {
t.Errorf("formatSSLHexField(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}