Cluster name change audit log event
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/31498841 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/31498841 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/31498841 Branch: refs/heads/audit_logging Commit: 314988410f5ebf2a22a8ebb16c3dd8376fc73be4 Parents: 2ece936 Author: Daniel Gergely <dgerg...@hortonworks.com> Authored: Mon Feb 22 10:37:37 2016 +0100 Committer: Toader, Sebastian <stoa...@hortonworks.com> Committed: Thu Mar 24 13:06:46 2016 +0100 ---------------------------------------------------------------------- .../ClusterNameChangeRequestAuditEvent.java | 89 ++++++++++++++++++++ .../ConfigurationChangeEventCreator.java | 20 +++++ 2 files changed, 109 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/31498841/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ClusterNameChangeRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ClusterNameChangeRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ClusterNameChangeRequestAuditEvent.java new file mode 100644 index 0000000..c247750 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ClusterNameChangeRequestAuditEvent.java @@ -0,0 +1,89 @@ +/* + * 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; + +/** + * Base class for start operation audit events. + */ +public class ClusterNameChangeRequestAuditEvent extends RequestAuditEvent { + + public static class ClusterNameChangeRequestAuditEventBuilder extends RequestAuditEventBuilder<ClusterNameChangeRequestAuditEvent, ClusterNameChangeRequestAuditEventBuilder> { + + private String oldName; + + private String newName; + + public ClusterNameChangeRequestAuditEventBuilder() { + super.withOperation("Cluster name change"); + } + + @Override + protected ClusterNameChangeRequestAuditEvent newAuditEvent() { + return new ClusterNameChangeRequestAuditEvent(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(", Old name(") + .append(oldName) + .append("), New name(") + .append(newName) + .append(")"); + } + + public ClusterNameChangeRequestAuditEventBuilder withOldName(String oldName) { + this.oldName = oldName; + return this; + } + + public ClusterNameChangeRequestAuditEventBuilder withNewName(String newName) { + this.newName = newName; + return this; + } + + } + + protected ClusterNameChangeRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected ClusterNameChangeRequestAuditEvent(ClusterNameChangeRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link ClusterNameChangeRequestAuditEvent} + * @return a builder instance + */ + public static ClusterNameChangeRequestAuditEventBuilder builder() { + return new ClusterNameChangeRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/31498841/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 e40dbdd..c59f276 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 @@ -27,9 +27,11 @@ 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.event.ClusterNameChangeRequestAuditEvent; import org.apache.ambari.server.audit.request.event.ConfigurationChangeRequestAuditEvent; import org.apache.ambari.server.audit.request.RequestAuditEventCreator; 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; @@ -80,6 +82,24 @@ public class ConfigurationChangeEventCreator implements RequestAuditEventCreator public AuditEvent createAuditEvent(Request request, Result result) { String username = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername(); + if(!request.getBody().getPropertySets().isEmpty()) { + Map<String, Object> map = request.getBody().getPropertySets().iterator().next(); + if(map.size() == 1 && map.containsKey(PropertyHelper.getPropertyId("Clusters","cluster_name"))) { + String newName = String.valueOf(map.get(PropertyHelper.getPropertyId("Clusters","cluster_name"))); + String oldName = request.getResource().getKeyValueMap().get(Resource.Type.Cluster); + return ClusterNameChangeRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withOldName(oldName) + .withNewName(newName) + .build(); + } + } + return ConfigurationChangeRequestAuditEvent.builder() .withTimestamp(DateTime.now()) .withRequestType(request.getRequestType())