Search in sources :

Example 56 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class GuardProxyCatalogTest method testInvocationBlocking6.

@SuppressWarnings("unchecked")
@Test
public void testInvocationBlocking6() throws Exception {
    Dictionary<String, Object> c1 = new Hashtable<>();
    c1.put(Constants.SERVICE_PID, "foobar");
    c1.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
    c1.put("doit", "a,b");
    Dictionary<String, Object> c2 = new Hashtable<>();
    c2.put(Constants.SERVICE_PID, "foobar2");
    c2.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
    c2.put("bar", "c");
    BundleContext bc = mockConfigAdminBundleContext(c1, c2);
    final Object proxy = testCreateProxy(bc, new Class[] { TestServiceAPI2.class }, (TestServiceAPI2) String::toUpperCase);
    // Invoke the service with role 'c'.
    Subject subject = new Subject();
    subject.getPrincipals().add(new RolePrincipal("a"));
    subject.getPrincipals().add(new RolePrincipal("b"));
    subject.getPrincipals().add(new RolePrincipal("c"));
    Subject.doAs(subject, (PrivilegedAction<Object>) () -> {
        try {
            ((TestServiceAPI2) proxy).doit("hello");
            fail("The invocation should not process as the 'doit' operation has no roles associated with it");
        } catch (SecurityException se) {
        // good
        }
        return null;
    });
}
Also used : Hashtable(java.util.Hashtable) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 57 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project activemq-artemis by apache.

the class ArtemisFeatureTest method executeCommand.

protected String executeCommand(final String command, final Long timeout, final Boolean silent) {
    String response;
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final PrintStream printStream = new PrintStream(byteArrayOutputStream);
    final Session commandSession = sessionFactory.create(System.in, printStream, printStream);
    commandSession.put("APPLICATION", System.getProperty("karaf.name", "root"));
    commandSession.put("USER", USER);
    FutureTask<String> commandFuture = new FutureTask<>(new Callable<String>() {

        @Override
        public String call() {
            Subject subject = new Subject();
            subject.getPrincipals().add(new UserPrincipal("admin"));
            subject.getPrincipals().add(new RolePrincipal("admin"));
            subject.getPrincipals().add(new RolePrincipal("manager"));
            subject.getPrincipals().add(new RolePrincipal("viewer"));
            return Subject.doAs(subject, new PrivilegedAction<String>() {

                @Override
                public String run() {
                    try {
                        if (!silent) {
                            System.out.println(command);
                            System.out.flush();
                        }
                        commandSession.execute(command);
                    } catch (Exception e) {
                        e.printStackTrace(System.err);
                    }
                    printStream.flush();
                    return byteArrayOutputStream.toString();
                }
            });
        }
    });
    try {
        executor.submit(commandFuture);
        response = commandFuture.get(timeout, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        e.printStackTrace(System.err);
        response = "SHELL COMMAND TIMED OUT: ";
    }
    LOG.info("Execute: " + command + " - Response:" + response);
    return response;
}
Also used : PrintStream(java.io.PrintStream) JsonString(javax.json.JsonString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Subject(javax.security.auth.Subject) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FutureTask(java.util.concurrent.FutureTask) PrivilegedAction(java.security.PrivilegedAction) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Session(org.apache.karaf.shell.api.console.Session) QueueSession(javax.jms.QueueSession)

Example 58 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project fabric8 by jboss-fuse.

the class ZookeeperBackingEngine method listRoles.

private List<RolePrincipal> listRoles(String name) {
    List<RolePrincipal> result = new ArrayList<RolePrincipal>();
    String userInfo = users.get(name);
    String[] infos = userInfo.split(",");
    for (int i = 1; i < infos.length; i++) {
        String roleName = infos[i];
        if (roleName.startsWith(GROUP_PREFIX)) {
            for (RolePrincipal rp : listRoles(roleName)) {
                if (!result.contains(rp)) {
                    result.add(rp);
                }
            }
        } else {
            RolePrincipal rp = new RolePrincipal(roleName);
            if (!result.contains(rp)) {
                result.add(rp);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 59 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project fabric8 by jboss-fuse.

the class ZookeeperBackingEngine method addRole.

/**
 * Add a role to a User.
 */
public void addRole(String username, String role) {
    String userInfos = users.get(username);
    if (userInfos != null) {
        for (RolePrincipal rp : listRoles(username)) {
            if (role.equals(rp.getName())) {
                return;
            }
        }
        for (GroupPrincipal gp : listGroups(username)) {
            if (role.equals(GROUP_PREFIX + gp.getName())) {
                return;
            }
        }
        String newUserInfos = userInfos + "," + role;
        users.put(username, newUserInfos);
    }
    saveUserProperties();
}
Also used : GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 60 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project ddf by codice.

the class SecurityAssertionJwt method getPrincipals.

@Override
public Set<Principal> getPrincipals() {
    Set<Principal> principals = new HashSet<>();
    Principal primary = getPrincipal();
    principals.add(primary);
    principals.add(new RolePrincipal(primary.getName()));
    for (AttributeStatement attributeStatement : getAttributeStatements()) {
        for (Attribute attr : attributeStatement.getAttributes()) {
            if (StringUtils.containsIgnoreCase(attr.getName(), "role")) {
                for (final String attrValue : attr.getValues()) {
                    principals.add(new RolePrincipal(attrValue));
                }
            }
        }
    }
    return principals;
}
Also used : Attribute(ddf.security.assertion.Attribute) AttributeStatement(ddf.security.assertion.AttributeStatement) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

Aggregations

RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)61 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)20 Subject (javax.security.auth.Subject)19 Principal (java.security.Principal)15 Test (org.junit.Test)15 LoginException (javax.security.auth.login.LoginException)14 IOException (java.io.IOException)13 NameCallback (javax.security.auth.callback.NameCallback)13 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)13 ArrayList (java.util.ArrayList)12 Callback (javax.security.auth.callback.Callback)11 PasswordCallback (javax.security.auth.callback.PasswordCallback)10 FailedLoginException (javax.security.auth.login.FailedLoginException)10 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)9 BundleContext (org.osgi.framework.BundleContext)8 Hashtable (java.util.Hashtable)7 HashSet (java.util.HashSet)6 File (java.io.File)4 Configuration (org.osgi.service.cm.Configuration)4 Attribute (ddf.security.assertion.Attribute)3