-
Notifications
You must be signed in to change notification settings - Fork 493
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
Server CA Manager cleanup #859
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ce38f3
Server CA Manager cleanup
azdagron 799ae8e
fix k8s-test attestation check
azdagron 6adfa34
address PR comments
azdagron eed5ffd
Merge remote-tracking branch 'origin/master' into server-ca-manager-c…
azdagron 111b823
Address more PR comments
azdagron 0f8decd
Merge remote-tracking branch 'origin' into server-ca-manager-cleanup
azdagron 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 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 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 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 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 |
---|---|---|
|
@@ -76,17 +76,12 @@ func (s *CATestSuite) SetupTest() { | |
|
||
func (s *CATestSuite) TestNoX509CASet() { | ||
s.ca.SetX509CA(nil) | ||
_, err := s.ca.SignX509CASVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
_, err := s.ca.SignX509CASVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().EqualError(err, "X509 CA is not available for signing") | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVID() { | ||
subject := pkix.Name{ | ||
Country: []string{"IGNORE ME"}, | ||
Organization: []string{"IGNORE ME"}, | ||
} | ||
|
||
svidChain, err := s.ca.SignX509SVID(ctx, s.generateCSRWithSubject("example.org", subject), X509Params{}) | ||
svidChain, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svidChain, 1) | ||
|
||
|
@@ -102,23 +97,31 @@ func (s *CATestSuite) TestSignX509SVID() { | |
|
||
// SPIFFE ID should be set to that of the trust domain | ||
if s.Len(svid.URIs, 1, "has no URIs") { | ||
s.Equal("spiffe://example.org", svid.URIs[0].String()) | ||
s.Equal("spiffe://example.org/workload", svid.URIs[0].String()) | ||
} | ||
|
||
// Subject is hard coded by the CA and should not be pulled from the CSR. | ||
s.Equal("O=SPIRE,C=US", svid.Subject.String()) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDCannotSignTrustDomainID() { | ||
csr := s.createCSR(&x509.CertificateRequest{ | ||
URIs: []*url.URL{makeTrustDomainID("example.org")}, | ||
}) | ||
_, err := s.ca.SignX509SVID(ctx, csr, X509Params{}) | ||
s.Require().EqualError(err, `"spiffe://example.org" is not a valid trust domain member SPIFFE ID: path is empty`) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDUsesDefaultTTLIfTTLUnspecified() { | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
s.Require().Equal(s.clock.Now().Add(time.Minute), svid[0].NotAfter) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDUsesDefaultTTLAndNoCNDNS() { | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
|
@@ -129,7 +132,7 @@ func (s *CATestSuite) TestSignX509SVIDUsesDefaultTTLAndNoCNDNS() { | |
|
||
func (s *CATestSuite) TestSignX509SVIDSingleDNS() { | ||
dnsList := []string{"somehost1"} | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{DNSList: dnsList}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{DNSList: dnsList}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
|
@@ -140,7 +143,7 @@ func (s *CATestSuite) TestSignX509SVIDSingleDNS() { | |
|
||
func (s *CATestSuite) TestSignX509SVIDMultipleDNS() { | ||
dnsList := []string{"somehost1", "somehost2", "somehost3"} | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{DNSList: dnsList}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{DNSList: dnsList}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
|
@@ -151,7 +154,7 @@ func (s *CATestSuite) TestSignX509SVIDMultipleDNS() { | |
|
||
func (s *CATestSuite) TestSignX509SVIDReturnsChainIfIntermediate() { | ||
s.setX509CA(true) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 3) | ||
s.Require().NotNil(svid[0]) | ||
|
@@ -160,44 +163,44 @@ func (s *CATestSuite) TestSignX509SVIDReturnsChainIfIntermediate() { | |
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDUsesTTLIfSpecified() { | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{TTL: time.Minute + time.Second}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{TTL: time.Minute + time.Second}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
s.Require().Equal(s.clock.Now().Add(time.Minute+time.Second), svid[0].NotAfter) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDCapsTTLToCATTL() { | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{TTL: time.Hour}) | ||
svid, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{TTL: time.Hour}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
s.Require().Equal(s.clock.Now().Add(10*time.Minute), svid[0].NotAfter) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDValidatesCSR() { | ||
_, err := s.ca.SignX509SVID(ctx, s.generateCSR("foo.com"), X509Params{}) | ||
s.Require().EqualError(err, `"spiffe://foo.com" does not belong to trust domain "example.org"`) | ||
func (s *CATestSuite) TestSignX509SVIDValidatesTrustDomain() { | ||
_, err := s.ca.SignX509SVID(ctx, s.generateCSRInDomain("foo.com"), X509Params{}) | ||
s.Require().EqualError(err, `"spiffe://foo.com/workload" does not belong to trust domain "example.org"`) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDWithEvilSubject() { | ||
csr := &x509.CertificateRequest{ | ||
Subject: pkix.Name{ | ||
CommonName: "mybank.example.org", | ||
}, | ||
URIs: []*url.URL{makeSpiffeID("example.org")}, | ||
URIs: []*url.URL{makeWorkloadID("example.org")}, | ||
} | ||
certs, err := s.ca.SignX509SVID(ctx, s.createCSR(csr), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Assert().NotEqual("mybank.example.org", certs[0].Subject.CommonName) | ||
} | ||
|
||
func (s *CATestSuite) TestSignX509SVIDIncrementsSerialNumber() { | ||
svid1, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid1, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid1, 1) | ||
s.Require().Equal(0, svid1[0].SerialNumber.Cmp(big.NewInt(1))) | ||
svid2, err := s.ca.SignX509SVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid2, err := s.ca.SignX509SVID(ctx, s.generateCSR(), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid2, 1) | ||
s.Require().Equal(0, svid2[0].SerialNumber.Cmp(big.NewInt(2))) | ||
|
@@ -249,11 +252,7 @@ func (s *CATestSuite) TestSignJWTSVIDValidatesJSR() { | |
} | ||
|
||
func (s *CATestSuite) TestSignX509CASVID() { | ||
subject := pkix.Name{ | ||
Country: []string{"IGNORE ME"}, | ||
Organization: []string{"IGNORE ME"}, | ||
} | ||
svidChain, err := s.ca.SignX509CASVID(ctx, s.generateCSRWithSubject("example.org", subject), X509Params{}) | ||
svidChain, err := s.ca.SignX509CASVID(ctx, s.generateCACSR("example.org"), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svidChain, 1) | ||
|
||
|
@@ -277,13 +276,18 @@ func (s *CATestSuite) TestSignX509CASVID() { | |
} | ||
|
||
func (s *CATestSuite) TestSignX509CASVIDUsesDefaultTTLIfTTLUnspecified() { | ||
svid, err := s.ca.SignX509CASVID(ctx, s.generateCSR("example.org"), X509Params{}) | ||
svid, err := s.ca.SignX509CASVID(ctx, s.generateCACSR("example.org"), X509Params{}) | ||
s.Require().NoError(err) | ||
s.Require().Len(svid, 1) | ||
s.Require().Equal(s.clock.Now().Add(-backdate), svid[0].NotBefore) | ||
s.Require().Equal(s.clock.Now().Add(time.Minute), svid[0].NotAfter) | ||
} | ||
|
||
func (s *CATestSuite) TestSignCAX509SVIDValidatesTrustDomain() { | ||
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. 💯 |
||
_, err := s.ca.SignX509SVID(ctx, s.generateCACSR("foo.com"), X509Params{}) | ||
s.Require().EqualError(err, `"spiffe://foo.com" does not belong to trust domain "example.org"`) | ||
} | ||
|
||
func (s *CATestSuite) setX509CA(upstreamBundle bool) { | ||
var upstreamChain []*x509.Certificate | ||
if upstreamBundle { | ||
|
@@ -304,16 +308,27 @@ func (s *CATestSuite) setJWTKey() { | |
}) | ||
} | ||
|
||
func (s *CATestSuite) generateCSR(trustDomain string) []byte { | ||
func (s *CATestSuite) generateCSR() []byte { | ||
return s.generateCSRInDomain("example.org") | ||
} | ||
|
||
func (s *CATestSuite) generateCSRInDomain(trustDomain string) []byte { | ||
return s.createCSR(&x509.CertificateRequest{ | ||
URIs: []*url.URL{makeSpiffeID(trustDomain)}, | ||
Subject: pkix.Name{ | ||
Country: []string{"IGNORE ME"}, | ||
Organization: []string{"IGNORE ME"}, | ||
}, | ||
URIs: []*url.URL{makeWorkloadID(trustDomain)}, | ||
}) | ||
} | ||
|
||
func (s *CATestSuite) generateCSRWithSubject(trustDomain string, subject pkix.Name) []byte { | ||
func (s *CATestSuite) generateCACSR(trustDomain string) []byte { | ||
return s.createCSR(&x509.CertificateRequest{ | ||
Subject: subject, | ||
URIs: []*url.URL{makeSpiffeID(trustDomain)}, | ||
Subject: pkix.Name{ | ||
Country: []string{"IGNORE ME"}, | ||
Organization: []string{"IGNORE ME"}, | ||
}, | ||
URIs: []*url.URL{makeTrustDomainID(trustDomain)}, | ||
}) | ||
} | ||
|
||
|
@@ -324,7 +339,7 @@ func (s *CATestSuite) createCSR(csr *x509.CertificateRequest) []byte { | |
} | ||
|
||
func (s *CATestSuite) generateJSR(trustDomain string, ttl time.Duration) *node.JSR { | ||
workloadId := makeSpiffeID(trustDomain) | ||
workloadId := makeWorkloadID(trustDomain) | ||
workloadId.Path = "foo" | ||
return &node.JSR{ | ||
SpiffeId: workloadId.String(), | ||
|
@@ -353,6 +368,10 @@ func (s *CATestSuite) createCACertificate(cn string, parent *x509.Certificate) * | |
return cert | ||
} | ||
|
||
func makeSpiffeID(trustDomain string) *url.URL { | ||
func makeWorkloadID(trustDomain string) *url.URL { | ||
return &url.URL{Scheme: "spiffe", Host: trustDomain, Path: "/workload"} | ||
} | ||
|
||
func makeTrustDomainID(trustDomain string) *url.URL { | ||
return &url.URL{Scheme: "spiffe", Host: trustDomain} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
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.
💯