Upgrade 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/a4a1fa98 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/a4a1fa98 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/a4a1fa98 Branch: refs/heads/audit_logging Commit: a4a1fa980f1858ef569b8e2fd6a3fbb16260e333 Parents: 872f85c Author: Daniel Gergely <[email protected]> Authored: Thu Feb 25 13:05:16 2016 +0100 Committer: Toader, Sebastian <[email protected]> Committed: Thu Mar 24 13:06:47 2016 +0100 ---------------------------------------------------------------------- .../actionmanager/ActionDBAccessorImpl.java | 1 + .../server/audit/TaskStatusAuditEvent.java | 20 +++- .../event/AddUpgradeRequestAuditEvent.java | 92 ++++++++++++++++ .../UpdateUpgradeItemRequestAuditEvent.java | 92 ++++++++++++++++ .../eventcreator/UpgradeEventCreator.java | 105 +++++++++++++++++++ .../eventcreator/UpgradeItemEventCreator.java | 102 ++++++++++++++++++ 6 files changed, 410 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java index be5a2e3..1105f74 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ActionDBAccessorImpl.java @@ -825,6 +825,7 @@ public class ActionDBAccessorImpl implements ActionDBAccessor { .withTaskId(String.valueOf(commandEntity.getTaskId())) .withHostName(commandEntity.getHostName()) .withOperation(commandEntity.getRoleCommand().toString() + " " + commandEntity.getRole().toString()) + .withDetails(commandEntity.getCommandDetail()) .withStatus(commandEntity.getStatus().toString()) .withRequestId(String.valueOf(requestId)) .withTimestamp(DateTime.now()) http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/audit/TaskStatusAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/TaskStatusAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/TaskStatusAuditEvent.java index 9de6500..212ada3 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/audit/TaskStatusAuditEvent.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/TaskStatusAuditEvent.java @@ -51,6 +51,11 @@ public class TaskStatusAuditEvent extends AbstractAuditEvent { */ private String operation; + /** + * Task command details + */ + private String details; + private TaskStatusAuditEventBuilder() { } @@ -67,8 +72,14 @@ public class TaskStatusAuditEvent extends AbstractAuditEvent { protected void buildAuditMessage(StringBuilder builder) { builder .append("Operation(") - .append(this.operation) - .append("), Status(") + .append(this.operation); + + if(details != null) { + builder.append("), Details(") + .append(this.details); + } + + builder.append("), Status(") .append(this.status) .append("), RequestId(") .append(this.requestId) @@ -105,6 +116,11 @@ public class TaskStatusAuditEvent extends AbstractAuditEvent { this.operation = operation; return this; } + + public TaskStatusAuditEventBuilder withDetails(String details) { + this.details = details; + return this; + } } private TaskStatusAuditEvent() { http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddUpgradeRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddUpgradeRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddUpgradeRequestAuditEvent.java new file mode 100644 index 0000000..49adf34 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddUpgradeRequestAuditEvent.java @@ -0,0 +1,92 @@ +/* + * 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 AddUpgradeRequestAuditEvent extends RequestAuditEvent { + + public static class AddUpgradeRequestAuditEventBuilder extends RequestAuditEventBuilder<AddUpgradeRequestAuditEvent, AddUpgradeRequestAuditEventBuilder> { + + private String repositoryVersion; + private String upgradeType; + private String clusterName; + + + public AddUpgradeRequestAuditEventBuilder() { + super.withOperation("Upgrade addition"); + } + + @Override + protected AddUpgradeRequestAuditEvent newAuditEvent() { + return new AddUpgradeRequestAuditEvent(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(", Repository version(") + .append(repositoryVersion) + .append("), Upgrade type(") + .append(upgradeType) + .append("), Cluster name(") + .append(clusterName) + .append(")"); + } + + public AddUpgradeRequestAuditEventBuilder withRepositoryVersion(String repositoryVersion) { + this.repositoryVersion = repositoryVersion; + return this; + } + + public AddUpgradeRequestAuditEventBuilder withUpgradeType(String upgradeType) { + this.upgradeType = upgradeType; + return this; + } + + public AddUpgradeRequestAuditEventBuilder withClusterName(String clusterName) { + this.clusterName = clusterName; + return this; + } + } + + protected AddUpgradeRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected AddUpgradeRequestAuditEvent(AddUpgradeRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link AddUpgradeRequestAuditEvent} + * @return a builder instance + */ + public static AddUpgradeRequestAuditEventBuilder builder() { + return new AddUpgradeRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/UpdateUpgradeItemRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/UpdateUpgradeItemRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/UpdateUpgradeItemRequestAuditEvent.java new file mode 100644 index 0000000..e622937 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/UpdateUpgradeItemRequestAuditEvent.java @@ -0,0 +1,92 @@ +/* + * 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 UpdateUpgradeItemRequestAuditEvent extends RequestAuditEvent { + + public static class UpdateUpgradeItemRequestAuditEventBuilder extends RequestAuditEventBuilder<UpdateUpgradeItemRequestAuditEvent, UpdateUpgradeItemRequestAuditEventBuilder> { + + private String stageId; + private String status; + private String requestId; + + + public UpdateUpgradeItemRequestAuditEventBuilder() { + super.withOperation("Action confirmation by the user"); + } + + @Override + protected UpdateUpgradeItemRequestAuditEvent newAuditEvent() { + return new UpdateUpgradeItemRequestAuditEvent(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(", Stage id(") + .append(stageId) + .append("), Status(") + .append(status) + .append("), Request id(") + .append(requestId) + .append(")"); + } + + public UpdateUpgradeItemRequestAuditEventBuilder withStageId(String stageId) { + this.stageId = stageId; + return this; + } + + public UpdateUpgradeItemRequestAuditEventBuilder withStatus(String status) { + this.status = status; + return this; + } + + public UpdateUpgradeItemRequestAuditEventBuilder withRequestId(String requestId) { + this.requestId = requestId; + return this; + } + } + + protected UpdateUpgradeItemRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected UpdateUpgradeItemRequestAuditEvent(UpdateUpgradeItemRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link UpdateUpgradeItemRequestAuditEvent} + * @return a builder instance + */ + public static UpdateUpgradeItemRequestAuditEventBuilder builder() { + return new UpdateUpgradeItemRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.java new file mode 100644 index 0000000..88ec5b0 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeEventCreator.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.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.AddAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.AddUpgradeRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.ChangeAlertGroupRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.DeleteAlertGroupRequestAuditEvent; +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 upgrade requests + * For resource type {@link Resource.Type#Upgrade} + * and request types {@link Request.Type#POST} + */ +public class UpgradeEventCreator 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); + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Request.Type> getRequestTypes() { + return requestTypes; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Resource.Type> getResourceTypes() { + return Collections.singleton(Resource.Type.Upgrade); + } + + /** + * {@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(); + + return AddUpgradeRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withRepositoryVersion(getProperty(request, "repository_version")) + .withUpgradeType(getProperty(request, "upgrade_type")) + .withClusterName(getProperty(request, "cluster_name")) + .build(); + + } + + private String getProperty(Request request, String propertyName) { + if(!request.getBody().getPropertySets().isEmpty()) { + return String.valueOf(request.getBody().getPropertySets().iterator().next().get(PropertyHelper.getPropertyId("Upgrade",propertyName))); + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/a4a1fa98/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeItemEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeItemEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeItemEventCreator.java new file mode 100644 index 0000000..dff546c --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/UpgradeItemEventCreator.java @@ -0,0 +1,102 @@ +/* + * 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.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.AddUpgradeRequestAuditEvent; +import org.apache.ambari.server.audit.request.event.UpdateUpgradeItemRequestAuditEvent; +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 upgrade requests + * For resource type {@link Resource.Type#Upgrade} + * and request types {@link Request.Type#POST} + */ +public class UpgradeItemEventCreator 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); + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Request.Type> getRequestTypes() { + return requestTypes; + } + + /** + * {@inheritDoc} + */ + @Override + public Set<Resource.Type> getResourceTypes() { + return Collections.singleton(Resource.Type.UpgradeItem); + } + + /** + * {@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(); + + return UpdateUpgradeItemRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withStatus(getProperty(request, "status")) + .withStageId(getProperty(request, "stage_id")) + .withRequestId(getProperty(request, "request_id")) + .build(); + + } + + private String getProperty(Request request, String propertyName) { + if(!request.getBody().getPropertySets().isEmpty()) { + return String.valueOf(request.getBody().getPropertySets().iterator().next().get(PropertyHelper.getPropertyId("UpgradeItem",propertyName))); + } + return null; + } +}
