@@ -22,22 +22,32 @@ package v3
2222
2323import (
2424 "context"
25+ "net/http"
2526
27+ corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2628 pbEnvoyAuthV3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
2729 "google.golang.org/grpc"
2830
31+ pbAuthenticationV1 "github.com/arangodb/kube-arangodb/integrations/authentication/v1/definition"
32+ "github.com/arangodb/kube-arangodb/pkg/util"
33+ "github.com/arangodb/kube-arangodb/pkg/util/errors"
34+ "github.com/arangodb/kube-arangodb/pkg/util/errors/panics"
2935 "github.com/arangodb/kube-arangodb/pkg/util/svc"
3036)
3137
32- func New () svc.Handler {
33- return & impl {}
38+ func New (authClient pbAuthenticationV1.AuthenticationV1Client ) svc.Handler {
39+ return & impl {
40+ authClient : authClient ,
41+ }
3442}
3543
3644var _ pbEnvoyAuthV3.AuthorizationServer = & impl {}
3745var _ svc.Handler = & impl {}
3846
3947type impl struct {
4048 pbEnvoyAuthV3.UnimplementedAuthorizationServer
49+
50+ authClient pbAuthenticationV1.AuthenticationV1Client
4151}
4252
4353func (i * impl ) Name () string {
@@ -53,10 +63,84 @@ func (i *impl) Register(registrar *grpc.Server) {
5363}
5464
5565func (i * impl ) Check (ctx context.Context , request * pbEnvoyAuthV3.CheckRequest ) (* pbEnvoyAuthV3.CheckResponse , error ) {
56- logger .Info ("Request Received" )
66+ resp , err := panics .RecoverO1 (func () (* pbEnvoyAuthV3.CheckResponse , error ) {
67+ return i .check (ctx , request )
68+ })
69+
70+ if err != nil {
71+ var v DeniedResponse
72+ if errors .As (err , & v ) {
73+ return v .GetCheckResponse ()
74+ }
75+ return nil , err
76+ }
77+ return resp , nil
78+ }
79+
80+ func (i * impl ) check (ctx context.Context , request * pbEnvoyAuthV3.CheckRequest ) (* pbEnvoyAuthV3.CheckResponse , error ) {
81+ ext := request .GetAttributes ().GetContextExtensions ()
82+
83+ if v , ok := ext [AuthConfigTypeKey ]; ! ok || v != AuthConfigTypeValue {
84+ return nil , DeniedResponse {
85+ Code : http .StatusBadRequest ,
86+ Message : & DeniedMessage {
87+ Message : "Auth plugin is not enabled for this request" ,
88+ },
89+ }
90+ }
91+
92+ authenticated , err := MergeAuthRequest (ctx , request , i .checkADBJWT )
93+ if err != nil {
94+ return nil , err
95+ }
96+
97+ if util .Optional (ext , AuthConfigAuthRequiredKey , AuthConfigKeywordFalse ) == AuthConfigKeywordTrue && authenticated == nil {
98+ return nil , DeniedResponse {
99+ Code : http .StatusUnauthorized ,
100+ Message : & DeniedMessage {
101+ Message : "Unauthorized" ,
102+ },
103+ }
104+ }
105+
106+ if authenticated != nil {
107+ return & pbEnvoyAuthV3.CheckResponse {
108+ HttpResponse : & pbEnvoyAuthV3.CheckResponse_OkResponse {
109+ OkResponse : & pbEnvoyAuthV3.OkHttpResponse {
110+ Headers : []* corev3.HeaderValueOption {
111+ {
112+ Header : & corev3.HeaderValue {
113+ Key : AuthUsernameHeader ,
114+ Value : authenticated .Username ,
115+ },
116+ AppendAction : corev3 .HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD ,
117+ },
118+ {
119+ Header : & corev3.HeaderValue {
120+ Key : AuthAuthenticatedHeader ,
121+ Value : "true" ,
122+ },
123+ AppendAction : corev3 .HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD ,
124+ },
125+ },
126+ },
127+ },
128+ }, nil
129+ }
130+
57131 return & pbEnvoyAuthV3.CheckResponse {
58132 HttpResponse : & pbEnvoyAuthV3.CheckResponse_OkResponse {
59- OkResponse : & pbEnvoyAuthV3.OkHttpResponse {},
133+ OkResponse : & pbEnvoyAuthV3.OkHttpResponse {
134+ Headers : []* corev3.HeaderValueOption {
135+ {
136+ Header : & corev3.HeaderValue {
137+ Key : AuthAuthenticatedHeader ,
138+ Value : "false" ,
139+ },
140+ AppendAction : corev3 .HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD ,
141+ },
142+ },
143+ },
60144 },
61145 }, nil
62146}
0 commit comments