org.apache.ranger.authorization.hadoop.RangerHdfsAuthorizer.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.ranger.authorization.hadoop.RangerHdfsAuthorizer.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
 * 
 * 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.ranger.authorization.hadoop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hdfs.server.namenode.INodeAttributeProvider;
import org.apache.hadoop.hdfs.server.namenode.INodeAttributes;
import org.apache.ranger.plugin.classloader.RangerPluginClassLoader;

public class RangerHdfsAuthorizer extends INodeAttributeProvider {
    private static final Log LOG = LogFactory.getLog(RangerHdfsAuthorizer.class);

    private static final String RANGER_PLUGIN_TYPE = "hdfs";
    private static final String[] RANGER_PLUGIN_LIB_DIR = new String[] { "lib/ranger-hdfs-plugin" };
    private static final String RANGER_HDFS_AUTHORIZER_IMPL_CLASSNAME = "org.apache.ranger.authorization.hadoop.RangerHdfsAuthorizer";

    private INodeAttributeProvider rangerHdfsAuthorizerImpl = null;
    private static RangerPluginClassLoader rangerPluginClassLoader = null;

    public RangerHdfsAuthorizer() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.RangerHdfsAuthorizer()");
        }

        this.init();

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.RangerHdfsAuthorizer()");
        }
    }

    public void init() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.init()");
        }

        try {

            rangerPluginClassLoader = RangerPluginClassLoader.getInstance(RANGER_PLUGIN_TYPE, this.getClass());

            @SuppressWarnings("unchecked")
            Class<INodeAttributeProvider> cls = (Class<INodeAttributeProvider>) Class
                    .forName(RANGER_HDFS_AUTHORIZER_IMPL_CLASSNAME, true, rangerPluginClassLoader);

            activatePluginClassLoader();

            rangerHdfsAuthorizerImpl = cls.newInstance();
        } catch (Exception e) {
            // check what need to be done
            LOG.error("Error Enabling RangerHdfsPluing", e);
        } finally {
            deactivatePluginClassLoader();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.init()");
        }
    }

    @Override
    public void start() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.start()");
        }

        try {
            activatePluginClassLoader();

            rangerHdfsAuthorizerImpl.start();
        } finally {
            deactivatePluginClassLoader();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.start()");
        }
    }

    @Override
    public void stop() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.stop()");
        }

        try {
            activatePluginClassLoader();

            rangerHdfsAuthorizerImpl.stop();
        } finally {
            deactivatePluginClassLoader();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.stop()");
        }
    }

    @Override
    public INodeAttributes getAttributes(String fullPath, INodeAttributes inode) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.getAttributes(" + fullPath + ")");
        }

        INodeAttributes ret = null;

        try {
            activatePluginClassLoader();

            ret = rangerHdfsAuthorizerImpl.getAttributes(fullPath, inode); // return default attributes
        } finally {
            deactivatePluginClassLoader();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.getAttributes(" + fullPath + "): " + ret);
        }

        return ret;
    }

    @Override
    public INodeAttributes getAttributes(String[] pathElements, INodeAttributes inode) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.getAttributes(pathElementsCount="
                    + (pathElements == null ? 0 : pathElements.length) + ")");
        }

        INodeAttributes ret = null;

        try {
            activatePluginClassLoader();

            ret = rangerHdfsAuthorizerImpl.getAttributes(pathElements, inode);
        } finally {
            deactivatePluginClassLoader();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.getAttributes(pathElementsCount="
                    + (pathElements == null ? 0 : pathElements.length) + "): " + ret);
        }

        return ret;
    }

    @Override
    public AccessControlEnforcer getExternalAccessControlEnforcer(AccessControlEnforcer defaultEnforcer) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> RangerHdfsAuthorizer.getExternalAccessControlEnforcer()");
        }

        AccessControlEnforcer ret = null;

        ret = rangerHdfsAuthorizerImpl.getExternalAccessControlEnforcer(defaultEnforcer);

        if (LOG.isDebugEnabled()) {
            LOG.debug("<== RangerHdfsAuthorizer.getExternalAccessControlEnforcer()");
        }

        return ret;
    }

    private void activatePluginClassLoader() {
        if (rangerPluginClassLoader != null) {
            rangerPluginClassLoader.activate();
        }
    }

    private void deactivatePluginClassLoader() {
        if (rangerPluginClassLoader != null) {
            rangerPluginClassLoader.deactivate();
        }
    }
}