-
Notifications
You must be signed in to change notification settings - Fork 92
Add skupper debug cert subcommand to inspect X.509 certificates #2575
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
Open
AryanP123
wants to merge
4
commits into
skupperproject:main
Choose a base branch
from
AryanP123:debug-cert-subcommand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed406d5
Add skupper debug cert subcommand to inspect X.509 certificates
AryanP123 aca857d
Fix formatting and address review feedback for debug cert
AryanP123 dc103aa
Address review feedback for debug cert-inspect
AryanP123 eb054ab
Address cert-inspect review feedback for signing and errors
AryanP123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,173 @@ | ||
| package cert | ||
|
|
||
| import ( | ||
| "crypto/ecdsa" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "text/tabwriter" | ||
| "time" | ||
|
|
||
| "github.com/skupperproject/skupper/internal/certs" | ||
| "github.com/skupperproject/skupper/internal/cmd/skupper/common/utils" | ||
| ) | ||
|
|
||
| // Info holds the decoded fields of an X.509 certificate for display. | ||
| type Info struct { | ||
| Name string `json:"name"` | ||
| Subject string `json:"subject"` | ||
| Issuer string `json:"issuer"` | ||
| SerialNumber string `json:"serialNumber"` | ||
| NotBefore string `json:"notBefore"` | ||
| NotAfter string `json:"notAfter"` | ||
| DNSNames []string `json:"dnsNames,omitempty"` | ||
| IPAddresses []string `json:"ipAddresses,omitempty"` | ||
| EmailAddresses []string `json:"emailAddresses,omitempty"` | ||
| URIs []string `json:"uris,omitempty"` | ||
| IsCA bool `json:"isCA"` | ||
| IsSigningCert *bool `json:"isSigningCert,omitempty"` | ||
| PublicKeyAlgorithm string `json:"publicKeyAlgorithm"` | ||
| PublicKeySize int `json:"publicKeySize"` | ||
| SignatureAlgorithm string `json:"signatureAlgorithm"` | ||
| Status string `json:"status,omitempty"` | ||
| CrExpiration string `json:"crExpiration,omitempty"` | ||
| } | ||
|
|
||
| func ParseCertificate(name string, certData []byte) (*Info, error) { | ||
| cert, err := certs.DecodeCertificate(certData) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return certToInfo(name, cert), nil | ||
| } | ||
|
|
||
| func ParseCertificateFile(name, path string) (*Info, error) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return ParseCertificate(name, data) | ||
| } | ||
|
|
||
| func certToInfo(name string, cert *x509.Certificate) *Info { | ||
| algo, size := publicKeyInfo(cert) | ||
| ips := make([]string, len(cert.IPAddresses)) | ||
| for i, ip := range cert.IPAddresses { | ||
| ips[i] = ip.String() | ||
| } | ||
| uris := make([]string, len(cert.URIs)) | ||
| for i, uri := range cert.URIs { | ||
| uris[i] = uri.String() | ||
| } | ||
| return &Info{ | ||
| Name: name, | ||
| Subject: formatName(cert.Subject), | ||
| Issuer: formatName(cert.Issuer), | ||
| SerialNumber: cert.SerialNumber.String(), | ||
| NotBefore: cert.NotBefore.Format(time.RFC3339), | ||
| NotAfter: cert.NotAfter.Format(time.RFC3339), | ||
| DNSNames: cert.DNSNames, | ||
| IPAddresses: ips, | ||
| EmailAddresses: cert.EmailAddresses, | ||
| URIs: uris, | ||
| IsCA: cert.IsCA, | ||
| PublicKeyAlgorithm: algo, | ||
| PublicKeySize: size, | ||
| SignatureAlgorithm: cert.SignatureAlgorithm.String(), | ||
| } | ||
| } | ||
|
|
||
| func formatName(name pkix.Name) string { | ||
| if name.CommonName != "" { | ||
| return name.CommonName | ||
| } | ||
| return name.String() | ||
| } | ||
|
|
||
| func publicKeyInfo(cert *x509.Certificate) (algorithm string, size int) { | ||
| switch pub := cert.PublicKey.(type) { | ||
| case *rsa.PublicKey: | ||
| return "RSA", pub.N.BitLen() | ||
| case *ecdsa.PublicKey: | ||
| return "ECDSA", pub.Curve.Params().BitSize | ||
| default: | ||
| return fmt.Sprintf("%T", cert.PublicKey), 0 | ||
| } | ||
| } | ||
|
|
||
| func formatSANs(info *Info) string { | ||
| var parts []string | ||
| parts = append(parts, info.DNSNames...) | ||
| parts = append(parts, info.IPAddresses...) | ||
| parts = append(parts, info.EmailAddresses...) | ||
| parts = append(parts, info.URIs...) | ||
| if len(parts) == 0 { | ||
| return "" | ||
| } | ||
| return strings.Join(parts, ", ") | ||
| } | ||
|
|
||
| // Display renders one or more certificates to stdout. | ||
| func Display(infos []Info, output string, detail bool) error { | ||
| if output != "" { | ||
| for _, info := range infos { | ||
| encoded, err := utils.Encode(output, info) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(encoded) | ||
| } | ||
| return nil | ||
| } | ||
| if detail && len(infos) == 1 { | ||
| displayDetail(infos[0]) | ||
| return nil | ||
| } | ||
| return displayList(infos) | ||
| } | ||
|
|
||
| func displayDetail(info Info) { | ||
| fmt.Printf("Name\t\t: %s\n", info.Name) | ||
| fmt.Printf("Subject\t\t: %s\n", info.Subject) | ||
| fmt.Printf("Issuer\t\t: %s\n", info.Issuer) | ||
| fmt.Printf("Serial Number\t: %s\n", info.SerialNumber) | ||
| fmt.Printf("Not Before\t: %s\n", info.NotBefore) | ||
| fmt.Printf("Not After\t: %s\n", info.NotAfter) | ||
| fmt.Printf("Is CA\t\t: %t\n", info.IsCA) | ||
| if info.IsSigningCert != nil { | ||
| fmt.Printf("Is Signing Cert\t: %t\n", *info.IsSigningCert) | ||
| } | ||
| fmt.Printf("SANs\t\t: %s\n", formatSANs(&info)) | ||
| if info.PublicKeySize > 0 { | ||
| fmt.Printf("Public Key\t: %s (%d bits)\n", info.PublicKeyAlgorithm, info.PublicKeySize) | ||
| } else { | ||
| fmt.Printf("Public Key\t: %s\n", info.PublicKeyAlgorithm) | ||
| } | ||
| fmt.Printf("Signature\t: %s\n", info.SignatureAlgorithm) | ||
| if info.Status != "" { | ||
| fmt.Printf("Status\t\t: %s\n", info.Status) | ||
| } | ||
| if info.CrExpiration != "" { | ||
| fmt.Printf("CR Expiration\t: %s\n", info.CrExpiration) | ||
| } | ||
| } | ||
|
|
||
| func displayList(infos []Info) error { | ||
| if len(infos) == 0 { | ||
| fmt.Println("No certificates found") | ||
| return nil | ||
| } | ||
| writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) | ||
| fmt.Fprintln(writer, "NAME\tSUBJECT\tISSUER\tNOT AFTER\tSANs") | ||
| for _, info := range infos { | ||
| sans := formatSANs(&info) | ||
| if len(sans) > 60 { | ||
| sans = sans[:57] + "..." | ||
| } | ||
| fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s\n", info.Name, info.Subject, info.Issuer, info.NotAfter, sans) | ||
| } | ||
| return writer.Flush() | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| package cert | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/skupperproject/skupper/internal/certs" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestParseCertificate(t *testing.T) { | ||
| caSecret, err := certs.GenerateSecret("test-ca", "test-ca.example.com", nil, 0, nil) | ||
| assert.NilError(t, err) | ||
|
|
||
| leafSecret, err := certs.GenerateSecret("test-cert", "test.example.com", []string{"test.example.com"}, 86400000000000, caSecret) | ||
| assert.NilError(t, err) | ||
|
|
||
| info, err := ParseCertificate("test-cert", leafSecret.Data["tls.crt"]) | ||
| assert.NilError(t, err) | ||
|
|
||
| assert.Equal(t, "test-cert", info.Name) | ||
| assert.Equal(t, "test.example.com", info.Subject) | ||
| assert.Equal(t, "test-ca.example.com", info.Issuer) | ||
| assert.Equal(t, "RSA", info.PublicKeyAlgorithm) | ||
| assert.Equal(t, 2048, info.PublicKeySize) | ||
| assert.Assert(t, len(info.NotBefore) > 0) | ||
| assert.Assert(t, len(info.NotAfter) > 0) | ||
| assert.DeepEqual(t, []string{"test.example.com"}, info.DNSNames) | ||
| } | ||
|
|
||
| func TestParseCertificateInvalidPEM(t *testing.T) { | ||
| _, err := ParseCertificate("bad", []byte("not a pem")) | ||
| assert.ErrorContains(t, err, "Could not decode PEM block") | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.