Host 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/d9ef0ee2 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d9ef0ee2 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d9ef0ee2 Branch: refs/heads/audit_logging Commit: d9ef0ee245e5abbc832e2cf4560098b7c22ac54d Parents: 0024cfa Author: Daniel Gergely <[email protected]> Authored: Tue Feb 23 10:13:53 2016 +0100 Committer: Toader, Sebastian <[email protected]> Committed: Thu Mar 24 13:06:46 2016 +0100 ---------------------------------------------------------------------- .../request/event/AddHostRequestAuditEvent.java | 75 ++++++++++++ .../event/DeleteHostRequestAuditEvent.java | 77 ++++++++++++ .../request/eventcreator/HostEventCreator.java | 119 +++++++++++++++++++ .../server/controller/ControllerModule.java | 2 + 4 files changed, 273 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/d9ef0ee2/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddHostRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddHostRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddHostRequestAuditEvent.java new file mode 100644 index 0000000..cab8e6e --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddHostRequestAuditEvent.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 AddHostRequestAuditEvent extends RequestAuditEvent { + + public static class AddHostRequestAuditEventBuilder extends RequestAuditEventBuilder<AddHostRequestAuditEvent, AddHostRequestAuditEventBuilder> { + + private String hostName; + + public AddHostRequestAuditEventBuilder() { + super.withOperation("Host addition"); + } + + @Override + protected AddHostRequestAuditEvent newAuditEvent() { + return new AddHostRequestAuditEvent(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(", Hostname(") + .append(hostName) + .append(")"); + } + + public AddHostRequestAuditEventBuilder withHostName(String hostName) { + this.hostName = hostName; + return this; + } + } + + protected AddHostRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected AddHostRequestAuditEvent(AddHostRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link AddHostRequestAuditEvent} + * @return a builder instance + */ + public static AddHostRequestAuditEventBuilder builder() { + return new AddHostRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/d9ef0ee2/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteHostRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteHostRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteHostRequestAuditEvent.java new file mode 100644 index 0000000..aa982d4 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteHostRequestAuditEvent.java @@ -0,0 +1,77 @@ +/* + * 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 DeleteHostRequestAuditEvent extends RequestAuditEvent { + + public static class DeleteHostRequestAuditEventBuilder extends RequestAuditEventBuilder<DeleteHostRequestAuditEvent, DeleteHostRequestAuditEventBuilder> { + + private String hostName; + + public DeleteHostRequestAuditEventBuilder() { + super.withOperation("Host deletion"); + } + + @Override + protected DeleteHostRequestAuditEvent newAuditEvent() { + return new DeleteHostRequestAuditEvent(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(", Hostname(") + .append(hostName) + .append(")"); + } + + public DeleteHostRequestAuditEventBuilder withHostName(String groupName) { + this.hostName = groupName; + return this; + } + + } + + protected DeleteHostRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected DeleteHostRequestAuditEvent(DeleteHostRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link DeleteHostRequestAuditEvent} + * @return a builder instance + */ + public static DeleteHostRequestAuditEventBuilder builder() { + return new DeleteHostRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/d9ef0ee2/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 new file mode 100644 index 0000000..fef39fb --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/HostEventCreator.java @@ -0,0 +1,119 @@ +/* + * 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.HashSet; +import java.util.Map; +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.StartOperationFailedAuditEvent; +import org.apache.ambari.server.audit.StartOperationSucceededAuditEvent; +import org.apache.ambari.server.audit.request.RequestAuditEventCreator; +import org.apache.ambari.server.audit.request.event.AddHostRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.DeleteAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.DeleteHostRequestAuditEvent; +import org.apache.ambari.server.controller.internal.RequestOperationLevel; +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 host requests (add, delete) + * For resource type {@link Resource.Type#HostComponent} + * and request types {@link Request.Type#POST} and {@link Request.Type#DELETE} + */ +public class HostEventCreator 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.POST); + requestTypes.add(Request.Type.DELETE); + } + + private Set<Resource.Type> resourceTypes = new HashSet<Resource.Type>(); + { + resourceTypes.add(Resource.Type.Host); + } + + /** {@inheritDoc} */ + @Override + public Set<Request.Type> getRequestTypes() { + return requestTypes; + } + + /** {@inheritDoc} */ + @Override + public Set<Resource.Type> getResourceTypes() { + return resourceTypes; + } + + /** {@inheritDoc} */ + @Override + public Set<ResultStatus.STATUS> getResultStatuses() { + // null makes this default + return null; + } + + @Override + public AuditEvent createAuditEvent(Request request, Result result) { + String username = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(); + + switch(request.getRequestType()) { + case DELETE: + return DeleteHostRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withHostName(request.getResource().getKeyValueMap().get(Resource.Type.Host)) + .build(); + case POST: + return AddHostRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withHostName(getHostName(request)) + .build(); + default: + return null; + } + } + + private String getHostName(Request request) { + if(!request.getBody().getNamedPropertySets().isEmpty()) { + return String.valueOf(request.getBody().getNamedPropertySets().iterator().next().getProperties().get(PropertyHelper.getPropertyId("Hosts","host_name"))); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/d9ef0ee2/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 2ea8c3a..4c2992a 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 @@ -48,6 +48,7 @@ import org.apache.ambari.server.audit.request.eventcreator.AlertGroupEventCreato 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.HostEventCreator; import org.apache.ambari.server.audit.request.eventcreator.PrivilegeEventCreator; import org.apache.ambari.server.audit.request.eventcreator.GroupEventCreator; import org.apache.ambari.server.audit.request.eventcreator.MemberEventCreator; @@ -424,6 +425,7 @@ public class ControllerModule extends AbstractModule { auditLogEventCreatorBinder.addBinding().to(RepositoryVersionEventCreator.class); auditLogEventCreatorBinder.addBinding().to(AlertGroupEventCreator.class); auditLogEventCreatorBinder.addBinding().to(AlertTargetEventCreator.class); + auditLogEventCreatorBinder.addBinding().to(HostEventCreator.class); bind(RequestAuditLogger.class).to(RequestAuditLoggerImpl.class); }
