@@ -22,13 +22,16 @@ package v3
2222
2323import (
2424 "context"
25+ "fmt"
2526 "net/http"
27+ "strings"
2628
2729 corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2830 pbEnvoyAuthV3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
2931 "google.golang.org/grpc"
3032
3133 pbAuthenticationV1 "github.com/arangodb/kube-arangodb/integrations/authentication/v1/definition"
34+ networkingApi "github.com/arangodb/kube-arangodb/pkg/apis/networking/v1alpha1"
3235 "github.com/arangodb/kube-arangodb/pkg/util"
3336 "github.com/arangodb/kube-arangodb/pkg/util/errors"
3437 "github.com/arangodb/kube-arangodb/pkg/util/errors/panics"
@@ -37,7 +40,7 @@ import (
3740
3841func New (authClient pbAuthenticationV1.AuthenticationV1Client ) svc.Handler {
3942 return & impl {
40- authClient : authClient ,
43+ helper : NewADBHelper ( authClient ) ,
4144 }
4245}
4346
@@ -47,7 +50,7 @@ var _ svc.Handler = &impl{}
4750type impl struct {
4851 pbEnvoyAuthV3.UnimplementedAuthorizationServer
4952
50- authClient pbAuthenticationV1. AuthenticationV1Client
53+ helper ADBHelper
5154}
5255
5356func (i * impl ) Name () string {
@@ -104,25 +107,62 @@ func (i *impl) check(ctx context.Context, request *pbEnvoyAuthV3.CheckRequest) (
104107 }
105108
106109 if authenticated != nil {
110+ var 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+ switch networkingApi .ArangoRouteSpecAuthenticationPassMode (strings .ToLower (util .Optional (ext , AuthConfigAuthPassModeKey , "" ))) {
128+ case networkingApi .ArangoRouteSpecAuthenticationPassModeOverride :
129+ token , ok , err := i .helper .Token (ctx , authenticated )
130+ if err != nil {
131+ return nil , err
132+ }
133+
134+ if ! ok {
135+ return nil , DeniedResponse {
136+ Code : http .StatusUnauthorized ,
137+ Message : & DeniedMessage {
138+ Message : "Unable to render token" ,
139+ },
140+ }
141+ }
142+
143+ headers = append (headers , & corev3.HeaderValueOption {
144+ Header : & corev3.HeaderValue {
145+ Key : "authorization" ,
146+ Value : fmt .Sprintf ("bearer %s" , token ),
147+ },
148+ AppendAction : corev3 .HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD ,
149+ },
150+ )
151+ case networkingApi .ArangoRouteSpecAuthenticationPassModeRemove :
152+ headers = append (headers , & corev3.HeaderValueOption {
153+ Header : & corev3.HeaderValue {
154+ Key : "authorization" ,
155+ },
156+ AppendAction : corev3 .HeaderValueOption_OVERWRITE_IF_EXISTS_OR_ADD ,
157+ KeepEmptyValue : false ,
158+ },
159+ )
160+ }
161+
107162 return & pbEnvoyAuthV3.CheckResponse {
108163 HttpResponse : & pbEnvoyAuthV3.CheckResponse_OkResponse {
109164 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- },
165+ Headers : headers ,
126166 },
127167 },
128168 }, nil
0 commit comments