Search in sources :

Example 1 with NoSuchUserException

use of com.sun.enterprise.security.auth.realm.NoSuchUserException in project Payara by payara.

the class DeleteFileUser method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the paramter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    // Get FileRealm class name, match it with what is expected.
    String fileRealmClassName = fileAuthRealm.getClassname();
    // Report error if provided impl is not the one expected
    if (fileRealmClassName != null && !fileRealmClassName.equals("com.sun.enterprise.security.auth.realm.file.FileRealm")) {
        report.setMessage(localStrings.getLocalString("delete.file.user.realmnotsupported", "Configured file realm {0} is not supported.", fileRealmClassName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // ensure we have the file associated with the authrealm
    String keyFile = null;
    for (Property fileProp : fileAuthRealm.getProperty()) {
        if (fileProp.getName().equals("file"))
            keyFile = fileProp.getValue();
    }
    final String kFile = keyFile;
    if (keyFile == null) {
        report.setMessage(localStrings.getLocalString("delete.file.user.keyfilenotfound", "There is no physical file associated with this file realm {0} ", authRealmName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    boolean exists = (new File(kFile)).exists();
    if (!exists) {
        report.setMessage(localStrings.getLocalString("file.realm.keyfilenonexistent", "The specified physical file {0} associated with the file realm {1} does not exist.", new Object[] { kFile, authRealmName }));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // hypothetically ?.
    try {
        ConfigSupport.apply(new SingleConfigCode<SecurityService>() {

            public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
                try {
                    realmsManager.createRealms(config);
                    final FileRealm fr = (FileRealm) realmsManager.getFromLoadedRealms(config.getName(), authRealmName);
                    fr.removeUser(userName);
                    fr.persist();
                    CreateFileUser.refreshRealm(config.getName(), authRealmName);
                    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                } catch (NoSuchUserException e) {
                    report.setMessage(localStrings.getLocalString("delete.file.user.usernotfound", "There is no such existing user {0} in the file realm {1}.", userName, authRealmName) + "  " + e.getLocalizedMessage());
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(e);
                } catch (BadRealmException e) {
                    report.setMessage(localStrings.getLocalString("delete.file.user.realmcorrupted", "Configured file realm {0} is corrupted.", authRealmName) + "  " + e.getLocalizedMessage());
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(e);
                } catch (Exception e) {
                    e.printStackTrace();
                    report.setMessage(localStrings.getLocalString("delete.file.user.userdeletefailed", "Removing User {0} from file realm {1} failed", userName, authRealmName) + "  " + e.getLocalizedMessage());
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    report.setFailureCause(e);
                }
                return null;
            }
        }, securityService);
    } catch (Exception e) {
        report.setMessage(localStrings.getLocalString("delete.file.user.userdeletefailed", "Removing User {0} from file realm {1} failed", userName, authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) ActionReport(org.glassfish.api.ActionReport) FileRealm(com.sun.enterprise.security.auth.realm.file.FileRealm) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) PropertyVetoException(java.beans.PropertyVetoException) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) PropertyVetoException(java.beans.PropertyVetoException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) Property(org.jvnet.hk2.config.types.Property) File(java.io.File)

Example 2 with NoSuchUserException

use of com.sun.enterprise.security.auth.realm.NoSuchUserException in project Payara by payara.

the class ListFileGroup method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the paramter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    try {
        // Get all users of this file realm. If a username has
        // been passed in through the --name CLI option use that
        FileRealm fr = getFileRealm(securityService, fileAuthRealm, report);
        if (fr == null) {
            // in the right cause of this situation
            return;
        }
        Enumeration groups = null;
        if (fileUserName != null) {
            fr.getUser(fileUserName);
            groups = fr.getGroupNames(fileUserName);
        } else {
            groups = fr.getGroupNames();
        }
        report.getTopMessagePart().setMessage(localStrings.getLocalString("list.file.group.success", "list-file-groups successful"));
        report.getTopMessagePart().setChildrenType("file-group");
        while (groups.hasMoreElements()) {
            final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
            part.setMessage((String) groups.nextElement());
        }
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (BadRealmException e) {
        report.setMessage(localStrings.getLocalString("list.file.group.realmcorrupted", "Configured file realm {0} is corrupted.", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    } catch (NoSuchUserException e) {
        report.setMessage(localStrings.getLocalString("list.file.group.usernotfound", "Specified file user {0} not found.", fileUserName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) Enumeration(java.util.Enumeration) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) ActionReport(org.glassfish.api.ActionReport) FileRealm(com.sun.enterprise.security.auth.realm.file.FileRealm)

Example 3 with NoSuchUserException

use of com.sun.enterprise.security.auth.realm.NoSuchUserException in project Payara by payara.

the class ListFileUser method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the paramter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    // Get FileRealm class name, match it with what is expected.
    String fileRealmClassName = fileAuthRealm.getClassname();
    // Report error if provided impl is not the one expected
    if (fileRealmClassName != null && !fileRealmClassName.equals("com.sun.enterprise.security.auth.realm.file.FileRealm")) {
        report.setMessage(localStrings.getLocalString("list.file.user.realmnotsupported", "Configured file realm {0} is not supported.", fileRealmClassName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // ensure we have the file associated with the authrealm
    String keyFile = null;
    for (Property fileProp : fileAuthRealm.getProperty()) {
        if (fileProp.getName().equals("file"))
            keyFile = fileProp.getValue();
    }
    if (keyFile == null) {
        report.setMessage(localStrings.getLocalString("list.file.user.keyfilenotfound", "There is no physical file associated with this file realm {0} ", authRealmName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    boolean exists = (new File(keyFile)).exists();
    if (!exists) {
        report.setMessage(localStrings.getLocalString("file.realm.keyfilenonexistent", "The specified physical file {0} associated with the file realm {1} does not exist.", new Object[] { keyFile, authRealmName }));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // We have the right impl so let's try to remove one
    FileRealm fr = null;
    try {
        realmsManager.createRealms(config);
        // account for updates to realms from outside this config sharing
        // same keyfile
        CreateFileUser.refreshRealm(config.getName(), authRealmName);
        fr = (FileRealm) realmsManager.getFromLoadedRealms(config.getName(), authRealmName);
        if (fr == null) {
            throw new NoSuchRealmException(authRealmName);
        }
    } catch (NoSuchRealmException e) {
        report.setMessage(localStrings.getLocalString("list.file.user.realmnotsupported", "Configured file realm {0} is not supported.", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    try {
        Enumeration users = fr.getUserNames();
        List userList = new ArrayList();
        while (users.hasMoreElements()) {
            final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
            String userName = (String) users.nextElement();
            part.setMessage(userName);
            Map userMap = new HashMap();
            userMap.put("name", userName);
            try {
                userMap.put("groups", Collections.list(fr.getGroupNames(userName)));
            } catch (NoSuchUserException ex) {
            // This should never be thrown since we just got the user name from the realm
            }
            userList.add(userMap);
        }
        Properties extraProperties = new Properties();
        extraProperties.put("users", userList);
        report.setExtraProperties(extraProperties);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (BadRealmException e) {
        report.setMessage(localStrings.getLocalString("list.file.user.realmcorrupted", "Configured file realm {0} is corrupted.", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) FileRealm(com.sun.enterprise.security.auth.realm.file.FileRealm) Properties(java.util.Properties) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.jvnet.hk2.config.types.Property) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with NoSuchUserException

use of com.sun.enterprise.security.auth.realm.NoSuchUserException in project Payara by payara.

the class LoginContextDriver method loginPrincipal.

/**
 * This method is used for logging in a run As principal. It creates
 * a JAAS subject whose credential is to type GSSUPName.
 * This is used primarily for runas
 */
public static void loginPrincipal(String username, String realmName) throws LoginException {
    // no realm provided, assuming default
    if (realmName == null || realmName.length() == 0) {
        realmName = Realm.getDefaultRealm();
    }
    final Subject s = new Subject();
    final org.glassfish.security.common.PrincipalImpl p = new org.glassfish.security.common.PrincipalImpl(username);
    final GSSUPName name = new GSSUPName(username, realmName);
    AppservAccessController.doPrivileged(new PrivilegedAction() {

        public java.lang.Object run() {
            s.getPrincipals().add(p);
            s.getPublicCredentials().add(name);
            return null;
        }
    });
    try {
        Realm realm = Realm.getInstance(realmName);
        Enumeration en = realm.getGroupNames(username);
        Set<Principal> principalSet = s.getPrincipals();
        while (en.hasMoreElements()) {
            principalSet.add(new Group((String) en.nextElement()));
        }
    } catch (InvalidOperationException ex) {
        _logger.log(Level.WARNING, SecurityLoggerInfo.invalidOperationForRealmError, new Object[] { username, realmName, ex.toString() });
    } catch (NoSuchUserException ex) {
        _logger.log(Level.WARNING, SecurityLoggerInfo.noSuchUserInRealmError, new Object[] { username, realmName, ex.toString() });
    } catch (NoSuchRealmException ex) {
        LoginException lex = new LoginException(ex.toString());
        lex.initCause(ex);
        throw lex;
    }
    setSecurityContext(username, s, realmName);
}
Also used : Group(org.glassfish.security.common.Group) Enumeration(java.util.Enumeration) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) Subject(javax.security.auth.Subject) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) GSSUPName(com.sun.enterprise.common.iiop.security.GSSUPName) PrivilegedAction(java.security.PrivilegedAction) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) LoginException(com.sun.enterprise.security.auth.login.common.LoginException) Realm(com.sun.enterprise.security.auth.realm.Realm) CertificateRealm(com.sun.enterprise.security.auth.realm.certificate.CertificateRealm) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Example 5 with NoSuchUserException

use of com.sun.enterprise.security.auth.realm.NoSuchUserException in project Payara by payara.

the class GetGroupNamesCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    Config tmp = null;
    try {
        tmp = configs.getConfigByName(target);
    } catch (Exception ex) {
    }
    if (tmp != null) {
        config = tmp;
    }
    if (tmp == null) {
        Server targetServer = domain.getServerNamed(target);
        if (targetServer != null) {
            config = domain.getConfigNamed(targetServer.getConfigRef());
        }
        com.sun.enterprise.config.serverbeans.Cluster cluster = domain.getClusterNamed(target);
        if (cluster != null) {
            config = domain.getConfigNamed(cluster.getConfigRef());
        }
    }
    ActionReporter report = (ActionReporter) context.getActionReport();
    try {
        String[] list = getGroupNames(realmName, userName);
        List<String> ret = Arrays.asList(list);
        report.setActionExitCode(ExitCode.SUCCESS);
        Properties props = new Properties();
        props.put("groups", ret);
        report.setExtraProperties(props);
        report.setMessage("" + ret);
    } catch (NoSuchRealmException ex) {
        report.setFailureCause(ex);
        report.setActionExitCode(ExitCode.FAILURE);
    } catch (BadRealmException ex) {
        report.setFailureCause(ex);
        report.setActionExitCode(ExitCode.FAILURE);
    } catch (InvalidOperationException ex) {
        report.setFailureCause(ex);
        report.setActionExitCode(ExitCode.FAILURE);
    } catch (NoSuchUserException ex) {
        report.setFailureCause(ex);
        report.setActionExitCode(ExitCode.FAILURE);
    }
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) Properties(java.util.Properties) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) ActionReporter(com.sun.enterprise.v3.common.ActionReporter)

Aggregations

NoSuchUserException (com.sun.enterprise.security.auth.realm.NoSuchUserException)5 BadRealmException (com.sun.enterprise.security.auth.realm.BadRealmException)4 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)3 FileRealm (com.sun.enterprise.security.auth.realm.file.FileRealm)3 Enumeration (java.util.Enumeration)3 ActionReport (org.glassfish.api.ActionReport)3 InvalidOperationException (com.sun.enterprise.security.auth.realm.InvalidOperationException)2 File (java.io.File)2 Properties (java.util.Properties)2 Property (org.jvnet.hk2.config.types.Property)2 GSSUPName (com.sun.enterprise.common.iiop.security.GSSUPName)1 Config (com.sun.enterprise.config.serverbeans.Config)1 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)1 Server (com.sun.enterprise.config.serverbeans.Server)1 LoginException (com.sun.enterprise.security.auth.login.common.LoginException)1 Realm (com.sun.enterprise.security.auth.realm.Realm)1 CertificateRealm (com.sun.enterprise.security.auth.realm.certificate.CertificateRealm)1 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)1 PropertyVetoException (java.beans.PropertyVetoException)1 Principal (java.security.Principal)1