1313import static org .elasticsearch .rest .RestStatus .*;
1414
1515import java .io .IOException ;
16+ import java .util .Arrays ;
17+ import java .util .HashSet ;
18+ import java .util .Set ;
19+ import org .elasticsearch .common .logging .Loggers ;
1620import org .elasticsearch .rest .StringRestResponse ;
1721
1822/**
@@ -22,49 +26,85 @@ public class HttpBasicServer extends HttpServer {
2226
2327 private final String user ;
2428 private final String password ;
29+ private final Set <String > whitelist ;
30+ private final String xForwardFor ;
2531
2632 @ Inject public HttpBasicServer (Settings settings , Environment environment , HttpServerTransport transport ,
2733 RestController restController ,
2834 NodeService nodeService ) {
2935 super (settings , environment , transport , restController , nodeService );
3036
31- this .user = settings .get ("http.basic.user" );
32- this .password = settings .get ("http.basic.password" );
37+ this .user = settings .get ("http.basic.user" , "admin" );
38+ this .password = settings .get ("http.basic.password" , "admin_pw" );
39+ this .whitelist = new HashSet <String >(Arrays .asList (
40+ settings .getAsArray ("http.basic.ipwhitelist" ,
41+ new String []{"localhost" , "127.0.0.1" })));
42+
43+ this .xForwardFor = settings .get ("http.basic.xforward" , "" );
44+ Loggers .getLogger (getClass ()).info ("using {}:{} with whitelist {}, xforward {}" ,
45+ user , password , whitelist , xForwardFor );
3346 }
3447
3548 @ Override
3649 public void internalDispatchRequest (final HttpRequest request , final HttpChannel channel ) {
37- if (shouldLetPass (request ) || authBasic (request )) {
50+ if (authBasic (request ) || isInIPWhitelist (request )) {
3851 super .internalDispatchRequest (request , channel );
39- } else {
52+ } else if (ping (request )) {
53+ // If not authorized do not show version information etc
54+ channel .sendResponse (new StringRestResponse (OK , "{\" pong\" :{}}" ));
55+ } else {
56+ String addr = getAddress (request );
57+ Loggers .getLogger (getClass ()).error ("UNAUTHORIZED type {}, address {}, path {}, request {}, content {}" ,
58+ request .method (), addr , request .path (), request .params (), request .content ().toUtf8 ());
4059 channel .sendResponse (new StringRestResponse (UNAUTHORIZED , "Authentication Required" ));
4160 }
4261 }
4362
44- private boolean shouldLetPass (final HttpRequest request ) {
45- return (request .method () == RestRequest .Method .GET ) && request .path ().equals ("/" );
63+ private boolean ping (final HttpRequest request ) {
64+ String path = request .path ();
65+ return (request .method () == RestRequest .Method .GET ) && path .equals ("/" );
4666 }
4767
4868 private boolean authBasic (final HttpRequest request ) {
4969 String authHeader = request .header ("Authorization" );
5070 if (authHeader == null )
5171 return false ;
5272
53- String [] split = authHeader .split (" " );
54- if (split .length < 1 || !split [0 ].equals ("Basic" ))
73+ String [] split = authHeader .split (" " , 2 );
74+ if (split .length != 2 || !split [0 ].equals ("Basic" ))
5575 return false ;
5676
57- String decoded ;
77+ String decoded = "" ;
5878 try {
5979 decoded = new String (Base64 .decode (split [1 ]));
80+ String [] userAndPassword = decoded .split (":" , 2 );
81+ String givenUser = userAndPassword [0 ];
82+ String givenPass = userAndPassword [1 ];
83+ return this .user .equals (givenUser ) && this .password .equals (givenPass );
6084 } catch (IOException e ) {
61- logger .warn ("Decoding of basic auth failed." );
85+ logger .warn ("Retrieving of user and password failed for " + decoded + " ," + e . getMessage () );
6286 return false ;
6387 }
88+ }
89+
90+ private String getAddress (HttpRequest request ) {
91+ String addr ;
92+ if (xForwardFor .isEmpty ())
93+ addr = request .header ("Host" );
94+ else
95+ // "X-Forwarded-For"
96+ addr = request .header (xForwardFor );
97+
98+ int portIndex = addr .indexOf (":" );
99+ if (portIndex >= 0 )
100+ addr = addr .substring (0 , portIndex );
101+ return addr ;
102+ }
64103
65- String [] userAndPassword = decoded .split (":" );
66- String givenUser = userAndPassword [0 ];
67- String givenPass = userAndPassword [1 ];
68- return this .user .equals (givenUser ) && this .password .equals (givenPass );
104+ private boolean isInIPWhitelist (HttpRequest request ) {
105+ String addr = getAddress (request );
106+ // Loggers.getLogger(getClass()).info("address {}, path {}, request {}",
107+ // addr, request.path(), request.params());
108+ return whitelist .contains (addr );
69109 }
70110}
0 commit comments