Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit 5349c9a

Browse files
mskdeepak-oracleyaminikb
authored andcommitted
Fixes #21356: Validate resource adapter name (#22137)
* Add validation logic for connector resources defined through app-scoped resources
1 parent cbf4aad commit 5349c9a

4 files changed

Lines changed: 125 additions & 6 deletions

File tree

appserver/deployment/dol/src/main/java/com/sun/enterprise/deployment/util/LocalStrings.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ enterprise.deployment.util.application.fail=Application validation fails for giv
4848
enterprise.deployment.util.descriptor.duplicate=Duplicate descriptor found for given jndi-name: [{0}]
4949
enterprise.deployment.util.application.lookup=Lookup failed for given jndi-name: [{0}]
5050
enterprise.deployment.util.unsupportedruntimealtdd=Unsupported external runtime alternate deployment descriptor [{0}]. The currently supported external runtime alternate deployment descriptors are GlassFish runtime deployment descriptors (the file name needs to contain "glassfish-")
51-
enterprise.deployment.util.resource.validation=JNDI lookup failed for the resource: Name: [{0}], Lookup: [{1}], Type: [{2}]
51+
enterprise.deployment.util.resource.validation=JNDI lookup failed for the resource: Name: [{0}], Lookup: [{1}], Type: [{2}]
52+
enterprise.deployment.util.ra.validation=Resource Adapter not present: RA Name: [{0}], Type: [{1}]

appserver/deployment/dol/src/main/java/com/sun/enterprise/deployment/util/ResourceValidator.java

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ public class ResourceValidator implements EventListener, ResourceValidatorVisito
8383
)
8484
private static final String RESOURCE_REF_JNDI_LOOKUP_FAILED = "AS-DEPLOYMENT-00026";
8585

86+
@LogMessageInfo(
87+
message = "Resource Adapter not present: RA Name: {0}, Type: {1}.",
88+
level = "SEVERE",
89+
cause = "Resource apapter specified is invalid.",
90+
action = "Configure the required resource adapter."
91+
)
92+
private static final String RESOURCE_REF_INVALID_RA = "AS-DEPLOYMENT-00027";
93+
8694
@LogMessageInfo(message = "Skipping resource validation")
8795
private static final String SKIP_RESOURCE_VALIDATION = "AS-DEPLOYMENT-00028";
8896

@@ -525,13 +533,21 @@ private void parseResources(ServiceReferenceDescriptor serviceRef, JndiNameEnvir
525533

526534
/**
527535
* Store the resource definitions in our namespace.
528-
* CFD and AODD are not valid in an AppClient.
536+
* CFD and AODD are not valid in an AppClient. O/w need to validate the ra-name in them.
529537
*/
530538
private void parseResources(ResourceDescriptor resourceDescriptor, JndiNameEnvironment env, AppResources appResources) {
531-
if (env instanceof ApplicationClientDescriptor)
532-
if (resourceDescriptor.getResourceType().equals(JavaEEResourceType.CFD) || resourceDescriptor.getResourceType().equals(JavaEEResourceType.AODD))
539+
JavaEEResourceType type = resourceDescriptor.getResourceType();
540+
if (type.equals(JavaEEResourceType.CFD) || type.equals(JavaEEResourceType.AODD)) {
541+
if (env instanceof ApplicationClientDescriptor)
533542
return;
534-
storeInNamespace(resourceDescriptor.getName(), env, appResources);
543+
// No need to type check as CFD and AODD extend from AbstractConnectorResourceDescriptor
544+
AbstractConnectorResourceDescriptor acrd = (AbstractConnectorResourceDescriptor) resourceDescriptor;
545+
appResources.store(new AppResource(resourceDescriptor.getName(), acrd.getResourceAdapter(), type.toString(), env, true));
546+
}
547+
else {
548+
// nothing to validate here. store the definitions in our namespace.
549+
storeInNamespace(resourceDescriptor.getName(), env, appResources);
550+
}
535551
}
536552

537553
/**
@@ -692,9 +708,82 @@ else if (jndiName.startsWith(ResourceConstants.JAVA_MODULE_SCOPE_PREFIX)) {
692708
*/
693709
private void validateResources(AppResources appResources) {
694710
for (AppResource resource : appResources.myResources) {
695-
if (resource.validate)
711+
if (!resource.validate)
712+
continue;
713+
if (resource.getType().equals("CFD") || resource.getType().equals("AODD"))
714+
validateRAName(resource);
715+
else
696716
validateJNDIRefs(resource, appResources.myNamespace);
697717
}
718+
// Validate the ra-names of app scoped resources
719+
// RA-name and the type of this resource are stored
720+
List<Map.Entry<String, String>> raNames = (List<Map.Entry<String, String>>)
721+
dc.getTransientAppMetadata().get(ResourceConstants.APP_SCOPED_RESOURCES_RA_NAMES);
722+
for (Map.Entry<String, String> entry: raNames) {
723+
validateRAName(entry.getKey(), entry.getValue());
724+
}
725+
}
726+
727+
/**
728+
* Validate the resource adapter names of @CFD, @AODD.
729+
*/
730+
private void validateRAName(AppResource resource) {
731+
validateRAName(resource.getJndiName(), resource.getType());
732+
}
733+
734+
/**
735+
* Strategy to validate the resource adapter name:
736+
*
737+
* 1) In case of stand-alone RA, look in the domain.xml and for default system RA's
738+
* 2) In case of embedded RA, compare it with names of RAR descriptors
739+
*
740+
* In case of null ra name, we fail the deployment.
741+
*/
742+
private void validateRAName(String raname, String type) {
743+
// No ra-name specified
744+
if (raname == null || raname.length() == 0) {
745+
deplLogger.log(Level.SEVERE, RESOURCE_REF_INVALID_RA,
746+
new Object[] {null, type});
747+
throw new DeploymentException(localStrings.getLocalString("enterprise.deployment.util.ra.validation",
748+
"Resource Adapter not present: RA Name: {0}, Type: {1}.",
749+
null, type));
750+
}
751+
int poundIndex = raname.indexOf("#");
752+
753+
// Pound not present: check for app named raname in domain.xml, check for system ra's
754+
if (poundIndex < 0) {
755+
if (domain.getApplications().getApplication(raname) != null)
756+
return;
757+
// System RA's - Copied from ConnectorConstants.java
758+
if (raname.equals("jmsra") || raname.equals("__ds_jdbc_ra") || raname.equals("jaxr-ra") ||
759+
raname.equals("__cp_jdbc_ra") || raname.equals("__xa_jdbc_ra") || raname.equals("__dm_jdbc_ra"))
760+
return;
761+
if(isEmbedded(raname))
762+
return;
763+
}
764+
// Embedded RA
765+
// In case the app name does not match, we fail the deployment
766+
else if (raname.substring(0, poundIndex).equals(application.getAppName())) {
767+
raname = raname.substring(poundIndex + 1);
768+
if(isEmbedded(raname))
769+
return;
770+
}
771+
deplLogger.log(Level.SEVERE, RESOURCE_REF_INVALID_RA,
772+
new Object[] {raname, type});
773+
throw new DeploymentException(localStrings.getLocalString(
774+
"enterprise.deployment.util.ra.validation",
775+
"Resource Adapter not present: RA Name: {0}, Type: {1}.",
776+
raname, type));
777+
}
778+
779+
private boolean isEmbedded(String raname) {
780+
String ranameWithRAR = raname + ".rar";
781+
// check for rar named this
782+
for (BundleDescriptor bd : application.getBundleDescriptors(ConnectorDescriptor.class)) {
783+
if(raname.equals(bd.getModuleName()) || ranameWithRAR.equals(bd.getModuleName()))
784+
return true;
785+
}
786+
return false;
698787
}
699788

700789
/**

appserver/resources/resources-runtime/src/main/java/org/glassfish/resources/module/ResourcesDeployer.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ private void processArchive(DeploymentContext dc) {
251251

252252
Map<String,Map<String, List>> appScopedResources = new HashMap<String,Map<String,List>>();
253253
Map<String, List<String>> jndiNames = new HashMap<String, List<String>>();
254+
List<Map.Entry<String, String>> raNames = new ArrayList<>();
254255
Map<String, String> fileNames = new HashMap<String, String>();
255256

256257
String appName = getAppNameFromDeployCmdParams(dc);
@@ -285,6 +286,9 @@ private void processArchive(DeploymentContext dc) {
285286
resourcesList.put(NON_CONNECTOR_RESOURCES, nonConnectorResources);
286287
for (org.glassfish.resources.api.Resource resource : nonConnectorResources) {
287288
String jndiName = extractJNDIName(resource);
289+
if (hasRAName(resource)) {
290+
raNames.add(new AbstractMap.SimpleEntry<>(extractRAName(resource), resource.getType()));
291+
}
288292
if (jndiName != null) {
289293
jndiNamesList.add(jndiName);
290294
}
@@ -295,6 +299,9 @@ private void processArchive(DeploymentContext dc) {
295299
resourcesList.put(CONNECTOR_RESOURCES, connectorResources);
296300
for (org.glassfish.resources.api.Resource resource : connectorResources) {
297301
String jndiName = extractJNDIName(resource);
302+
if (hasRAName(resource)) {
303+
raNames.add(new AbstractMap.SimpleEntry<>(extractRAName(resource), resource.getType()));
304+
}
298305
if (jndiName != null) {
299306
jndiNamesList.add(jndiName);
300307
}
@@ -304,6 +311,7 @@ private void processArchive(DeploymentContext dc) {
304311
appScopedResources.put(moduleName, resourcesList);
305312
}
306313
dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_JNDI_NAMES, jndiNames);
314+
dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_RA_NAMES, raNames);
307315
dc.addTransientAppMetaData(APP_SCOPED_RESOURCES_MAP, appScopedResources);
308316
ApplicationInfo appInfo = appRegistry.get(appName);
309317
if(appInfo != null){
@@ -329,6 +337,26 @@ private String extractJNDIName(org.glassfish.resources.api.Resource resource) {
329337
return (String) attrs.get(JNDI_NAME);
330338
}
331339

340+
private boolean hasRAName(org.glassfish.resources.api.Resource resource) {
341+
return resource.getType().equals(ADMIN_OBJECT_RESOURCE) ||
342+
resource.getType().equals(CONNECTOR_CONNECTION_POOL) ||
343+
resource.getType().equals(RESOURCE_ADAPTER_CONFIG) ||
344+
resource.getType().equals(CONNECTOR_WORK_SECURITY_MAP);
345+
}
346+
347+
/**
348+
* Extract the RA name for a connector resource. Collecting for resource validation.
349+
*
350+
* @param resource
351+
* @return resource adapter name
352+
*/
353+
private String extractRAName(org.glassfish.resources.api.Resource resource) {
354+
if (resource.getType().equals(ADMIN_OBJECT_RESOURCE))
355+
return (String)resource.getAttributes().get(RES_ADAPTER);
356+
else
357+
return (String)resource.getAttributes().get(RES_ADAPTER_NAME);
358+
}
359+
332360
private static void validateResourcesXML(File file, ResourcesXMLParser parser) throws ResourceConflictException {
333361
String filePath = file.getPath();
334362
SunResourcesXML sunResourcesXML = new SunResourcesXML(filePath, parser.getResourcesList());

nucleus/resources/src/main/java/org/glassfish/resourcebase/resources/api/ResourceConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public static enum TriState {
150150

151151
public final static String APP_META_DATA_RESOURCES = "app-level-resources-config";
152152
public final static String APP_SCOPED_RESOURCES_JNDI_NAMES = "app-scoped-resources-jndi-names";
153+
public final static String APP_SCOPED_RESOURCES_RA_NAMES = "app-scoped-resources-ra-names";
153154
public final static String APP_SCOPED_RESOURCES_MAP = "app-scoped-resources-map";
154155

155156
}

0 commit comments

Comments
 (0)