2018-05-17 12:35:11 +00:00
|
|
|
/*
|
2019-06-19 02:53:49 +00:00
|
|
|
Copyright 2019 The Kubernetes Authors.
|
2018-05-17 12:35:11 +00:00
|
|
|
|
|
|
|
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 annotations
|
|
|
|
|
|
|
|
import (
|
2019-06-19 02:53:49 +00:00
|
|
|
"crypto/tls"
|
2018-05-17 12:35:11 +00:00
|
|
|
"fmt"
|
2019-06-19 02:53:49 +00:00
|
|
|
"strings"
|
2018-05-17 12:35:11 +00:00
|
|
|
|
2022-10-04 15:06:16 +00:00
|
|
|
"context"
|
|
|
|
|
2019-06-19 02:53:49 +00:00
|
|
|
pb "github.com/moul/pb/grpcbin/go-grpc"
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-19 02:53:49 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2021-07-13 06:08:29 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2019-06-19 02:53:49 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
|
|
2018-05-17 12:35:11 +00:00
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeAnnotation("backend-protocol - GRPC", func() {
|
2023-06-12 10:25:49 +00:00
|
|
|
f := framework.NewDefaultFramework("grpc", framework.WithHTTPBunEnabled())
|
2018-05-17 12:35:11 +00:00
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should use grpc_pass in the configuration file", func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.NewGRPCFortuneTellerDeployment()
|
2019-06-19 02:53:49 +00:00
|
|
|
|
|
|
|
host := "grpc"
|
|
|
|
|
|
|
|
annotations := map[string]string{
|
|
|
|
"nginx.ingress.kubernetes.io/backend-protocol": "GRPC",
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, "fortune-teller", 50051, annotations)
|
2019-06-19 02:53:49 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %v", host))
|
2019-06-19 02:53:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
2020-02-19 03:08:56 +00:00
|
|
|
return strings.Contains(server, "grpc_pass") &&
|
|
|
|
strings.Contains(server, "grpc_set_header") &&
|
|
|
|
!strings.Contains(server, "proxy_pass")
|
2019-06-19 02:53:49 +00:00
|
|
|
})
|
2018-05-17 12:35:11 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return OK for service with backend protocol GRPC", func() {
|
2020-02-13 14:49:00 +00:00
|
|
|
f.NewGRPCBinDeployment()
|
|
|
|
|
2019-06-19 02:53:49 +00:00
|
|
|
host := "echo"
|
|
|
|
|
2023-06-11 18:49:47 +00:00
|
|
|
svc := &corev1.Service{
|
2019-06-19 02:53:49 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-02-13 14:49:00 +00:00
|
|
|
Name: "grpcbin-test",
|
2019-06-19 02:53:49 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2020-02-13 14:49:00 +00:00
|
|
|
ExternalName: fmt.Sprintf("grpcbin.%v.svc.cluster.local", f.Namespace),
|
2019-06-19 02:53:49 +00:00
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 9000,
|
|
|
|
TargetPort: intstr.FromInt(9000),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
annotations := map[string]string{
|
2019-06-19 02:53:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/backend-protocol": "GRPC",
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:49:00 +00:00
|
|
|
ing := framework.NewSingleIngressWithTLS(host, "/", host, []string{host}, f.Namespace, "grpcbin-test", 9000, annotations)
|
2019-06-19 02:53:49 +00:00
|
|
|
|
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "grpc_pass grpc://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, _ := grpc.Dial(f.GetNginxIP()+":443",
|
|
|
|
grpc.WithTransportCredentials(
|
|
|
|
credentials.NewTLS(&tls.Config{
|
|
|
|
ServerName: "echo",
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := pb.NewGRPCBinClient(conn)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
res, err := client.HeadersUnary(ctx, &pb.EmptyMessage{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
2019-06-19 02:53:49 +00:00
|
|
|
|
|
|
|
metadata := res.GetMetadata()
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), metadata["content-type"].Values[0], "application/grpc")
|
2023-06-27 06:30:30 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), metadata[":authority"].Values[0], host)
|
2019-06-19 02:53:49 +00:00
|
|
|
})
|
|
|
|
|
2021-07-13 06:08:29 +00:00
|
|
|
ginkgo.It("authorization metadata should be overwritten by external auth response headers", func() {
|
|
|
|
f.NewGRPCBinDeployment()
|
|
|
|
host := "echo"
|
|
|
|
|
2023-06-11 18:49:47 +00:00
|
|
|
svc := &corev1.Service{
|
2021-07-13 06:08:29 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "grpcbin-test",
|
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
|
|
|
ExternalName: fmt.Sprintf("grpcbin.%v.svc.cluster.local", f.Namespace),
|
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 9000,
|
|
|
|
TargetPort: intstr.FromInt(9000),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
|
|
|
annotations := map[string]string{
|
2023-06-12 10:25:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/auth-url": fmt.Sprintf("http://%s/response-headers?authorization=foo", f.HTTPBunIP),
|
2021-07-13 06:08:29 +00:00
|
|
|
"nginx.ingress.kubernetes.io/auth-response-headers": "Authorization",
|
|
|
|
"nginx.ingress.kubernetes.io/backend-protocol": "GRPC",
|
|
|
|
}
|
|
|
|
|
|
|
|
ing := framework.NewSingleIngressWithTLS(host, "/", host, []string{host}, f.Namespace, "grpcbin-test", 9000, annotations)
|
|
|
|
|
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "grpc_pass grpc://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, _ := grpc.Dial(f.GetNginxIP()+":443",
|
|
|
|
grpc.WithTransportCredentials(
|
|
|
|
credentials.NewTLS(&tls.Config{
|
|
|
|
ServerName: "echo",
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := pb.NewGRPCBinClient(conn)
|
|
|
|
ctx := metadata.AppendToOutgoingContext(context.Background(),
|
|
|
|
"authorization", "bar")
|
|
|
|
|
|
|
|
res, err := client.HeadersUnary(ctx, &pb.EmptyMessage{})
|
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
|
|
|
|
|
|
|
metadata := res.GetMetadata()
|
|
|
|
assert.Equal(ginkgo.GinkgoT(), "foo", metadata["authorization"].Values[0])
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should return OK for service with backend protocol GRPCS", func() {
|
2020-02-13 14:49:00 +00:00
|
|
|
f.NewGRPCBinDeployment()
|
|
|
|
|
2019-06-19 02:53:49 +00:00
|
|
|
host := "echo"
|
|
|
|
|
2023-06-11 18:49:47 +00:00
|
|
|
svc := &corev1.Service{
|
2019-06-19 02:53:49 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2020-02-13 14:49:00 +00:00
|
|
|
Name: "grpcbin-test",
|
2019-06-19 02:53:49 +00:00
|
|
|
Namespace: f.Namespace,
|
|
|
|
},
|
|
|
|
Spec: corev1.ServiceSpec{
|
2020-02-13 14:49:00 +00:00
|
|
|
ExternalName: fmt.Sprintf("grpcbin.%v.svc.cluster.local", f.Namespace),
|
2019-06-19 02:53:49 +00:00
|
|
|
Type: corev1.ServiceTypeExternalName,
|
|
|
|
Ports: []corev1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: host,
|
|
|
|
Port: 9001,
|
|
|
|
TargetPort: intstr.FromInt(9001),
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.EnsureService(svc)
|
|
|
|
|
2019-12-13 05:47:11 +00:00
|
|
|
annotations := map[string]string{
|
2019-06-19 02:53:49 +00:00
|
|
|
"nginx.ingress.kubernetes.io/backend-protocol": "GRPCS",
|
2020-02-14 00:19:07 +00:00
|
|
|
"nginx.ingress.kubernetes.io/configuration-snippet": fmt.Sprintf(`
|
2019-06-19 02:53:49 +00:00
|
|
|
# without this setting NGINX sends echo instead
|
2020-02-14 00:19:07 +00:00
|
|
|
grpc_ssl_name grpcbin.%v.svc.cluster.local;
|
2019-06-19 02:53:49 +00:00
|
|
|
grpc_ssl_server_name on;
|
|
|
|
grpc_ssl_ciphers HIGH:!aNULL:!MD5;
|
2020-02-14 00:19:07 +00:00
|
|
|
`, f.Namespace),
|
2019-06-19 02:53:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:49:00 +00:00
|
|
|
ing := framework.NewSingleIngressWithTLS(host, "/", host, []string{host}, f.Namespace, "grpcbin-test", 9001, annotations)
|
2019-06-19 02:53:49 +00:00
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, "grpc_pass grpcs://upstream_balancer;")
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, _ := grpc.Dial(f.GetNginxIP()+":443",
|
|
|
|
grpc.WithTransportCredentials(
|
|
|
|
credentials.NewTLS(&tls.Config{
|
|
|
|
ServerName: "echo",
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := pb.NewGRPCBinClient(conn)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
res, err := client.HeadersUnary(ctx, &pb.EmptyMessage{})
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err)
|
2019-06-19 02:53:49 +00:00
|
|
|
|
|
|
|
metadata := res.GetMetadata()
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), metadata["content-type"].Values[0], "application/grpc")
|
2018-05-17 12:35:11 +00:00
|
|
|
})
|
|
|
|
})
|