Search in sources :

Example 86 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project oozie by apache.

the class TestJavaActionExecutor method testChildKill.

public void testChildKill() throws Exception {
    final JobConf clusterConf = createJobConf();
    FileSystem fileSystem = FileSystem.get(clusterConf);
    Path confFile = new Path("/tmp/cluster-conf.xml");
    OutputStream out = fileSystem.create(confFile);
    clusterConf.writeXml(out);
    out.close();
    String confFileName = fileSystem.makeQualified(confFile).toString() + "#core-site.xml";
    final String actionXml = "<java>" + "<job-tracker>" + getJobTrackerUri() + "</job-tracker>" + "<name-node>" + getNameNodeUri() + "</name-node>" + "<main-class> " + SleepJob.class.getName() + " </main-class>" + "<arg>-mt</arg>" + "<arg>300000</arg>" + "<archive>" + confFileName + "</archive>" + "</java>";
    final Context context = createContext(actionXml, null);
    final String runningJob = submitAction(context);
    YarnApplicationState state = waitUntilYarnAppState(runningJob, EnumSet.of(YarnApplicationState.RUNNING));
    assertEquals(YarnApplicationState.RUNNING, state);
    WorkflowJob wfJob = context.getWorkflow();
    Configuration conf = null;
    if (wfJob.getConf() != null) {
        conf = new XConfiguration(new StringReader(wfJob.getConf()));
    }
    String launcherTag = LauncherHelper.getActionYarnTag(conf, wfJob.getParentId(), context.getAction());
    JavaActionExecutor ae = new JavaActionExecutor();
    final Configuration jobConf = ae.createBaseHadoopConf(context, XmlUtils.parseXml(actionXml));
    jobConf.set(LauncherMain.CHILD_MAPREDUCE_JOB_TAGS, LauncherHelper.getTag(launcherTag));
    jobConf.setLong(LauncherMain.OOZIE_JOB_LAUNCH_TIME, context.getAction().getStartTime().getTime());
    // We have to use a proper UGI for retrieving the child apps, because the WF is
    // submitted as a test user, not as the current login user
    UserGroupInformationService ugiService = Services.get().get(UserGroupInformationService.class);
    final UserGroupInformation ugi = ugiService.getProxyUser(getTestUser());
    final Set<ApplicationId> childSet = new HashSet<>();
    // wait until we have a child MR job
    waitFor(60_000, new Predicate() {

        @Override
        public boolean evaluate() throws Exception {
            return ugi.doAs(new PrivilegedExceptionAction<Boolean>() {

                @Override
                public Boolean run() throws Exception {
                    childSet.clear();
                    childSet.addAll(LauncherMain.getChildYarnJobs(jobConf));
                    return childSet.size() > 0;
                }
            });
        }
    });
    assertEquals(1, childSet.size());
    // kill the action - based on the job tag, the SleepJob is expected to be killed too
    ae.kill(context, context.getAction());
    HadoopAccessorService hadoopAccessorService = getHadoopAccessorService();
    Configuration config = hadoopAccessorService.createConfiguration(getJobTrackerUri());
    YarnClient yarnClient = hadoopAccessorService.createYarnClient(getTestUser(), config);
    // check that both the launcher & MR job were successfully killed
    ApplicationId jobId = childSet.iterator().next();
    assertEquals(YarnApplicationState.KILLED, yarnClient.getApplicationReport(jobId).getYarnApplicationState());
    assertTrue(ae.isCompleted(context.getAction().getExternalStatus()));
    assertEquals(WorkflowAction.Status.DONE, context.getAction().getStatus());
    assertEquals(JavaActionExecutor.KILLED, context.getAction().getExternalStatus());
    assertEquals(FinalApplicationStatus.KILLED, yarnClient.getApplicationReport(ConverterUtils.toApplicationId(runningJob)).getFinalApplicationStatus());
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) XConfiguration(org.apache.oozie.util.XConfiguration) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) HadoopAccessorService(org.apache.oozie.service.HadoopAccessorService) ActionExecutorException(org.apache.oozie.action.ActionExecutorException) IOException(java.io.IOException) YarnClient(org.apache.hadoop.yarn.client.api.YarnClient) UserGroupInformationService(org.apache.oozie.service.UserGroupInformationService) XConfiguration(org.apache.oozie.util.XConfiguration) FileSystem(org.apache.hadoop.fs.FileSystem) StringReader(java.io.StringReader) WorkflowJob(org.apache.oozie.client.WorkflowJob) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) JobConf(org.apache.hadoop.mapred.JobConf) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) HashSet(java.util.HashSet)

Example 87 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project derby by apache.

the class NetworkServerControlImpl method blockingStart.

/**
 * Start a network server
 *
 * @param consoleWriter   PrintWriter to which server console will be
 *                        output. Null will disable console output.
 *
 * @exception Exception throws an exception if an error occurs
 */
public void blockingStart(PrintWriter consoleWriter) throws Exception {
    startNetworkServer();
    setLogWriter(consoleWriter);
    cloudscapeLogWriter = Monitor.getStream().getPrintWriter();
    if (SanityManager.DEBUG && debugOutput) {
        memCheck.showmem();
        mc = new memCheck(200000);
        mc.start();
    }
    // Open a server socket listener
    try {
        serverSocket = AccessController.doPrivileged(new PrivilegedExceptionAction<ServerSocket>() {

            public ServerSocket run() throws IOException {
                return createServerSocket();
            }
        });
    } catch (PrivilegedActionException e) {
        Exception e1 = e.getException();
        // throws an exception when the severity is S (or U).
        if (e1 instanceof UnknownHostException) {
            consolePropertyMessage("DRDA_UnknownHost.S", hostArg);
        } else if (e1 instanceof IOException) {
            consolePropertyMessage("DRDA_ListenPort.S", new String[] { Integer.toString(portNumber), hostArg, // string to the user.
            e1.toString() });
        } else {
            throw e1;
        }
    } catch (Exception e) {
        // If we find other (unexpected) errors, we ultimately exit--so make
        // sure we print the error message before doing so (Beetle 5033).
        throwUnexpectedException(e);
    }
    switch(getSSLMode()) {
        default:
        case SSL_OFF:
            consolePropertyMessage("DRDA_Ready.I", new String[] { Integer.toString(portNumber), att_srvclsnm, versionString });
            break;
        case SSL_BASIC:
            consolePropertyMessage("DRDA_SSLReady.I", new String[] { Integer.toString(portNumber), att_srvclsnm, versionString });
            consolePropertyMessage("DRDA_EnabledProtocols.I", new String[] { getEnabledProtocols((SSLServerSocket) serverSocket), att_srvclsnm, versionString });
            break;
        case SSL_PEER_AUTHENTICATION:
            consolePropertyMessage("DRDA_SSLClientAuthReady.I", new String[] { Integer.toString(portNumber), att_srvclsnm, versionString });
            consolePropertyMessage("DRDA_EnabledProtocols.I", new String[] { getEnabledProtocols((SSLServerSocket) serverSocket), att_srvclsnm, versionString });
            break;
    }
    // First, register any MBeans. We do this before we start accepting
    // connections from the clients to ease testing of JMX (DERBY-3689).
    // This way we know that once we can connect to the network server,
    // the MBeans will be available.
    ManagementService mgmtService = ((ManagementService) getSystemModule(Module.JMX));
    final Object versionMBean = mgmtService.registerMBean(new Version(getNetProductVersionHolder(), SystemPermission.SERVER), VersionMBean.class, "type=Version,jar=derbynet.jar");
    final Object networkServerMBean = mgmtService.registerMBean(new NetworkServerMBeanImpl(this), NetworkServerMBean.class, "type=NetworkServer");
    // We accept clients on a separate thread so we don't run into a problem
    // blocking on the accept when trying to process a shutdown
    final ClientThread clientThread = AccessController.doPrivileged(new PrivilegedExceptionAction<ClientThread>() {

        public ClientThread run() throws Exception {
            return new ClientThread(thisControl, serverSocket);
        }
    });
    clientThread.start();
    try {
        // wait until we are told to shutdown or someone sends an InterruptedException
        synchronized (shutdownSync) {
            try {
                while (!shutdown) {
                    shutdownSync.wait();
                }
            } catch (InterruptedException e) {
                shutdown = true;
            }
        }
        try {
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                public Void run() {
                    // Need to interrupt the memcheck thread if it is sleeping.
                    if (mc != null)
                        mc.interrupt();
                    // interrupt client thread
                    clientThread.interrupt();
                    return null;
                }
            });
        } catch (Exception exception) {
            consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
        }
        // Close out the sessions
        synchronized (sessionTable) {
            for (Session session : sessionTable.values()) {
                try {
                    session.close();
                } catch (Exception exception) {
                    consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
                }
            }
        }
        synchronized (threadList) {
            // interupt any connection threads still active
            for (final DRDAConnThread threadi : threadList) {
                try {
                    threadi.close();
                    AccessController.doPrivileged(new PrivilegedAction<Void>() {

                        public Void run() {
                            threadi.interrupt();
                            return null;
                        }
                    });
                } catch (Exception exception) {
                    consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
                }
            }
            threadList.clear();
        }
        // close the listener socket
        try {
            serverSocket.close();
        } catch (IOException e) {
            consolePropertyMessage("DRDA_ListenerClose.S", true);
        } catch (Exception exception) {
            consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
        }
        // they can close down
        try {
            synchronized (runQueue) {
                runQueue.notifyAll();
            }
        } catch (Exception exception) {
            consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
        }
        // And now unregister any MBeans.
        try {
            mgmtService.unregisterMBean(versionMBean);
            mgmtService.unregisterMBean(networkServerMBean);
        } catch (Exception exception) {
            consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
        }
        if (shutdownDatabasesOnShutdown) {
            // Shutdown Derby
            try {
                // tell driver to shutdown the engine
                if (cloudscapeDriver != null) {
                    // DERBY-2109: pass user credentials for driver shutdown
                    final Properties p = new Properties();
                    if (userArg != null) {
                        p.setProperty("user", userArg);
                    }
                    if (passwordArg != null) {
                        p.setProperty("password", passwordArg);
                    }
                    // DERBY-6224: DriverManager.deregisterDriver() requires
                    // an extra permission in JDBC 4.2 and later. Invoke
                    // system shutdown with deregister=false to avoid the
                    // need for the extre permission in the default server
                    // policy. Since the JVM is about to terminate, we don't
                    // care whether the JDBC driver is deregistered.
                    cloudscapeDriver.connect("jdbc:derby:;shutdown=true;deregister=false", p);
                }
            } catch (SQLException sqle) {
                // If we can't shutdown Derby, perhaps, authentication has
                // failed or System Privileges weren't granted. We will just
                // print a message to the console and proceed.
                String expectedState = StandardException.getSQLStateFromIdentifier(SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN);
                if (!expectedState.equals(sqle.getSQLState())) {
                    consolePropertyMessage("DRDA_ShutdownWarning.I", sqle.getMessage());
                }
            } catch (Exception exception) {
                consolePrintAndIgnore("DRDA_UnexpectedException.S", exception, true);
            }
        }
        consolePropertyMessage("DRDA_ShutdownSuccess.I", new String[] { att_srvclsnm, versionString });
    } catch (Exception ex) {
        try {
            // If the console printing is not available,  then we have
            // a simple stack trace print below to atleast print some
            // exception info
            consolePrintAndIgnore("DRDA_UnexpectedException.S", ex, true);
        } catch (Exception e) {
        }
        ex.printStackTrace();
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) PrivilegedActionException(java.security.PrivilegedActionException) SQLException(java.sql.SQLException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) SSLServerSocket(javax.net.ssl.SSLServerSocket) Properties(java.util.Properties) AccessControlException(java.security.AccessControlException) SQLException(java.sql.SQLException) StandardException(org.apache.derby.shared.common.error.StandardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ManagementService(org.apache.derby.iapi.services.jmx.ManagementService) Version(org.apache.derby.mbeans.Version)

Example 88 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project derby by apache.

the class OptimizerTracer method unloadTool.

/**
 * <p>
 * Print the optimizer trace and turn off tracing. Takes optional parameters:
 * </p>
 *
 * <ul>
 * <li><b>fileName</b> - Where to write the optimizer trace. If omitted, the trace is written to System.out.</li>
 * </ul>
 */
public void unloadTool(final String... configurationParameters) throws SQLException {
    try {
        final OptTrace tracer = OptimizerTrace.getOptimizerTracer();
        boolean needsClosing = false;
        PrintWriter pw;
        if ((configurationParameters != null) && (configurationParameters.length > 0)) {
            pw = AccessController.doPrivileged(new PrivilegedExceptionAction<PrintWriter>() {

                public PrintWriter run() throws SQLException {
                    try {
                        String fileName = configurationParameters[0];
                        File outputFile = new File(fileName);
                        if (outputFile.exists()) {
                            throw PublicAPI.wrapStandardException(StandardException.newException(SQLState.DATA_FILE_EXISTS, fileName));
                        }
                        return new PrintWriter(outputFile);
                    } catch (IOException ioe) {
                        throw new IllegalArgumentException(ioe.getMessage(), ioe);
                    }
                }
            });
            needsClosing = true;
        } else {
            pw = new PrintWriter(System.out);
        }
        if (tracer != null) {
            tracer.printToWriter(pw);
            pw.flush();
        }
        if (needsClosing) {
            pw.close();
        }
    } catch (Exception e) {
        if (e.getMessage() == null) {
            Throwable cause = e.getCause();
            if ((cause != null) && (cause instanceof SQLException)) {
                throw (SQLException) cause;
            }
        }
        throw wrap(e);
    } finally {
        OptimizerTrace.setOptimizerTracer(null);
    }
}
Also used : SQLException(java.sql.SQLException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) File(java.io.File) StandardException(org.apache.derby.shared.common.error.StandardException) IOException(java.io.IOException) SQLException(java.sql.SQLException) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace) PrintWriter(java.io.PrintWriter)

Example 89 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project derby by apache.

the class DRDAServerStarter method boot.

public void boot(boolean create, java.util.Properties properties) {
    if (server != null) {
        if (SanityManager.DEBUG)
            SanityManager.THROWASSERT("Network server starter module booted twice.");
        return;
    }
    // Load the server class indirectly so that Derby does not require the network code
    try {
        serverClass = Class.forName(serverClassName);
    } catch (ClassNotFoundException cnfe) {
        Monitor.logTextMessage(MessageId.CONN_NETWORK_SERVER_CLASS_FIND, serverClassName);
        return;
    } catch (java.lang.Error e) {
        Monitor.logTextMessage(MessageId.CONN_NETWORK_SERVER_CLASS_LOAD, serverClassName, e.getMessage());
        return;
    }
    try {
        Constructor serverConstructor;
        try {
            serverConstructor = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor>() {

                public Constructor run() throws NoSuchMethodException, SecurityException {
                    if (listenAddress == null)
                        return serverClass.getConstructor(new Class[] { String.class, String.class });
                    else
                        return serverClass.getConstructor(new Class[] { java.net.InetAddress.class, Integer.TYPE, String.class, String.class });
                }
            });
        } catch (PrivilegedActionException e) {
            Exception e1 = e.getException();
            Monitor.logTextMessage(MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e1.getMessage());
            e.printStackTrace(Monitor.getStream().getPrintWriter());
            return;
        }
        findStartStopMethods(serverClass);
        if (listenAddress == null) {
            server = serverConstructor.newInstance(new Object[] { userArg, passwordArg });
        } else {
            server = serverConstructor.newInstance(new Object[] { listenAddress, portNumber, userArg, passwordArg });
        }
        serverThread = getMonitor().getDaemonThread(this, "NetworkServerStarter", false);
        serverThread.start();
    } catch (Exception e) {
        Monitor.logTextMessage(MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage());
        server = null;
        e.printStackTrace(Monitor.getStream().getPrintWriter());
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) Constructor(java.lang.reflect.Constructor) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) InetAddress(java.net.InetAddress) PrivilegedActionException(java.security.PrivilegedActionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 90 with PrivilegedExceptionAction

use of java.security.PrivilegedExceptionAction in project derby by apache.

the class StorageFactoryService method removeServiceRoot.

// end of getDirectoryPath
public boolean removeServiceRoot(final String serviceName) {
    if (!(rootStorageFactory instanceof WritableStorageFactory))
        return false;
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws StandardException, IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
                StorageFactory storageFactory = privGetStorageFactoryInstance(true, serviceName, null, null);
                try {
                    if (SanityManager.DEBUG) {
                        // Run this through getCanonicalServiceName as
                        // an extra sanity check. Prepending the
                        // protocol lead in to the canonical name from
                        // the storage factory should be enough.
                        String tmpCanonical = getCanonicalServiceName(getProtocolLeadIn() + storageFactory.getCanonicalName());
                        // These should give the same result.
                        SanityManager.ASSERT(tmpCanonical.equals(getProtocolLeadIn() + storageFactory.getCanonicalName()));
                        SanityManager.ASSERT(serviceName.equals(tmpCanonical), "serviceName = " + serviceName + " ; protocolLeadIn + " + "storageFactory.getCanoicalName = " + tmpCanonical);
                    }
                    StorageFile serviceDirectory = storageFactory.newStorageFile(null);
                    return serviceDirectory.deleteAll() ? this : null;
                } finally {
                    storageFactory.shutdown();
                }
            }
        }) != null;
    } catch (PrivilegedActionException pae) {
        return false;
    }
}
Also used : WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFactory(org.apache.derby.io.StorageFactory) PrivilegedActionException(java.security.PrivilegedActionException) WritableStorageFactory(org.apache.derby.io.WritableStorageFactory) StorageFile(org.apache.derby.io.StorageFile) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Aggregations

PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)387 IOException (java.io.IOException)199 PrivilegedActionException (java.security.PrivilegedActionException)135 Test (org.junit.Test)104 Connection (org.apache.hadoop.hbase.client.Connection)81 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)76 Table (org.apache.hadoop.hbase.client.Table)62 TableName (org.apache.hadoop.hbase.TableName)57 Result (org.apache.hadoop.hbase.client.Result)56 Scan (org.apache.hadoop.hbase.client.Scan)55 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)53 Delete (org.apache.hadoop.hbase.client.Delete)48 InterruptedIOException (java.io.InterruptedIOException)47 Cell (org.apache.hadoop.hbase.Cell)38 CellScanner (org.apache.hadoop.hbase.CellScanner)38 Configuration (org.apache.hadoop.conf.Configuration)36 File (java.io.File)33 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)33 Path (org.apache.hadoop.fs.Path)23 ArrayList (java.util.ArrayList)22