Permalink
Browse files

Filter out protected buckets from list

Change-Id: I86959f2b46fe512b57ebed3c7b8ee64afc74179a
Reviewed-on: http://review.couchbase.org/11728
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Tested-by: Matt Ingenthron <matt@couchbase.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
1 parent 17dae89 commit 06ff86fd410558924a87dd69fe71e6316991800b @avsej avsej committed with ingenthr Dec 19, 2011
View
3 src/main/java/org/couchbase/mock/Bucket.java
@@ -140,4 +140,7 @@ public String getPassword() {
return password;
}
+ public String getName() {
+ return name;
+ }
}
View
25 src/main/java/org/couchbase/mock/http/Authenticator.java
@@ -43,12 +43,12 @@ public Authenticator(String username, String password, Map<String, Bucket> bucke
@Override
public boolean checkCredentials(String username, String password) {
- if ("default".equals(bucketName) ||
- (adminName.equals(username) && adminPass.equals(password))) {
+ if (adminName.equals(username) && adminPass.equals(password)) {
return true;
}
- Bucket bucket = buckets.get(bucketName);
- if (bucket == null) {
+
+ Bucket bucket = buckets.get(username);
+ if (bucket == null || !bucketName.equals(username)) {
return false;
}
return bucket.getPassword().equals(password);
@@ -58,15 +58,22 @@ public boolean checkCredentials(String username, String password) {
public Result authenticate(HttpExchange exchange) {
String requestPath = exchange.getRequestURI().getPath();
Matcher m = Pattern.compile("/pools/\\w+/buckets/(\\w+)/?.*").matcher(requestPath);
- bucketName = "default";
+ bucketName = null;
if (m.find()) {
bucketName = m.group(1);
}
- if (bucketName.equals("default")) {
- return new Authenticator.Success(new HttpPrincipal("default", realm));
- } else {
- return super.authenticate(exchange);
+
+ if (!exchange.getRequestHeaders().containsKey("Authorization")) {
+ Bucket bucket = buckets.get(bucketName);
+ if (bucket == null || bucket.getPassword().isEmpty()) {
+ return new Authenticator.Success(new HttpPrincipal("", realm));
+ }
}
+ return super.authenticate(exchange);
+ }
+
+ public String getAdminName() {
+ return adminName;
}
}
View
16 src/main/java/org/couchbase/mock/http/PoolsHandler.java
@@ -24,6 +24,7 @@
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
+import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.couchbase.mock.Bucket;
@@ -43,6 +44,7 @@ public PoolsHandler(CouchbaseMock mock) {
public void handle(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
OutputStream body = exchange.getResponseBody();
+ String bucketName = exchange.getPrincipal().getName();
byte[] payload;
if (path.matches("^/pools/?$")) {
@@ -59,13 +61,15 @@ public void handle(HttpExchange exchange) throws IOException {
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets$")) {
// GET /pools/:poolName/buckets
- StringWriter sw = new StringWriter();
- sw.append("[");
- for (Bucket bb : mock.getBuckets().values()) {
- sw.append(bb.getJSON());
+ JSONArray buckets = new JSONArray();
+ for (Bucket bucket : mock.getBuckets().values()) {
+ if (mock.getAuthenticator().getAdminName().equals(bucketName) || // Administrator
+ (bucketName.isEmpty() && bucket.getPassword().isEmpty()) || // Public
+ bucket.getName().equals(bucketName)) { // Protected
+ buckets.add(JSONObject.fromObject(bucket.getJSON()));
+ }
}
- sw.append("]");
- payload = sw.toString().getBytes();
+ payload = buckets.toString().getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, payload.length);
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets/[^/]+$")) {
View
31 src/test/java/org/couchbase/mock/JMembaseTest.java
@@ -27,12 +27,19 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+import net.sf.json.JSONSerializer;
import org.couchbase.mock.util.Base64;
+
/**
* Basic testing of JMembase
*
@@ -125,6 +132,30 @@ public void testDefaultBucketShouldBeAccessibleForEveryone() throws IOException
}
}
+ public void testProtectedBucketsShouldBeFilteredOutFromList() throws IOException {
+ System.out.println("testProtectedBucketsShouldBeFilteredOutFromList");
+ URL url = new URL("http://localhost:" + instance.getHttpPort() + "/pools/default/buckets");
+ HttpURLConnection conn = null;
+
+ try {
+ conn = (HttpURLConnection) url.openConnection();
+ assertNotNull(conn);
+ assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
+ StringBuilder sb = new StringBuilder();
+ BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ String line;
+ while ((line = in.readLine()) != null) {
+ sb.append(line);
+ }
+ JSONArray json = (JSONArray)JSONSerializer.toJSON(sb.toString());
+ assertEquals(1, json.size());
+ JSONObject bucket = (JSONObject) json.get(0);
+ assertEquals("default", bucket.get("name"));
+ } catch (Exception ex) {
+ fail(ex.getMessage());
+ }
+ }
+
public void testHandleHttpRequestNetwork() throws IOException {
System.out.println("testHandleHttpRequestNetwork");
StringWriter sw = new StringWriter();

0 comments on commit 06ff86f

Please sign in to comment.