-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Audit now logs message even if mismatch of parameter count. (#886)
* Audit now logs message even if mismatch of parameter count. Fixed exception audit. Signed-off-by: Tomas Langer <tomas.langer@oracle.com> * Unit test Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
- Loading branch information
1 parent
747e8f5
commit 0b44057
Showing
5 changed files
with
114 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
security/security/src/test/java/io/helidon/security/DefaultAuditProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.security; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.helidon.config.Config; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.helidon.common.CollectionsHelper.listOf; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.startsWith; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* Unit test for {@link DefaultAuditProvider}. | ||
*/ | ||
class DefaultAuditProviderTest { | ||
@Test | ||
void testMessageFormatting() { | ||
DefaultAuditProvider provider = DefaultAuditProvider.create(Config.empty()); | ||
AuditEvent event = createEvent("Unit tests: first: \"%s\"; second: \"%s\"", | ||
listOf(AuditEvent.AuditParam.plain("first", "data"), | ||
AuditEvent.AuditParam.sensitive("second", "secret"))); | ||
|
||
String message = provider.formatMessage(event); | ||
|
||
assertThat(message, is("Unit tests: first: \"data\"; second: \"(sensitive)\"")); | ||
} | ||
|
||
@Test | ||
void testMessageFormattingTooManyParams() { | ||
DefaultAuditProvider provider = DefaultAuditProvider.create(Config.empty()); | ||
AuditEvent event = createEvent("Unit tests: first: \"%s\"; second: \"%s\"", | ||
listOf(AuditEvent.AuditParam.plain("first", "data"), | ||
AuditEvent.AuditParam.sensitive("second", "secret"), | ||
AuditEvent.AuditParam.plain("third", "thirdData"))); | ||
|
||
String message = provider.formatMessage(event); | ||
|
||
assertThat(message, is("Unit tests: first: \"data\"; second: \"(sensitive)\"")); | ||
} | ||
|
||
@Test | ||
void testMessageFormattingNotEnoughParams() { | ||
DefaultAuditProvider provider = DefaultAuditProvider.create(Config.empty()); | ||
AuditEvent event = createEvent("Unit tests: first: \"%s\"; second: \"%s\"", | ||
listOf(AuditEvent.AuditParam.plain("first", "data"))); | ||
|
||
String message = provider.formatMessage(event); | ||
|
||
assertThat(message, startsWith("Formatting failed for format: Unit tests: first: \"%s\"; second: \"%s\", parameters: ")); | ||
} | ||
|
||
private AuditEvent createEvent(String messageFormat, List<AuditEvent.AuditParam> params) { | ||
return new AuditEvent() { | ||
@Override | ||
public String eventType() { | ||
return "unit-test"; | ||
} | ||
|
||
@Override | ||
public Optional<Throwable> throwable() { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public List<AuditParam> params() { | ||
return params; | ||
} | ||
|
||
@Override | ||
public String messageFormat() { | ||
return messageFormat; | ||
} | ||
|
||
@Override | ||
public AuditSeverity severity() { | ||
return null; | ||
} | ||
}; | ||
} | ||
} |