git » kxd » commit e7b953e

kxd: Remove unnecessary function NameToString

author Alberto Bertogli
2024-09-08 10:13:22 UTC
committer Alberto Bertogli
2024-09-08 10:14:19 UTC
parent 8a9f6a3eb4decc556ea0d9ae36cf0d4b5435f41e

kxd: Remove unnecessary function NameToString

The function NameToString converts a pkix.Name to a human-friendly string,
however the default .String method on it is already good enough, and
very similar in practice to NameToString anyway.

So this patch removes the NameToString function to simplify the code,
replacing it with pkix.Name.String.

kxd/email.go +1 -25
kxd/hook.go +1 -1

diff --git a/kxd/email.go b/kxd/email.go
index 64de4ef..529cdba 100644
--- a/kxd/email.go
+++ b/kxd/email.go
@@ -3,8 +3,6 @@ package main
 import (
 	"bytes"
 	"crypto/x509"
-	"crypto/x509/pkix"
-	"fmt"
 	"net/smtp"
 	"strings"
 	"text/template"
@@ -34,7 +32,7 @@ On: {{.TimeString}}
 
 Client certificate:
   Signature: {{printf "%.16s" (printf "%x" .Cert.Signature)}}...
-  Subject: {{NameToString .Cert.Subject}}
+  Subject: {{.Cert.Subject}}
 
 Authorizing chains:
 {{range .Chains}}  {{ChainToString .}}
@@ -46,34 +44,12 @@ var emailTmpl = template.New("email")
 
 func init() {
 	emailTmpl.Funcs(map[string]interface{}{
-		"NameToString":  NameToString,
 		"ChainToString": ChainToString,
 	})
 
 	template.Must(emailTmpl.Parse(emailTmplBody))
 }
 
-// NameToString converts a pkix.Name from a certificate to a human-friendly
-// string.
-func NameToString(name pkix.Name) string {
-	s := make([]string, 0)
-	for _, c := range name.Country {
-		s = append(s, fmt.Sprintf("C=%s", c))
-	}
-	for _, o := range name.Organization {
-		s = append(s, fmt.Sprintf("O=%s", o))
-	}
-	for _, o := range name.OrganizationalUnit {
-		s = append(s, fmt.Sprintf("OU=%s", o))
-	}
-
-	if name.CommonName != "" {
-		s = append(s, fmt.Sprintf("N=%s", name.CommonName))
-	}
-
-	return strings.Join(s, " ")
-}
-
 // SendMail sends an email notifying of an access to the given key.
 func SendMail(kc *KeyConfig, req *Request,
 	chains [][]*x509.Certificate) error {
diff --git a/kxd/hook.go b/kxd/hook.go
index 9d3405d..61be704 100644
--- a/kxd/hook.go
+++ b/kxd/hook.go
@@ -55,7 +55,7 @@ func RunHook(kc *KeyConfig, req *Request, chains [][]*x509.Certificate) error {
 	cmd.Env = append(cmd.Env,
 		fmt.Sprintf("CLIENT_CERT_SIGNATURE=%x", clientCert.Signature))
 	cmd.Env = append(cmd.Env,
-		"CLIENT_CERT_SUBJECT="+NameToString(clientCert.Subject))
+		"CLIENT_CERT_SUBJECT="+clientCert.Subject.String())
 
 	for i, chain := range chains {
 		cmd.Env = append(cmd.Env,