-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trust DataPlaneUserSAN from Activator to Queue-Proxy (#14452)
* Trust DataPlaneUserSAN from Activator to Queue-Proxy * Fix lint * Fix plate * Remove * Use read lock * bump pkg * Use DataPlaneUserSAN instead of DataPlaneUserName
- Loading branch information
Showing
11 changed files
with
166 additions
and
24 deletions.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2023 The Knative Authors | ||
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 certificate | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"knative.dev/networking/pkg/certificates" | ||
pkgnet "knative.dev/pkg/network" | ||
"knative.dev/serving/pkg/activator/handler" | ||
) | ||
|
||
// TLSContext returns DialTLSContextFunc. | ||
func (cr *CertCache) TLSContext() pkgnet.DialTLSContextFunc { | ||
return cr.dialTLSContext | ||
} | ||
|
||
// dialTLSContext handles TLS dialer | ||
func (cr *CertCache) dialTLSContext(ctx context.Context, network, addr string) (net.Conn, error) { | ||
return dialTLSContext(ctx, network, addr, cr) | ||
} | ||
|
||
// dialTLSContext handles verify SAN before calling DialTLSWithBackOff. | ||
func dialTLSContext(ctx context.Context, network, addr string, cr *CertCache) (net.Conn, error) { | ||
cr.certificatesMux.RLock() | ||
// Clone the certificate Pool such that the one used by the client will be different from the one that will get updated is CA is replaced. | ||
tlsConf := cr.TLSConf.Clone() | ||
tlsConf.RootCAs = tlsConf.RootCAs.Clone() | ||
cr.certificatesMux.RUnlock() | ||
|
||
revID := handler.RevIDFrom(ctx) | ||
san := certificates.DataPlaneUserSAN(revID.Namespace) | ||
|
||
tlsConf.VerifyConnection = verifySAN(san) | ||
return pkgnet.DialTLSWithBackOff(ctx, network, addr, tlsConf) | ||
} | ||
|
||
func verifySAN(san string) func(tls.ConnectionState) error { | ||
return func(cs tls.ConnectionState) error { | ||
if len(cs.PeerCertificates) == 0 { | ||
return errors.New("no PeerCertificates provided") | ||
} | ||
for _, name := range cs.PeerCertificates[0].DNSNames { | ||
if name == san { | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("san %q does not have a matching name in %v", san, cs.PeerCertificates[0].DNSNames) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2023 The Knative Authors | ||
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 certificate | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"testing" | ||
) | ||
|
||
// TestVerifySAN tests verifySAN. | ||
func TestVerifySAN(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
san string | ||
expErr bool | ||
}{{ | ||
name: "first SAN", | ||
san: "knative-knative-serving", | ||
expErr: false, | ||
}, { | ||
name: "second SAN", | ||
san: "data-plane.knative.dev", | ||
expErr: false, | ||
}, { | ||
name: "non existent SAN", | ||
san: "foo", | ||
expErr: true, | ||
}} | ||
|
||
// tlsCrt contains two SANs knative-knative-serving and data-plane.knative.dev. | ||
block, _ := pem.Decode(tlsCrt) | ||
if block == nil { | ||
t.Fatal("failed to parse certificate PEM") | ||
} | ||
|
||
serverCert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
t.Fatalf("failed to parse certificate: %v", err) | ||
} | ||
|
||
tlsConnectionState := tls.ConnectionState{ | ||
PeerCertificates: []*x509.Certificate{serverCert}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := verifySAN(test.san)(tlsConnectionState) | ||
if test.expErr && err == nil { | ||
t.Fatalf("failed to verify SAN") | ||
} | ||
if !test.expErr && err != nil { | ||
t.Fatalf("failed to verify SAN: %v", err) | ||
} | ||
}) | ||
} | ||
} |
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
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