Skip to content

Commit abba016

Browse files
Merge pull request #13043 from Avishka-Shamendra/apim-governance-core
Fix Governance Bugs and Gateway Visibility Bugs
2 parents 5144e49 + 482f92a commit abba016

File tree

4 files changed

+67
-71
lines changed

4 files changed

+67
-71
lines changed

Diff for: components/apimgt/org.wso2.carbon.apimgt.governance.impl/src/main/java/org/wso2/carbon/apimgt/governance/impl/dao/constants/SQLConstants.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public class SQLConstants {
294294

295295
public static final String DELETE_REQ_POLICY_MAPPING_FOR_ARTIFACT =
296296
"DELETE FROM GOV_REQUEST_POLICY " +
297-
"WHERE REQ_ID = (" +
297+
"WHERE REQ_ID IN (" +
298298
" SELECT GR.REQ_ID " +
299299
" FROM GOV_REQUEST GR " +
300300
" JOIN GOV_ARTIFACT GA ON GR.ARTIFACT_KEY = GA.ARTIFACT_KEY " +
@@ -317,8 +317,7 @@ public class SQLConstants {
317317
"RULESET_ID, RULE_NAME, VIOLATED_PATH, MESSAGE) VALUES (?, ?, ?, ?, ?, ?)";
318318

319319
public static final String DELETE_POLICY_RUN_FOR_ARTIFACT_AND_POLICY = "DELETE FROM GOV_POLICY_RUN " +
320-
"WHERE ARTIFACT_KEY IN ( SELECT ARTIFACT_KEY FROM GOV_ARTIFACT WHERE ARTIFACT_REF_ID = ? " +
321-
"AND ARTIFACT_TYPE = ? AND ORGANIZATION = ? ) AND POLICY_ID = ?";
320+
"WHERE ARTIFACT_KEY = ? AND POLICY_ID = ?";
322321

323322
public static final String DELETE_POLICY_RUN_FOR_POLICY = "DELETE FROM GOV_POLICY_RUN " +
324323
"WHERE POLICY_ID = ?";
@@ -335,9 +334,11 @@ public class SQLConstants {
335334
"IN ( SELECT ARTIFACT_KEY FROM GOV_ARTIFACT WHERE ARTIFACT_REF_ID = ? " +
336335
"AND ARTIFACT_TYPE = ? AND ORGANIZATION = ? )";
337336

338-
public static final String DELETE_RULESET_RUN_FOR_ARTIFACT_AND_RULESET = "DELETE FROM GOV_RULESET_RUN " +
339-
"WHERE ARTIFACT_KEY IN ( SELECT GA.ARTIFACT_KEY FROM GOV_ARTIFACT GA WHERE GA.ARTIFACT_REF_ID = ? " +
340-
"AND GA.ARTIFACT_TYPE = ? AND GA.ORGANIZATION = ? ) AND RULESET_ID = ?";
337+
public static final String DELETE_RULESET_RUN_FOR_ARTIFACT_AND_POLICY = "DELETE FROM GOV_RULESET_RUN " +
338+
"WHERE ARTIFACT_KEY = ? AND RULESET_ID IN ( " +
339+
" SELECT RULESET_ID FROM GOV_POLICY_RULESET " +
340+
" WHERE POLICY_ID = ? " +
341+
")";
341342

342343
public static final String DELETE_RULESET_RUN_FOR_RULESET = "DELETE FROM GOV_RULESET_RUN " +
343344
"WHERE RULESET_ID = ?";
@@ -363,10 +364,14 @@ public class SQLConstants {
363364
"WHERE ARTIFACT_KEY IN (SELECT ARTIFACT_KEY FROM GOV_ARTIFACT WHERE ARTIFACT_REF_ID = ? " +
364365
"AND ARTIFACT_TYPE = ? AND ORGANIZATION = ?)";
365366

366-
public static final String DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_RULESET = "DELETE FROM GOV_RULE_VIOLATION " +
367-
"WHERE RULESET_RUN_ID IN ( SELECT GRR.RULESET_RUN_ID FROM GOV_RULESET_RUN GRR JOIN GOV_ARTIFACT GA " +
368-
"ON GRR.ARTIFACT_KEY = GA.ARTIFACT_KEY " +
369-
"WHERE GA.ARTIFACT_REF_ID = ? AND GA.ARTIFACT_TYPE = ? AND GA.ORGANIZATION = ? AND GRR.RULESET_ID = ? )";
367+
public static final String DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_POLICY = "DELETE FROM GOV_RULE_VIOLATION " +
368+
"WHERE RULESET_RUN_ID IN ( " +
369+
" SELECT GRR.RULESET_RUN_ID " +
370+
" FROM GOV_RULESET_RUN GRR " +
371+
" JOIN GOV_POLICY_RULESET GPRS ON GRR.RULESET_ID = GPRS.RULESET_ID " +
372+
" WHERE GRR.ARTIFACT_KEY = ? " +
373+
" AND GPRS.POLICY_ID = ? " +
374+
")";
370375

371376
public static final String DELETE_RULE_VIOLATIONS_FOR_RULESET = "DELETE FROM GOV_RULE_VIOLATION " +
372377
"WHERE RULESET_ID = ?";

Diff for: components/apimgt/org.wso2.carbon.apimgt.governance.impl/src/main/java/org/wso2/carbon/apimgt/governance/impl/dao/impl/ComplianceMgtDAOImpl.java

+46-54
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@
4343
import java.util.List;
4444
import java.util.Map;
4545
import java.util.Set;
46+
import java.util.concurrent.locks.Lock;
47+
import java.util.concurrent.locks.ReentrantLock;
4648

4749
/**
4850
* This class represents the DAO class related to assessing compliance of APIs
4951
*/
5052
public class ComplianceMgtDAOImpl implements ComplianceMgtDAO {
5153

5254
private static final Log log = LogFactory.getLog(ComplianceMgtDAOImpl.class);
55+
private static final Lock resultWrtiteDelLock = new ReentrantLock();
56+
5357
private ComplianceMgtDAOImpl() {
5458

5559
}
@@ -504,23 +508,25 @@ public void addComplianceEvalResults(String artifactRefId, ArtifactType artifact
504508
Map<String, List<RuleViolation>> rulesetViolationsMap, String organization)
505509
throws APIMGovernanceException {
506510

507-
List<String> rulesetIds = new ArrayList<>(rulesetViolationsMap.keySet());
511+
resultWrtiteDelLock.lock();
508512
try (Connection connection = APIMGovernanceDBUtil.getConnection()) {
513+
String artifactKey = getArtifactKey(connection, artifactRefId, artifactType, organization);
514+
if (artifactKey == null) {
515+
throw new APIMGovernanceException(APIMGovExceptionCodes.ARTIFACT_NOT_FOUND, artifactRefId);
516+
}
509517
connection.setAutoCommit(false);
510518

511519
try {
512-
clearOldRuleViolations(connection, artifactRefId, artifactType, rulesetIds, organization);
513-
clearOldRulesetRuns(connection, artifactRefId, artifactType, rulesetIds, organization);
514-
clearOldPolicyRun(connection, artifactRefId, artifactType, policyId, organization);
515-
connection.commit();
520+
clearOldRuleViolations(connection, artifactKey, policyId);
521+
clearOldRulesetRuns(connection, artifactKey, policyId);
522+
clearOldPolicyRun(connection, artifactKey, policyId);
523+
connection.commit(); // Required to release the locks and avoid deadlocks
516524

517-
String artifactKey = getArtifactKey(connection, artifactRefId, artifactType, organization);
518525
addPolicyRun(connection, artifactKey, policyId);
519-
520526
for (Map.Entry<String, List<RuleViolation>> entry : rulesetViolationsMap.entrySet()) {
521527
String rulesetId = entry.getKey();
522528
List<RuleViolation> ruleViolations = entry.getValue();
523-
String rulesetResultId = addRulesetRuns(connection, artifactKey, rulesetId,
529+
String rulesetResultId = addRulesetRun(connection, artifactKey, rulesetId,
524530
ruleViolations.isEmpty());
525531
addRuleViolations(connection, rulesetResultId, ruleViolations);
526532
}
@@ -534,82 +540,64 @@ public void addComplianceEvalResults(String artifactRefId, ArtifactType artifact
534540
} catch (SQLException e) {
535541
throw new APIMGovernanceException(APIMGovExceptionCodes.ERROR_WHILE_SAVING_GOVERNANCE_RESULT,
536542
e, artifactRefId);
543+
} finally {
544+
resultWrtiteDelLock.unlock();
537545
}
538546
}
539547

540548
/**
541549
* Clear old policy run results for the artifact
542550
*
543-
* @param connection Connection
544-
* @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
545-
* @param artifactType Artifact Type
546-
* @param policyId Policy ID
547-
* @param organization Organization
551+
* @param connection Connection
552+
* @param artifactKey Artifact Key
553+
* @param policyId Policy ID
548554
* @throws SQLException If an error occurs while clearing the old policy result
549555
*/
550-
private void clearOldPolicyRun(Connection connection, String artifactRefId, ArtifactType artifactType,
551-
String policyId, String organization)
556+
private void clearOldPolicyRun(Connection connection, String artifactKey, String policyId)
552557
throws SQLException {
553558

554559
String sqlQuery = SQLConstants.DELETE_POLICY_RUN_FOR_ARTIFACT_AND_POLICY;
555560
try (PreparedStatement prepStmnt = connection.prepareStatement(sqlQuery)) {
556-
prepStmnt.setString(1, artifactRefId);
557-
prepStmnt.setString(2, String.valueOf(artifactType));
558-
prepStmnt.setString(3, organization);
559-
prepStmnt.setString(4, policyId);
561+
prepStmnt.setString(1, artifactKey);
562+
prepStmnt.setString(2, policyId);
560563
prepStmnt.executeUpdate();
561564
}
562565
}
563566

564567
/**
565568
* Clear old ruleset runs for the artifact
566569
*
567-
* @param connection Connection
568-
* @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
569-
* @param artifactType Artifact Type
570-
* @param rulesetIds List of Ruleset IDs
571-
* @param organization Organization
570+
* @param connection Connection
571+
* @param artifactKey Artifact Reference ID (ID of the artifact on APIM side)
572+
* @param policyId Policy ID
572573
* @throws SQLException If an error occurs while clearing the old ruleset results
573574
*/
574-
private void clearOldRulesetRuns(Connection connection, String artifactRefId, ArtifactType artifactType,
575-
List<String> rulesetIds, String organization) throws SQLException {
575+
private void clearOldRulesetRuns(Connection connection, String artifactKey, String policyId) throws SQLException {
576576

577-
String sqlQuery = SQLConstants.DELETE_RULESET_RUN_FOR_ARTIFACT_AND_RULESET;
577+
String sqlQuery = SQLConstants.DELETE_RULESET_RUN_FOR_ARTIFACT_AND_POLICY;
578578
try (PreparedStatement prepStmnt = connection.prepareStatement(sqlQuery)) {
579-
for (String rulesetId : rulesetIds) {
580-
prepStmnt.setString(1, artifactRefId);
581-
prepStmnt.setString(2, String.valueOf(artifactType));
582-
prepStmnt.setString(3, organization);
583-
prepStmnt.setString(4, rulesetId);
584-
prepStmnt.addBatch();
585-
}
586-
prepStmnt.executeBatch();
579+
prepStmnt.setString(1, artifactKey);
580+
prepStmnt.setString(2, policyId);
581+
prepStmnt.executeUpdate();
587582
}
588583
}
589584

590585
/**
591586
* Clear rule violations for the artifact and rulesets
592587
*
593-
* @param connection Connection
594-
* @param artifactRefId Artifact Reference ID (ID of the artifact on APIM side)
595-
* @param artifactType Artifact Type
596-
* @param rulesetIds List of Ruleset IDs
597-
* @param organization Organization
588+
* @param connection Connection
589+
* @param artifactKey Artifact Key
590+
* @param policyId List of Ruleset IDs
598591
* @throws SQLException If an error occurs while clearing the rule violations
599592
*/
600-
private void clearOldRuleViolations(Connection connection, String artifactRefId, ArtifactType artifactType,
601-
List<String> rulesetIds, String organization) throws SQLException {
593+
private void clearOldRuleViolations(Connection connection, String artifactKey,
594+
String policyId) throws SQLException {
602595

603-
String sqlQuery = SQLConstants.DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_RULESET;
596+
String sqlQuery = SQLConstants.DELETE_RULE_VIOLATIONS_FOR_ARTIFACT_AND_POLICY;
604597
try (PreparedStatement prepStmnt = connection.prepareStatement(sqlQuery)) {
605-
for (String rulesetId : rulesetIds) {
606-
prepStmnt.setString(1, artifactRefId);
607-
prepStmnt.setString(2, String.valueOf(artifactType));
608-
prepStmnt.setString(3, organization);
609-
prepStmnt.setString(4, rulesetId);
610-
prepStmnt.addBatch();
611-
}
612-
prepStmnt.executeBatch();
598+
prepStmnt.setString(1, artifactKey);
599+
prepStmnt.setString(2, policyId);
600+
prepStmnt.execute();
613601
}
614602
}
615603

@@ -661,7 +649,7 @@ private void addPolicyRun(Connection connection, String artifactKey, String poli
661649
}
662650

663651
/**
664-
* Add a ruleset compliance evaluation result
652+
* Add or update a ruleset compliance evaluation result
665653
*
666654
* @param connection Connection
667655
* @param artifactKey Artifact Key
@@ -670,8 +658,8 @@ private void addPolicyRun(Connection connection, String artifactKey, String poli
670658
* @return Ruleset Result ID
671659
* @throws SQLException If an error occurs while adding the ruleset compliance evaluation result
672660
*/
673-
private String addRulesetRuns(Connection connection, String artifactKey, String rulesetId,
674-
boolean isRulesetEvalSuccess) throws SQLException {
661+
private String addRulesetRun(Connection connection, String artifactKey, String rulesetId,
662+
boolean isRulesetEvalSuccess) throws SQLException {
675663

676664
String sqlQuery = SQLConstants.ADD_RULESET_RUN;
677665
try (PreparedStatement prepStmnt = connection.prepareStatement(sqlQuery)) {
@@ -1104,6 +1092,8 @@ public List<String> getPendingPoliciesForArtifact(String artifactRefId,
11041092
@Override
11051093
public void deleteArtifact(String artifactRefId, ArtifactType artifactType, String organization)
11061094
throws APIMGovernanceException {
1095+
1096+
resultWrtiteDelLock.lock();
11071097
try (Connection connection = APIMGovernanceDBUtil.getConnection()) {
11081098
connection.setAutoCommit(false);
11091099

@@ -1164,6 +1154,8 @@ public void deleteArtifact(String artifactRefId, ArtifactType artifactType, Stri
11641154
} catch (SQLException e) {
11651155
throw new APIMGovernanceException(APIMGovExceptionCodes
11661156
.ERROR_WHILE_DELETING_GOVERNANCE_DATA, e, artifactRefId);
1157+
} finally {
1158+
resultWrtiteDelLock.unlock();
11671159
}
11681160
}
11691161
}

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -5847,12 +5847,11 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM
58475847
checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(),
58485848
api.getAccessControlRoles());
58495849
// populate relevant external info environment
5850-
List<Environment> environments = null;
5850+
String environmentString = null;
58515851
if (api.getEnvironments() != null) {
5852-
environments = APIUtil.getEnvironmentsOfAPI(api);
5852+
environmentString = String.join(",", api.getEnvironments());
58535853
}
5854-
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization,
5855-
userNameWithoutChange));
5854+
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
58565855
//CORS . if null is returned, set default config from the configuration
58575856
if (api.getCorsConfiguration() == null) {
58585857
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/AbstractAPIManager.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1211,11 +1211,11 @@ protected void populateAPIInformation(String uuid, String organization, API api)
12111211
Organization org = new Organization(organization);
12121212
api.setOrganization(organization);
12131213
// environment
1214-
List<Environment> environments = null;
1214+
String environmentString = null;
12151215
if (api.getEnvironments() != null) {
1216-
environments = APIUtil.getEnvironmentsOfAPI(api);
1216+
environmentString = String.join(",", api.getEnvironments());
12171217
}
1218-
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environments, organization, username));
1218+
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
12191219
// workflow status
12201220
APIIdentifier apiId = api.getId();
12211221
WorkflowDTO workflow;

0 commit comments

Comments
 (0)