-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Websocket security is done in the next commit
- Loading branch information
Showing
13 changed files
with
390 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
[users] | ||
# List of users with their password allowed to access Zeppelin. | ||
# To use a different strategy (LDAP / Database / ...) check the shiro doc at http://shiro.apache.org/configuration.html#Configuration-INISections | ||
admin = password1 | ||
user1 = password2 | ||
user2 = password3 | ||
|
||
[main] | ||
|
||
# Let's use some in-memory caching to reduce the number of runtime lookups against Stormpath. | ||
# A real application might want to use a more robust caching solution (e.g. ehcache or a | ||
# distributed cache). When using such caches, be aware of your cache TTL settings: too high | ||
# a TTL and the cache won't reflect any potential changes in Stormpath fast enough. Too low | ||
# and the cache could evict too often, reducing performance. | ||
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager | ||
securityManager.cacheManager = $cacheManager | ||
|
||
|
||
[urls] | ||
|
||
# anon means the access is anonymous. | ||
# authcBasic means Basic Auth Security | ||
# To enfore security, comment the line below and uncomment the next one | ||
/** = anon | ||
#/** = authcBasic | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#Shiro Authentication | ||
To connect to Zeppelin, users will be asked to enter their credentials. Once logged, a user has access to all notes including other users notes. | ||
This a a first step toward full security as implemented by this pull request (https://github.com/apache/incubator-zeppelin/pull/53). | ||
|
||
#Security setup | ||
1. Secure the HTTP channel: Comment the line "/** = anon" and uncomment the line "/** = authcBasic" in the file conf/shiro.ini. Read more about he shiro.ini file format at the following URL http://shiro.apache.org/configuration.html#Configuration-INISections. | ||
2. Secure the Websocket channel : Set to property "zeppelin.anonymous.allowed" to "true" in the file conf/zeppelin-site.xml. You can start by renaming conf/zeppelin-site.xml.template to conf/zeppelin-site.xml | ||
3. Start Zeppelin : bin/zeppelin.sh | ||
4. point your browser to http://localhost:8080 | ||
5. Login using one of the user/password combinations defined in the conf/shiro.ini file. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zeppelin.rest; | ||
|
||
import org.apache.zeppelin.conf.ZeppelinConfiguration; | ||
import org.apache.zeppelin.server.JsonResponse; | ||
import org.apache.zeppelin.ticket.TicketContainer; | ||
import org.apache.zeppelin.utils.SecurityUtils; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Zeppelin security rest api endpoint. | ||
* | ||
*/ | ||
@Path("/security") | ||
@Produces("application/json") | ||
public class SecurityRestApi { | ||
/** | ||
* Required by Swagger. | ||
*/ | ||
public SecurityRestApi() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Get ticket | ||
* Returns username & ticket | ||
* for anonymous access, username is always anonymous. | ||
* After getting this ticket, access through websockets become safe | ||
* | ||
* @return 200 response | ||
*/ | ||
@GET | ||
@Path("ticket") | ||
public Response ticket() { | ||
ZeppelinConfiguration conf = ZeppelinConfiguration.create(); | ||
String principal = SecurityUtils.getPrincipal(); | ||
JsonResponse response; | ||
// ticket set to anonymous for anonymous user. Simplify testing. | ||
String ticket; | ||
if ("anonymous".equals(principal)) | ||
ticket = "anonymous"; | ||
else | ||
ticket = TicketContainer.instance.getTicket(principal); | ||
|
||
Map<String, String> data = new HashMap<>(); | ||
data.put("principal", principal); | ||
data.put("ticket", ticket); | ||
|
||
response = new JsonResponse(Response.Status.OK, "", data); | ||
return response.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
zeppelin-server/src/main/java/org/apache/zeppelin/ticket/TicketContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zeppelin.ticket; | ||
|
||
import java.util.Calendar; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Very simple ticket container | ||
* No cleanup is done, since the same user accross different devices share the same ticket | ||
* The Map size is at most the number of different user names having access to a Zeppelin instance | ||
*/ | ||
|
||
|
||
public class TicketContainer { | ||
private static class Entry { | ||
public final String ticket; | ||
// lastAccessTime still unused | ||
public final long lastAccessTime; | ||
|
||
Entry(String ticket) { | ||
this.ticket = ticket; | ||
this.lastAccessTime = Calendar.getInstance().getTimeInMillis(); | ||
} | ||
} | ||
|
||
private Map<String, Entry> sessions = new ConcurrentHashMap<>(); | ||
|
||
public static final TicketContainer instance = new TicketContainer(); | ||
|
||
/** | ||
* For test use | ||
* @param principal | ||
* @param ticket | ||
* @return true if ticket assigned to principal. | ||
*/ | ||
public boolean isValid(String principal, String ticket) { | ||
if ("anonymous".equals(principal) && "anonymous".equals(ticket)) | ||
return true; | ||
Entry entry = sessions.get(principal); | ||
return entry != null && entry.ticket.equals(ticket); | ||
} | ||
|
||
/** | ||
* get or create ticket for Websocket authentication assigned to authenticated shiro user | ||
* For unathenticated user (anonymous), always return ticket value "anonymous" | ||
* @param principal | ||
* @return | ||
*/ | ||
public synchronized String getTicket(String principal) { | ||
Entry entry = sessions.get(principal); | ||
String ticket; | ||
if (entry == null) { | ||
if (principal.equals("anonymous")) | ||
ticket = "anonymous"; | ||
else | ||
ticket = UUID.randomUUID().toString(); | ||
} else { | ||
ticket = entry.ticket; | ||
} | ||
entry = new Entry(ticket); | ||
sessions.put(principal, entry); | ||
return ticket; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
zeppelin-server/src/test/java/org/apache/zeppelin/rest/SecurityRestApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.zeppelin.rest; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import org.apache.commons.httpclient.methods.GetMethod; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SecurityRestApiTest extends AbstractTestRestApi { | ||
Gson gson = new Gson(); | ||
|
||
@BeforeClass | ||
public static void init() throws Exception { | ||
AbstractTestRestApi.startUp(); | ||
} | ||
|
||
@AfterClass | ||
public static void destroy() throws Exception { | ||
AbstractTestRestApi.shutDown(); | ||
} | ||
|
||
@Test | ||
public void testTicket() throws IOException { | ||
GetMethod get = httpGet("/security/ticket"); | ||
get.addRequestHeader("Origin", "http://localhost"); | ||
Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), | ||
new TypeToken<Map<String, Object>>(){}.getType()); | ||
Map<String, String> body = (Map<String, String>) resp.get("body"); | ||
assertEquals("anonymous", body.get("principal")); | ||
assertEquals("anonymous", body.get("ticket")); | ||
get.releaseConnection(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.