-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add attribute support for certificate subject #129
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ import ( | |
|
||
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" | ||
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1" | ||
cmpki "github.com/cert-manager/cert-manager/pkg/util/pki" | ||
|
||
"github.com/cert-manager/csi-lib/manager" | ||
"github.com/cert-manager/csi-lib/metadata" | ||
|
||
|
@@ -56,32 +58,58 @@ func RequestForMetadata(meta metadata.Metadata) (*manager.CertificateRequestBund | |
} | ||
} | ||
|
||
commonName, err := expand(meta, attrs[csiapi.CommonNameKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.CommonNameKey, err) | ||
var request = &x509.CertificateRequest{} | ||
if lSubjStr, ok := attrs[csiapi.LiteralSubjectKey]; ok && len(lSubjStr) > 0 { | ||
lSubjStr, err = expand(meta, lSubjStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.LiteralSubjectKey, err) | ||
} | ||
request.RawSubject, err = cmpki.ParseSubjectStringToRawDerBytes(lSubjStr) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.LiteralSubjectKey, err) | ||
} | ||
} else { | ||
request.Subject = pkix.Name{} | ||
request.Subject.CommonName, err = expand(meta, attrs[csiapi.CommonNameKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.CommonNameKey, err) | ||
} | ||
if len(attrs[csiapi.SerialNumberKey]) > 0 { | ||
request.Subject.SerialNumber = attrs[csiapi.SerialNumberKey] | ||
} | ||
for k, v := range map[*[]string]string{ | ||
&request.Subject.Organization: csiapi.OrganizationsKey, | ||
&request.Subject.OrganizationalUnit: csiapi.OrganizationalUnitsKey, | ||
&request.Subject.Country: csiapi.CountriesKey, | ||
&request.Subject.Province: csiapi.ProvincesKey, | ||
&request.Subject.Locality: csiapi.LocalitiesKey, | ||
&request.Subject.StreetAddress: csiapi.StreetAddressesKey, | ||
&request.Subject.PostalCode: csiapi.PostalCodesKey, | ||
} { | ||
if len(attrs[v]) > 0 { | ||
var e, err = expand(meta, attrs[v]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", v, err) | ||
} | ||
*k = strings.Split(e, ",") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: I remember that you commented on a PR implementing a similar feature in cert-manager: cert-manager/cert-manager#4502 (comment) I think we're likely to need a similar approach here as I suggested in there. CSV parsing seems like a good solution. Example test string:
Should produce exactly 3 street address entries There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @inteon Hey, I unfortunately don't have the time to revisit this and my team ended up scraping the associated project. Anyone is welcome to pick up where I left off, otherwise it will probably be a few months until I can carve out time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for letting us know! |
||
} | ||
} | ||
} | ||
dns, err := parseDNSNames(meta, attrs[csiapi.DNSNamesKey]) | ||
request.DNSNames, err = parseDNSNames(meta, attrs[csiapi.DNSNamesKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.DNSNamesKey, err) | ||
} | ||
uris, err := parseURIs(meta, attrs[csiapi.URISANsKey]) | ||
request.IPAddresses, err = parseIPAddresses(attrs[csiapi.IPSANsKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.URISANsKey, err) | ||
return nil, fmt.Errorf("%q: %w", csiapi.IPSANsKey, err) | ||
} | ||
ips, err := parseIPAddresses(attrs[csiapi.IPSANsKey]) | ||
request.URIs, err = parseURIs(meta, attrs[csiapi.URISANsKey]) | ||
if err != nil { | ||
return nil, fmt.Errorf("%q: %w", csiapi.IPSANsKey, err) | ||
return nil, fmt.Errorf("%q: %w", csiapi.URISANsKey, err) | ||
} | ||
|
||
return &manager.CertificateRequestBundle{ | ||
Request: &x509.CertificateRequest{ | ||
Subject: pkix.Name{ | ||
CommonName: commonName, | ||
}, | ||
DNSNames: dns, | ||
IPAddresses: ips, | ||
URIs: uris, | ||
}, | ||
Request: request, | ||
IsCA: strings.ToLower(attrs[csiapi.IsCAKey]) == "true", | ||
Namespace: attrs[csiapi.K8sVolumeContextKeyPodNamespace], | ||
Duration: duration, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: this will need many more test cases IMO, to ensure that the splitting functionality works as expected!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SgtCoDFish If I may push back, I felt like if I added too many permutations, I'd essentially be testing
github.com/cert-manager/cert-manager/pkg/util/pki.ParseSubjectStringToRawDerBytes
, because that's doing the heavy lifting in this specific case. That said, if you think there is a branch of this logic that could use a better test, I'm open to anything.