Alert target (=notification) audit event creator
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/0024cfa8 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/0024cfa8 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/0024cfa8 Branch: refs/heads/audit_logging Commit: 0024cfa853ce416eedef3e365862a11c2b78f4c5 Parents: 85f26fa Author: Daniel Gergely <[email protected]> Authored: Mon Feb 22 15:22:39 2016 +0100 Committer: Toader, Sebastian <[email protected]> Committed: Thu Mar 24 13:06:46 2016 +0100 ---------------------------------------------------------------------- .../event/AddAlertTargetRequestAuditEvent.java | 134 +++++++++++++++++ .../ChangeAlertTargetRequestAuditEvent.java | 134 +++++++++++++++++ .../DeleteAlertTargetRequestAuditEvent.java | 75 ++++++++++ .../eventcreator/AlertTargetEventCreator.java | 150 +++++++++++++++++++ .../server/controller/ControllerModule.java | 2 + 5 files changed, 495 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/0024cfa8/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertTargetRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertTargetRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertTargetRequestAuditEvent.java new file mode 100644 index 0000000..60958e3 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertTargetRequestAuditEvent.java @@ -0,0 +1,134 @@ +/* + * 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.event; + +import java.util.List; + +import org.apache.ambari.server.audit.request.RequestAuditEvent; +import org.apache.commons.lang.StringUtils; + +public class AddAlertTargetRequestAuditEvent extends RequestAuditEvent { + + public static class AddAlertTargetRequestAuditEventBuilder extends RequestAuditEventBuilder<AddAlertTargetRequestAuditEvent, AddAlertTargetRequestAuditEventBuilder> { + + private String name; + private String description; + private String notificationType; + private List<String> groupIds; + private String emailFrom; + private List<String> emailRecipients; + private List<String> alertStates; + + public AddAlertTargetRequestAuditEventBuilder() { + super.withOperation("Notification addition"); + } + + @Override + protected AddAlertTargetRequestAuditEvent newAuditEvent() { + return new AddAlertTargetRequestAuditEvent(this); + } + + /** + * Appends to the event the details of the incoming request. + * + * @param builder builder for the audit event details. + */ + @Override + protected void buildAuditMessage(StringBuilder builder) { + super.buildAuditMessage(builder); + + builder.append(", Notification name(") + .append(name) + .append("), Description(") + .append(description) + .append("), Notification type(") + .append(notificationType) + .append("), Group IDs(") + .append(StringUtils.join(groupIds, ", ")); + + if (emailFrom != null) { + builder.append("), Email from(") + .append(emailFrom); + } + + if(emailRecipients != null && !emailRecipients.isEmpty()) { + builder.append("), Email to(") + .append(StringUtils.join(emailRecipients, ", ")); + } + builder.append("), Alert states(") + .append(StringUtils.join(alertStates, ", ")) + .append(")"); + } + + public AddAlertTargetRequestAuditEventBuilder withName(String name) { + this.name = name; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withDescription(String description) { + this.description = description; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withNotificationType(String notificationType) { + this.notificationType = notificationType; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withGroupIds(List<String> groupIds) { + this.groupIds = groupIds; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withEmailFrom(String emailFrom) { + this.emailFrom = emailFrom; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withEmailRecipients(List<String> emailRecipients) { + this.emailRecipients = emailRecipients; + return this; + } + + public AddAlertTargetRequestAuditEventBuilder withAlertStates(List<String> alertStates) { + this.alertStates = alertStates; + return this; + } + } + + protected AddAlertTargetRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected AddAlertTargetRequestAuditEvent(AddAlertTargetRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link AddAlertTargetRequestAuditEvent} + * + * @return a builder instance + */ + public static AddAlertTargetRequestAuditEventBuilder builder() { + return new AddAlertTargetRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/0024cfa8/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertTargetRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertTargetRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertTargetRequestAuditEvent.java new file mode 100644 index 0000000..e8f2c84 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertTargetRequestAuditEvent.java @@ -0,0 +1,134 @@ +/* + * 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.event; + +import java.util.List; + +import org.apache.ambari.server.audit.request.RequestAuditEvent; +import org.apache.commons.lang.StringUtils; + +public class ChangeAlertTargetRequestAuditEvent extends RequestAuditEvent { + + public static class ChangeAlertTargetRequestAuditEventBuilder extends RequestAuditEventBuilder<ChangeAlertTargetRequestAuditEvent, ChangeAlertTargetRequestAuditEventBuilder> { + + private String name; + private String description; + private String notificationType; + private List<String> groupIds; + private String emailFrom; + private List<String> emailRecipients; + private List<String> alertStates; + + public ChangeAlertTargetRequestAuditEventBuilder() { + super.withOperation("Notification change"); + } + + @Override + protected ChangeAlertTargetRequestAuditEvent newAuditEvent() { + return new ChangeAlertTargetRequestAuditEvent(this); + } + + /** + * Appends to the event the details of the incoming request. + * + * @param builder builder for the audit event details. + */ + @Override + protected void buildAuditMessage(StringBuilder builder) { + super.buildAuditMessage(builder); + + builder.append(", Notification name(") + .append(name) + .append("), Description(") + .append(description) + .append("), Notification type(") + .append(notificationType) + .append("), Group IDs(") + .append(StringUtils.join(groupIds, ", ")); + + if (emailFrom != null) { + builder.append("), Email from(") + .append(emailFrom); + } + + if(emailRecipients != null && !emailRecipients.isEmpty()) { + builder.append("), Email to(") + .append(StringUtils.join(emailRecipients, ", ")); + } + builder.append("), Alert states(") + .append(StringUtils.join(alertStates, ", ")) + .append(")"); + } + + public ChangeAlertTargetRequestAuditEventBuilder withName(String name) { + this.name = name; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withDescription(String description) { + this.description = description; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withNotificationType(String notificationType) { + this.notificationType = notificationType; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withGroupIds(List<String> groupIds) { + this.groupIds = groupIds; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withEmailFrom(String emailFrom) { + this.emailFrom = emailFrom; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withEmailRecipients(List<String> emailRecipients) { + this.emailRecipients = emailRecipients; + return this; + } + + public ChangeAlertTargetRequestAuditEventBuilder withAlertStates(List<String> alertStates) { + this.alertStates = alertStates; + return this; + } + } + + protected ChangeAlertTargetRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected ChangeAlertTargetRequestAuditEvent(ChangeAlertTargetRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link ChangeAlertTargetRequestAuditEvent} + * + * @return a builder instance + */ + public static ChangeAlertTargetRequestAuditEventBuilder builder() { + return new ChangeAlertTargetRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/0024cfa8/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertTargetRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertTargetRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertTargetRequestAuditEvent.java new file mode 100644 index 0000000..9dae221 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertTargetRequestAuditEvent.java @@ -0,0 +1,75 @@ +/* + * 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.event; + +import org.apache.ambari.server.audit.request.RequestAuditEvent; + +public class DeleteAlertTargetRequestAuditEvent extends RequestAuditEvent { + + public static class DeleteAlertTargetRequestAuditEventBuilder extends RequestAuditEventBuilder<DeleteAlertTargetRequestAuditEvent, DeleteAlertTargetRequestAuditEventBuilder> { + + private String id; + + public DeleteAlertTargetRequestAuditEventBuilder() { + super.withOperation("Notification removal"); + } + + @Override + protected DeleteAlertTargetRequestAuditEvent newAuditEvent() { + return new DeleteAlertTargetRequestAuditEvent(this); + } + + /** + * Appends to the event the details of the incoming request. + * @param builder builder for the audit event details. + */ + @Override + protected void buildAuditMessage(StringBuilder builder) { + super.buildAuditMessage(builder); + + builder.append(", Notification ID(") + .append(id) + .append(")"); + } + + public DeleteAlertTargetRequestAuditEventBuilder withId(String id) { + this.id = id; + return this; + } + } + + protected DeleteAlertTargetRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected DeleteAlertTargetRequestAuditEvent(DeleteAlertTargetRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link DeleteAlertTargetRequestAuditEvent} + * @return a builder instance + */ + public static DeleteAlertTargetRequestAuditEventBuilder builder() { + return new DeleteAlertTargetRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/0024cfa8/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertTargetEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertTargetEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertTargetEventCreator.java new file mode 100644 index 0000000..aa462cc --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertTargetEventCreator.java @@ -0,0 +1,150 @@ +/* + * 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.eventcreator; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +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.audit.AuditEvent; +import org.apache.ambari.server.audit.request.RequestAuditEventCreator; +import org.apache.ambari.server.audit.request.event.AddAlertTargetRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.ChangeAlertTargetRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.DeleteAlertTargetRequestAuditEvent; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.joda.time.DateTime; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.User; + +/** + * This creator handles alert target requests + * For resource type {@link Resource.Type#AlertTarget} + * and request types {@link Request.Type#POST}, {@link Request.Type#PUT} and {@link Request.Type#DELETE} + */ +public class AlertTargetEventCreator implements RequestAuditEventCreator { + + /** + * Set of {@link Request.Type}s that are handled by this plugin + */ + private Set<Request.Type> requestTypes = new HashSet<Request.Type>(); + + { + requestTypes.add(Request.Type.PUT); + requestTypes.add(Request.Type.POST); + requestTypes.add(Request.Type.DELETE); + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Request.Type> getRequestTypes() { + return requestTypes; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Resource.Type> getResourceTypes() { + return Collections.singleton(Resource.Type.AlertTarget); + } + + /** + * {@inheritDoc} + */ + @Override + public Set<ResultStatus.STATUS> getResultStatuses() { + return null; + } + + @Override + public AuditEvent createAuditEvent(Request request, Result result) { + String username = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(); + + switch(request.getRequestType()) { + case POST: + return AddAlertTargetRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withName(getProperty(request, "name")) + .withDescription(getProperty(request, "description")) + .withAlertStates(getPropertyList(request, "alert_states")) + .withGroupIds(getPropertyList(request, "groups")) + .withNotificationType(getProperty(request, "notification_type")) + .withEmailFrom(getProperty(request, "properties/mail.smtp.from")) + .withEmailRecipients(getPropertyList(request, "properties/ambari.dispatch.recipients")) + .build(); + case PUT: + return ChangeAlertTargetRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withName(getProperty(request, "name")) + .withDescription(getProperty(request, "description")) + .withAlertStates(getPropertyList(request, "alert_states")) + .withGroupIds(getPropertyList(request, "groups")) + .withNotificationType(getProperty(request, "notification_type")) + .withEmailFrom(getProperty(request, "properties/mail.smtp.from")) + .withEmailRecipients(getPropertyList(request, "properties/ambari.dispatch.recipients")) + .build(); + case DELETE: + return DeleteAlertTargetRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withId(request.getResource().getKeyValueMap().get(Resource.Type.AlertTarget)) + .build(); + default: + return null; + } + } + + private List<String> getPropertyList(Request request, String propertyName) { + if(!request.getBody().getNamedPropertySets().isEmpty()) { + List<String> list = (List<String>) request.getBody().getNamedPropertySets().iterator().next().getProperties().get(PropertyHelper.getPropertyId("AlertTarget",propertyName)); + if (list != null) { + return list; + } + } + return Collections.emptyList(); + } + + private String getProperty(Request request, String propertyName) { + if(!request.getBody().getPropertySets().isEmpty()) { + return String.valueOf(request.getBody().getPropertySets().iterator().next().get(PropertyHelper.getPropertyId("AlertTarget",propertyName))); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/0024cfa8/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java index f47ffec..2ea8c3a 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java @@ -45,6 +45,7 @@ import org.apache.ambari.server.audit.request.RequestAuditEventCreator; import org.apache.ambari.server.audit.request.RequestAuditLogger; import org.apache.ambari.server.audit.request.RequestAuditLoggerImpl; import org.apache.ambari.server.audit.request.eventcreator.AlertGroupEventCreator; +import org.apache.ambari.server.audit.request.eventcreator.AlertTargetEventCreator; import org.apache.ambari.server.audit.request.eventcreator.BlueprintEventCreator; import org.apache.ambari.server.audit.request.eventcreator.BlueprintExportEventCreator; import org.apache.ambari.server.audit.request.eventcreator.PrivilegeEventCreator; @@ -422,6 +423,7 @@ public class ControllerModule extends AbstractModule { auditLogEventCreatorBinder.addBinding().to(RepositoryEventCreator.class); auditLogEventCreatorBinder.addBinding().to(RepositoryVersionEventCreator.class); auditLogEventCreatorBinder.addBinding().to(AlertGroupEventCreator.class); + auditLogEventCreatorBinder.addBinding().to(AlertTargetEventCreator.class); bind(RequestAuditLogger.class).to(RequestAuditLoggerImpl.class); }
