-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[Zeppelin-1611] - Support PAM (System User) Authentication #1589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
zeppelin-server/src/main/java/org/apache/zeppelin/realm/PamRealm.java
This file contains hidden or 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,91 @@ | ||
| /* | ||
| * 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.realm; | ||
|
|
||
| import org.apache.shiro.authc.AuthenticationException; | ||
| import org.apache.shiro.authc.AuthenticationInfo; | ||
| import org.apache.shiro.authc.AuthenticationToken; | ||
| import org.apache.shiro.authc.SimpleAuthenticationInfo; | ||
| import org.apache.shiro.authc.UsernamePasswordToken; | ||
| import org.apache.shiro.authz.AuthorizationInfo; | ||
| import org.apache.shiro.authz.SimpleAuthorizationInfo; | ||
| import org.apache.shiro.crypto.hash.DefaultHashService; | ||
| import org.apache.shiro.crypto.hash.Hash; | ||
| import org.apache.shiro.crypto.hash.HashRequest; | ||
| import org.apache.shiro.crypto.hash.HashService; | ||
| import org.apache.shiro.realm.AuthorizingRealm; | ||
| import org.apache.shiro.subject.PrincipalCollection; | ||
| import org.jvnet.libpam.PAM; | ||
| import org.jvnet.libpam.PAMException; | ||
| import org.jvnet.libpam.UnixUser; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * An {@code AuthorizingRealm} base on libpam4j. | ||
| */ | ||
| public class PamRealm extends AuthorizingRealm { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ZeppelinHubRealm.class); | ||
|
|
||
| private String service; | ||
|
|
||
| @Override | ||
| protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { | ||
| Set<String> roles = new LinkedHashSet<>(); | ||
|
|
||
| UserPrincipal user = principals.oneByType(UserPrincipal.class); | ||
|
|
||
| if (user != null){ | ||
| roles.addAll(user.getUnixUser().getGroups()); | ||
| } | ||
|
|
||
| return new SimpleAuthorizationInfo(roles); | ||
| } | ||
|
|
||
| @Override | ||
| protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) | ||
| throws AuthenticationException { | ||
|
|
||
| UsernamePasswordToken userToken = (UsernamePasswordToken) token; | ||
| UnixUser user; | ||
|
|
||
| try { | ||
| user = (new PAM(this.getService())) | ||
| .authenticate(userToken.getUsername(), new String(userToken.getPassword())); | ||
| } catch (PAMException e) { | ||
| throw new AuthenticationException("Authentication failed for PAM.", e); | ||
| } | ||
|
|
||
| return new SimpleAuthenticationInfo( | ||
| new UserPrincipal(user), | ||
| userToken.getCredentials(), | ||
| getName()); | ||
| } | ||
|
|
||
| public String getService() { | ||
| return service; | ||
| } | ||
|
|
||
| public void setService(String service) { | ||
| this.service = service; | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
zeppelin-server/src/main/java/org/apache/zeppelin/realm/UserPrincipal.java
This file contains hidden or 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,46 @@ | ||
| /* | ||
| * 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.realm; | ||
|
|
||
| import org.jvnet.libpam.UnixUser; | ||
|
|
||
| import java.security.Principal; | ||
|
|
||
| /** | ||
| * A {@code java.security.Principal} implememtation for use with Shiro {@code PamRealm} | ||
| */ | ||
| public class UserPrincipal implements Principal { | ||
| private final UnixUser userName; | ||
|
|
||
| public UserPrincipal(UnixUser userName) { | ||
| this.userName = userName; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return userName.getUserName(); | ||
| } | ||
|
|
||
| public UnixUser getUnixUser() { | ||
| return userName; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.valueOf(userName.getUserName()); | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
zeppelin-server/src/test/java/org/apache/zeppelin/realm/PamRealmTest.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * 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.realm; | ||
|
|
||
| import org.apache.shiro.authc.AuthenticationInfo; | ||
| import org.apache.shiro.authc.UsernamePasswordToken; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assume.assumeTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * The test will only be executed if the environment variables PAM_USER and PAM_PASS are present. They should | ||
| * contain username and password of an valid system user to make the test pass. The service needs to be configured | ||
| * under /etc/pam.d/sshd to resolve and authenticate the system user. | ||
| * | ||
| * Contains main() function so the test can be executed manually. | ||
| * | ||
| * Set in MacOS to run in IDE(A): | ||
| * $ launchctl setenv PAM_USER user | ||
| * $ launchctl setenv PAM_PASS xxxxx | ||
| */ | ||
| public class PamRealmTest { | ||
|
|
||
| @Test | ||
| public void testDoGetAuthenticationInfo() { | ||
| PamRealm realm = new PamRealm(); | ||
| realm.setService("sshd"); | ||
|
|
||
| String pam_user = System.getenv("PAM_USER"); | ||
| String pam_pass = System.getenv("PAM_PASS"); | ||
| assumeTrue(pam_user != null); | ||
| assumeTrue(pam_pass != null); | ||
|
|
||
| // mock shiro auth token | ||
| UsernamePasswordToken authToken = mock(UsernamePasswordToken.class); | ||
| when(authToken.getUsername()).thenReturn(pam_user); | ||
| when(authToken.getPassword()).thenReturn(pam_pass.toCharArray()); | ||
| when(authToken.getCredentials()).thenReturn(pam_pass); | ||
|
|
||
| AuthenticationInfo authInfo = realm.doGetAuthenticationInfo(authToken); | ||
|
|
||
| assertTrue(authInfo.getCredentials() != null); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| PamRealmTest test = new PamRealmTest(); | ||
| test.testDoGetAuthenticationInfo(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add configuration into
conf/shiro.ini.template?