11package cvo
22
33import (
4- "context"
4+ "crypto/tls"
5+ "crypto/x509"
6+ "crypto/x509/pkix"
57 "errors"
68 "fmt"
7- "io"
89 "net/http"
910 "net/http/httptest"
1011 "sort"
@@ -16,7 +17,6 @@ import (
1617 "github.com/google/go-cmp/cmp"
1718 "github.com/prometheus/client_golang/prometheus"
1819 dto "github.com/prometheus/client_model/go"
19- authenticationv1 "k8s.io/api/authentication/v1"
2020 corev1 "k8s.io/api/core/v1"
2121 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222 "k8s.io/client-go/tools/record"
@@ -1019,27 +1019,6 @@ func metricParts(t *testing.T, metric prometheus.Metric, labels ...string) strin
10191019 return strings .Join (parts , " " )
10201020}
10211021
1022- type fakeClient struct {
1023- }
1024-
1025- func (c * fakeClient ) Create (_ context.Context , tokenReview * authenticationv1.TokenReview , _ metav1.CreateOptions ) (* authenticationv1.TokenReview , error ) {
1026- if tokenReview != nil {
1027- ret := tokenReview .DeepCopy ()
1028- if tokenReview .Spec .Token == "good" {
1029- ret .Status .Authenticated = true
1030- ret .Status .User .Username = "system:serviceaccount:openshift-monitoring:prometheus-k8s"
1031- }
1032- if tokenReview .Spec .Token == "authenticated" {
1033- ret .Status .Authenticated = true
1034- }
1035- if tokenReview .Spec .Token == "error" {
1036- return nil , errors .New ("fake error" )
1037- }
1038- return ret , nil
1039- }
1040- return nil , errors .New ("nil input" )
1041- }
1042-
10431022type okHandler struct {
10441023}
10451024
@@ -1051,112 +1030,62 @@ func Test_authHandler(t *testing.T) {
10511030 tests := []struct {
10521031 name string
10531032 handler * authHandler
1054- method string
1055- body io.Reader
1056- headerKey string
1057- headerValue string
1033+ clientCN string
1034+ provideCert bool
10581035 expectedStatusCode int
10591036 expectedBody string
10601037 }{
10611038 {
1062- name : "good " ,
1039+ name : "allowed CN - prometheus-k8s " ,
10631040 handler : & authHandler {
1064- ctx : context .TODO (),
10651041 downstream : & okHandler {},
1066- client : & fakeClient {},
10671042 },
1068- method : "GET" ,
1069- headerKey : "Authorization" ,
1070- headerValue : "Bearer good" ,
1043+ clientCN : "system:serviceaccount:openshift-monitoring:prometheus-k8s" ,
1044+ provideCert : true ,
10711045 expectedStatusCode : http .StatusOK ,
10721046 expectedBody : "ok" ,
10731047 },
10741048 {
1075- name : "empty bearer token" ,
1076- handler : & authHandler {
1077- ctx : context .TODO (),
1078- downstream : & okHandler {},
1079- client : & fakeClient {},
1080- },
1081- method : "GET" ,
1082- headerKey : "Authorization" ,
1083- headerValue : "Bearer " ,
1084- expectedStatusCode : 401 ,
1085- expectedBody : "empty Bearer token\n " ,
1086- },
1087- {
1088- name : "authenticated" ,
1089- handler : & authHandler {
1090- ctx : context .TODO (),
1091- downstream : & okHandler {},
1092- client : & fakeClient {},
1093- },
1094- method : "GET" ,
1095- headerKey : "Authorization" ,
1096- headerValue : "Bearer authenticated" ,
1097- expectedStatusCode : 401 ,
1098- expectedBody : "failed to authorize\n " ,
1099- },
1100- {
1101- name : "bad" ,
1049+ name : "unauthorized CN" ,
11021050 handler : & authHandler {
1103- ctx : context .TODO (),
11041051 downstream : & okHandler {},
1105- client : & fakeClient {},
11061052 },
1107- method : "GET" ,
1108- headerKey : "Authorization" ,
1109- headerValue : "Bearer bad" ,
1110- expectedStatusCode : 401 ,
1111- expectedBody : "failed to authorize\n " ,
1053+ clientCN : "system:serviceaccount:default:unauthorized" ,
1054+ provideCert : true ,
1055+ expectedStatusCode : http .StatusForbidden ,
1056+ expectedBody : "unauthorized CN\n " ,
11121057 },
11131058 {
1114- name : "failed to get the Authorization header " ,
1059+ name : "no client certificate " ,
11151060 handler : & authHandler {
1116- ctx : context .TODO (),
11171061 downstream : & okHandler {},
1118- client : & fakeClient {},
11191062 },
1120- method : "GET" ,
1121- expectedStatusCode : 401 ,
1122- expectedBody : "failed to get the Authorization header\n " ,
1123- },
1124- {
1125- name : "failed to get the Bearer token" ,
1126- handler : & authHandler {
1127- ctx : context .TODO (),
1128- downstream : & okHandler {},
1129- client : & fakeClient {},
1130- },
1131- method : "GET" ,
1132- headerKey : "Authorization" ,
1133- headerValue : "xxx bad" ,
1134- expectedStatusCode : 401 ,
1135- expectedBody : "failed to get the Bearer token\n " ,
1136- },
1137- {
1138- name : "error" ,
1139- handler : & authHandler {
1140- ctx : context .TODO (),
1141- downstream : & okHandler {},
1142- client : & fakeClient {},
1143- },
1144- method : "GET" ,
1145- headerKey : "Authorization" ,
1146- headerValue : "Bearer error" ,
1147- expectedStatusCode : 500 ,
1148- expectedBody : "failed to authorize due to an internal error\n " ,
1063+ provideCert : false ,
1064+ expectedStatusCode : http .StatusUnauthorized ,
1065+ expectedBody : "client certificate required\n " ,
11491066 },
11501067 }
11511068 for _ , tt := range tests {
11521069 t .Run (tt .name , func (t * testing.T ) {
11531070 rr := httptest .NewRecorder ()
11541071
1155- req , err := http .NewRequest (tt . method , "url-not-important" , tt . body )
1072+ req , err := http .NewRequest ("GET" , "url-not-important" , nil )
11561073 if err != nil {
11571074 t .Fatal (err )
11581075 }
1159- req .Header .Set (tt .headerKey , tt .headerValue )
1076+
1077+ // Mock TLS connection state with client certificate
1078+ if tt .provideCert {
1079+ req .TLS = & tls.ConnectionState {
1080+ PeerCertificates : []* x509.Certificate {
1081+ {
1082+ Subject : pkix.Name {
1083+ CommonName : tt .clientCN ,
1084+ },
1085+ },
1086+ },
1087+ }
1088+ }
11601089
11611090 tt .handler .ServeHTTP (rr , req )
11621091 if diff := cmp .Diff (tt .expectedStatusCode , rr .Code ); diff != "" {
0 commit comments