Search in sources :

Example 56 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class DeleteAuthRealm 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
 */
@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    try {
        ConfigSupport.apply(new SingleConfigCode<SecurityService>() {

            public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
                param.getAuthRealm().remove(authRealm);
                // temporary fix - since the SecurityConfigListener is  not being called on an realm delete.
                SecurityConfigListener.authRealmDeleted(authRealm);
                return null;
            }
        }, securityService);
    } catch (TransactionFailure e) {
        report.setMessage(localStrings.getLocalString("delete.auth.realm.fail", "Deletion of Authrealm {0} failed", authRealmName) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) ActionReport(org.glassfish.api.ActionReport)

Example 57 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode 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
 */
@Override
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 (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) 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) 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 58 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class DeleteMessageSecurityProvider 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
 */
@Override
public void execute(AdminCommandContext context) {
    ActionReport report = context.getActionReport();
    List<ProviderConfig> pcs = msgSecCfg.getProviderConfig();
    for (ProviderConfig pc : pcs) {
        if (pc.getProviderId().equals(providerId)) {
            thePC = pc;
            try {
                ConfigSupport.apply(new SingleConfigCode<MessageSecurityConfig>() {

                    public Object run(MessageSecurityConfig param) throws PropertyVetoException, TransactionFailure {
                        if ((param.getDefaultProvider() != null) && param.getDefaultProvider().equals(thePC.getProviderId())) {
                            param.setDefaultProvider(null);
                        }
                        if ((param.getDefaultClientProvider() != null) && param.getDefaultClientProvider().equals(thePC.getProviderId())) {
                            param.setDefaultClientProvider(null);
                        }
                        param.getProviderConfig().remove(thePC);
                        return null;
                    }
                }, msgSecCfg);
            } catch (TransactionFailure e) {
                e.printStackTrace();
                report.setMessage(localStrings.getLocalString("delete.message.security.provider.fail", "Deletion of message security provider named {0} failed", providerId));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                report.setFailureCause(e);
                return;
            }
            /*report.setMessage(localStrings.getLocalString(
                    "delete.message.security.provider.success", 
                    "Deletion of message security provider {0} completed " +
                    "successfully", providerId));*/
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
            return;
        }
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ProviderConfig(com.sun.enterprise.config.serverbeans.ProviderConfig) MessageSecurityConfig(com.sun.enterprise.config.serverbeans.MessageSecurityConfig) ActionReport(org.glassfish.api.ActionReport)

Example 59 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class DeleteJaccProvider method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    try {
        List<JaccProvider> jaccProviders = securityService.getJaccProvider();
        JaccProvider jprov = null;
        for (JaccProvider jaccProv : jaccProviders) {
            if (jaccProv.getName().equals(jaccprovider)) {
                jprov = jaccProv;
                break;
            }
        }
        final JaccProvider jaccprov = jprov;
        ConfigSupport.apply(new SingleConfigCode<SecurityService>() {

            public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
                param.getJaccProvider().remove(jaccprov);
                return null;
            }
        }, securityService);
    } catch (TransactionFailure e) {
        report.setMessage(localStrings.getLocalString("delete.jacc.provider.fail", "Deletion of JaccProvider {0} failed", jaccprovider) + "  " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) JaccProvider(com.sun.enterprise.config.serverbeans.JaccProvider) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) ActionReport(org.glassfish.api.ActionReport)

Example 60 with SingleConfigCode

use of org.jvnet.hk2.config.SingleConfigCode in project Payara by payara.

the class CreateConnectorSecurityMap method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the parameter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (securityMapName == null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noSecurityMapName", "No security map name specified"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (principals == null && userGroups == null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noPrincipalsOrGroupsMap", "Either the principal or the user group has to be specified while creating a security map." + " Both cannot be null."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (principals != null && userGroups != null) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.specifyPrincipalsOrGroupsMap", "A work-security-map can have either (any number of) group mapping or (any number of) principals" + " mapping but not both. Specify --principals or --usergroups."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    Collection<ConnectorConnectionPool> ccPools = domain.getResources().getResources(ConnectorConnectionPool.class);
    if (!doesPoolNameExist(poolName, ccPools)) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.noSuchPoolFound", "Connector connection pool {0} does not exist. Please specify a valid pool name.", poolName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (doesMapNameExist(poolName, securityMapName, ccPools)) {
        report.setMessage(localStrings.getLocalString("create.connector.security.map.duplicate", "A security map named {0} already exists for connector connection pool {1}. Please give a" + " different map name.", securityMapName, poolName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // get all the security maps for this pool.....
    List<SecurityMap> maps = getAllSecurityMapsForPool(poolName, ccPools);
    if (principals != null) {
        for (String principal : principals) {
            if (isPrincipalExisting(principal, maps)) {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.principal_exists", "The principal {0} already exists in connector connection pool {1}. Please give a " + "different principal name.", principal, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    if (userGroups != null) {
        for (String userGroup : userGroups) {
            if (isUserGroupExisting(userGroup, maps)) {
                report.setMessage(localStrings.getLocalString("create.connector.security.map.usergroup_exists", "The user-group {0} already exists in connector connection pool {1}. Please give a" + " different user-group name.", userGroup, poolName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    ConnectorConnectionPool connPool = null;
    for (ConnectorConnectionPool ccp : ccPools) {
        if (ccp.getName().equals(poolName)) {
            connPool = ccp;
        }
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<ConnectorConnectionPool>() {

            public Object run(ConnectorConnectionPool ccp) throws PropertyVetoException, TransactionFailure {
                List<SecurityMap> securityMaps = ccp.getSecurityMap();
                SecurityMap newResource = ccp.createChild(SecurityMap.class);
                newResource.setName(securityMapName);
                if (principals != null) {
                    for (String p : principals) {
                        newResource.getPrincipal().add(p);
                    }
                }
                if (userGroups != null) {
                    for (String u : userGroups) {
                        newResource.getUserGroup().add(u);
                    }
                }
                BackendPrincipal backendPrincipal = newResource.createChild(BackendPrincipal.class);
                backendPrincipal.setUserName(mappedusername);
                if (mappedpassword != null && !mappedpassword.isEmpty()) {
                    backendPrincipal.setPassword(mappedpassword);
                }
                newResource.setBackendPrincipal(backendPrincipal);
                securityMaps.add(newResource);
                return newResource;
            }
        }, connPool);
    } catch (TransactionFailure tfe) {
        Object[] params = { securityMapName, poolName };
        report.setMessage(localStrings.getLocalString("create.connector.security.map.fail", "Unable to create connector security map {0} for connector connection pool {1} ", params) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConnectorConnectionPool(org.glassfish.connectors.config.ConnectorConnectionPool) SecurityMap(org.glassfish.connectors.config.SecurityMap) BackendPrincipal(org.glassfish.connectors.config.BackendPrincipal) List(java.util.List) ActionReport(org.glassfish.api.ActionReport)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)153 PropertyVetoException (java.beans.PropertyVetoException)130 ActionReport (org.glassfish.api.ActionReport)76 Config (com.sun.enterprise.config.serverbeans.Config)47 Property (org.jvnet.hk2.config.types.Property)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 List (java.util.List)23 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)19 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 CommandTarget (org.glassfish.config.support.CommandTarget)15 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)15 Protocol (org.glassfish.grizzly.config.dom.Protocol)15 Target (org.glassfish.internal.api.Target)15 Test (org.junit.Test)15 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)13 ArrayList (java.util.ArrayList)11 Protocols (org.glassfish.grizzly.config.dom.Protocols)11 Resource (com.sun.enterprise.config.serverbeans.Resource)9 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)9 Properties (java.util.Properties)9