-
Notifications
You must be signed in to change notification settings - Fork 97
Add support for name constraints #433
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
nickperry
wants to merge
3
commits into
cert-manager:main
Choose a base branch
from
nickperry:feature-nameconstraints
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
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ import ( | |
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/asn1" | ||
| "encoding/base64" | ||
| "encoding/pem" | ||
| "errors" | ||
| "fmt" | ||
|
|
@@ -818,6 +820,105 @@ func TestPCASign(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // --- ApiPassthrough tests --- | ||
|
|
||
| // ASN.1 structure for GeneralSubtree (RFC 5280) | ||
| type GeneralSubtree struct { | ||
| Base asn1.RawValue | ||
| Minimum int `asn1:"optional,default:0"` | ||
| Maximum int `asn1:"optional"` | ||
| } | ||
|
|
||
| // ASN.1 structure for NameConstraints (RFC 5280) | ||
| type NameConstraints struct { | ||
| Permitted []GeneralSubtree `asn1:"tag:0,optional"` | ||
| Excluded []GeneralSubtree `asn1:"tag:1,optional"` | ||
| } | ||
|
|
||
| func createCSRWithNameConstraints() ([]byte, error) { | ||
|
|
||
| nc := NameConstraints{ | ||
| Permitted: []GeneralSubtree{{Base: asn1.RawValue{Tag: 2, Class: asn1.ClassContextSpecific, Bytes: []byte("permitted.com")}}}, | ||
| Excluded: []GeneralSubtree{{Base: asn1.RawValue{Tag: 2, Class: asn1.ClassContextSpecific, Bytes: []byte("excluded.com")}}}, | ||
| } | ||
| ncBytes, err := asn1.Marshal(nc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ext := pkix.Extension{ | ||
| Id: asn1.ObjectIdentifier{2, 5, 29, 30}, | ||
| Value: ncBytes, | ||
| Critical: true, | ||
| } | ||
| tpl := x509.CertificateRequest{ | ||
| Subject: pkix.Name{CommonName: "with-nc"}, | ||
| ExtraExtensions: []pkix.Extension{ext}, | ||
| } | ||
| key, _ := rsa.GenerateKey(rand.Reader, 2048) | ||
| csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &tpl, key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}), nil | ||
| } | ||
|
|
||
| func createCSRWithoutNameConstraints() ([]byte, error) { | ||
| tpl := x509.CertificateRequest{ | ||
| Subject: pkix.Name{CommonName: "no-nc"}, | ||
| } | ||
| key, _ := rsa.GenerateKey(rand.Reader, 2048) | ||
| csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &tpl, key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes}), nil | ||
| } | ||
|
|
||
| func TestPCASign_ApiPassthrough(t *testing.T) { | ||
| client := &workingACMPCAClient{} | ||
| provisioner := PCAProvisioner{arn: arn, pcaClient: client} | ||
|
|
||
| // With Name Constraints | ||
| csrWithNC, err := createCSRWithNameConstraints() | ||
| require.NoError(t, err) | ||
| crWithNC := &cmapi.CertificateRequest{ | ||
| Spec: cmapi.CertificateRequestSpec{Request: csrWithNC}, | ||
| } | ||
| _ = provisioner.Sign(context.TODO(), crWithNC, logr.Discard()) | ||
| got := client.issueCertInput | ||
| if assert.NotNil(t, got, "Expected IssueCertificateInput with Name Constraints") { | ||
| assert.NotNil(t, got.ApiPassthrough, "ApiPassthrough should be set when Name Constraints present") | ||
| if assert.NotNil(t, got.ApiPassthrough.Extensions) { | ||
| // This assumes only one custom extension is present. If support | ||
| // for additional extensions via apiPassthrough is added, this | ||
| // test will need to be updated. | ||
|
Comment on lines
+893
to
+895
Contributor
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. Can we future proof this test? |
||
| assert.Len(t, got.ApiPassthrough.Extensions.CustomExtensions, 1) | ||
| ext := got.ApiPassthrough.Extensions.CustomExtensions[0] | ||
| assert.Equal(t, "2.5.29.30", *ext.ObjectIdentifier) | ||
| assert.True(t, *ext.Critical) | ||
| // Decode and check permitted/excluded DNS names | ||
| ncBytes, err := base64.StdEncoding.DecodeString(*ext.Value) | ||
| require.NoError(t, err) | ||
| // Look for 'permitted.com' and 'excluded.com' in the raw bytes | ||
| assert.Contains(t, string(ncBytes), "permitted.com", "Permitted DNS name should be present") | ||
| assert.Contains(t, string(ncBytes), "excluded.com", "Excluded DNS name should be present") | ||
| } | ||
| } | ||
|
|
||
| // Without Name Constraints | ||
| csrNoNC, err := createCSRWithoutNameConstraints() | ||
| require.NoError(t, err) | ||
| crNoNC := &cmapi.CertificateRequest{ | ||
| Spec: cmapi.CertificateRequestSpec{Request: csrNoNC}, | ||
| } | ||
| _ = provisioner.Sign(context.TODO(), crNoNC, logr.Discard()) | ||
| got = client.issueCertInput | ||
| if assert.NotNil(t, got, "Expected IssueCertificateInput without Name Constraints") { | ||
| assert.Nil(t, got.ApiPassthrough, "ApiPassthrough should be nil when Name Constraints absent") | ||
| } | ||
| } | ||
|
|
||
| func TestPCASignValidity(t *testing.T) { | ||
| now := time.Now() | ||
| client := &workingACMPCAClient{} | ||
|
|
||
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.
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.
Can we create a function to generate the extensions blob? This way in the future someone just has to add to that function instead of changing all this logic around (and by extensions the tests).
We can say something like "if list empty return nil", which will make the api passthrough blob look like {subject: nil, extensions:nil}, which acts as if the object is not present.
Picturing something like