Skip to content

Commit c2c8cc2

Browse files
author
Sebastian Choina
committed
Added basic authorization via HTTP Header to bypass all ACL checking.
1 parent a2a2e24 commit c2c8cc2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/main/java/org/elasticsearch/rest/action/readonlyrest/ConfigurationHelper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.elasticsearch.rest.action.readonlyrest;
22

3+
import org.elasticsearch.common.Base64;
4+
import org.elasticsearch.common.base.Charsets;
35
import org.elasticsearch.common.logging.ESLogger;
46
import org.elasticsearch.common.settings.Settings;
57

@@ -19,6 +21,8 @@ public class ConfigurationHelper {
1921
private final static String K_RESP_REQ_FORBIDDEN = "response_if_req_forbidden";
2022
final public boolean enabled;
2123
final public String forbiddenResponse;
24+
final public String authKeyBase64;
25+
private final static String K_AUTH_KEY = "auth_key";
2226

2327

2428
public ConfigurationHelper(Settings settings, ESLogger logger) {
@@ -44,6 +48,16 @@ public ConfigurationHelper(Settings settings, ESLogger logger) {
4448
this.forbiddenResponse = t;
4549
}
4650

51+
String key = s.get(K_AUTH_KEY);
52+
if (key != null) {
53+
key = key.trim();
54+
}
55+
if (isNullOrEmpty(key)) {
56+
this.authKeyBase64 = null;
57+
} else {
58+
this.authKeyBase64 = Base64.encodeBytes(key.getBytes(Charsets.UTF_8));
59+
}
60+
4761
}
4862

4963
public static boolean isNullOrEmpty(String s){

src/main/java/org/elasticsearch/rest/action/readonlyrest/ReadonlyRestAction.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* <pre>
2424
* readonlyrest:
2525
* enable: true
26+
* auth_key: secretAuthKey // this can bypasses all other rules and allows for operation if matched
2627
* allow_localhost: true
2728
* whitelist: [192.168.1.144]
2829
* forbidden_uri_re: .*bar_me_pls.*
@@ -57,6 +58,13 @@ public ReadonlyRestAction(final Settings settings, Client client, RestController
5758

5859
@Override
5960
public void process(RestRequest request, RestChannel channel, RestFilterChain filterChain) {
61+
if (isAuthorisedToBypassACL(request, conf)) {
62+
logger.debug("Auth ok, will bypass filters");
63+
ok(request, filterChain, channel);
64+
return;
65+
} else {
66+
logger.debug("Cannot bypass filters via Authorization");
67+
}
6068
ACLRequest aclReq = new ACLRequest(request, channel);
6169
String reason = acl.check(aclReq);
6270
if(reason == null){
@@ -73,6 +81,21 @@ public void process(RestRequest request, RestChannel channel, RestFilterChain fi
7381
}
7482
});
7583
}
84+
85+
protected boolean isAuthorisedToBypassACL(RestRequest request, ConfigurationHelper conf) {
86+
logger.debug("Auth key: {}", conf.authKeyBase64);
87+
if (conf.authKeyBase64 == null) {
88+
return false;
89+
}
90+
String authVal = request.header("Authorization");
91+
logger.debug("Auth header: {}", authVal);
92+
if (authVal == null) {
93+
return false;
94+
}
95+
String val = authVal.replace("Basic ", "").trim();
96+
return val.equals(conf.authKeyBase64);
97+
}
98+
7699
public void ok(RestRequest request, RestFilterChain filterChain, RestChannel channel ){
77100
filterChain.continueProcessing(request, channel);
78101
}

0 commit comments

Comments
 (0)