Search in sources :

Example 51 with SimpleCredentials

use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.

the class ReplicaCrashResilienceTest method run.

private void run(final Repository repository) throws Exception {
    System.out.println("Setup...");
    try {
        Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
        final Node rootNode = session.getRootNode();
        if (rootNode.hasNode(REPLICA_CRASH_TEST)) {
            // then cleanup first
            rootNode.getNode(REPLICA_CRASH_TEST).remove();
            session.save();
        }
        session.refresh(false);
        final Node replicaCrashTestNode = rootNode.addNode(REPLICA_CRASH_TEST);
        replicaCrashTestNode.addNode(WRITER_INFOS);
        session.save();
    } catch (RepositoryException e1) {
        e1.printStackTrace();
        System.exit(1);
    }
    // this runnable will go ahead and start writing nodes to the repo
    // the structure is:
    // /replicaCrashTest/writerInfos
    //                           - level 1 pointer
    //                           - level 2 pointer
    // /replicaCrashTest/[1-n]/[1-1000]
    Runnable writer = new Runnable() {

        @Override
        public void run() {
            int level1Pointer = 1;
            int level2Pointer = 1;
            Session session = null;
            try {
                session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
            } catch (Exception e1) {
                e1.printStackTrace();
                System.exit(1);
            }
            System.out.println("Writer: Test start.");
            while (true) {
                try {
                    final String level1 = String.valueOf(level1Pointer);
                    final String level2 = String.valueOf(level2Pointer);
                    final Node rootNode = session.getRootNode();
                    final Node replicaCrashTestNode = rootNode.getNode(REPLICA_CRASH_TEST);
                    final Node writerInfosNode = replicaCrashTestNode.getNode(WRITER_INFOS);
                    Node level1Node;
                    if (replicaCrashTestNode.hasNode(level1)) {
                        level1Node = replicaCrashTestNode.getNode(level1);
                    } else {
                        level1Node = replicaCrashTestNode.addNode(level1);
                        System.out.println("Writer: Created level1 node: " + level1Node);
                    }
                    Node level2Node = level1Node.addNode(level2);
                    System.out.println("Writer: Created level2 node: " + level2Node);
                    writerInfosNode.setProperty(LEVEL1POINTER, level1Pointer);
                    writerInfosNode.setProperty(LEVEL2POINTER, level2Pointer);
                    session.save();
                } catch (com.google.common.util.concurrent.UncheckedExecutionException e) {
                    System.out.println("Got an UncheckedException (levels: " + level1Pointer + "/" + level2Pointer + ") from the google cache probably: " + e);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e2) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                    continue;
                } catch (Throwable e) {
                    // yes, one should not catch Throwable - but this is a test only
                    e.printStackTrace(System.out);
                    System.exit(1);
                }
                if (++level2Pointer > 1000) {
                    level2Pointer = 1;
                    level1Pointer++;
                }
            }
        }
    };
    Thread th1 = new Thread(writer);
    System.out.println("Launching writer...");
    th1.start();
    Runnable reader = new Runnable() {

        @Override
        public void run() {
            long level1Pointer = 1;
            long level2Pointer = 1;
            Session session = null;
            try {
                session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
                Node rootNode = session.getRootNode();
                Node replicaCrashTestNode = rootNode.getNode(REPLICA_CRASH_TEST);
                Node writerInfos;
                while (true) {
                    try {
                        final String level1 = String.valueOf(level1Pointer);
                        final String level2 = String.valueOf(level2Pointer);
                        session.refresh(false);
                        writerInfos = replicaCrashTestNode.getNode(WRITER_INFOS);
                        long writerLevel1Pointer = writerInfos.getProperty(LEVEL1POINTER).getLong();
                        long writerLevel2Pointer = writerInfos.getProperty(LEVEL2POINTER).getLong();
                        long writerPointer = writerLevel1Pointer * 1000 + writerLevel2Pointer;
                        long myPointer = level1Pointer * 1000 + level2Pointer;
                        long diff = writerPointer - myPointer;
                        if (diff < 100) {
                            System.out.println("Reader: Closer than 100, waiting...level1=" + level1 + ", level2=" + level2);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                System.exit(1);
                            }
                            continue;
                        }
                        rootNode = session.getRootNode();
                        replicaCrashTestNode = rootNode.getNode(REPLICA_CRASH_TEST);
                        Node level1Node = replicaCrashTestNode.getNode(level1);
                        if (!level1Node.hasNode(level2)) {
                            System.err.println("Reader: NOT FOUND: level1=" + level1 + ", level2=" + level2);
                            Thread.sleep(500);
                            session.refresh(false);
                            System.err.println("Reader: Reverifying once...");
                            rootNode = session.getRootNode();
                            replicaCrashTestNode = rootNode.getNode(REPLICA_CRASH_TEST);
                            level1Node = replicaCrashTestNode.getNode(level1);
                            final boolean hasNode = level1Node.hasNode(level2);
                            if (hasNode) {
                                System.err.println("Reader: yup, exists: " + hasNode + ", level1=" + level1 + ", level2=" + level2);
                            } else {
                                System.err.println("Reader: not found: level1=" + level1 + ", level2=" + level2);
                            }
                        } else {
                            // read it
                            Node level2Node = level1Node.getNode(level2);
                            System.out.println("Reader: verified level1=" + level1 + ", level2=" + level2);
                        }
                    } catch (com.google.common.util.concurrent.UncheckedExecutionException e) {
                        System.out.println("Got an UncheckedException from the google cache probably: " + e);
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e2) {
                            e.printStackTrace();
                            System.exit(1);
                        }
                        continue;
                    } catch (Throwable e) {
                        // yes, one should not catch Throwable - but this is a test only
                        e.printStackTrace(System.out);
                        System.exit(1);
                    }
                    if (++level2Pointer > 1000) {
                        level2Pointer = 1;
                        level1Pointer++;
                    }
                }
            } catch (RepositoryException e1) {
                e1.printStackTrace(System.out);
                System.exit(1);
            }
            System.out.println("Test start.");
        }
    };
    Thread th2 = new Thread(reader);
    Thread.sleep(1000);
    th2.start();
    System.out.println("Waiting for writer to finish...");
    th1.join();
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) RepositoryException(javax.jcr.RepositoryException) LoginException(javax.jcr.LoginException) SimpleCredentials(javax.jcr.SimpleCredentials) Session(javax.jcr.Session)

Example 52 with SimpleCredentials

use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.

the class TokenProviderImplTest method testCreateTokenFromInvalidCredentials.

@Test
public void testCreateTokenFromInvalidCredentials() throws Exception {
    List<Credentials> invalid = new ArrayList<Credentials>();
    invalid.add(new GuestCredentials());
    invalid.add(new TokenCredentials("sometoken"));
    invalid.add(new ImpersonationCredentials(new GuestCredentials(), null));
    invalid.add(new SimpleCredentials("unknownUserId", new char[0]));
    for (Credentials creds : invalid) {
        assertNull(tokenProvider.createToken(creds));
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) ArrayList(java.util.ArrayList) GuestCredentials(javax.jcr.GuestCredentials) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) GuestCredentials(javax.jcr.GuestCredentials) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) Test(org.junit.Test)

Example 53 with SimpleCredentials

use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.

the class TokenProviderImplTest method testCreateTokenWithExpirationParam.

@Test
public void testCreateTokenWithExpirationParam() throws Exception {
    SimpleCredentials sc = new SimpleCredentials(userId, new char[0]);
    sc.setAttribute(TokenProvider.PARAM_TOKEN_EXPIRATION, 100000);
    TokenInfo info = tokenProvider.createToken(sc);
    assertTokenInfo(info, userId);
    Tree tokenTree = getTokenTree(info);
    assertNotNull(tokenTree);
    assertTrue(tokenTree.exists());
    assertTrue(tokenTree.hasProperty(TokenProvider.PARAM_TOKEN_EXPIRATION));
    assertEquals(100000, tokenTree.getProperty(TokenProvider.PARAM_TOKEN_EXPIRATION).getValue(Type.LONG).longValue());
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Tree(org.apache.jackrabbit.oak.api.Tree) TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) Test(org.junit.Test)

Example 54 with SimpleCredentials

use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.

the class TokenLoginModuleTest method testSimpleCredentialsWithAttribute.

@Test
public void testSimpleCredentialsWithAttribute() throws Exception {
    ContentSession cs = null;
    try {
        SimpleCredentials sc = new SimpleCredentials("test", new char[0]);
        sc.setAttribute(".token", "");
        cs = login(sc);
        fail("Unsupported credentials login should fail");
    } catch (LoginException e) {
    // success
    } finally {
        if (cs != null) {
            cs.close();
        }
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) ContentSession(org.apache.jackrabbit.oak.api.ContentSession) LoginException(javax.security.auth.login.LoginException) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 55 with SimpleCredentials

use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.

the class TokenProviderImplTest method testCreateTokenWithInvalidExpirationParam.

@Test
public void testCreateTokenWithInvalidExpirationParam() throws Exception {
    SimpleCredentials sc = new SimpleCredentials(userId, new char[0]);
    sc.setAttribute(TokenProvider.PARAM_TOKEN_EXPIRATION, "invalid");
    try {
        tokenProvider.createToken(sc);
        fail();
    } catch (NumberFormatException e) {
    // success
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Test(org.junit.Test)

Aggregations

SimpleCredentials (javax.jcr.SimpleCredentials)289 Test (org.junit.Test)142 Session (javax.jcr.Session)83 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)60 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)53 User (org.apache.jackrabbit.api.security.user.User)41 Credentials (javax.jcr.Credentials)39 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)35 UserManager (org.apache.jackrabbit.api.security.user.UserManager)34 LoginException (javax.security.auth.login.LoginException)30 Node (javax.jcr.Node)28 RepositoryException (javax.jcr.RepositoryException)25 Principal (java.security.Principal)22 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)21 GuestCredentials (javax.jcr.GuestCredentials)20 LoginException (javax.jcr.LoginException)19 TokenCredentials (org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)19 AuthInfo (org.apache.jackrabbit.oak.api.AuthInfo)18 Before (org.junit.Before)18 ImpersonationCredentials (org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials)17