Skip to content

Commit

Permalink
[release-v1.6] Tests for encryption with Kourier local gateway (knati…
Browse files Browse the repository at this point in the history
…ve#1235)

* Enable internal-tls on ocp-tls (knative#1203)

* Enable internal-tls on OCP 4.8

* Use tls to match JOB name

* Add a target to enable internal-tls in Makefile (knative#1224)

* Add a target to enable internal-tls in Makefile

* Update CI template for internal-tls enabled

* Tests for encryption with Kourier local gateway (knative#13263)

* Generate Secrets

* Commit generated cert-secret.yaml

* httpproxy enables tls client

* httpproxy uses https when CA_CERT specified

* Pass CA_CERT and SERVER_NAME env variables properly to tests

* Avoid using cluster-local certificates for external services

* Enable tls tests for cluster-local Kourier gateway

* Need to create test resources including the test namespace first
before installing Knative so that applying
test/config/tls/cert-secret.yaml succeeds

* TMP: Enable tls in the standard e2e make target - test purposes

* Fix indentation

* Use knative-serving-ingress ns for deploying server-certs

* Deploy certificates at test phase

* Separate test and install of installing certs

* Wait for knative-serving-ingress to exist

* Revert "TMP: Enable tls in the standard e2e make target - test purposes"

This reverts commit 54fabb3.

Co-authored-by: Kenjiro Nakayama <[email protected]>
  • Loading branch information
mgencur and nak3 authored Sep 9, 2022
1 parent 12d9276 commit 5879247
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/kind-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ jobs:
echo "SYSTEM_NAMESPACE=$SYSTEM_NAMESPACE" >> $GITHUB_ENV
echo "GATEWAY_OVERRIDE=$GATEWAY_OVERRIDE" >> $GITHUB_ENV
echo "GATEWAY_NAMESPACE_OVERRIDE=$GATEWAY_NAMESPACE_OVERRIDE" >> $GITHUB_ENV
echo "CA_CERT=$CA_CERT" >> $GITHUB_ENV
echo "SERVER_NAME=$SERVER_NAME" >> $GITHUB_ENV
- name: Test ${{ matrix.test-suite }}
run: |
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ test-e2e:
./openshift/e2e-tests.sh
.PHONY: test-e2e

test-e2e-tls:
ENABLE_INTERNAL_TLS="true" ./openshift/e2e-tests.sh
.PHONY: test-e2e-tls

test-images:
for img in $(TEST_IMAGES); do \
KO_DOCKER_REPO=$(DOCKER_REPO_OVERRIDE) ko resolve --tags=$(TEST_IMAGE_TAG) $(KO_FLAGS) -RBf $$img ; \
Expand Down
5 changes: 4 additions & 1 deletion openshift/ci-operator/generate-ci-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ branch=${1-'knative-v0.6.0'}
openshift=${2-'4.3'}
promotion_disabled=${3-false}
generate_continuous=${4-false}
internal_tls_enabled=${5-false}

if [[ "$branch" == "knative-next" ]]; then
promotion_name="knative-nightly"
Expand Down Expand Up @@ -150,7 +151,9 @@ EOF

print_single_test "e2e-aws-ocp-${openshift//./}" "make test-e2e" "" "true" "generic-claim" ""

if [[ "$generate_continuous" == true ]]; then
if [[ "$internal_tls_enabled" == true ]]; then
print_single_test "e2e-aws-ocp-${openshift//./}-continuous" "make test-e2e-tls" "" "true" "generic-claim" "${cron}"
elif [[ "$generate_continuous" == true ]]; then
print_single_test "e2e-aws-ocp-${openshift//./}-continuous" "make test-e2e" "" "true" "generic-claim" "${cron}"
fi

Expand Down
2 changes: 1 addition & 1 deletion openshift/ci-operator/update-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CONFIG=$CONFIGDIR/openshift-knative-serving-release-$VERSION
PERIODIC_CONFIG=$PERIODIC_CONFIGDIR/openshift-knative-serving-release-$VERSION-periodics.yaml
CURDIR=$(dirname $0)

# $1=branch $2=openshift $3=promotion_disabled $4=generate_continuous
# $1=branch $2=openshift $3=promotion_disabled $4=generate_continuous $5=internal_tls_enabled(optional)
$CURDIR/generate-ci-config.sh knative-$VERSION 4.6 true false > ${CONFIG}__46.yaml
$CURDIR/generate-ci-config.sh knative-$VERSION 4.7 true false > ${CONFIG}__47.yaml
$CURDIR/generate-ci-config.sh knative-$VERSION 4.8 true false > ${CONFIG}__48.yaml
Expand Down
26 changes: 26 additions & 0 deletions openshift/e2e-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ spec:
logging.enable-request-log: "true"
EOF

# TODO: Only one cluster enables internal-tls but it should be enabled by default when the feature is stable.
if [[ ${ENABLE_INTERNAL_TLS} == "true" ]]; then
oc patch knativeserving knative-serving \
-n "${SERVING_NAMESPACE}" \
--type merge --patch '{"spec": {"config": {"network": {"internal-encryption": "true"}}}}'
oc patch knativeserving knative-serving \
-n "${SERVING_NAMESPACE}" \
--type merge --patch '{"spec": {"config": {"kourier": {"cluster-cert-secret": "server-certs"}}}}'
# Deploy certificates for testing TLS with cluster-local gateway
timeout 600 '[[ $(oc get ns $SERVING_INGRESS_NAMESPACE -oname | wc -l) == 0 ]]' || return 1
yq read --doc 1 ./test/config/tls/cert-secret.yaml | sed "s/knative-serving/${SERVING_INGRESS_NAMESPACE}/" | oc apply -f -
echo "Restart activator to mount the certificates"
oc delete pod -n ${SERVING_NAMESPACE} -l app=activator
oc wait --timeout=60s --for=condition=Available deployment -n ${SERVING_NAMESPACE} activator
echo "internal-encryption is enabled"
fi

# Wait for 4 pods to appear first
timeout 600 '[[ $(oc get pods -n $SERVING_NAMESPACE --no-headers | wc -l) -lt 4 ]]' || return 1
wait_until_pods_running $SERVING_NAMESPACE || return 1
Expand Down Expand Up @@ -269,6 +286,15 @@ function prepare_knative_serving_tests_nightly {
export GATEWAY_OVERRIDE=kourier
export GATEWAY_NAMESPACE_OVERRIDE="$SERVING_INGRESS_NAMESPACE"
export INGRESS_CLASS=kourier.ingress.networking.knative.dev

if [[ ${ENABLE_INTERNAL_TLS} == "true" ]]; then
# Deploy CA cert for testing TLS with cluster-local gateway
yq read --doc 0 ./test/config/tls/cert-secret.yaml | oc apply -f -
# This needs to match the name of Secret in test/config/tls/cert-secret.yaml
export CA_CERT=ca-cert
# This needs to match $san from test/config/tls/generate.sh
export SERVER_NAME=knative.dev
fi
}

function run_e2e_tests(){
Expand Down
2 changes: 2 additions & 0 deletions openshift/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ env

failed=0

export ENABLE_INTERNAL_TLS="${ENABLE_INTERNAL_TLS:-false}"

(( !failed )) && install_knative || failed=1
(( !failed )) && prepare_knative_serving_tests_nightly || failed=2
(( !failed )) && run_e2e_tests || failed=3
Expand Down
30 changes: 30 additions & 0 deletions test/config/tls/cert-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2022 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.

apiVersion: v1
kind: Secret
metadata:
name: ca-cert
namespace: serving-tests
data:
ca.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURSVENDQWkyZ0F3SUJBZ0lVQ2pESkw2RStjQ3FVVGdOdXNkY0g2Z2pNRStNd0RRWUpLb1pJaHZjTkFRRUwKQlFBd01qRWFNQmdHQTFVRUNnd1JTMjVoZEdsMlpTQkRiMjF0ZFc1cGRIa3hGREFTQmdOVkJBTU1DMlY0WVcxdwpiR1V1WTI5dE1CNFhEVEl5TURnek1UQTVNVE14TVZvWERUSXpNRGd6TVRBNU1UTXhNVm93TWpFYU1CZ0dBMVVFCkNnd1JTMjVoZEdsMlpTQkRiMjF0ZFc1cGRIa3hGREFTQmdOVkJBTU1DMlY0WVcxd2JHVXVZMjl0TUlJQklqQU4KQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBbkdWdVROa3pkcm10dEdqOWRWWjBiYlp2d3UwaQpjd1I5MUZ1U3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DFhZnhSRHBKWUR3T0hjMS9YMHd6ZldkKzBobTBZb29yVE5CZGJ0MFdoaU1XCm1pZ2hHc2UwSnJQTnJtc0tlL3JhbnRBV1hXc2p5akUwQVNON21tTGg5dUVubmU3eElPR1dnZUo2aFFJREFRQUIKb3hvd0dEQVdCZ05WSFJFRUR6QU5nZ3RyYm1GMGFYWmxMbVJsZGpBTkJna3Foa2lHOXcwQkFRc0ZBQU9DQVFFQQpHN2xraFI5VC9OMFIxLzc1WGJ5dWdlOWgrYXFHTzdxblAyTHg2aHo3bHdjOFM2VTVNS09EaW9zRXhMOStKYzhSCkorbTJjTmFsR3pOTG1ER3FDbDBJU3g4SGRhc1ZQbW9McFlpamFlRWNXS1JoVzZaTUNyVlorOEpnQWVmcTFSaFQKMnBqM05OTllWZ2NaWVJsV2NydlBnWlFZZ3JYTGhCWCtPKzRQWmNXUlZzem1yYncwWms1aE53ZWNWc1lvbHRLVwpCdEpCNmx5WDBLcDdNcURqUXBnZUR4WnVTbElMaktTK1J6ejU0L3ZxTDhpQTJrN0Z6TWNUS1IydXhTRGtwK2lZCkQrQ2cyenBnb3c2RFJxeEowWmJ3YXZRY3ozWHQrc0FUYjZIcG10WFNyVHk1R2E4L0R2U1ZvSGl2YlNTMkJ5ZloKQ3BBZkhLT0FBUGdHbUFOaGJpQ2VhZz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
68 changes: 68 additions & 0 deletions test/config/tls/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

# Copyright 2022 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.

# This script generates test/config/tls/cert-secret.yaml.

san="knative.dev"

# Create CA key and cert
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=Knative Community/CN=example.com' -keyout rootCAKey.pem -out rootCACert.pem

# Create server key
openssl req -out tls.csr -newkey rsa:2048 -nodes -keyout tls.key -subj "/CN=example.com/O=Knative Community" -addext "subjectAltName = DNS:$san"

# Create server certs
openssl x509 -req -extfile <(printf "subjectAltName=DNS:$san") -days 365 -in tls.csr -CA rootCACert.pem -CAkey rootCAKey.pem -CAcreateserial -out tls.crt

CA_CERT=$(cat rootCACert.pem | base64 | tr -d '\n')
TLS_KEY=$(cat tls.key | base64 | tr -d '\n')
TLS_CERT=$(cat tls.crt | base64 | tr -d '\n')

cat <<EOF > cert-secret.yaml
# Copyright 2022 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.
apiVersion: v1
kind: Secret
metadata:
name: ca-cert
namespace: serving-tests
data:
ca.crt: ${CA_CERT}
---
apiVersion: v1
kind: Secret
metadata:
name: server-certs
namespace: knative-serving
data:
tls.key: ${TLS_KEY}
tls.crt: ${TLS_CERT}
EOF

# Clean up
rm -f rootCACert.pem rootCAKey.pem tls.key tls.crt tls.csr rootCACert.srl
18 changes: 13 additions & 5 deletions test/e2e-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ function install() {
YTT_FILES+=("${REPO_ROOT_DIR}/test/config/resource-quota/resource-quota.yaml")
fi

if (( ENABLE_TLS )); then
YTT_FILES+=("${REPO_ROOT_DIR}/test/config/tls/cert-secret.yaml")
fi

local ytt_result=$(mktemp)
local ytt_post_install_result=$(mktemp)
local ytt_flags=""
Expand Down Expand Up @@ -360,11 +364,15 @@ function install() {

if (( ENABLE_TLS )); then
echo "Patch to config-network to enable internal encryption"
kubectl patch configmap/config-network \
-n ${SYSTEM_NAMESPACE} \
--type merge \
-p '{"data":{"internal-encryption":"true"}}'

toggle_feature internal-encryption true config-network
if [[ "$INGRESS_CLASS" == "kourier.ingress.networking.knative.dev" ]]; then
echo "Point Kourier local gateway to custom server certificates"
toggle_feature cluster-cert-secret server-certs config-kourier
# This needs to match the name of Secret in test/config/tls/cert-secret.yaml
export CA_CERT=ca-cert
# This needs to match $san from test/config/tls/generate.sh
export SERVER_NAME=knative.dev
fi
echo "Restart activator to mount the certificates"
kubectl delete pod -n ${SYSTEM_NAMESPACE} -l app=activator
kubectl wait --timeout=60s --for=condition=Available deployment -n ${SYSTEM_NAMESPACE} activator
Expand Down
31 changes: 29 additions & 2 deletions test/e2e/service_to_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"testing"

corev1 "k8s.io/api/core/v1"
netapi "knative.dev/networking/pkg/apis/networking"
ptr "knative.dev/pkg/ptr"
pkgTest "knative.dev/pkg/test"
ingress "knative.dev/pkg/test/ingress"
"knative.dev/pkg/test/logstream"
Expand All @@ -45,6 +47,8 @@ const (
targetHostEnv = "TARGET_HOST"
gatewayHostEnv = "GATEWAY_HOST"
helloworldResponse = "Hello World! How about some tasty noodles?"
caCertDirectory = "/var/lib/knative/ca"
caCertPath = caCertDirectory + "/ca.crt"
)

// testCases for table-driven testing.
Expand Down Expand Up @@ -109,6 +113,16 @@ func testProxyToHelloworld(t *testing.T, clients *test.Clients, helloworldURL *u
})
}

caSecretName := os.Getenv("CA_CERT")

// External services use different TLS certificates than cluster-local services.
// Not passing CA_CERT will make the httpproxy use plain http to connect to the
// target service.
if caSecretName != "" && !accessibleExternal {
envVars = append(envVars, []corev1.EnvVar{{Name: "CA_CERT", Value: caCertPath},
{Name: "SERVER_NAME", Value: os.Getenv("SERVER_NAME")}}...)
}

// Set up httpproxy app.
t.Log("Creating a Service for the httpproxy test app.")
names := test.ResourceNames{
Expand All @@ -118,11 +132,24 @@ func testProxyToHelloworld(t *testing.T, clients *test.Clients, helloworldURL *u

test.EnsureTearDown(t, clients, &names)

resources, err := v1test.CreateServiceReady(t, clients, &names,
serviceOptions := []rtesting.ServiceOption{
rtesting.WithEnv(envVars...),
rtesting.WithConfigAnnotations(map[string]string{
"sidecar.istio.io/inject": strconv.FormatBool(inject),
}))
}),
}

if caSecretName != "" && !accessibleExternal {
serviceOptions = append(serviceOptions, rtesting.WithVolume("ca-certs", caCertDirectory, corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: caSecretName,
Optional: ptr.Bool(false),
}}),
)
}

resources, err := v1test.CreateServiceReady(t, clients, &names, serviceOptions...)

if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
Expand Down
46 changes: 45 additions & 1 deletion test/test_images/httpproxy/httpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package main

import (
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"log"
"os"
Expand Down Expand Up @@ -61,12 +64,17 @@ func main() {
routedHost = gateway
}

scheme := "http"
if caCert := os.Getenv("CA_CERT"); caCert != "" {
scheme = "https"
}
target := &url.URL{
Scheme: "http",
Scheme: scheme,
Host: routedHost,
}
log.Print("target is ", target)
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = newTLSEnabledTransport()
proxy.ErrorHandler = func(w http.ResponseWriter, req *http.Request, err error) {
log.Print("error reverse proxying request: ", err)
http.Error(w, err.Error(), http.StatusBadGateway)
Expand All @@ -84,3 +92,39 @@ func main() {
log.Print("Listening on address: ", address)
test.ListenAndServeGracefully(address, handler)
}

func newTLSEnabledTransport() http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport).Clone()
if caCert := os.Getenv("CA_CERT"); caCert != "" {
rootCAs, err := createRootCAs(caCert)
if err != nil {
log.Fatal(err)
return transport
}
transport.TLSClientConfig = &tls.Config{
RootCAs: rootCAs,
// If SERVER_NAME is not set the empty value will make the
// TLS client infer the ServerName from the hostname.
ServerName: os.Getenv("SERVER_NAME"),
}
}
return transport
}

func createRootCAs(caCertFile string) (*x509.CertPool, error) {
pemData, err := os.ReadFile(caCertFile)
if err != nil {
return nil, err
}
rootCAs, err := x509.SystemCertPool()
if rootCAs == nil || err != nil {
if err != nil {
log.Printf("Failed to load cert poll from system: %v. Will create a new cert pool.", err)
}
rootCAs = x509.NewCertPool()
}
if !rootCAs.AppendCertsFromPEM(pemData) {
return nil, errors.New("failed to add the certificate to the root CA")
}
return rootCAs, nil
}

0 comments on commit 5879247

Please sign in to comment.