EronWright commented on a change in pull request #110: BOOKKEEPER-391 Support 
Kerberos authentication of bookkeeper
URL: https://github.com/apache/bookkeeper/pull/110#discussion_r129738693
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/sasl/SaslServerState.java
 ##########
 @@ -0,0 +1,294 @@
+/*
+ Licensed to Diennea S.r.l. under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. Diennea S.r.l. 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.bookkeeper.sasl;
+
+import java.io.IOException;
+import java.security.Principal;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.HashMap;
+import java.util.Map;
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import javax.security.auth.login.LoginException;
+import javax.security.sasl.AuthorizeCallback;
+import javax.security.sasl.RealmCallback;
+import javax.security.sasl.Sasl;
+import javax.security.sasl.SaslException;
+import javax.security.sasl.SaslServer;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.zookeeper.server.auth.KerberosName;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Server side Sasl implementation
+ */
+
+public class SaslServerState {
+
+    private static final org.slf4j.Logger LOG = 
LoggerFactory.getLogger(SaslServerState.class);
+
+    private SaslServer saslServer;
+    private ServerConfiguration serverConfiguration;
+
+    public SaslServerState(ServerConfiguration serverConfiguration) throws 
IOException, SaslException, LoginException {
+        this.serverConfiguration = serverConfiguration;
+        Subject subject = loginServer();
+        if (subject == null) {
+            throw new IOException("Could not init JAAS server subject");
+        }
+        saslServer = createSaslServer(subject, serverConfiguration);
+    }
+
+    private SaslServer createSaslServer(final Subject subject, 
ServerConfiguration serverConfiguration) throws SaslException, IOException {
+
+        SaslServerCallbackHandler callbackHandler = new 
SaslServerCallbackHandler(Configuration.getConfiguration(),
+            serverConfiguration);
+        if (subject.getPrincipals().size() > 0) {
+            try {
+                final Object[] principals = subject.getPrincipals().toArray();
+                final Principal servicePrincipal = (Principal) principals[0];
+                LOG.debug("Authentication will use SASL/JAAS/Kerberos, 
servicePrincipal is {}", servicePrincipal);
+
+                final String servicePrincipalNameAndHostname = 
servicePrincipal.getName();
+                int indexOf = servicePrincipalNameAndHostname.indexOf("/");
+                final String serviceHostnameAndKerbDomain = 
servicePrincipalNameAndHostname.substring(indexOf + 1,
+                    servicePrincipalNameAndHostname.length());
+                int indexOfAt = serviceHostnameAndKerbDomain.indexOf("@");
+
+                final String servicePrincipalName, serviceHostname;
+                if (indexOf > 0) {
+                    servicePrincipalName = 
servicePrincipalNameAndHostname.substring(0, indexOf);
+                    serviceHostname = 
serviceHostnameAndKerbDomain.substring(0, indexOfAt);
+                } else {
+                    servicePrincipalName = 
servicePrincipalNameAndHostname.substring(0, indexOfAt);
+                    serviceHostname = null;
+                }
+
+                try {
+                    return Subject.doAs(subject, new 
PrivilegedExceptionAction<SaslServer>() {
+                        @Override
+                        public SaslServer run() {
+                            try {
+                                SaslServer saslServer;
+                                saslServer = Sasl.createSaslServer("GSSAPI", 
servicePrincipalName, serviceHostname, null,
+                                    callbackHandler);
+                                return saslServer;
+                            } catch (SaslException e) {
+                                throw new RuntimeException(e);
+                            }
+                        }
+                    }
+                    );
+                } catch (PrivilegedActionException e) {
+                    throw new SaslException("error on GSSAPI boot", 
e.getCause());
+                }
+            } catch (IndexOutOfBoundsException e) {
+                throw new SaslException("error on GSSAPI boot", e);
+            }
+        } else {
+            LOG.debug("Authentication will use SASL/JAAS/DIGEST-MD5");
+            return Sasl.createSaslServer("DIGEST-MD5", 
SaslConstants.SASL_BOOKKEEPER_PROTOCOL,
+                SaslConstants.SASL_MD5_DUMMY_HOSTNAME, null, callbackHandler);
+        }
+    }
+
+    private Subject loginServer() throws SaslException, LoginException {
+
+        String configurationEntry = 
serverConfiguration.getString(SaslConstants.JAAS_BOOKIE_SECTION_NAME,
+            SaslConstants.JAAS_DEFAULT_BOOKIE_SECTION_NAME);
+
+        AppConfigurationEntry[] entries = Configuration.getConfiguration()
+            .getAppConfigurationEntry(configurationEntry);
+        if (entries == null) {
+            LOG.info("JAAS not configured or no "
+                + configurationEntry + " present in JAAS Configuration file");
+            return null;
+        }
+        LoginContext loginContext = new LoginContext(configurationEntry, new 
ClientCallbackHandler(null));
+        loginContext.login();
+        return loginContext.getSubject();
+
+    }
+
+    private static class ClientCallbackHandler implements CallbackHandler {
+
+        private String password = null;
+
+        public ClientCallbackHandler(String password) {
+            this.password = password;
+        }
+
+        @Override
+        public void handle(Callback[] callbacks) throws
+            UnsupportedCallbackException {
+            for (Callback callback : callbacks) {
+                if (callback instanceof NameCallback) {
+                    NameCallback nc = (NameCallback) callback;
+                    nc.setName(nc.getDefaultName());
+                } else {
+                    if (callback instanceof PasswordCallback) {
+                        PasswordCallback pc = (PasswordCallback) callback;
+                        if (password != null) {
+                            pc.setPassword(this.password.toCharArray());
+                        }
+                    } else {
+                        if (callback instanceof RealmCallback) {
+                            RealmCallback rc = (RealmCallback) callback;
+                            rc.setText(rc.getDefaultText());
+                        } else {
+                            if (callback instanceof AuthorizeCallback) {
+                                AuthorizeCallback ac = (AuthorizeCallback) 
callback;
+                                String authid = ac.getAuthenticationID();
+                                String authzid = ac.getAuthorizationID();
+                                if (authid.equals(authzid)) {
+                                    ac.setAuthorized(true);
+                                } else {
+                                    ac.setAuthorized(false);
+                                }
+                                if (ac.isAuthorized()) {
+                                    ac.setAuthorizedID(authzid);
+                                }
+                            } else {
+                                throw new 
UnsupportedCallbackException(callback, "Unrecognized SASL ClientCallback");
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    public boolean isComplete() {
+        return saslServer.isComplete();
+    }
+
+    public String getUserName() {
+        return saslServer.getAuthorizationID();
+    }
+
+    public byte[] response(byte[] token) throws SaslException {
+        try {
+            byte[] retval = saslServer.evaluateResponse(token);
+            return retval;
+        } catch (SaslException e) {
+            LOG.error("response: Failed to evaluate client token", e);
+            throw e;
+        }
+    }
+
+    private static class SaslServerCallbackHandler implements CallbackHandler {
+
+        private static final String USER_PREFIX = "user_";
+
+        private String userName;
+        private final Map<String, String> credentials = new HashMap<>();
+
+        public SaslServerCallbackHandler(Configuration configuration, 
ServerConfiguration serverConfiguration) throws IOException {
+            String configurationEntry = 
serverConfiguration.getString(SaslConstants.JAAS_BOOKIE_SECTION_NAME,
+                SaslConstants.JAAS_DEFAULT_BOOKIE_SECTION_NAME);
+            AppConfigurationEntry configurationEntries[] = 
configuration.getAppConfigurationEntry(configurationEntry);
+
+            if (configurationEntries == null) {
+                String errorMessage = "Could not find a '" + 
configurationEntry + "' entry in this configuration: Server cannot start.";
+
+                throw new IOException(errorMessage);
+            }
+            credentials.clear();
+            for (AppConfigurationEntry entry : configurationEntries) {
+                Map<String, ?> options = entry.getOptions();
+                // Populate DIGEST-MD5 user -> password map with JAAS 
configuration entries from the "Server" section.
+                // Usernames are distinguished from other options by prefixing 
the username with a "user_" prefix.
+                for (Map.Entry<String, ?> pair : options.entrySet()) {
+                    String key = pair.getKey();
+                    if (key.startsWith(USER_PREFIX)) {
+                        String userName = key.substring(USER_PREFIX.length());
+                        credentials.put(userName, (String) pair.getValue());
+                    }
+                }
+            }
+        }
+
+        public void handle(Callback[] callbacks) throws 
UnsupportedCallbackException {
+            for (Callback callback : callbacks) {
+                if (callback instanceof NameCallback) {
+                    handleNameCallback((NameCallback) callback);
+                } else if (callback instanceof PasswordCallback) {
+                    handlePasswordCallback((PasswordCallback) callback);
+                } else if (callback instanceof RealmCallback) {
+                    handleRealmCallback((RealmCallback) callback);
+                } else if (callback instanceof AuthorizeCallback) {
+                    handleAuthorizeCallback((AuthorizeCallback) callback);
+                }
+            }
+        }
+
+        private void handleNameCallback(NameCallback nc) {
+            // check to see if this user is in the user password database.
+            if (credentials.get(nc.getDefaultName()) == null) {
+                LOG.error("User '" + nc.getDefaultName() + "' not found in 
list of JAAS DIGEST-MD5 users.");
+                return;
+            }
+            nc.setName(nc.getDefaultName());
+            userName = nc.getDefaultName();
+        }
+
+        private void handlePasswordCallback(PasswordCallback pc) {
+            if (credentials.containsKey(userName)) {
+                pc.setPassword(credentials.get(userName).toCharArray());
+            } else {
+                LOG.info("No password found for user: " + userName);
+            }
+        }
+
+        private void handleRealmCallback(RealmCallback rc) {
+            LOG.debug("client supplied realm: " + rc.getDefaultText());
+            rc.setText(rc.getDefaultText());
+        }
+
+        private void handleAuthorizeCallback(AuthorizeCallback ac) {
+            String authenticationID = ac.getAuthenticationID();
+            String authorizationID = ac.getAuthorizationID();
 
 Review comment:
   To add some color, this is similar to the proxy user support that is 
pervasive in Hadoop.  For example, you configure HDFS to trust the YARN service 
to access files on behalf of users that are interacting with YARN.
   
   In the Bookkeeper context, the service would be configured to treat the user 
referenced by `authenticationID` as a superuser account that is allowed to 
impersonate other users.  After authentication succeeds, you use the user 
referenced by `authorizationId` as the basis for authorization decisions, i.e. 
as the effective user.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to