@@ -46,6 +46,17 @@ public class SecurityRestFilter implements RestHandler {
46
46
private final ThreadContext threadContext ;
47
47
private final boolean extractClientCertificate ;
48
48
49
+ public enum ActionType {
50
+ Authentication ("Authentication" ),
51
+ SecondaryAuthentication ("Secondary authentication" ),
52
+ RequestHandling ("Request handling" );
53
+
54
+ private final String name ;
55
+ ActionType (String name ) { this .name = name ; }
56
+ @ Override
57
+ public String toString () { return name ; }
58
+ }
59
+
49
60
public SecurityRestFilter (Settings settings , ThreadContext threadContext , AuthenticationService authenticationService ,
50
61
SecondaryAuthenticator secondaryAuthenticator , RestHandler restHandler , boolean extractClientCertificate ) {
51
62
this .settings = settings ;
@@ -89,17 +100,21 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
89
100
logger .trace ("Found secondary authentication {} in REST request [{}]" , secondaryAuthentication , requestUri );
90
101
}
91
102
RemoteHostHeader .process (request , threadContext );
92
- restHandler .handleRequest (request , channel , client );
103
+ try {
104
+ restHandler .handleRequest (request , channel , client );
105
+ } catch (Exception e ) {
106
+ handleException (ActionType .RequestHandling , request , channel , e );
107
+ }
93
108
},
94
- e -> handleException ("Secondary authentication" , request , channel , e )));
95
- }, e -> handleException (" Authentication" , request , channel , e )));
109
+ e -> handleException (ActionType . SecondaryAuthentication , request , channel , e )));
110
+ }, e -> handleException (ActionType . Authentication , request , channel , e )));
96
111
} else {
97
112
restHandler .handleRequest (request , channel , client );
98
113
}
99
114
}
100
115
101
- private void handleException (String actionType , RestRequest request , RestChannel channel , Exception e ) {
102
- logger .debug (new ParameterizedMessage ("{} failed for REST request [{}]" , actionType , request .uri ()), e );
116
+ protected void handleException (ActionType actionType , RestRequest request , RestChannel channel , Exception e ) {
117
+ logger .debug (new ParameterizedMessage ("{} failed for REST request [{}]" , actionType . name () , request .uri ()), e );
103
118
final RestStatus restStatus = ExceptionsHelper .status (e );
104
119
try {
105
120
channel .sendResponse (new BytesRestResponse (channel , restStatus , e ) {
@@ -109,11 +124,14 @@ private void handleException(String actionType, RestRequest request, RestChannel
109
124
110
125
@ Override
111
126
public Map <String , List <String >> filterHeaders (Map <String , List <String >> headers ) {
112
- if (headers .containsKey ("Warning" )) {
113
- headers = Maps .copyMapWithRemovedEntry (headers , "Warning" );
114
- }
115
- if (headers .containsKey ("X-elastic-product" )) {
116
- headers = Maps .copyMapWithRemovedEntry (headers , "X-elastic-product" );
127
+ if (actionType != ActionType .RequestHandling
128
+ || (restStatus == RestStatus .UNAUTHORIZED || restStatus == RestStatus .FORBIDDEN )) {
129
+ if (headers .containsKey ("Warning" )) {
130
+ headers = Maps .copyMapWithRemovedEntry (headers , "Warning" );
131
+ }
132
+ if (headers .containsKey ("X-elastic-product" )) {
133
+ headers = Maps .copyMapWithRemovedEntry (headers , "X-elastic-product" );
134
+ }
117
135
}
118
136
return headers ;
119
137
}
0 commit comments