Skip to content

Commit 2504662

Browse files
committed
Straight to 1.0
0 parents  commit 2504662

File tree

9 files changed

+241
-0
lines changed

9 files changed

+241
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.class
2+
*.jar
3+
*.zip

COPYING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
elasticsearch-http-basic is Copyright (c) 2011, Florian and Felix Gilcher

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2011 Florian Gilcher <florian.gilcher@asquera.de>, Felix Gilcher <felix.gilcher@asquera.de>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# HTTP Basic auth for ElasticSearch
2+
3+
This plugin provides an extension of ElasticSearchs HTTP Transport module to enable HTTP Basic authorization.
4+
5+
## Installation
6+
7+
Copy `elasticsearch-http-basic-<version>.jar` to `plugins/http-basic`. Or use `bin/plugin` to do so.
8+
9+
## Configuration
10+
11+
The plugin is disabled by default. Enabling basic authorization will disable the default HTTP Transport module.
12+
13+
```
14+
http.basic.enable = true
15+
http.basic.user = "my_username"
16+
http.basic.password = "my_password"
17+
```
18+
19+
Be aware that the password is stored in plain text.

build.gradle

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'java'
2+
3+
version = '1.0'
4+
5+
sourceSets.main.resources.srcDirs 'src/main/java'
6+
sourceSets.test.resources.srcDirs 'src/test/java'
7+
8+
configurations {
9+
distLib
10+
}
11+
12+
if (hasProperty("xlint")) {
13+
tasks.withType(Compile) {
14+
options.compilerArgs << "-Xlint:unchecked"
15+
}
16+
}
17+
18+
jar {
19+
manifest {
20+
attributes 'Implementation-Title': 'Elasticsearch HTTP Basic', 'Implementation-Version': '1.0'
21+
}
22+
from sourceSets.main
23+
}
24+
25+
repositories {
26+
mavenCentral()
27+
mavenRepo name: "sonatype", url: "https://oss.sonatype.org/content/groups/public/"
28+
}
29+
30+
dependencies {
31+
compile group: "org.elasticsearch", name: 'elasticsearch', version: "0.18.4"
32+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.asquera.elasticsearch.plugins.http;
2+
3+
import org.elasticsearch.http.*;
4+
import org.elasticsearch.common.settings.Settings;
5+
import org.elasticsearch.env.Environment;
6+
import org.elasticsearch.node.service.NodeService;
7+
import org.elasticsearch.rest.RestController;
8+
import org.elasticsearch.common.inject.Inject;
9+
import org.elasticsearch.common.Base64;
10+
11+
import org.elasticsearch.rest.StringRestResponse;
12+
13+
import static org.elasticsearch.rest.RestStatus.*;
14+
15+
import java.io.IOException;
16+
17+
public class HttpBasicServer extends HttpServer {
18+
private final String user;
19+
private final String password;
20+
21+
@Inject public HttpBasicServer(Settings settings, Environment environment, HttpServerTransport transport,
22+
RestController restController,
23+
NodeService nodeService) {
24+
super(settings, environment, transport, restController, nodeService);
25+
26+
this.user = settings.get("http.basic.user");
27+
this.password = settings.get("http.basic.password");
28+
}
29+
30+
public void internalDispatchRequest(final HttpRequest request, final HttpChannel channel) {
31+
if (authBasic(request)) {
32+
super.internalDispatchRequest(request, channel);
33+
} else {
34+
channel.sendResponse(new StringRestResponse(FORBIDDEN));
35+
}
36+
}
37+
38+
private boolean authBasic(final HttpRequest request){
39+
String authHeader = request.header("Authorization");
40+
41+
if (authHeader == null) {
42+
return false;
43+
}
44+
45+
String[] split = authHeader.split(" ");
46+
String decoded = null;
47+
48+
try {
49+
decoded = new String(Base64.decode(split[1]));
50+
} catch (IOException e) {
51+
logger.warn("Decoding of basic auth failed.");
52+
return false;
53+
}
54+
55+
String[] user_and_password = decoded.split(":");
56+
String given_user = user_and_password[0];
57+
String given_pass = user_and_password[1];
58+
59+
if (this.user.equals(given_user) &&
60+
this.password.equals(given_pass)) {
61+
return true;
62+
} else {
63+
return false;
64+
}
65+
}
66+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.asquera.elasticsearch.plugins.http;
21+
22+
import org.elasticsearch.common.inject.AbstractModule;
23+
import com.asquera.elasticsearch.plugins.http.HttpBasicServer;
24+
import org.elasticsearch.http.HttpServerModule;
25+
import org.elasticsearch.common.settings.Settings;
26+
27+
/**
28+
* @author Florian Gilcher (florian.gilcher@asquera.de)
29+
*/
30+
public class HttpBasicServerModule extends HttpServerModule {
31+
32+
public HttpBasicServerModule(Settings settings) {
33+
super(settings);
34+
}
35+
36+
@Override protected void configure() {
37+
bind(HttpBasicServer.class).asEagerSingleton();
38+
}
39+
}
40+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.asquera.elasticsearch.plugins.http;
2+
3+
import org.elasticsearch.common.inject.Inject;
4+
import org.elasticsearch.common.inject.Module;
5+
import org.elasticsearch.common.settings.Settings;
6+
import org.elasticsearch.plugins.AbstractPlugin;
7+
import org.elasticsearch.http.HttpServerModule;
8+
import org.elasticsearch.http.HttpServer;
9+
import com.asquera.elasticsearch.plugins.http.HttpBasicServer;
10+
import com.asquera.elasticsearch.plugins.http.HttpBasicServerModule;
11+
import org.elasticsearch.common.component.LifecycleComponent;
12+
import org.elasticsearch.common.settings.ImmutableSettings;
13+
14+
import java.util.Collection;
15+
16+
import static org.elasticsearch.common.collect.Lists.*;
17+
18+
/**
19+
* @author Florian Gilcher (florian.gilcher@asquera.de)
20+
*/
21+
public class HttpBasicServerPlugin extends AbstractPlugin {
22+
private final Settings settings;
23+
24+
@Inject public HttpBasicServerPlugin(Settings settings) {
25+
this.settings = settings;
26+
}
27+
28+
@Override public String name() {
29+
return "http-basic-server-plugin";
30+
}
31+
32+
@Override public String description() {
33+
return "HTTP Basic Server Plugin";
34+
}
35+
36+
@Override public Collection<Class<? extends Module>> modules() {
37+
Collection<Class<? extends Module>> modules = newArrayList();
38+
if (settings.getAsBoolean("http.basic.enabled", false)) {
39+
modules.add(HttpBasicServerModule.class);
40+
}
41+
return modules;
42+
}
43+
44+
@Override public Collection<Class<? extends LifecycleComponent>> services() {
45+
Collection<Class<? extends LifecycleComponent>> services = newArrayList();
46+
if (settings.getAsBoolean("http.basic.enabled", false)) {
47+
services.add(HttpBasicServer.class);
48+
}
49+
return services;
50+
}
51+
52+
@Override public Settings additionalSettings() {
53+
if (settings.getAsBoolean("http.basic.enabled", false)) {
54+
return ImmutableSettings.settingsBuilder().put("http.enabled", false).build();
55+
} else {
56+
return ImmutableSettings.Builder.EMPTY_SETTINGS;
57+
}
58+
}
59+
}

src/main/java/es-plugin.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
plugin=com.asquera.elasticsearch.plugins.http.HttpBasicServerPlugin

0 commit comments

Comments
 (0)