Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audit logs for API documents #12732

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,8 @@ public static class AuditLogConstants {
public static final String APPLICATION = "Application";
public static final String SUBSCRIPTION = "Subscription";
public static final String KEY_MANAGER = "KeyManager/IdP";
public static final String DOCUMENT = "Document";
public static final String DOCUMENT_CONTENT = "DocumentContent";

public static final String NAME = "name";
public static final String SCOPE = "scope";
Expand All @@ -2200,6 +2202,9 @@ public static class AuditLogConstants {
public static final String PROVIDER = "provider";
public static final String OWNER = "owner";
public static final String TIER = "tier";
public static final String API_ID = "apiId";
public static final String DOCUMENT_ID = "documentId";
public static final String TYPE = "type";
public static final String REQUESTED_TIER = "requested_tier";
public static final String CALLBACK = "callbackURL";
public static final String GROUPS = "groups";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,11 @@ public String retrieveServiceKeyByApiId(int apiId, int tenantId) throws APIManag
public void removeDocumentation(String apiId, String docId, String organization) throws APIManagementException {
try {
apiPersistenceInstance.deleteDocumentation(new Organization(organization), apiId, docId);
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.DOCUMENT_ID, docId);
apiLogObject.put(APIConstants.AuditLogConstants.API_ID, apiId);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.DOCUMENT, apiLogObject.toString(),
APIConstants.AuditLogConstants.DELETED, this.username);
} catch (DocumentationPersistenceException e) {
throw new APIManagementException("Error while deleting the document " + docId);
}
Expand All @@ -2351,6 +2356,13 @@ public Documentation updateDocumentation(String apiId, Documentation documentati
org.wso2.carbon.apimgt.persistence.dto.Documentation updatedDoc = apiPersistenceInstance
.updateDocumentation(new Organization(organization), apiId, mappedDoc);
if (updatedDoc != null) {
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, documentation.getName());
apiLogObject.put(APIConstants.AuditLogConstants.TYPE, documentation.getType());
apiLogObject.put(APIConstants.AuditLogConstants.DOCUMENT_ID, documentation.getId());
apiLogObject.put(APIConstants.AuditLogConstants.API_ID, apiId);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.DOCUMENT, apiLogObject.toString(),
APIConstants.AuditLogConstants.UPDATED, this.username);
return DocumentMapper.INSTANCE.toDocumentation(updatedDoc);
}
} catch (DocumentationPersistenceException e) {
Expand All @@ -2370,6 +2382,13 @@ public Documentation addDocumentation(String uuid, Documentation documentation,
org.wso2.carbon.apimgt.persistence.dto.Documentation addedDoc = apiPersistenceInstance.addDocumentation(
new Organization(organization), uuid, mappedDoc);
if (addedDoc != null) {
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, addedDoc.getName());
apiLogObject.put(APIConstants.AuditLogConstants.TYPE, addedDoc.getType());
apiLogObject.put(APIConstants.AuditLogConstants.DOCUMENT_ID, addedDoc.getId());
apiLogObject.put(APIConstants.AuditLogConstants.API_ID, uuid);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.DOCUMENT, apiLogObject.toString(),
APIConstants.AuditLogConstants.CREATED, this.username);
return DocumentMapper.INSTANCE.toDocumentation(addedDoc);
}
} catch (DocumentationPersistenceException e) {
Expand Down Expand Up @@ -5662,6 +5681,11 @@ public void addDocumentationContent(String uuid, String docId, String organizati
mappedContent = DocumentMapper.INSTANCE.toDocumentContent(content);
DocumentContent doc = apiPersistenceInstance.addDocumentationContent(new Organization(organization), uuid, docId,
mappedContent);
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.DOCUMENT_ID, docId);
apiLogObject.put(APIConstants.AuditLogConstants.API_ID, uuid);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.DOCUMENT_CONTENT, apiLogObject.toString(),
APIConstants.AuditLogConstants.UPDATED, this.username);
} catch (DocumentationPersistenceException e) {
throw new APIManagementException("Error while adding content to doc " + docId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7045,7 +7045,7 @@ public static DocumentBuilderFactory getSecuredDocumentBuilder() {
public static void logAuditMessage(String entityType, String entityInfo, String action, String performedBy) {

JSONObject jsonObject = new JSONObject();
jsonObject.put("typ", entityType);
jsonObject.put("type", entityType);
jsonObject.put("action", action);
jsonObject.put("performedBy", performedBy);
if (entityInfo != null && !StringUtils.isBlank(entityInfo)) {
Expand Down
Loading