Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -21,6 +21,7 @@
import static io.gravitee.repository.management.model.ApiKey.AuditEvent.APIKEY_RENEWED;
import static io.gravitee.repository.management.model.Audit.AuditProperties.API;
import static io.gravitee.repository.management.model.Audit.AuditProperties.API_KEY;
import static io.gravitee.repository.management.model.Audit.AuditProperties.API_PRODUCT;
import static io.gravitee.repository.management.model.Audit.AuditProperties.APPLICATION;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
Expand Down Expand Up @@ -707,21 +708,30 @@ private void createAuditLog(
key
.getSubscriptions()
.forEach(subscription -> {
boolean isApiProduct = isApiProductSubscription(subscription);
String referenceId = isApiProduct ? subscription.getReferenceId() : subscription.getApi();
String applicationId = key.getApplication() != null ? key.getApplication().getId() : null;

Map<Audit.AuditProperties, String> properties = new LinkedHashMap<>();
properties.put(API_KEY, key.getKey());
properties.put(API, subscription.getApi());
properties.put(APPLICATION, key.getApplication().getId());
auditService.createApiAuditLog(
executionContext,
AuditService.AuditLogData.builder()
.properties(properties)
.event(event)
.createdAt(eventDate)
.oldValue(previousApiKey)
.newValue(key)
.build(),
subscription.getApi()
);
properties.put(isApiProduct ? API_PRODUCT : API, referenceId);
properties.put(APPLICATION, applicationId);

AuditService.AuditLogData auditLogData = AuditService.AuditLogData.builder()
.properties(properties)
.event(event)
.createdAt(eventDate)
.oldValue(previousApiKey)
.newValue(key)
.build();

if (isApiProduct) {
auditService.createApiProductAuditLog(executionContext, auditLogData, referenceId);
} else if (referenceId != null) {
auditService.createApiAuditLog(executionContext, auditLogData, referenceId);
} else {
auditService.createApplicationAuditLog(executionContext, auditLogData, applicationId);
}
});
}

Expand Down Expand Up @@ -774,7 +784,6 @@ private void triggerNotifierService(
}

private boolean isApiProductSubscription(SubscriptionEntity subscription) {
var referenceType = subscription.getReferenceType();
return referenceType != null && SubscriptionReferenceType.API_PRODUCT.name().equals(referenceType);
return SubscriptionReferenceType.API_PRODUCT.name().equals(subscription.getReferenceType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public void shouldGenerate() throws TechnicalException {
// Prepare subscription
when(subscription.getId()).thenReturn(SUBSCRIPTION_ID);
when(subscription.getApplication()).thenReturn(APPLICATION_ID);
when(subscription.getApi()).thenReturn(API_ID);
when(subscription.getEndingAt()).thenReturn(Date.from(new Date().toInstant().plus(1, ChronoUnit.DAYS)));
when(subscriptionService.findByIdIn(List.of(SUBSCRIPTION_ID))).thenReturn(Set.of(subscription));
// Stub API Key creation
Expand Down Expand Up @@ -219,6 +220,7 @@ public void shouldGenerateWithCustomApiKey() throws TechnicalException {
when(subscription.getId()).thenReturn(SUBSCRIPTION_ID);
when(subscription.getEndingAt()).thenReturn(Date.from(new Date().toInstant().plus(1, ChronoUnit.DAYS)));
when(subscription.getApplication()).thenReturn(APPLICATION_ID);
when(subscription.getApi()).thenReturn(API_ID);
when(subscriptionService.findByIdIn(List.of(SUBSCRIPTION_ID))).thenReturn(Set.of(subscription));
// Stub API Key creation
when(apiKeyRepository.create(any())).thenAnswer(returnsFirstArg());
Expand Down Expand Up @@ -737,6 +739,7 @@ public void shouldUpdateExpired() throws TechnicalException {

SubscriptionEntity subscription = new SubscriptionEntity();
subscription.setId("subscription-id");
subscription.setApi(API_ID);

ApiKeyEntity apiKeyEntity = new ApiKeyEntity();
apiKeyEntity.setId("api-key-id");
Expand Down Expand Up @@ -769,6 +772,42 @@ public void shouldUpdateExpired() throws TechnicalException {
);
}

@Test
public void shouldAuditApiProductWhenUpdatingExpirationForApiProductSubscription() throws TechnicalException {
final String productId = "api-product-id";

ApiKey existingApiKey = new ApiKey();
existingApiKey.setApplication(APPLICATION_ID);
when(apiKeyRepository.findById("api-key-id")).thenReturn(Optional.of(existingApiKey));

SubscriptionEntity subscription = new SubscriptionEntity();
subscription.setId("subscription-id");
subscription.setReferenceType(SubscriptionReferenceType.API_PRODUCT.name());
subscription.setReferenceId(productId);

ApiKeyEntity apiKeyEntity = new ApiKeyEntity();
apiKeyEntity.setId("api-key-id");
apiKeyEntity.setApplication(application);
apiKeyEntity.setKey("ABC");
apiKeyEntity.setPaused(true);
apiKeyEntity.setSubscriptions(Set.of(subscription));
apiKeyEntity.setExpireAt(new Date());

when(subscriptionService.findById(any())).thenReturn(subscription);
when(applicationService.findById(eq(GraviteeContext.getExecutionContext()), anyString())).thenReturn(application);
when(subscriptionService.findByIdIn(any())).thenReturn(Set.of(subscription));

apiKeyService.update(GraviteeContext.getExecutionContext(), apiKeyEntity);

verify(apiKeyRepository, times(1)).update(existingApiKey);
assertFalse("isRevoked", existingApiKey.isRevoked());
assertTrue("isPaused", existingApiKey.isPaused());

verify(notifierService, never()).trigger(eq(GraviteeContext.getExecutionContext()), eq(ApiHook.APIKEY_EXPIRED), any(), any());
verify(auditService, times(1)).createApiProductAuditLog(eq(GraviteeContext.getExecutionContext()), any(), eq(productId));
verify(auditService, never()).createApiAuditLog(any(), any(), any());
}

@Test
public void shouldFindBySubscription() throws TechnicalException {
String subscriptionId = "my-subscription";
Expand Down