-
Notifications
You must be signed in to change notification settings - Fork 142
Added mTLS between ate system components #237
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
Zoe Zhao (zoez7)
wants to merge
10
commits into
agent-substrate:main
Choose a base branch
from
zoez7:mtls
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
10 commits
Select commit
Hold shift + click to select a range
4d305d5
Add mTLS between atenet router and ate apiserver
zoez7 5770799
Add ate-apiserver to atelet mTLS
zoez7 3edfcf4
Present a real client cert from atecontroller to ateapi in mtls mode
zoez7 ab2e515
Unify atenet router's ateapi auth flags
zoez7 486fe99
Move atenet router cmd/config into the router package
zoez7 e39db9f
Updated TLS version. Limited ExtKeyUsageServerAuth to only Atelet.
zoez7 ad3ce61
Merge remote-tracking branch 'upstream/main' into mtls
zoez7 620a215
replaced 'annoymous' with authn failure by switching ate apiserver to…
zoez7 9806829
Merge remote-tracking branch 'upstream/main' into mtls
zoez7 0003f76
Updated ASN Object Identifier and the PodIdentity JSON bytes to match…
zoez7 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
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 |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "math/big" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/agent-substrate/substrate/internal/substratex509" | ||
| ) | ||
|
|
||
| // makeTestCA mints a self-signed CA and returns it along with a pool | ||
| // containing it as the sole root. | ||
| func makeTestCA(t *testing.T) (*x509.Certificate, *ecdsa.PrivateKey, *x509.CertPool) { | ||
| t.Helper() | ||
|
|
||
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| if err != nil { | ||
| t.Fatalf("generating CA key: %v", err) | ||
| } | ||
| template := &x509.Certificate{ | ||
| SerialNumber: big.NewInt(1), | ||
| Subject: pkix.Name{CommonName: "test-ca"}, | ||
| NotBefore: time.Now().Add(-time.Hour), | ||
| NotAfter: time.Now().Add(time.Hour), | ||
| IsCA: true, | ||
| BasicConstraintsValid: true, | ||
| KeyUsage: x509.KeyUsageCertSign, | ||
| } | ||
| der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) | ||
| if err != nil { | ||
| t.Fatalf("creating CA certificate: %v", err) | ||
| } | ||
| cert, err := x509.ParseCertificate(der) | ||
| if err != nil { | ||
| t.Fatalf("parsing CA certificate: %v", err) | ||
| } | ||
| pool := x509.NewCertPool() | ||
| pool.AddCert(cert) | ||
| return cert, key, pool | ||
| } | ||
|
|
||
| // makeLeafCert mints a server leaf certificate signed by the given CA. If | ||
| // podUID is non-empty it is embedded in a PodIdentity extension. | ||
| func makeLeafCert(t *testing.T, ca *x509.Certificate, caKey *ecdsa.PrivateKey, podUID string) *x509.Certificate { | ||
| t.Helper() | ||
|
|
||
| key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| if err != nil { | ||
| t.Fatalf("generating leaf key: %v", err) | ||
| } | ||
| template := &x509.Certificate{ | ||
| SerialNumber: big.NewInt(2), | ||
| NotBefore: time.Now().Add(-time.Hour), | ||
| NotAfter: time.Now().Add(time.Hour), | ||
| KeyUsage: x509.KeyUsageDigitalSignature, | ||
| ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
| } | ||
| if podUID != "" { | ||
| // AddPodIdentityToCertificate requires all fields to be non-empty; | ||
| // only PodUID matters to these tests. | ||
| err := substratex509.AddPodIdentityToCertificate(&substratex509.PodIdentity{ | ||
| Namespace: "ate-system", | ||
| ServiceAccountName: "atelet", | ||
| ServiceAccountUID: "sa-uid", | ||
| PodName: "atelet-abc", | ||
| PodUID: podUID, | ||
| NodeName: "node-1", | ||
| NodeUID: "node-uid", | ||
| }, template) | ||
| if err != nil { | ||
| t.Fatalf("adding PodIdentity extension: %v", err) | ||
| } | ||
| } | ||
| der, err := x509.CreateCertificate(rand.Reader, template, ca, &key.PublicKey, caKey) | ||
| if err != nil { | ||
| t.Fatalf("creating leaf certificate: %v", err) | ||
| } | ||
| cert, err := x509.ParseCertificate(der) | ||
| if err != nil { | ||
| t.Fatalf("parsing leaf certificate: %v", err) | ||
| } | ||
| return cert | ||
| } | ||
|
|
||
| func TestVerifyAteletServerCert(t *testing.T) { | ||
| ca, caKey, roots := makeTestCA(t) | ||
| otherCA, otherCAKey, _ := makeTestCA(t) | ||
|
|
||
| const uid = "5a2e1c9f-0b57-4a52-9f6e-2f6d3a1b8c4d" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| leaf *x509.Certificate | ||
| expectedUID string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "matching UID succeeds", | ||
| leaf: makeLeafCert(t, ca, caKey, uid), | ||
| expectedUID: uid, | ||
| }, | ||
| { | ||
| name: "mismatched UID fails", | ||
| leaf: makeLeafCert(t, ca, caKey, "some-other-uid"), | ||
| expectedUID: uid, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "missing pod UID extension fails", | ||
| leaf: makeLeafCert(t, ca, caKey, ""), | ||
| expectedUID: uid, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "cert from untrusted CA fails", | ||
| leaf: makeLeafCert(t, otherCA, otherCAKey, uid), | ||
| expectedUID: uid, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| verify := verifyAteletServerCert(roots, tc.expectedUID) | ||
| err := verify(tls.ConnectionState{ | ||
| PeerCertificates: []*x509.Certificate{tc.leaf}, | ||
| }) | ||
| if gotErr := err != nil; gotErr != tc.wantErr { | ||
| t.Fatalf("verify returned error %v, wantErr=%v", err, tc.wantErr) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| t.Run("no peer certificate fails", func(t *testing.T) { | ||
| verify := verifyAteletServerCert(roots, uid) | ||
| if err := verify(tls.ConnectionState{}); err == nil { | ||
| t.Fatal("verify succeeded, want error") | ||
| } | ||
| }) | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Should we include DNS or IP SANs in the certs so we don't need to skip this? https://spiffe.io/docs/latest/spiffe-specs/x509-svid/#2-spiffe-id says:
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.
The problem is that kubelet first waits for the pod certificate volume mount https://github.com/kubernetes/kubernetes/blob/v1.34.0/pkg/kubelet/kubelet.go#L2066 before creating a pod, if we try to add IPAddress in cmd/podcertcontroller/internal/podidentitysigner/podidentitysigner.go, it will be empty.