com.buaa.cfs.security.ShellBasedUnixGroupsMapping.java Source code

Java tutorial

Introduction

Here is the source code for com.buaa.cfs.security.ShellBasedUnixGroupsMapping.java

Source

/**
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.buaa.cfs.security;

import com.buaa.cfs.utils.Shell;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * A simple shell-based implementation of {@link GroupMappingServiceProvider} that exec's the <code>groups</code> shell
 * command to fetch the group memberships of a given user.
 */
public class ShellBasedUnixGroupsMapping implements GroupMappingServiceProvider {

    private static final Log LOG = LogFactory.getLog(ShellBasedUnixGroupsMapping.class);

    /**
     * Returns list of groups for a user
     *
     * @param user get groups for this user
     *
     * @return list of groups for a given user
     */
    @Override
    public List<String> getGroups(String user) throws IOException {
        return getUnixGroups(user);
    }

    /**
     * Caches groups, no need to do that for this provider
     */
    @Override
    public void cacheGroupsRefresh() throws IOException {
        // does nothing in this provider of user to groups mapping
    }

    /**
     * Adds groups to cache, no need to do that for this provider
     *
     * @param groups unused
     */
    @Override
    public void cacheGroupsAdd(List<String> groups) throws IOException {
        // does nothing in this provider of user to groups mapping
    }

    /**
     * Get the current user's group list from Unix by running the command 'groups' NOTE. For non-existing user it will
     * return EMPTY list
     *
     * @param user user name
     *
     * @return the groups list that the <code>user</code> belongs to. The primary group is returned first.
     *
     * @throws IOException if encounter any error when running the command
     */
    private static List<String> getUnixGroups(final String user) throws IOException {
        String result = "";
        try {
            result = Shell.execCommand(Shell.getGroupsForUserCommand(user));
        } catch (Shell.ExitCodeException e) {
            // if we didn't get the group - just return empty list;
            LOG.warn("got exception trying to get groups for user " + user + ": " + e.getMessage());
            return new LinkedList<String>();
        }

        StringTokenizer tokenizer = new StringTokenizer(result, Shell.TOKEN_SEPARATOR_REGEX);
        List<String> groups = new LinkedList<String>();
        while (tokenizer.hasMoreTokens()) {
            groups.add(tokenizer.nextToken());
        }

        // remove duplicated primary group
        if (!Shell.WINDOWS) {
            for (int i = 1; i < groups.size(); i++) {
                if (groups.get(i).equals(groups.get(0))) {
                    groups.remove(i);
                    break;
                }
            }
        }

        return groups;
    }
}