Example usage for javax.crypto SecretKey getEncoded

List of usage examples for javax.crypto SecretKey getEncoded

Introduction

In this page you can find the example usage for javax.crypto SecretKey getEncoded.

Prototype

public byte[] getEncoded();

Source Link

Document

Returns the key in its primary encoding format, or null if this key does not support encoding.

Usage

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.RMStateStoreTestBase.java

void testRMAppStateStore(RMStateStoreHelper stateStoreHelper, StoreStateVerifier verifier) throws Exception {
    long submitTime = System.currentTimeMillis();
    long startTime = System.currentTimeMillis() + 1234;
    Configuration conf = new YarnConfiguration();
    RMStateStore store = stateStoreHelper.getRMStateStore();
    TestDispatcher dispatcher = new TestDispatcher();
    store.setRMDispatcher(dispatcher);/*from  w  w w.  j  a v  a2  s .co m*/

    RMContext rmContext = mock(RMContext.class);
    when(rmContext.getStateStore()).thenReturn(store);

    AMRMTokenSecretManager appTokenMgr = spy(new AMRMTokenSecretManager(conf, rmContext));

    MasterKeyData masterKeyData = appTokenMgr.createNewMasterKey();
    when(appTokenMgr.getMasterKey()).thenReturn(masterKeyData);

    ClientToAMTokenSecretManagerInRM clientToAMTokenMgr = new ClientToAMTokenSecretManagerInRM();

    ApplicationAttemptId attemptId1 = ApplicationAttemptId.fromString("appattempt_1352994193343_0001_000001");
    ApplicationId appId1 = attemptId1.getApplicationId();
    storeApp(store, appId1, submitTime, startTime);
    verifier.afterStoreApp(store, appId1);

    // create application token and client token key for attempt1
    Token<AMRMTokenIdentifier> appAttemptToken1 = generateAMRMToken(attemptId1, appTokenMgr);
    SecretKey clientTokenKey1 = clientToAMTokenMgr.createMasterKey(attemptId1);

    ContainerId containerId1 = storeAttempt(store, attemptId1, "container_1352994193343_0001_01_000001",
            appAttemptToken1, clientTokenKey1, dispatcher);

    String appAttemptIdStr2 = "appattempt_1352994193343_0001_000002";
    ApplicationAttemptId attemptId2 = ApplicationAttemptId.fromString(appAttemptIdStr2);

    // create application token and client token key for attempt2
    Token<AMRMTokenIdentifier> appAttemptToken2 = generateAMRMToken(attemptId2, appTokenMgr);
    SecretKey clientTokenKey2 = clientToAMTokenMgr.createMasterKey(attemptId2);

    ContainerId containerId2 = storeAttempt(store, attemptId2, "container_1352994193343_0001_02_000001",
            appAttemptToken2, clientTokenKey2, dispatcher);

    ApplicationAttemptId attemptIdRemoved = ApplicationAttemptId
            .fromString("appattempt_1352994193343_0002_000001");
    ApplicationId appIdRemoved = attemptIdRemoved.getApplicationId();
    storeApp(store, appIdRemoved, submitTime, startTime);
    storeAttempt(store, attemptIdRemoved, "container_1352994193343_0002_01_000001", null, null, dispatcher);
    verifier.afterStoreAppAttempt(store, attemptIdRemoved);

    RMApp mockRemovedApp = mock(RMApp.class);
    RMAppAttemptMetrics mockRmAppAttemptMetrics = mock(RMAppAttemptMetrics.class);
    HashMap<ApplicationAttemptId, RMAppAttempt> attempts = new HashMap<ApplicationAttemptId, RMAppAttempt>();
    ApplicationSubmissionContext context = new ApplicationSubmissionContextPBImpl();
    context.setApplicationId(appIdRemoved);
    when(mockRemovedApp.getSubmitTime()).thenReturn(submitTime);
    when(mockRemovedApp.getApplicationSubmissionContext()).thenReturn(context);
    when(mockRemovedApp.getAppAttempts()).thenReturn(attempts);
    when(mockRemovedApp.getUser()).thenReturn("user1");
    RMAppAttempt mockRemovedAttempt = mock(RMAppAttempt.class);
    when(mockRemovedAttempt.getAppAttemptId()).thenReturn(attemptIdRemoved);
    when(mockRemovedAttempt.getRMAppAttemptMetrics()).thenReturn(mockRmAppAttemptMetrics);
    when(mockRmAppAttemptMetrics.getAggregateAppResourceUsage())
            .thenReturn(new AggregateAppResourceUsage(0, 0, 0));
    attempts.put(attemptIdRemoved, mockRemovedAttempt);
    store.removeApplication(mockRemovedApp);

    // remove application directory recursively.
    storeApp(store, appIdRemoved, submitTime, startTime);
    storeAttempt(store, attemptIdRemoved, "container_1352994193343_0002_01_000001", null, null, dispatcher);
    store.removeApplication(mockRemovedApp);

    // let things settle down
    Thread.sleep(1000);
    store.close();

    // give tester a chance to modify app state in the store
    modifyAppState();

    // load state
    store = stateStoreHelper.getRMStateStore();
    store.setRMDispatcher(dispatcher);
    RMState state = store.loadState();
    Map<ApplicationId, ApplicationStateData> rmAppState = state.getApplicationState();

    ApplicationStateData appState = rmAppState.get(appId1);
    // app is loaded
    assertNotNull(appState);
    // app is loaded correctly
    assertEquals(submitTime, appState.getSubmitTime());
    assertEquals(startTime, appState.getStartTime());
    // submission context is loaded correctly
    assertEquals(appId1, appState.getApplicationSubmissionContext().getApplicationId());
    ApplicationAttemptStateData attemptState = appState.getAttempt(attemptId1);
    // attempt1 is loaded correctly
    assertNotNull(attemptState);
    assertEquals(attemptId1, attemptState.getAttemptId());
    assertEquals(-1000, attemptState.getAMContainerExitStatus());
    // attempt1 container is loaded correctly
    assertEquals(containerId1, attemptState.getMasterContainer().getId());
    // attempt1 client token master key is loaded correctly
    assertArrayEquals(clientTokenKey1.getEncoded(),
            attemptState.getAppAttemptTokens().getSecretKey(RMStateStore.AM_CLIENT_TOKEN_MASTER_KEY_NAME));
    assertEquals("context", appState.getCallerContext().getContext());

    attemptState = appState.getAttempt(attemptId2);
    // attempt2 is loaded correctly
    assertNotNull(attemptState);
    assertEquals(attemptId2, attemptState.getAttemptId());
    // attempt2 container is loaded correctly
    assertEquals(containerId2, attemptState.getMasterContainer().getId());
    // attempt2 client token master key is loaded correctly
    assertArrayEquals(clientTokenKey2.getEncoded(),
            attemptState.getAppAttemptTokens().getSecretKey(RMStateStore.AM_CLIENT_TOKEN_MASTER_KEY_NAME));

    //******* update application/attempt state *******//
    ApplicationStateData appState2 = ApplicationStateData.newInstance(appState.getSubmitTime(),
            appState.getStartTime(), appState.getUser(), appState.getApplicationSubmissionContext(),
            RMAppState.FINISHED, "appDiagnostics", 1234, appState.getCallerContext());
    appState2.attempts.putAll(appState.attempts);
    store.updateApplicationState(appState2);

    ApplicationAttemptStateData oldAttemptState = attemptState;
    ApplicationAttemptStateData newAttemptState = ApplicationAttemptStateData.newInstance(
            oldAttemptState.getAttemptId(), oldAttemptState.getMasterContainer(),
            oldAttemptState.getAppAttemptTokens(), oldAttemptState.getStartTime(), RMAppAttemptState.FINISHED,
            "myTrackingUrl", "attemptDiagnostics", FinalApplicationStatus.SUCCEEDED, 100,
            oldAttemptState.getFinishTime(), 0, 0, 0, 0, 0, 0, "myTrackingUrl");
    store.updateApplicationAttemptState(newAttemptState);

    // test updating the state of an app/attempt whose initial state was not
    // saved.
    ApplicationId dummyAppId = ApplicationId.newInstance(1234, 10);
    ApplicationSubmissionContext dummyContext = new ApplicationSubmissionContextPBImpl();
    dummyContext.setApplicationId(dummyAppId);
    ApplicationStateData dummyApp = ApplicationStateData.newInstance(appState.getSubmitTime(),
            appState.getStartTime(), appState.getUser(), dummyContext, RMAppState.FINISHED, "appDiagnostics",
            1234, null);
    store.updateApplicationState(dummyApp);

    ApplicationAttemptId dummyAttemptId = ApplicationAttemptId.newInstance(dummyAppId, 6);
    ApplicationAttemptStateData dummyAttempt = ApplicationAttemptStateData.newInstance(dummyAttemptId,
            oldAttemptState.getMasterContainer(), oldAttemptState.getAppAttemptTokens(),
            oldAttemptState.getStartTime(), RMAppAttemptState.FINISHED, "myTrackingUrl", "attemptDiagnostics",
            FinalApplicationStatus.SUCCEEDED, 111, oldAttemptState.getFinishTime(), 0, 0, 0, 0, 0, 0,
            "myTrackingUrl");
    store.updateApplicationAttemptState(dummyAttempt);

    // let things settle down
    Thread.sleep(1000);
    store.close();

    // check updated application state.
    store = stateStoreHelper.getRMStateStore();
    store.setRMDispatcher(dispatcher);
    RMState newRMState = store.loadState();
    Map<ApplicationId, ApplicationStateData> newRMAppState = newRMState.getApplicationState();
    assertNotNull(newRMAppState.get(dummyApp.getApplicationSubmissionContext().getApplicationId()));
    ApplicationStateData updatedAppState = newRMAppState.get(appId1);
    assertEquals(appState.getApplicationSubmissionContext().getApplicationId(),
            updatedAppState.getApplicationSubmissionContext().getApplicationId());
    assertEquals(appState.getSubmitTime(), updatedAppState.getSubmitTime());
    assertEquals(appState.getStartTime(), updatedAppState.getStartTime());
    assertEquals(appState.getUser(), updatedAppState.getUser());
    // new app state fields
    assertEquals(RMAppState.FINISHED, updatedAppState.getState());
    assertEquals("appDiagnostics", updatedAppState.getDiagnostics());
    assertEquals(1234, updatedAppState.getFinishTime());

    // check updated attempt state
    assertNotNull(newRMAppState.get(dummyApp.getApplicationSubmissionContext().getApplicationId())
            .getAttempt(dummyAttemptId));
    ApplicationAttemptStateData updatedAttemptState = updatedAppState
            .getAttempt(newAttemptState.getAttemptId());
    assertEquals(oldAttemptState.getAttemptId(), updatedAttemptState.getAttemptId());
    assertEquals(containerId2, updatedAttemptState.getMasterContainer().getId());
    assertArrayEquals(clientTokenKey2.getEncoded(),
            attemptState.getAppAttemptTokens().getSecretKey(RMStateStore.AM_CLIENT_TOKEN_MASTER_KEY_NAME));
    // new attempt state fields
    assertEquals(RMAppAttemptState.FINISHED, updatedAttemptState.getState());
    assertEquals("myTrackingUrl", updatedAttemptState.getFinalTrackingUrl());
    assertEquals("attemptDiagnostics", updatedAttemptState.getDiagnostics());
    assertEquals(100, updatedAttemptState.getAMContainerExitStatus());
    assertEquals(FinalApplicationStatus.SUCCEEDED, updatedAttemptState.getFinalApplicationStatus());

    // assert store is in expected state after everything is cleaned
    assertTrue(stateStoreHelper.isFinalStateValid());

    store.close();
}

From source file:de.innovationgate.wgpublisher.WGACore.java

public void startup() throws ServletException {
    try {//from  w  w w.  ja va2 s. c o  m
        this.log.info("Starting up " + WGABrand.getName() + " service");

        // prepare HDB
        TMLScriptHDBListenerFactory listenerFactory = new TMLScriptHDBListenerFactory(this);
        WGHierarchicalDatabase.setDefaultListenerFactory(listenerFactory);
        WGHierarchicalDatabase.setDefaultStartupImpl(null);
        _hdbCoreListener = new WGHierarchicalDatabaseCoreListener() {

            public void databaseCreated(WGHierarchicalDatabase hdb) {
                // Eventually load and initialize model
                WGDatabase db = hdb.getWrappedDB();
                try {
                    HDBModel.createModelObject(WGACore.this, db);
                } catch (Exception e) {
                    WGACore.this.log.error("Error initializing HDB model for database " + db.getDbReference(),
                            e);
                }
            }

            public void databaseRemoved(WGHierarchicalDatabase hdb) {
            }

        };
        WGHierarchicalDatabase.addCoreListener(_hdbCoreListener);

        String configFilePath = retrieveConfigPath();
        // init DES-Encrypter
        File desKeyFile = new File(configFilePath, "des.key");
        desEncrypter = new DESEncrypter();
        try {
            try {
                desEncrypter.init(desKeyFile);
                log.info("DESEncrypter initialized using keyfile '" + desKeyFile.getPath() + "'.");
            } catch (DESEncrypter.PersistentKeyException e) {
                log.warn(
                        "Unable to create or restore encryption key - generating temporary key. Session replication will not work with this key. Ensure the application server has read/write access to '"
                                + desKeyFile.getPath() + "'.",
                        e);
                // init with temp key
                desEncrypter.init();
                log.info("DESEncrypter initialized with temporary key.");
            }
        } catch (GeneralSecurityException e) {
            // VM does not support 'des' algorithm - should not happen in
            // wga supported VMs
            log.error("Unable to create DESEncrypter.", e);
            throw new ServletException("wga publisher initialization failure");
        }

        // init symmetric encryption engine
        File keyFile = new File(configFilePath, "openwga.key");
        _symmetricEncryptionEngine = new SymmetricEncryptionEngine();
        try {
            byte[] keyBytes = null;
            if (!keyFile.exists()) {
                log.info("SymmetricEncryptionEngine: Generating new key file: '" + keyFile.getAbsolutePath()
                        + "'.");
                SecretKey key = _symmetricEncryptionEngine.generateKey();
                keyBytes = key.getEncoded();
                FileOutputStream keyOut = new FileOutputStream(keyFile);
                keyOut.write(keyBytes);
                keyOut.close();
            } else {
                log.info("SymmetricEncryptionEngine: Using keyfile '" + keyFile.getAbsolutePath() + "'.");
                FileInputStream keyIn = new FileInputStream(keyFile);
                ByteArrayOutputStream keyOut = new ByteArrayOutputStream();
                WGUtils.inToOut(keyIn, keyOut, 128);
                keyIn.close();
                keyBytes = keyOut.toByteArray();
            }
            _symmetricEncryptionEngine.init(keyBytes);
        } catch (Exception e) {
            log.error("Unable to init symmetric encryption engine.", e);
            throw new ServletException("Unable to init symmetric encryption engine", e);
        }

        // get config xml document
        boolean configMigrated = false;
        this.configFile = retrieveConfigFile();

        if (!configFile.exists()) {
            // no new style wga configuration - check if we have to migrate an old one
            File oldConfigFile = retrieveOldConfigFile();
            if (oldConfigFile.exists()) {
                migrateWGAConfiguration(oldConfigFile, configFile);
                configMigrated = true;
            } else {
                // no previous old style config - create default config
                createDefaultWGAConfiguration(configFile);
            }
        }

        log.info("Using config file: " + configFile.getAbsolutePath());
        this.configFileLastModified = this.configFile.lastModified();
        parseConfigFile();
        adaptWGAConfigurationToVersion();

        initQuartz();
        _deployer.startup();
        _calledSequenceIds = CacheFactory.createCache("WGACore_calledSequenceIds", 10000, null);

        String dataPath = System.getProperty(SYSPROPERTY_DATAPATH);
        if (dataPath != null) {
            _wgaDataDir = new File(dataPath);
        } else {
            _wgaDataDir = new File(configFile.getParent(), "wgadata");
        }

        if (!_wgaDataDir.exists()) {
            if (!_wgaDataDir.mkdir()) {
                log.error("Unable to create WGA data directory '" + _wgaDataDir.getPath()
                        + "'. Some WGA functionalities that rely on this will not work!");
                _wgaDataDir = null;

            }
        } else if (!_wgaDataDir.isDirectory()) {
            log.error("Unable to create WGA data directory '" + _wgaDataDir.getPath()
                    + "' because some other file uses the same name. Some WGA functionalities that rely on this will not work!");
            _wgaDataDir = null;
        }

        String hsqlRoot = System
                .getProperty(de.innovationgate.webgate.api.hsql.WGDatabaseImpl.SYSPROPERTY_HSQL_ROOT);
        if (WGUtils.isEmpty(hsqlRoot)) {
            log.info("Setting root directory for embedded HSQLDB databases to config dir: "
                    + configFile.getParentFile().getAbsolutePath());
            System.setProperty(de.innovationgate.webgate.api.hsql.WGDatabaseImpl.SYSPROPERTY_HSQL_ROOT,
                    configFile.getParentFile().getAbsolutePath());
        } else {
            log.info("Root directory for embedded HSQLDB databases is: " + hsqlRoot);
        }

        String authFileRoot = System.getProperty(FileAuthenticationModule.SYSPROPERTY_AUTH_FOLDER);
        if (WGUtils.isEmpty(authFileRoot)) {
            log.info("Setting root directory for XML authentication files to config dir: "
                    + configFile.getParentFile().getAbsolutePath());
            System.setProperty(FileAuthenticationModule.SYSPROPERTY_AUTH_FOLDER,
                    configFile.getParentFile().getAbsolutePath());
        } else {
            log.info("Root directory for XML authentication files is: " + authFileRoot);
        }

        // Prepare problem registry
        _problemRegistry = new ProblemRegistry(this);
        ProblemOccasion occ = new UpdateConfigOccasion();
        getProblemRegistry().clearProblemOccasion(occ);

        // init login bruteForceLoginBlocker
        bruteForceLoginBlocker = new BruteForceLoginBlocker(this);

        // retrieve general configuration
        initReadGeneralConfig(false);

        // Init some managers
        _systemContainerManager = new SystemContainerManager(this);
        _pageConnectionManager = new PageConnectionManager();
        _independentWebSocketManager = new IndependentWebSocketManager();
        _eventManager = new EventManager(this);
        _persManager = new PersonalisationManager(this);

        // Retrieve media key and element mappings
        initReadMappings();

        // Create expression engines
        ExpressionEngineFactory.createEngines(this);

        // Init default serializer
        initDefaultSerializer();

        // Initialize custom core listeners
        initCustomCoreListeners();

        // Read domain configurations
        Map<String, WGADomain> newDomainConfigs = initReadDomains();

        deployErrorPage();

        // Init scheduler before db connection, so system containers in dbs can add jobs
        _scheduler = new Scheduler(this);

        // Init module registry
        initModuleRegistry();

        // Init JMX
        _jmx = new JMX(this);

        // fire pre connect event
        fireCoreEvent(new WGACoreEvent(WGACoreEvent.TYPE_STARTUP_PRE_CONNECT, null, this));

        // Create server option readers
        _variousServerOptionReader = getConfigOptionReader(new OptionFetcher() {
            @Override
            public Map<String, String> fetch(WGAConfiguration config) {
                return config.getServerOptions();
            }
        }, WGAServerOptionsModuleType.class, VariousOptionsCollector.class);

        _servicesServerOptionReader = getConfigOptionReader(new OptionFetcher() {
            @Override
            public Map<String, String> fetch(WGAConfiguration config) {
                return config.getServerOptions();
            }
        }, WGAServerOptionsModuleType.class, ServicesCollector.class);

        // connect plugins
        updatePlugins(newDomainConfigs);

        // Log most important available modules
        logCategoryInfo("Modules", 1);
        logModuleRegistry();

        // Some tasks that adapt options in registry to those configured in the WGA configuration
        adaptConfigurationToRegistry(configMigrated);

        // init cluster service
        initClusterService(null);

        // init session manager
        initHttpSessionManager(null);

        // Init event manager
        _eventManager.reloadConfig();

        // Init filter mappings (which may be feeded from registry)
        initReadFilterMappings();

        // Init access logger - Must be after modreg so JDBC drivers from mod dependencies are already loaded
        initAccessLogger();

        // Init file derivate manager
        this.fileDerivateManager.init(_wgaConfiguration);

        // open database servers (must be before domains, to allow pers db connections)
        updateDatabaseServers();

        // Startup domain configurations (must be after plugin connection so that domains can use plugin-provided functionalities)
        initStartupDomains(newDomainConfigs);

        // Init design manager
        logCategoryInfo("Design Sources", 1);
        _designManager = new WGADesignManager(this,
                _wgaConfiguration.getDesignConfiguration().getDesignSources());

        // Init mail service
        WGFactory.setMailService(new WGAMailService(this));

        logCategoryInfo("Workflow engine", 1);
        Class defaultWorkflowEngine = WGFactory.getDefaultWorkflowEngine();
        ModuleDefinition wfDef = getModuleRegistry().getModuleDefinition(WorkflowEngineModuleType.class,
                defaultWorkflowEngine);
        if (wfDef != null) {
            getLog().info("Default workflow engine is: " + wfDef.getTitle(Locale.ENGLISH));
        } else {
            getLog().info("Default workflow engine is: " + defaultWorkflowEngine.getName()
                    + " (custom unregistered engine)");
        }

        // open content databases
        updateContentDBs();

        // fire post connect event
        fireCoreEvent(new WGACoreEvent(WGACoreEvent.TYPE_STARTUP_POST_CONNECT, null, this));

        // Load scheduler jobs after db connection, so they can refer the connected dbs
        updateScheduler();

        // Init shares
        updateShares();

        this.getServletContext().setAttribute(WGACore.ATTRIB_CONTENTDBS, this.contentdbs);

        // init TestCore
        initTestCore();

        /*
        // Perform initial daily DB maintenance if in devmode, because devmode servers normally do not run 24 hours
        if ("true".equals(System.getProperty(SYSPROPERTY_DEVELOPMENT_MODE))) {
        performDbMaintenanceForDevmode();
        }
        */

        // notify LuceneManger
        logCategoryInfo("Lucene Fulltext Index", 1);
        if (luceneManager != null) {
            luceneManager.startup();
        }

        initExternalFileServing();

        // start external file serving maintenance
        _externalFileMaintenanceTask = new ExternalFileServingMaintenanceTask(this);
        _externalFileMaintenanceTask.start();

        // Setup integrated JMX server
        _jmx.setup();

        // Start timer tasks
        this.timer = new WGACoreTimer(this);

        // Enable daily db backend maintenance if applicable
        if (isRunSingleNodeFunctionalities()) {
            WGFactory.getInstance().setDatabaseBackendMaintenanceEnabled(true);
        }

        // Init finished
        logCategoryInfo(WGAVersion.WGAPUBLISHER_PRODUCT_NAME + " ready", 1);
        WGFactory.getInstance().closeSessions();
        fireCoreEvent(new WGACoreEvent(WGACoreEvent.TYPE_ONLINE, null, this));

    } catch (Exception exc) {
        log.fatal("Fatal error initializing WGA", exc);
        throw new ServletException("Servlet initialization failure", exc);

    } catch (Error err) {
        log.fatal("Fatal error initializing WGA", err);
        throw new ServletException("Servlet initialization failure", err);
    } finally {
        WGPDispatcher dispatcher = getDispatcher();
        if (dispatcher != null) {
            dispatcher.setServePages(true);
        }
    }
}