@@ -22,22 +22,32 @@ package v3
22
22
23
23
import (
24
24
"context"
25
+ "net/http"
25
26
27
+ corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
26
28
pbEnvoyAuthV3 "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
27
29
"google.golang.org/grpc"
28
30
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"
29
35
"github.com/arangodb/kube-arangodb/pkg/util/svc"
30
36
)
31
37
32
- func New () svc.Handler {
33
- return & impl {}
38
+ func New (authClient pbAuthenticationV1.AuthenticationV1Client ) svc.Handler {
39
+ return & impl {
40
+ authClient : authClient ,
41
+ }
34
42
}
35
43
36
44
var _ pbEnvoyAuthV3.AuthorizationServer = & impl {}
37
45
var _ svc.Handler = & impl {}
38
46
39
47
type impl struct {
40
48
pbEnvoyAuthV3.UnimplementedAuthorizationServer
49
+
50
+ authClient pbAuthenticationV1.AuthenticationV1Client
41
51
}
42
52
43
53
func (i * impl ) Name () string {
@@ -53,10 +63,84 @@ func (i *impl) Register(registrar *grpc.Server) {
53
63
}
54
64
55
65
func (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
+
57
131
return & pbEnvoyAuthV3.CheckResponse {
58
132
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
+ },
60
144
},
61
145
}, nil
62
146
}
0 commit comments