AMBARI-15804. Audit logging cleanup and tests. (Daniel Gergely via stoader)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/af13ef73 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/af13ef73 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/af13ef73 Branch: refs/heads/trunk Commit: af13ef73931bf672536199b944d4c3e26ad24ac4 Parents: 7f8da72 Author: Daniel Gergely <[email protected]> Authored: Tue Apr 12 14:59:45 2016 +0200 Committer: Toader, Sebastian <[email protected]> Committed: Tue Apr 12 14:59:45 2016 +0200 ---------------------------------------------------------------------- .../kerberos/AbstractKerberosAuditEvent.java | 26 +- .../CreatePrincipalKerberosAuditEvent.java | 3 +- .../DestroyPrincipalKerberosAuditEvent.java | 3 +- .../request/ActivateUserRequestAuditEvent.java | 2 +- .../AddComponentToHostRequestAuditEvent.java | 15 +- .../ConfigurationChangeEventCreator.java | 15 +- .../request/eventcreator/HostEventCreator.java | 32 ++- .../eventcreator/PrivilegeEventCreator.java | 3 +- .../RepositoryVersionEventCreator.java | 10 +- .../RequestAuditEventCreatorHelper.java | 37 +-- .../request/eventcreator/UserEventCreator.java | 16 +- .../kerberos/CreateKeytabFilesServerAction.java | 6 +- .../kerberos/CreatePrincipalsServerAction.java | 2 + .../kerberos/DestroyPrincipalsServerAction.java | 2 + .../kerberos/FinalizeKerberosServerAction.java | 2 + .../server/audit/TaskStatusAuditEventTest.java | 62 +++++ ...angeSecurityStateKerberosAuditEventTest.java | 61 +++++ .../CreateKeyTabKerberosAuditEventTest.java | 59 +++++ .../CreatePrincipalKerberosAuditEventTest.java | 55 ++++ .../DestroyPrincipalKerberosAuditEventTest.java | 55 ++++ .../creator/AlertGroupEventCreatorTest.java | 105 ++++++++ .../creator/AlertTargetEventCreatorTest.java | 115 +++++++++ .../creator/AuditEventCreatorTestBase.java | 84 ++++++ .../creator/AuditEventCreatorTestHelper.java | 253 +++++++++++++++++++ .../creator/BlueprintEventCreatorTest.java | 78 ++++++ .../BlueprintExportEventCreatorTest.java | 50 ++++ .../creator/ComponentEventCreatorTest.java | 229 +++++++++++++++++ .../ConfigurationChangeEventCreatorTest.java | 93 +++++++ .../creator/CredentialEventCreatorTest.java | 61 +++++ .../creator/DefaultEventCreatorTest.java | 51 ++++ .../request/creator/GroupEventCreatorTest.java | 79 ++++++ .../request/creator/HostEventCreatorTest.java | 109 ++++++++ .../request/creator/MemberEventCreatorTest.java | 103 ++++++++ .../creator/PrivilegeEventCreatorTest.java | 112 ++++++++ .../RecomendationIgnoreEventCreatorTest.java | 41 +++ .../creator/RepositoryEventCreatorTest.java | 88 +++++++ .../RepositoryVersionEventCreatorTest.java | 163 ++++++++++++ .../creator/RequestEventCreatorTest.java | 53 ++++ .../ServiceConfigDownloadEventCreatorTest.java | 58 +++++ .../creator/ServiceEventCreatorTest.java | 185 ++++++++++++++ .../creator/UnauthorizedEventCreatorTest.java | 60 +++++ .../creator/UpgradeEventCreatorTest.java | 59 +++++ .../creator/UpgradeItemEventCreatorTest.java | 60 +++++ .../request/creator/UserEventCreatorTest.java | 147 +++++++++++ .../ValidationIgnoreEventCreatorTest.java | 41 +++ .../creator/ViewInstanceEventCreatorTest.java | 110 ++++++++ .../creator/ViewPrivilegeEventCreatorTest.java | 90 +++++++ 47 files changed, 3076 insertions(+), 67 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/AbstractKerberosAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/AbstractKerberosAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/AbstractKerberosAuditEvent.java index 77c162c..3de4e48 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/AbstractKerberosAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/AbstractKerberosAuditEvent.java @@ -42,6 +42,16 @@ public class AbstractKerberosAuditEvent extends AbstractAuditEvent { protected String reasonOfFailure; /** + * ID of the related request + */ + protected Long requestId; + + /** + * ID of the related task + */ + protected Long taskId; + + /** * Builds and audit log message based on the member variables * * @param builder builder for the audit event details. @@ -60,7 +70,11 @@ public class AbstractKerberosAuditEvent extends AbstractAuditEvent { .append(reasonOfFailure); } - builder.append(")"); + builder.append("), RequestId(") + .append(requestId) + .append("), TaskId(") + .append(taskId) + .append(")"); } public TBuilder withOperation(String operation) { @@ -72,6 +86,16 @@ public class AbstractKerberosAuditEvent extends AbstractAuditEvent { this.reasonOfFailure = reasonOfFailure; return (TBuilder) this; } + + public TBuilder withRequestId(Long requestId) { + this.requestId = requestId; + return (TBuilder) this; + } + + public TBuilder withTaskId(Long taskId) { + this.taskId = taskId; + return (TBuilder) this; + } } protected AbstractKerberosAuditEvent() { http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/CreatePrincipalKerberosAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/CreatePrincipalKerberosAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/CreatePrincipalKerberosAuditEvent.java index e3fbbe4..51c3c6b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/CreatePrincipalKerberosAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/CreatePrincipalKerberosAuditEvent.java @@ -35,7 +35,8 @@ public class CreatePrincipalKerberosAuditEvent extends AbstractKerberosAuditEven protected void buildAuditMessage(StringBuilder builder) { super.buildAuditMessage(builder); builder.append(", Principal(") - .append(principal); + .append(principal) + .append(")"); } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/DestroyPrincipalKerberosAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/DestroyPrincipalKerberosAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/DestroyPrincipalKerberosAuditEvent.java index ba56f21..1e3dd36 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/DestroyPrincipalKerberosAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/kerberos/DestroyPrincipalKerberosAuditEvent.java @@ -42,7 +42,8 @@ public class DestroyPrincipalKerberosAuditEvent extends AbstractKerberosAuditEve super.buildAuditMessage(builder); builder.append(", Principal(") - .append(principal); + .append(principal) + .append(")"); } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/ActivateUserRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/ActivateUserRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/ActivateUserRequestAuditEvent.java index df5726d..c947fb1 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/ActivateUserRequestAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/ActivateUserRequestAuditEvent.java @@ -41,7 +41,7 @@ public class ActivateUserRequestAuditEvent extends RequestAuditEvent { private String username; public ActivateUserRequestAuditEventBuilder() { - super.withOperation("Set user activate"); + super.withOperation("Set user active/inactive"); } @Override http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddComponentToHostRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddComponentToHostRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddComponentToHostRequestAuditEvent.java index 2c9eedd..c248665 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddComponentToHostRequestAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/event/request/AddComponentToHostRequestAuditEvent.java @@ -18,12 +18,15 @@ package org.apache.ambari.server.audit.event.request; +import java.util.Set; + import javax.annotation.concurrent.Immutable; import org.apache.ambari.server.audit.request.RequestAuditEvent; +import org.apache.commons.lang.StringUtils; /** - * Audit event for adding component to a host + * Audit event for adding components to a host */ @Immutable public class AddComponentToHostRequestAuditEvent extends RequestAuditEvent { @@ -38,7 +41,7 @@ public class AddComponentToHostRequestAuditEvent extends RequestAuditEvent { /** * Component name */ - private String component; + private Set<String> components; public AddComponentToHostRequestAuditEventBuilder() { super.withOperation("Component addition to host"); @@ -60,8 +63,8 @@ public class AddComponentToHostRequestAuditEvent extends RequestAuditEvent { builder.append(", Hostname(") .append(hostName) - .append(", Component(") - .append(component) + .append("), Component(") + .append(components == null ? "" : StringUtils.join(components, ", ")) .append(")"); } @@ -70,8 +73,8 @@ public class AddComponentToHostRequestAuditEvent extends RequestAuditEvent { return this; } - public AddComponentToHostRequestAuditEventBuilder withComponent(String component) { - this.component = component; + public AddComponentToHostRequestAuditEventBuilder withComponents(Set<String> component) { + this.components = component; return this; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ConfigurationChangeEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ConfigurationChangeEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ConfigurationChangeEventCreator.java index 139f6f6..6589658 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ConfigurationChangeEventCreator.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ConfigurationChangeEventCreator.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.ambari.server.api.services.Request; import org.apache.ambari.server.api.services.Result; import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.api.util.TreeNode; import org.apache.ambari.server.audit.event.AuditEvent; import org.apache.ambari.server.audit.event.request.ClusterNameChangeRequestAuditEvent; import org.apache.ambari.server.audit.event.request.ConfigurationChangeRequestAuditEvent; @@ -32,6 +33,7 @@ import org.apache.ambari.server.controller.internal.ServiceConfigVersionResource import org.apache.ambari.server.controller.spi.Resource; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; /** * This creator handles operation requests (start, stop, install, etc) @@ -81,8 +83,8 @@ public class ConfigurationChangeEventCreator implements RequestAuditEventCreator public AuditEvent createAuditEvent(Request request, Result result) { if (!request.getBody().getPropertySets().isEmpty()) { - Map<String, Object> map = request.getBody().getPropertySets().iterator().next(); - if (map.size() == 1 && map.containsKey(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID)) { + Map<String, Object> map = Iterables.getFirst(request.getBody().getPropertySets(), null); + if (map != null && map.size() == 1 && map.containsKey(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID)) { String newName = String.valueOf(map.get(ClusterResourceProvider.CLUSTER_NAME_PROPERTY_ID)); String oldName = request.getResource().getKeyValueMap().get(Resource.Type.Cluster); return ClusterNameChangeRequestAuditEvent.builder() @@ -134,10 +136,11 @@ public class ConfigurationChangeEventCreator implements RequestAuditEventCreator * @return */ private Map<String, Object> getServiceConfigMap(Result result) { - if (result.getResultTree().getChild("resources") != null && - !result.getResultTree().getChild("resources").getChildren().isEmpty() && - result.getResultTree().getChild("resources").getChildren().iterator().next().getObject() != null) { - return result.getResultTree().getChild("resources").getChildren().iterator().next().getObject().getPropertiesMap().get(""); + if (result.getResultTree().getChild("resources") != null) { + TreeNode<Resource> first = Iterables.getFirst(result.getResultTree().getChild("resources").getChildren(), null); + if(first != null && first.getObject() != null) { + return first.getObject().getPropertiesMap().get(""); + } } return null; } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java index 7c27d19..60fc77c 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java @@ -18,9 +18,13 @@ package org.apache.ambari.server.audit.request.eventcreator; +import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.ambari.server.api.services.NamedPropertySet; import org.apache.ambari.server.api.services.Request; import org.apache.ambari.server.api.services.Result; import org.apache.ambari.server.api.services.ResultStatus; @@ -33,6 +37,7 @@ import org.apache.ambari.server.controller.internal.HostResourceProvider; import org.apache.ambari.server.controller.spi.Resource; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; /** * This creator handles host requests (add, delete, add component) @@ -109,7 +114,7 @@ public class HostEventCreator implements RequestAuditEventCreator { .withUrl(request.getURI()) .withRemoteIp(request.getRemoteAddress()) .withHostName(getHostNameFromQuery(request)) - .withComponent(getHostComponent(request)) + .withComponents(getHostComponents(request)) .build(); default: return null; @@ -121,14 +126,18 @@ public class HostEventCreator implements RequestAuditEventCreator { * @param request * @return */ - private String getHostComponent(Request request) { - if (!request.getBody().getNamedPropertySets().isEmpty()) { - Set<Map<String, String>> set = (Set<Map<String, String>>) request.getBody().getNamedPropertySets().iterator().next().getProperties().get("host_components"); + private Set<String> getHostComponents(Request request) { + Set<String> components = new HashSet<>(); + NamedPropertySet propertySet = Iterables.getFirst(request.getBody().getNamedPropertySets(), null); + if (propertySet != null && propertySet.getProperties().get("host_components") instanceof Set) { + Set<Map<String, String>> set = (Set<Map<String, String>>) propertySet.getProperties().get("host_components"); if (set != null && !set.isEmpty()) { - return set.iterator().next().get(HostComponentResourceProvider.HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID); + for(Map<String, String> element : set) { + components.add(element.get(HostComponentResourceProvider.HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID)); + } } } - return null; + return components; } /** @@ -138,11 +147,12 @@ public class HostEventCreator implements RequestAuditEventCreator { */ private String getHostNameFromQuery(Request request) { final String key = HostResourceProvider.HOST_NAME_PROPERTY_ID; - if (request.getBody().getQueryString().contains(key)) { - String q = request.getBody().getQueryString(); - int startIndex = q.indexOf(key) + key.length() + 1; - int endIndex = q.indexOf("&", startIndex) == -1 ? q.length() : q.indexOf("&", startIndex); - return q.substring(startIndex, endIndex); + if (request.getURI().contains(key)) { + Pattern pattern = Pattern.compile(".*" + key + "\\s*=\\s*([^&\\s]+).*"); + Matcher matcher = pattern.matcher(request.getURI()); + if(matcher.find()) { + return matcher.group(1); + } } return null; } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/PrivilegeEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/PrivilegeEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/PrivilegeEventCreator.java index cd14dd1..5c476c6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/PrivilegeEventCreator.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/PrivilegeEventCreator.java @@ -37,6 +37,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; /** * This creator handles privilege requests @@ -100,7 +101,7 @@ public class PrivilegeEventCreator implements RequestAuditEventCreator { .withGroups(groups) .build(); case POST: - String role = users.isEmpty() ? (groups.isEmpty() ? null : groups.keySet().iterator().next()) : users.keySet().iterator().next(); + String role = users.isEmpty() ? Iterables.getFirst(groups.keySet(), null) : Iterables.getFirst(users.keySet(), null); return PrivilegeChangeRequestAuditEvent.builder() .withTimestamp(System.currentTimeMillis()) .withRequestType(request.getRequestType()) http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RepositoryVersionEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RepositoryVersionEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RepositoryVersionEventCreator.java index 30fd377..2f09b74 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RepositoryVersionEventCreator.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RepositoryVersionEventCreator.java @@ -37,6 +37,7 @@ import org.apache.ambari.server.controller.internal.RepositoryVersionResourcePro import org.apache.ambari.server.controller.spi.Resource; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; /** * This creator handles privilege requests @@ -138,12 +139,11 @@ public class RepositoryVersionEventCreator implements RequestAuditEventCreator { Map<String, List<Map<String, String>>> result = new HashMap<String, List<Map<String, String>>>(); - if (!request.getBody().getPropertySets().isEmpty()) { - if (request.getBody().getPropertySets().iterator().next().get("operating_systems") instanceof Set) { - Set<Object> set = (Set<Object>) request.getBody().getPropertySets().iterator().next().get("operating_systems"); + Map<String, Object> first = Iterables.getFirst(request.getBody().getPropertySets(), null); - result = createResultForOperationSystems(set); - } + if (first != null && first.get("operating_systems") instanceof Set) { + Set<Object> set = (Set<Object>) first.get("operating_systems"); + result = createResultForOperationSystems(set); } return result; } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestAuditEventCreatorHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestAuditEventCreatorHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestAuditEventCreatorHelper.java index a02428f..f7a6e36 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestAuditEventCreatorHelper.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestAuditEventCreatorHelper.java @@ -23,8 +23,11 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.apache.ambari.server.api.services.NamedPropertySet; import org.apache.ambari.server.api.services.Request; +import com.google.common.collect.Iterables; + /** * The purpose of this class is to retrieve information from {@link Request} objects. * This information can be a single value or a list of values. @@ -38,8 +41,9 @@ public class RequestAuditEventCreatorHelper { * @return */ public static String getNamedProperty(Request request, String propertyName) { - if (isValid(request, propertyName)) { - return String.valueOf(request.getBody().getNamedPropertySets().iterator().next().getProperties().get(propertyName)); + NamedPropertySet first = Iterables.getFirst(request.getBody().getNamedPropertySets(), null); + if (first != null && first.getProperties().get(propertyName) instanceof String) { + return String.valueOf(first.getProperties().get(propertyName)); } return null; } @@ -51,8 +55,9 @@ public class RequestAuditEventCreatorHelper { * @return */ public static List<String> getNamedPropertyList(Request request, String propertyName) { - if (isValidList(request, propertyName)) { - List<String> list = (List<String>) request.getBody().getNamedPropertySets().iterator().next().getProperties().get(propertyName); + NamedPropertySet first = Iterables.getFirst(request.getBody().getNamedPropertySets(), null); + if (first != null && first.getProperties().get(propertyName) instanceof List) { + List<String> list = (List<String>) first.getProperties().get(propertyName); if (list != null) { return list; } @@ -61,30 +66,6 @@ public class RequestAuditEventCreatorHelper { } /** - * Checks if the property is valid: can be found and has correct type - * @param request - * @param propertyName - * @return - */ - private static boolean isValid(Request request, String propertyName) { - return !request.getBody().getNamedPropertySets().isEmpty() && - request.getBody().getNamedPropertySets().iterator().next().getProperties() != null && - request.getBody().getNamedPropertySets().iterator().next().getProperties().get(propertyName) instanceof String; - } - - /** - * Checks if the property is a valid list: can be found and has correct type - * @param request - * @param propertyName - * @return - */ - private static boolean isValidList(Request request, String propertyName) { - return !request.getBody().getNamedPropertySets().isEmpty() && - request.getBody().getNamedPropertySets().iterator().next().getProperties() != null && - request.getBody().getNamedPropertySets().iterator().next().getProperties().get(propertyName) instanceof List; - } - - /** * Returns a property from a request * @param request * @param propertyName http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UserEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UserEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UserEventCreator.java index 54d02a7..bc469ac 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UserEventCreator.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UserEventCreator.java @@ -18,6 +18,7 @@ package org.apache.ambari.server.audit.request.eventcreator; +import java.util.Map; import java.util.Set; import org.apache.ambari.server.api.services.Request; @@ -33,6 +34,7 @@ import org.apache.ambari.server.controller.internal.UserResourceProvider; import org.apache.ambari.server.controller.spi.Resource; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; /** * This creator handles user requests @@ -166,7 +168,8 @@ public class UserEventCreator implements RequestAuditEventCreator { * @return */ private boolean hasAdmin(Request request) { - return !request.getBody().getPropertySets().isEmpty() && request.getBody().getPropertySets().iterator().next().containsKey(UserResourceProvider.USER_ADMIN_PROPERTY_ID); + Map<String, Object> first = Iterables.getFirst(request.getBody().getPropertySets(), null); + return first != null && first.containsKey(UserResourceProvider.USER_ADMIN_PROPERTY_ID); } /** @@ -175,7 +178,8 @@ public class UserEventCreator implements RequestAuditEventCreator { * @return */ private boolean hasActive(Request request) { - return !request.getBody().getPropertySets().isEmpty() && request.getBody().getPropertySets().iterator().next().containsKey(UserResourceProvider.USER_ACTIVE_PROPERTY_ID); + Map<String, Object> first = Iterables.getFirst(request.getBody().getPropertySets(), null); + return first != null && first.containsKey(UserResourceProvider.USER_ACTIVE_PROPERTY_ID); } /** @@ -184,7 +188,8 @@ public class UserEventCreator implements RequestAuditEventCreator { * @return */ private boolean hasOldPassword(Request request) { - return !request.getBody().getPropertySets().isEmpty() && request.getBody().getPropertySets().iterator().next().containsKey(UserResourceProvider.USER_OLD_PASSWORD_PROPERTY_ID); + Map<String, Object> first = Iterables.getFirst(request.getBody().getPropertySets(), null); + return first != null && first.containsKey(UserResourceProvider.USER_OLD_PASSWORD_PROPERTY_ID); } /** @@ -193,8 +198,9 @@ public class UserEventCreator implements RequestAuditEventCreator { * @return */ private String getUsername(Request request) { - if (!request.getBody().getPropertySets().isEmpty()) { - return String.valueOf(request.getBody().getPropertySets().iterator().next().get(UserResourceProvider.USER_USERNAME_PROPERTY_ID)); + Map<String, Object> first = Iterables.getFirst(request.getBody().getPropertySets(), null); + if (first != null) { + return String.valueOf(first.get(UserResourceProvider.USER_USERNAME_PROPERTY_ID)); } return null; } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreateKeytabFilesServerAction.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreateKeytabFilesServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreateKeytabFilesServerAction.java index 8c728db..732b06d 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreateKeytabFilesServerAction.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreateKeytabFilesServerAction.java @@ -151,7 +151,11 @@ public class CreateKeytabFilesServerAction extends KerberosServerAction { throws AmbariException { - CreateKeyTabKerberosAuditEvent.CreateKeyTabKerberosAuditEventBuilder auditEventBuilder = CreateKeyTabKerberosAuditEvent.builder().withTimestamp(System.currentTimeMillis()); + CreateKeyTabKerberosAuditEvent.CreateKeyTabKerberosAuditEventBuilder auditEventBuilder = CreateKeyTabKerberosAuditEvent.builder(); + auditEventBuilder.withTimestamp(System.currentTimeMillis()); + auditEventBuilder.withRequestId(getHostRoleCommand().getRequestId()); + auditEventBuilder.withTaskId(getHostRoleCommand().getTaskId()); + CommandReport commandReport = null; String message = null; try { http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreatePrincipalsServerAction.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreatePrincipalsServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreatePrincipalsServerAction.java index 03a181e..61cc5df 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreatePrincipalsServerAction.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/CreatePrincipalsServerAction.java @@ -176,6 +176,8 @@ public class CreatePrincipalsServerAction extends KerberosServerAction { ActionLog actionLog) { CreatePrincipalKerberosAuditEvent.CreatePrincipalKerberosAuditEventBuilder auditEventBuilder = CreatePrincipalKerberosAuditEvent.builder() .withTimestamp(System.currentTimeMillis()) + .withRequestId(getHostRoleCommand().getRequestId()) + .withTaskId(getHostRoleCommand().getTaskId()) .withPrincipal(principal); CreatePrincipalResult result = null; String message = null; http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java index 95dd7a3..2bdcd81 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/DestroyPrincipalsServerAction.java @@ -93,6 +93,8 @@ public class DestroyPrincipalsServerAction extends KerberosServerAction { actionLog.writeStdOut(message); DestroyPrincipalKerberosAuditEvent.DestroyPrincipalKerberosAuditEventBuilder auditEventBuilder = DestroyPrincipalKerberosAuditEvent.builder() .withTimestamp(System.currentTimeMillis()) + .withRequestId(getHostRoleCommand().getRequestId()) + .withTaskId(getHostRoleCommand().getTaskId()) .withPrincipal(evaluatedPrincipal); try { http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/FinalizeKerberosServerAction.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/FinalizeKerberosServerAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/FinalizeKerberosServerAction.java index 922cadb..7205d09 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/FinalizeKerberosServerAction.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/kerberos/FinalizeKerberosServerAction.java @@ -98,6 +98,8 @@ public class FinalizeKerberosServerAction extends KerberosServerAction { .withComponent(sch.getServiceComponentName()) .withHostName(sch.getHostName()) .withState(sch.getDesiredSecurityState().toString()) + .withRequestId(getHostRoleCommand().getRequestId()) + .withTaskId(getHostRoleCommand().getTaskId()) .build(); auditLog(auditEvent); } http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/TaskStatusAuditEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/TaskStatusAuditEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/TaskStatusAuditEventTest.java new file mode 100644 index 0000000..8f769c7 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/TaskStatusAuditEventTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit; + +import org.apache.ambari.server.actionmanager.HostRoleStatus; +import org.apache.ambari.server.audit.event.TaskStatusAuditEvent; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class TaskStatusAuditEventTest { + + @Test + public void testAuditMessage() throws Exception { + // Given + String testUserName = "USER1"; + String testOperation = "START MYCOMPONENT"; + String testRequestDetails = "Start MyComponent"; + String testHostName = "ambari.example.com"; + HostRoleStatus testStatus = HostRoleStatus.IN_PROGRESS; + Long testRequestId = 100L; + Long testTaskId = 99L; + + TaskStatusAuditEvent event = TaskStatusAuditEvent.builder() + .withTimestamp(System.currentTimeMillis()) + .withUserName(testUserName) + .withOperation(testOperation) + .withRequestId(testRequestId.toString()) + .withDetails(testRequestDetails) + .withHostName(testHostName) + .withStatus(testStatus.name()) + .withTaskId(testTaskId.toString()) + .build(); + + // When + String actualAuditMessage = event.getAuditMessage(); + + // Then + String expectedAuditMessage = String.format("User(%s), Operation(%s), Details(%s), Status(%s), RequestId(%d), TaskId(%d), Hostname(%s)", testUserName, testOperation, testRequestDetails, testStatus, testRequestId, testTaskId, testHostName); + + assertThat(actualAuditMessage, equalTo(expectedAuditMessage)); + + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/ChangeSecurityStateKerberosAuditEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/ChangeSecurityStateKerberosAuditEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/ChangeSecurityStateKerberosAuditEventTest.java new file mode 100644 index 0000000..c835536 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/ChangeSecurityStateKerberosAuditEventTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.kerberos; + +import org.apache.ambari.server.audit.event.kerberos.ChangeSecurityStateKerberosAuditEvent; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class ChangeSecurityStateKerberosAuditEventTest { + + @Test + public void testAuditMessage() throws Exception { + // Given + String testOperation = "Change security state"; + String testHostName = "testhost.example.com"; + String testService = "MyService"; + String testComponent = "MyComponent"; + String testState = "MyState"; + Long testRequestId = 100L; + Long testTaskId = 99L; + + ChangeSecurityStateKerberosAuditEvent event = ChangeSecurityStateKerberosAuditEvent.builder() + .withTimestamp(System.currentTimeMillis()) + .withOperation(testOperation) + .withRequestId(testRequestId) + .withTaskId(testTaskId) + .withHostName(testHostName) + .withComponent(testComponent) + .withService(testService) + .withState(testState) + .build(); + + // When + String actualAuditMessage = event.getAuditMessage(); + + // Then + String expectedAuditMessage = String.format("Operation(%s), Status(Success), RequestId(%s), TaskId(%s), Hostname(%s), Service(%s), Component(%s), State(%s)", testOperation, testRequestId, testTaskId, testHostName, testService, testComponent, testState); + + assertThat(actualAuditMessage, equalTo(expectedAuditMessage)); + + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreateKeyTabKerberosAuditEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreateKeyTabKerberosAuditEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreateKeyTabKerberosAuditEventTest.java new file mode 100644 index 0000000..dbc2583 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreateKeyTabKerberosAuditEventTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.kerberos; + +import org.apache.ambari.server.audit.event.kerberos.CreateKeyTabKerberosAuditEvent; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class CreateKeyTabKerberosAuditEventTest { + + @Test + public void testAuditMessage() throws Exception { + // Given + String testOperation = "Create keytab file"; + String testPrincipal = "testPrincipal"; + String testHostName = "testhost.example.com"; + String testKeyTabFile = "/tmp/mykeytabfile.ktf"; + Long testRequestId = 100L; + Long testTaskId = 99L; + + CreateKeyTabKerberosAuditEvent event = CreateKeyTabKerberosAuditEvent.builder() + .withTimestamp(System.currentTimeMillis()) + .withOperation(testOperation) + .withRequestId(testRequestId) + .withPrincipal(testPrincipal) + .withTaskId(testTaskId) + .withHostName(testHostName) + .withKeyTabFilePath(testKeyTabFile) + .build(); + + // When + String actualAuditMessage = event.getAuditMessage(); + + // Then + String expectedAuditMessage = String.format("Operation(%s), Status(Success), RequestId(%s), TaskId(%s), Principal(%s), Hostname(%s), Keytab file(%s)", testOperation, testRequestId, testTaskId, testPrincipal, testHostName, testKeyTabFile); + + assertThat(actualAuditMessage, equalTo(expectedAuditMessage)); + + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreatePrincipalKerberosAuditEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreatePrincipalKerberosAuditEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreatePrincipalKerberosAuditEventTest.java new file mode 100644 index 0000000..c3eb96f --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/CreatePrincipalKerberosAuditEventTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.kerberos; + +import org.apache.ambari.server.audit.event.kerberos.CreatePrincipalKerberosAuditEvent; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class CreatePrincipalKerberosAuditEventTest { + + @Test + public void testAuditMessage() throws Exception { + // Given + String testOperation = "Create principal"; + String testPrincipal = "testPrincipal"; + Long testRequestId = 100L; + Long testTaskId = 99L; + + CreatePrincipalKerberosAuditEvent event = CreatePrincipalKerberosAuditEvent.builder() + .withTimestamp(System.currentTimeMillis()) + .withOperation(testOperation) + .withRequestId(testRequestId) + .withPrincipal(testPrincipal) + .withTaskId(testTaskId) + .build(); + + // When + String actualAuditMessage = event.getAuditMessage(); + + // Then + String expectedAuditMessage = String.format("Operation(%s), Status(Success), RequestId(%d), TaskId(%d), Principal(%s)", testOperation, testRequestId, testTaskId, testPrincipal); + + assertThat(actualAuditMessage, equalTo(expectedAuditMessage)); + + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/DestroyPrincipalKerberosAuditEventTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/DestroyPrincipalKerberosAuditEventTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/DestroyPrincipalKerberosAuditEventTest.java new file mode 100644 index 0000000..901f362 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/kerberos/DestroyPrincipalKerberosAuditEventTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.kerberos; + +import org.apache.ambari.server.audit.event.kerberos.DestroyPrincipalKerberosAuditEvent; +import org.junit.Test; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class DestroyPrincipalKerberosAuditEventTest { + + @Test + public void testAuditMessage() throws Exception { + // Given + String testOperation = "Destroy principal"; + String testPrincipal = "testPrincipal"; + Long testRequestId = 100L; + Long testTaskId = 99L; + + DestroyPrincipalKerberosAuditEvent event = DestroyPrincipalKerberosAuditEvent.builder() + .withTimestamp(System.currentTimeMillis()) + .withOperation(testOperation) + .withRequestId(testRequestId) + .withPrincipal(testPrincipal) + .withTaskId(testTaskId) + .build(); + + // When + String actualAuditMessage = event.getAuditMessage(); + + // Then + String expectedAuditMessage = String.format("Operation(%s), Status(Success), RequestId(%d), TaskId(%d), Principal(%s)", testOperation, testRequestId, testTaskId, testPrincipal); + + assertThat(actualAuditMessage, equalTo(expectedAuditMessage)); + + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertGroupEventCreatorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertGroupEventCreatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertGroupEventCreatorTest.java new file mode 100644 index 0000000..1e04677 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertGroupEventCreatorTest.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import junit.framework.Assert; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.audit.event.AuditEvent; +import org.apache.ambari.server.audit.event.request.AddAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.event.request.ChangeAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.event.request.DeleteAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.request.eventcreator.AlertGroupEventCreator; +import org.apache.ambari.server.controller.internal.AlertGroupResourceProvider; +import org.apache.ambari.server.controller.spi.Resource; +import org.junit.Test; + +public class AlertGroupEventCreatorTest extends AuditEventCreatorTestBase{ + + @Test + public void postTest() { + AlertGroupEventCreator creator = new AlertGroupEventCreator(); + + Map<String,Object> properties = new HashMap<>(); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_NAME, "GroupName"); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_DEFINITIONS, Arrays.asList("D","E","F","S")); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_TARGETS, Arrays.asList("T","G","T","S")); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.POST, Resource.Type.AlertGroup, properties, null); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Alert group addition), RequestType(POST), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Alert group name(GroupName), Definition IDs(D, E, F, S), Notification IDs(T, G, T, S)"; + + Assert.assertTrue("Class mismatch", event instanceof AddAlertGroupRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + + @Test + public void putTest() { + AlertGroupEventCreator creator = new AlertGroupEventCreator(); + + Map<String,Object> properties = new HashMap<>(); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_NAME, "GroupName"); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_DEFINITIONS, Arrays.asList("D","E","F","S")); + properties.put(AlertGroupResourceProvider.ALERT_GROUP_TARGETS, Arrays.asList("T","G","T","S")); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.PUT, Resource.Type.AlertGroup, properties, null); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Alert group change), RequestType(PUT), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Alert group name(GroupName), Definition IDs(D, E, F, S), Notification IDs(T, G, T, S)"; + + Assert.assertTrue("Class mismatch", event instanceof ChangeAlertGroupRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + + @Test + public void deleteTest() { + AlertGroupEventCreator creator = new AlertGroupEventCreator(); + + Map<Resource.Type,String> resource = new HashMap<>(); + resource.put(Resource.Type.AlertGroup, "999"); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.DELETE, Resource.Type.AlertGroup, null, resource); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Alert group removal), RequestType(DELETE), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Alert group ID(999)"; + + Assert.assertTrue("Class mismatch", event instanceof DeleteAlertGroupRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertTargetEventCreatorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertTargetEventCreatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertTargetEventCreatorTest.java new file mode 100644 index 0000000..019044c --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AlertTargetEventCreatorTest.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import junit.framework.Assert; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.audit.event.AuditEvent; +import org.apache.ambari.server.audit.event.request.AddAlertTargetRequestAuditEvent; +import org.apache.ambari.server.audit.event.request.ChangeAlertTargetRequestAuditEvent; +import org.apache.ambari.server.audit.event.request.DeleteAlertTargetRequestAuditEvent; +import org.apache.ambari.server.audit.request.eventcreator.AlertTargetEventCreator; +import org.apache.ambari.server.controller.internal.AlertTargetResourceProvider; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.notifications.dispatchers.EmailDispatcher; +import org.apache.ambari.server.state.services.AlertNoticeDispatchService; +import org.junit.Test; + +public class AlertTargetEventCreatorTest extends AuditEventCreatorTestBase { + + @Test + public void postTest() { + AlertTargetEventCreator creator = new AlertTargetEventCreator(); + + Map<String,Object> properties = new HashMap<>(); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_DESCRIPTION, "Target description"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_NAME, "Target name"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_NOTIFICATION_TYPE, "NotifType"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES + "/" + EmailDispatcher.JAVAMAIL_FROM_PROPERTY, "email"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_STATES, Arrays.asList("S","T","A","T","E","S")); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_GROUPS, Arrays.asList("G","R","P","S")); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES + "/" + AlertNoticeDispatchService.AMBARI_DISPATCH_RECIPIENTS, Arrays.asList("[email protected]","[email protected]","[email protected]")); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.POST, Resource.Type.AlertTarget, properties, null); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Notification addition), RequestType(POST), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Notification name(Target name), Description(Target description), Notification type(NotifType), Group IDs(G, R, P, S), Email from(email), Email to([email protected], [email protected], [email protected]), Alert states(S, T, A, T, E, S)"; + + Assert.assertTrue("Class mismatch", event instanceof AddAlertTargetRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + + @Test + public void putTest() { + AlertTargetEventCreator creator = new AlertTargetEventCreator(); + + Map<String,Object> properties = new HashMap<>(); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_DESCRIPTION, "Target description"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_NAME, "Target name"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_NOTIFICATION_TYPE, "NotifType"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES + "/" + EmailDispatcher.JAVAMAIL_FROM_PROPERTY, "email"); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_STATES, Arrays.asList("S","T","A","T","E","S")); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_GROUPS, Arrays.asList("G","R","P","S")); + properties.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES + "/" + AlertNoticeDispatchService.AMBARI_DISPATCH_RECIPIENTS, Arrays.asList("[email protected]","[email protected]","[email protected]")); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.PUT, Resource.Type.AlertTarget, properties, null); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Notification change), RequestType(PUT), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Notification name(Target name), Description(Target description), Notification type(NotifType), Group IDs(G, R, P, S), Email from(email), Email to([email protected], [email protected], [email protected]), Alert states(S, T, A, T, E, S)"; + + Assert.assertTrue("Class mismatch", event instanceof ChangeAlertTargetRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + + @Test + public void deleteTest() { + AlertTargetEventCreator creator = new AlertTargetEventCreator(); + + Map<Resource.Type,String> resource = new HashMap<>(); + resource.put(Resource.Type.AlertTarget, "888"); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.DELETE, Resource.Type.AlertTarget, null, resource); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Notification removal), RequestType(DELETE), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Notification ID(888)"; + + Assert.assertTrue("Class mismatch", event instanceof DeleteAlertTargetRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestBase.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestBase.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestBase.java new file mode 100644 index 0000000..2fdbe40 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestBase.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import java.util.Collection; +import java.util.Collections; + +import org.junit.BeforeClass; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; + +public class AuditEventCreatorTestBase { + + protected final static String userName = "testuser"; + + @BeforeClass + public static void beforeClass() { + SecurityContextHolder.setContext(new SecurityContext() { + @Override + public Authentication getAuthentication() { + return new Authentication() { + @Override + public Collection<? extends GrantedAuthority> getAuthorities() { + return null; + } + + @Override + public Object getCredentials() { + return null; + } + + @Override + public Object getDetails() { + return null; + } + + @Override + public Object getPrincipal() { + return new User(userName, "password", Collections.EMPTY_LIST); + } + + @Override + public boolean isAuthenticated() { + return true; + } + + @Override + public void setAuthenticated(boolean b) throws IllegalArgumentException { + + } + + @Override + public String getName() { + return ((User) getPrincipal()).getUsername(); + } + }; + } + + @Override + public void setAuthentication(Authentication authentication) { + + } + }); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestHelper.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestHelper.java new file mode 100644 index 0000000..86c1ebb --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/AuditEventCreatorTestHelper.java @@ -0,0 +1,253 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.ambari.server.api.query.Query; +import org.apache.ambari.server.api.query.render.Renderer; +import org.apache.ambari.server.api.resources.ResourceDefinition; +import org.apache.ambari.server.api.resources.ResourceInstance; +import org.apache.ambari.server.api.resources.SubResourceDefinition; +import org.apache.ambari.server.api.services.NamedPropertySet; +import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.RequestBody; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.api.util.TreeNode; +import org.apache.ambari.server.audit.AuditLogger; +import org.apache.ambari.server.audit.event.AuditEvent; +import org.apache.ambari.server.audit.request.RequestAuditLogger; +import org.apache.ambari.server.audit.request.RequestAuditLoggerImpl; +import org.apache.ambari.server.audit.request.eventcreator.RequestAuditEventCreator; +import org.apache.ambari.server.controller.spi.PageRequest; +import org.apache.ambari.server.controller.spi.Predicate; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.spi.SortRequest; +import org.apache.ambari.server.controller.spi.TemporalInfo; +import org.easymock.Capture; +import org.easymock.EasyMock; + +public class AuditEventCreatorTestHelper { + + public static AuditEvent getEvent(RequestAuditEventCreator eventCreator, Request request, Result result) { + Set<RequestAuditEventCreator> creatorSet = new HashSet<>(); + creatorSet.add(eventCreator); + + AuditLogger auditLogger = EasyMock.createNiceMock(AuditLogger.class); + EasyMock.expect(auditLogger.isEnabled()).andReturn(true).anyTimes(); + Capture<AuditEvent> capture = EasyMock.newCapture(); + auditLogger.log(EasyMock.capture(capture)); + EasyMock.expectLastCall(); + EasyMock.replay(auditLogger); + + RequestAuditLogger requestAuditLogger = new RequestAuditLoggerImpl(auditLogger, creatorSet); + + requestAuditLogger.log(request, result); + + return capture.getValue(); + } + + public static Request createRequest(final Request.Type requestType, final Resource.Type resourceType, final Map<String,Object> properties, final Map<Resource.Type, String> resource) { + return createRequest(requestType, resourceType, properties, resource, ""); + } + + public static Request createRequest(final Request.Type requestType, final Resource.Type resourceType, final Map<String,Object> properties, final Map<Resource.Type, String> resource, final String queryString) { + return new Request() { + + RequestBody body = new RequestBody(); + + @Override + public Result process() { + return null; + } + + @Override + public ResourceInstance getResource() { + return new ResourceInstance() { + @Override + public void setKeyValueMap(Map<Resource.Type, String> keyValueMap) { + + } + + @Override + public Map<Resource.Type, String> getKeyValueMap() { + return resource; + } + + @Override + public Query getQuery() { + return null; + } + + @Override + public ResourceDefinition getResourceDefinition() { + return new ResourceDefinition() { + @Override + public String getPluralName() { + return null; + } + + @Override + public String getSingularName() { + return null; + } + + @Override + public Resource.Type getType() { + return resourceType; + } + + @Override + public Set<SubResourceDefinition> getSubResourceDefinitions() { + return null; + } + + @Override + public List<PostProcessor> getPostProcessors() { + return null; + } + + @Override + public Renderer getRenderer(String name) throws IllegalArgumentException { + return null; + } + + @Override + public Collection<String> getCreateDirectives() { + return null; + } + + @Override + public boolean isCreatable() { + return false; + } + + @Override + public Collection<String> getUpdateDirectives() { + return null; + } + }; + } + + @Override + public Map<String, ResourceInstance> getSubResources() { + return null; + } + + @Override + public boolean isCollectionResource() { + return false; + } + }; + } + + @Override + public String getURI() { + return "http://example.com:8080/api/v1/test" + queryString; + } + + @Override + public Type getRequestType() { + return requestType; + } + + @Override + public int getAPIVersion() { + return 0; + } + + @Override + public Predicate getQueryPredicate() { + return null; + } + + @Override + public Map<String, TemporalInfo> getFields() { + return null; + } + + @Override + public RequestBody getBody() { + if(properties != null) { + NamedPropertySet nps = new NamedPropertySet("", properties); + body.addPropertySet(nps); + } + return body; + } + + @Override + public Map<String, List<String>> getHttpHeaders() { + return null; + } + + @Override + public PageRequest getPageRequest() { + return null; + } + + @Override + public SortRequest getSortRequest() { + return null; + } + + @Override + public Renderer getRenderer() { + return null; + } + + @Override + public String getRemoteAddress() { + return "1.2.3.4"; + } + }; + } + + public static Result createResult(final ResultStatus status) { + return createResult(status, null); + } + + public static Result createResult(final ResultStatus status, final TreeNode<Resource> resultTree) { + return new Result() { + @Override + public TreeNode<Resource> getResultTree() { + return resultTree; + } + + @Override + public boolean isSynchronous() { + return false; + } + + @Override + public ResultStatus getStatus() { + return status; + } + + @Override + public void setResultStatus(ResultStatus status) { + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintEventCreatorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintEventCreatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintEventCreatorTest.java new file mode 100644 index 0000000..705b468 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintEventCreatorTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import junit.framework.Assert; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.audit.event.AuditEvent; +import org.apache.ambari.server.audit.event.request.AddBlueprintRequestAuditEvent; +import org.apache.ambari.server.audit.event.request.DeleteBlueprintRequestAuditEvent; +import org.apache.ambari.server.audit.request.eventcreator.BlueprintEventCreator; +import org.apache.ambari.server.controller.spi.Resource; +import org.junit.Test; + +public class BlueprintEventCreatorTest extends AuditEventCreatorTestBase { + + @Test + public void postTest() { + BlueprintEventCreator creator = new BlueprintEventCreator(); + + Map<Resource.Type,String> resource = new HashMap<>(); + resource.put(Resource.Type.Blueprint, "myBluePrint"); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.POST, Resource.Type.Blueprint, null, resource); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Upload blueprint), RequestType(POST), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Blueprint name(myBluePrint)"; + + Assert.assertTrue("Class mismatch", event instanceof AddBlueprintRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + + @Test + public void deleteTest() { + BlueprintEventCreator creator = new BlueprintEventCreator(); + + Map<Resource.Type,String> resource = new HashMap<>(); + resource.put(Resource.Type.Blueprint, "myBluePrint"); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.DELETE, Resource.Type.Blueprint, null, resource); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Delete blueprint), RequestType(DELETE), url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), Blueprint name(myBluePrint)"; + + Assert.assertTrue("Class mismatch", event instanceof DeleteBlueprintRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/af13ef73/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintExportEventCreatorTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintExportEventCreatorTest.java b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintExportEventCreatorTest.java new file mode 100644 index 0000000..f826b5a --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/BlueprintExportEventCreatorTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.server.audit.request.creator; + +import junit.framework.Assert; + +import org.apache.ambari.server.api.services.Request; +import org.apache.ambari.server.api.services.Result; +import org.apache.ambari.server.api.services.ResultStatus; +import org.apache.ambari.server.audit.event.AuditEvent; +import org.apache.ambari.server.audit.event.request.BlueprintExportRequestAuditEvent; +import org.apache.ambari.server.audit.request.eventcreator.BlueprintExportEventCreator; +import org.apache.ambari.server.controller.spi.Resource; +import org.junit.Test; + +public class BlueprintExportEventCreatorTest extends AuditEventCreatorTestBase { + + @Test + public void getTest() { + BlueprintExportEventCreator creator = new BlueprintExportEventCreator(); + + Request request = AuditEventCreatorTestHelper.createRequest(Request.Type.GET, Resource.Type.Cluster, null, null, "?format=blueprint"); + Result result = AuditEventCreatorTestHelper.createResult(new ResultStatus(ResultStatus.STATUS.OK)); + + AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, result); + + String actual = event.getAuditMessage(); + String expected = "User(" + userName + "), RemoteIp(1.2.3.4), Operation(Blueprint export), RequestType(GET), url(http://example.com:8080/api/v1/test?format=blueprint), ResultStatus(200 OK)"; + + Assert.assertTrue("Class mismatch", event instanceof BlueprintExportRequestAuditEvent); + Assert.assertEquals(expected, actual); + Assert.assertTrue(actual.contains(userName)); + } +}
