kube CSR: preload UI fields

This commit is contained in:
Mikaël Cluseau
2026-07-20 12:20:47 +02:00
parent 4d1deeb773
commit d2aefba82e
5 changed files with 76 additions and 2 deletions
+38
View File
@@ -2,6 +2,8 @@ package main
import (
"bytes"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log"
@@ -355,3 +357,39 @@ func parseCertDuration(d string, now time.Time) (t time.Time, err error) {
return
}
type KubeCSRParseReq struct {
CSR string `json:"csr"`
}
type KubeCSRParseResp struct {
User string `json:"user,omitempty"`
Group string `json:"group,omitempty"`
}
func wsParseKubeCSR(req *restful.Request, resp *restful.Response) {
var r KubeCSRParseReq
if err := req.ReadEntity(&r); err != nil {
wsError(resp, err)
return
}
block, _ := pem.Decode([]byte(r.CSR))
if block == nil || block.Type != "CERTIFICATE REQUEST" {
wsError(resp, fmt.Errorf("invalid CSR PEM"))
return
}
cr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
wsError(resp, fmt.Errorf("parse CSR: %w", err))
return
}
out := KubeCSRParseResp{User: cr.Subject.CommonName}
if len(cr.Subject.Organization) > 0 {
out.Group = cr.Subject.Organization[0]
}
resp.WriteAsJson(out)
}