Skip to content

Commit

Permalink
Small amount of cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Arjan Tijms <arjan.tijms@omnifish.ee>
  • Loading branch information
arjantijms committed Aug 15, 2023
1 parent 2ca5185 commit 7770a46
Showing 1 changed file with 87 additions and 88 deletions.
Expand Up @@ -16,6 +16,10 @@

package com.sun.enterprise.security.cli;

import static org.glassfish.api.ActionReport.ExitCode.FAILURE;
import static org.glassfish.api.ActionReport.ExitCode.SUCCESS;
import static org.glassfish.api.admin.ServerEnvironment.DEFAULT_INSTANCE_NAME;

import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Properties;
Expand All @@ -29,7 +33,6 @@
import org.glassfish.api.admin.AdminCommandSecurity;
import org.glassfish.api.admin.ExecuteOn;
import org.glassfish.api.admin.RuntimeType;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.config.support.CommandTarget;
import org.glassfish.config.support.TargetType;
import org.glassfish.hk2.api.PerLookup;
Expand Down Expand Up @@ -72,15 +75,14 @@
*
* @author Nandini Ektare
*/

@Service(name = "create-message-security-provider")
@PerLookup
@I18n("create.message.security.provider")
@ExecuteOn({ RuntimeType.DAS, RuntimeType.INSTANCE })
@TargetType({ CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CONFIG })
public class CreateMessageSecurityProvider implements AdminCommand, AdminCommandSecurity.Preauthorization {

final private static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(CreateMessageSecurityProvider.class);
private static final LocalStringManagerImpl localStrings = new LocalStringManagerImpl(CreateMessageSecurityProvider.class);

private static final String SERVER = "server";
private static final String CLIENT = "client";
Expand Down Expand Up @@ -127,57 +129,56 @@ public class CreateMessageSecurityProvider implements AdminCommand, AdminCommand
private String target;

@Inject
@Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
@Named(DEFAULT_INSTANCE_NAME)
private Config config;

@Inject
private Domain domain;

@AccessRequired.NewChild(type = MessageSecurityConfig.class)
private SecurityService secService;
private SecurityService securityService;

@Override
public boolean preAuthorization(AdminCommandContext context) {
config = CLIUtil.chooseConfig(domain, target, context.getActionReport());
if (config == null) {
return false;
}
secService = config.getSecurityService();

securityService = config.getSecurityService();
return true;
}

/**
* Executes the command with the command parameters passed as Properties where the keys are parameter names and the values the
* parameter values
* Executes the command with the command parameters passed as Properties where the keys are parameter names and the
* values the parameter values
*
* @param context information
*/
@Override
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();

List<MessageSecurityConfig> mscs = secService.getMessageSecurityConfig();
List<MessageSecurityConfig> messageSecurityConfigs = securityService.getMessageSecurityConfig();

// Let's find the correct MessageSecurityConfig. As of now,
// there can be only two of them - one for SOAP and one for
// Let's find the correct MessageSecurityConfig. As of now, there can be only two of them - one for SOAP and one for
// HttpServlet
MessageSecurityConfig msgSecCfg = null;
for (MessageSecurityConfig msc : mscs) {
if (msc.getAuthLayer().equals(authLayer)) {
msgSecCfg = msc;
MessageSecurityConfig messageSecurityConfig = null;
for (MessageSecurityConfig candidateMessageSecurityConfig : messageSecurityConfigs) {
if (candidateMessageSecurityConfig.getAuthLayer().equals(authLayer)) {
messageSecurityConfig = candidateMessageSecurityConfig;
}
}

// If there is message security config for this type of layer
// then, add a new provider config under it provided it is not duplicate
if (msgSecCfg != null) {
// check if there exists a provider config by the
// specified provider name; if so return failure.
List<ProviderConfig> pcs = msgSecCfg.getProviderConfig();
for (ProviderConfig pc : pcs) {
if (pc.getProviderId().equals(providerId)) {
if (messageSecurityConfig != null) {
// Check if there exists a provider config by the specified provider name; if so return failure.
for (ProviderConfig providerConfig : messageSecurityConfig.getProviderConfig()) {
if (providerConfig.getProviderId().equals(providerId)) {
report.setMessage(localStrings.getLocalString("create.message.security.provider.duplicatefound",
"Message security provider named {0} exists. " + "Cannot add duplicate.", providerId));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
"Message security provider named {0} exists. " + "Cannot add duplicate.", providerId));
report.setActionExitCode(FAILURE);
return;
}
}
Expand All @@ -187,108 +188,106 @@ public void execute(AdminCommandContext context) {
ConfigSupport.apply(new SingleConfigCode<MessageSecurityConfig>() {
@Override
public Object run(MessageSecurityConfig param) throws PropertyVetoException, TransactionFailure {
ProviderConfig newPC = param.createChild(ProviderConfig.class);
populateProviderConfigElement(newPC);
param.getProviderConfig().add(newPC);
// Depending on the providerType of the new provider
// the isDefaultProvider=true results in creation of
// either default-provider attribute or
// default-client-provider or BOTH in the message
ProviderConfig newProviderConfig = param.createChild(ProviderConfig.class);
populateProviderConfigElement(newProviderConfig);
param.getProviderConfig().add(newProviderConfig);

// Depending on the providerType of the new provider the isDefaultProvider=true results in creation of
// either default-provider attribute or default-client-provider or BOTH in the message
// security config object
if (isDefaultProvider) {
if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER))
if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER)) {
param.setDefaultProvider(providerId);
}

if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER))
if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER)) {
param.setDefaultClientProvider(providerId);
}
}
return newPC;

return newProviderConfig;
}
}, msgSecCfg);
}, messageSecurityConfig);
} catch (TransactionFailure e) {
report.setMessage(localStrings.getLocalString("create.message.security.provider.fail",
"Creation of message security provider named {0} failed", providerId));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
"Creation of message security provider named {0} failed", providerId));
report.setActionExitCode(FAILURE);
report.setFailureCause(e);
return;
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
report.setActionExitCode(SUCCESS);
report.setMessage(localStrings.getLocalString("create.message.security.provider.success",
"Creation of message security provider named {0} completed " + "successfully", providerId));
}
// Now if there is NO message security config for this type of layer
// then, first add a message security config for the layer and then
// add a provider config under this message security config
else {
"Creation of message security provider named {0} completed " + "successfully", providerId));
} else {

// Now if there is NO message security config for this type of layer then, first add a message security config
// for the layer and then add a provider config under this message security config

try {
ConfigSupport.apply(new SingleConfigCode<SecurityService>() {
@Override
public Object run(SecurityService param) throws PropertyVetoException, TransactionFailure {
MessageSecurityConfig newMSC = param.createChild(MessageSecurityConfig.class);
newMSC.setAuthLayer(authLayer);
param.getMessageSecurityConfig().add(newMSC);

ProviderConfig newPC = newMSC.createChild(ProviderConfig.class);

populateProviderConfigElement(newPC);
newMSC.getProviderConfig().add(newPC);
// Depending on the providerType of the new provider
// the isDefaultProvider=true results in creation of
// either default-provider attribute or
// default-client-provider or BOTH in the message
MessageSecurityConfig newMessageSecurityConfig = param.createChild(MessageSecurityConfig.class);
newMessageSecurityConfig.setAuthLayer(authLayer);
param.getMessageSecurityConfig().add(newMessageSecurityConfig);

ProviderConfig newProviderConfig = newMessageSecurityConfig.createChild(ProviderConfig.class);

populateProviderConfigElement(newProviderConfig);
newMessageSecurityConfig.getProviderConfig().add(newProviderConfig);

// Depending on the providerType of the new provider the isDefaultProvider=true results in creation of
// either default-provider attribute or default-client-provider or BOTH in the message
// security config object
if (isDefaultProvider) {
if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER))
newMSC.setDefaultProvider(providerId);
if (providerType.equals(SERVER) || providerType.equals(CLIENT_SERVER)) {
newMessageSecurityConfig.setDefaultProvider(providerId);
}

if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER))
newMSC.setDefaultClientProvider(providerId);
if (providerType.equals(CLIENT) || providerType.equals(CLIENT_SERVER)) {
newMessageSecurityConfig.setDefaultClientProvider(providerId);
}
}
return newMSC;

return newMessageSecurityConfig;
}
}, secService);
}, securityService);
} catch (TransactionFailure e) {
report.setMessage(localStrings.getLocalString("create.message.security.provider.fail",
"Creation of message security provider named {0} failed", providerId));
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
"Creation of message security provider named {0} failed", providerId));
report.setActionExitCode(FAILURE);
report.setFailureCause(e);
return;
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
/* report.setMessage(localStrings.getLocalString(
"create.message.security.provider.success",
"Creation of message security provider named {0} completed " +
"successfully", providerId)); */

report.setActionExitCode(SUCCESS);
}
}

private void populateProviderConfigElement(ProviderConfig newProviderConfig) throws PropertyVetoException, TransactionFailure {

newProviderConfig.setClassName(providerClass);
newProviderConfig.setProviderId(providerId);
newProviderConfig.setProviderType(providerType);

// create a new RequestPolicy config and add as child of this
// new Provider Config
RequestPolicy reqPolicy = newProviderConfig.createChild(RequestPolicy.class);
reqPolicy.setAuthSource(requestAuthSource);
reqPolicy.setAuthRecipient(requestAuthRecipient);
newProviderConfig.setRequestPolicy(reqPolicy);

// create a new ResponsePolicy config and add as child of this
// new Provider Config
ResponsePolicy respPolicy = newProviderConfig.createChild(ResponsePolicy.class);
respPolicy.setAuthSource(responseAuthSource);
respPolicy.setAuthRecipient(responseAuthRecipient);
newProviderConfig.setResponsePolicy(respPolicy);

// add properties
// Create a new RequestPolicy config and add as child of this new Provider Config
RequestPolicy newRequestPolicy = newProviderConfig.createChild(RequestPolicy.class);
newRequestPolicy.setAuthSource(requestAuthSource);
newRequestPolicy.setAuthRecipient(requestAuthRecipient);
newProviderConfig.setRequestPolicy(newRequestPolicy);

// Create a new ResponsePolicy config and add as child of this new Provider Config
ResponsePolicy newResponsePolicy = newProviderConfig.createChild(ResponsePolicy.class);
newResponsePolicy.setAuthSource(responseAuthSource);
newResponsePolicy.setAuthRecipient(responseAuthRecipient);
newProviderConfig.setResponsePolicy(newResponsePolicy);

// Add properties
if (properties != null) {
for (Object propname : properties.keySet()) {
Property newprop = newProviderConfig.createChild(Property.class);
newprop.setName((String) propname);
newprop.setValue(properties.getProperty((String) propname));
newProviderConfig.getProperty().add(newprop);
Property newProperty = newProviderConfig.createChild(Property.class);
newProperty.setName((String) propname);
newProperty.setValue(properties.getProperty((String) propname));
newProviderConfig.getProperty().add(newProperty);
}
}
}
Expand Down

0 comments on commit 7770a46

Please sign in to comment.