Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Commit fa25f8a

Browse files
author
matt-rich
authored
Merge pull request #415 from CJSCommonPlatform/access-control-component
Access control component
2 parents 1d5b3e9 + ebb7f05 commit fa25f8a

File tree

9 files changed

+61
-25
lines changed

9 files changed

+61
-25
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
55

66
## [Unreleased]
77

8+
### Changed
9+
- Improved logging for annotation scanning
10+
- Add component to audit interceptor so that audit messages can distinguish between components even
11+
when they are deployed in a single WAR.
12+
813
## [2.0.0-rc5] - 2017-06-20
914

1015
### Changed

core/src/main/java/uk/gov/justice/services/core/audit/DefaultAuditService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ public void initialise() {
4040
* Orchestrates the auditing of the action, uses a blacklist regex pattern to skip auditing if
4141
* required.
4242
*
43-
* @param envelope - the envelope to be audited.
43+
* @param envelope the envelope to be audited
44+
* @param component the component that requested the action to be audited
4445
*/
45-
public void audit(final JsonEnvelope envelope) {
46+
@Override
47+
public void audit(final JsonEnvelope envelope, final String component) {
4648

4749
final String actionName = envelope.metadata().name();
4850

4951
if (auditBlacklistPattern.matcher(actionName).matches()) {
5052
logger.info(format("Skipping auditing of action %s due to configured blacklist pattern %s.", actionName, auditBlacklist));
5153
} else {
52-
auditClient.auditEntry(envelope);
54+
auditClient.auditEntry(envelope, component);
5355
}
5456
}
5557
}

core/src/main/java/uk/gov/justice/services/core/audit/LocalAuditInterceptor.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
*/
1515
public class LocalAuditInterceptor implements Interceptor {
1616

17+
private static final String UNKNOWN_COMPONENT = "UNKNOWN";
18+
1719
@Inject
1820
AuditService auditService;
1921

2022
@Override
2123
public InterceptorContext process(final InterceptorContext interceptorContext, final InterceptorChain interceptorChain) {
22-
recordAudit(interceptorContext.inputEnvelope());
24+
final String component = (String) interceptorContext.getInputParameter("component").orElse(UNKNOWN_COMPONENT);
25+
recordAudit(interceptorContext.inputEnvelope(), component);
2326

2427
final InterceptorContext outputContext = interceptorChain.processNext(interceptorContext);
2528
final Optional<JsonEnvelope> jsonEnvelope = outputContext.outputEnvelope();
2629

27-
jsonEnvelope.ifPresent(this::recordAudit);
30+
jsonEnvelope.ifPresent(envelope -> recordAudit(envelope, component));
2831
return outputContext;
2932
}
3033

31-
private void recordAudit(final JsonEnvelope jsonEnvelope) {
32-
auditService.audit(jsonEnvelope);
34+
private void recordAudit(final JsonEnvelope jsonEnvelope, final String component) {
35+
auditService.audit(jsonEnvelope, component);
3336
}
34-
}
37+
}

core/src/main/java/uk/gov/justice/services/core/audit/SimpleAuditClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public class SimpleAuditClient implements AuditClient {
2424
ServiceContextNameProvider serviceContextNameProvider;
2525

2626
@Override
27-
public void auditEntry(final JsonEnvelope envelope) {
28-
logger.info(createAuditMessageFrom(envelope));
27+
public void auditEntry(final JsonEnvelope envelope, final String component) {
28+
logger.info(createAuditMessageFrom(envelope, component));
2929
}
3030

31-
private String createAuditMessageFrom(final JsonEnvelope envelope) {
31+
private String createAuditMessageFrom(final JsonEnvelope envelope, final String component) {
3232

3333
return new JSONObject()
3434
.put("serviceContext", serviceContextNameProvider.getServiceContextName())
35+
.put("component", component)
3536
.put("envelope", new JSONObject(envelope.toString()))
3637
.toString(2);
3738
}

core/src/test/java/uk/gov/justice/services/core/audit/DefaultAuditServiceTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public class DefaultAuditServiceTest {
2121

2222
private static final String ACTION_NAME = "test.action";
23+
private static final String COMPONENT = "test-component";
2324

2425
@Mock
2526
private AuditClient auditClient;
@@ -45,29 +46,29 @@ public void setup() {
4546
@Test
4647
public void shouldAuditWithDefaultEmptyBlacklist() throws Exception {
4748
initialisePattern("");
48-
auditService.audit(jsonEnvelope);
49+
auditService.audit(jsonEnvelope, COMPONENT);
4950

50-
verify(auditClient, times(1)).auditEntry(jsonEnvelope);
51+
verify(auditClient, times(1)).auditEntry(jsonEnvelope, COMPONENT);
5152
}
5253

5354
@Test
5455
public void shouldAuditNonBlacklistedAction() throws Exception {
5556
initialisePattern(".*\\.action");
5657
when(metadata.name()).thenReturn("some-action");
5758

58-
auditService.audit(jsonEnvelope);
59+
auditService.audit(jsonEnvelope, COMPONENT);
5960

60-
verify(auditClient, times(1)).auditEntry(jsonEnvelope);
61+
verify(auditClient, times(1)).auditEntry(jsonEnvelope, COMPONENT);
6162
}
6263

6364
@Test
6465
public void shouldNotAuditBlacklistedAction() {
6566
initialisePattern(".*\\.action");
6667

67-
auditService.audit(jsonEnvelope);
68+
auditService.audit(jsonEnvelope, COMPONENT);
6869

6970
verify(logger, times(1)).info("Skipping auditing of action test.action due to configured blacklist pattern .*\\.action.");
70-
verify(auditClient, never()).auditEntry(jsonEnvelope);
71+
verify(auditClient, never()).auditEntry(jsonEnvelope, COMPONENT);
7172
}
7273

7374
private void initialisePattern(final String pattern) {

core/src/test/java/uk/gov/justice/services/core/audit/LocalAuditInterceptorTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
@RunWith(MockitoJUnitRunner.class)
2929
public class LocalAuditInterceptorTest {
3030

31-
private static final int AUDIT_PRIORITY = 2000;
31+
private static final String COMPONENT = "test-component";
32+
private static final String UNKNOWN_COMPONENT = "UNKNOWN";
3233

3334
@Mock
3435
private JsonEnvelope inputEnvelope;
@@ -57,11 +58,22 @@ public void setup() throws Exception {
5758
@Test
5859
public void shouldApplyAccessControlToInputIfLocalComponent() throws Exception {
5960
final InterceptorContext inputContext = interceptorContextWithInput(inputEnvelope);
61+
inputContext.setInputParameter("component", COMPONENT);
6062

6163
interceptorChain.processNext(inputContext);
6264

63-
verify(auditService).audit(inputEnvelope);
64-
verify(auditService).audit(outputEnvelope);
65+
verify(auditService).audit(inputEnvelope, COMPONENT);
66+
verify(auditService).audit(outputEnvelope, COMPONENT);
67+
}
68+
69+
@Test
70+
public void shouldUseUnknownComponentIfComponentNotSet() throws Exception {
71+
final InterceptorContext inputContext = interceptorContextWithInput(inputEnvelope);
72+
73+
interceptorChain.processNext(inputContext);
74+
75+
verify(auditService).audit(inputEnvelope, UNKNOWN_COMPONENT);
76+
verify(auditService).audit(outputEnvelope, UNKNOWN_COMPONENT);
6577
}
6678

6779
@Adapter(COMMAND_API)
@@ -82,4 +94,4 @@ public void dummyMethod() {
8294

8395
}
8496
}
85-
}
97+
}

core/src/test/java/uk/gov/justice/services/core/audit/SimpleAuditClientTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
@RunWith(MockitoJUnitRunner.class)
2222
public class SimpleAuditClientTest {
2323

24+
private static final String COMPONENT = "test-component";
25+
2426
@Mock
2527
Logger logger;
2628

@@ -45,7 +47,7 @@ public void shouldPrependTheAppNameToTheEnvelopeJsonAndLog() throws Exception {
4547
when(envelope.toString()).thenReturn(envelopeJson);
4648
when(serviceContextNameProvider.getServiceContextName()).thenReturn(serviceContextName);
4749

48-
simpleAuditClient.auditEntry(envelope);
50+
simpleAuditClient.auditEntry(envelope, COMPONENT);
4951

5052
final ArgumentCaptor<String> argumentCaptor = forClass(String.class);
5153

@@ -55,6 +57,7 @@ public void shouldPrependTheAppNameToTheEnvelopeJsonAndLog() throws Exception {
5557

5658
with(json)
5759
.assertEquals("serviceContext", serviceContextName)
60+
.assertEquals("component", COMPONENT)
5861
.assertEquals("envelope.propertyName", propertyValue);
5962
}
6063
}

framework-api/framework-api-core/src/main/java/uk/gov/justice/services/core/audit/AuditClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
import uk.gov.justice.services.messaging.JsonEnvelope;
44

5+
/**
6+
* Interface for audit clients.
7+
*/
58
public interface AuditClient {
69

7-
void auditEntry(final JsonEnvelope envelope);
10+
/**
11+
* Record an audit entry.
12+
* @param envelope the message to audit
13+
* @param component the component that generated the audit entry
14+
*/
15+
void auditEntry(final JsonEnvelope envelope, final String component);
816
}

framework-api/framework-api-core/src/main/java/uk/gov/justice/services/core/audit/AuditService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public interface AuditService {
1010
/**
1111
* Orchestrates the auditing of the action.
1212
*
13-
* @param envelope - the envelope to be audited.
13+
* @param envelope the envelope to be audited
14+
* @param component the name of the component that the action came into
1415
*/
15-
void audit(final JsonEnvelope envelope);
16+
void audit(final JsonEnvelope envelope, final String component);
1617
}

0 commit comments

Comments
 (0)