View instance event creator
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f5148002 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f5148002 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f5148002 Branch: refs/heads/audit_logging Commit: f5148002cd3b5ed568f4b86c1664831f2f729820 Parents: 0f31cca Author: Daniel Gergely <[email protected]> Authored: Thu Feb 18 15:17:02 2016 +0100 Committer: Toader, Sebastian <[email protected]> Committed: Thu Mar 24 13:06:45 2016 +0100 ---------------------------------------------------------------------- .../AddViewInstanceRequestAuditEvent.java | 113 ++++++++++++++ .../ChangeViewInstanceRequestAuditEvent.java | 109 ++++++++++++++ .../DeleteViewInstanceRequestAuditEvent.java | 91 ++++++++++++ .../eventcreator/ViewInstanceEventCreator.java | 147 +++++++++++++++++++ .../server/controller/ControllerModule.java | 2 + 5 files changed, 462 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/f5148002/ambari-server/src/main/java/org/apache/ambari/server/audit/request/AddViewInstanceRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/AddViewInstanceRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/AddViewInstanceRequestAuditEvent.java new file mode 100644 index 0000000..9d1b393 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/AddViewInstanceRequestAuditEvent.java @@ -0,0 +1,113 @@ +/* + * 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; + +import java.util.List; + +import org.apache.commons.lang.StringUtils; + +public class AddViewInstanceRequestAuditEvent extends RequestAuditEvent { + + public static class AddViewInstanceRequestAuditEventBuilder extends RequestAuditEventBuilder<AddViewInstanceRequestAuditEvent, AddViewInstanceRequestAuditEventBuilder> { + + private String description; + + private String name; + + private String type; + + private String displayName; + + private String version; + + public AddViewInstanceRequestAuditEventBuilder() { + super.withOperation("View addition"); + } + + @Override + protected AddViewInstanceRequestAuditEvent newAuditEvent() { + return new AddViewInstanceRequestAuditEvent(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(", Type(") + .append(type) + .append("), Version(") + .append(version) + .append("), Name(") + .append(name) + .append("), Display name(") + .append(displayName) + .append("), Description(") + .append(description) + .append(")"); + } + + public AddViewInstanceRequestAuditEventBuilder withDescription(String description) { + this.description = description; + return this; + } + + public AddViewInstanceRequestAuditEventBuilder withName(String name) { + this.name = name; + return this; + } + + public AddViewInstanceRequestAuditEventBuilder withType(String type) { + this.type = type; + return this; + } + + public AddViewInstanceRequestAuditEventBuilder withDisplayName(String displayName) { + this.displayName = displayName; + return this; + } + + public AddViewInstanceRequestAuditEventBuilder withVersion(String version) { + this.version = version; + return this; + } + } + + protected AddViewInstanceRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected AddViewInstanceRequestAuditEvent(AddViewInstanceRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link AddViewInstanceRequestAuditEvent} + * @return a builder instance + */ + public static AddViewInstanceRequestAuditEventBuilder builder() { + return new AddViewInstanceRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/f5148002/ambari-server/src/main/java/org/apache/ambari/server/audit/request/ChangeViewInstanceRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/ChangeViewInstanceRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/ChangeViewInstanceRequestAuditEvent.java new file mode 100644 index 0000000..8dd31ae --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/ChangeViewInstanceRequestAuditEvent.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for changeitional 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; + +public class ChangeViewInstanceRequestAuditEvent extends RequestAuditEvent { + + public static class ChangeViewInstanceRequestAuditEventBuilder extends RequestAuditEventBuilder<ChangeViewInstanceRequestAuditEvent, ChangeViewInstanceRequestAuditEventBuilder> { + + private String description; + + private String name; + + private String type; + + private String displayName; + + private String version; + + public ChangeViewInstanceRequestAuditEventBuilder() { + super.withOperation("View change"); + } + + @Override + protected ChangeViewInstanceRequestAuditEvent newAuditEvent() { + return new ChangeViewInstanceRequestAuditEvent(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(", Type(") + .append(type) + .append("), Version(") + .append(version) + .append("), Name(") + .append(name) + .append("), Display name(") + .append(displayName) + .append("), Description(") + .append(description) + .append(")"); + } + + public ChangeViewInstanceRequestAuditEventBuilder withDescription(String description) { + this.description = description; + return this; + } + + public ChangeViewInstanceRequestAuditEventBuilder withName(String name) { + this.name = name; + return this; + } + + public ChangeViewInstanceRequestAuditEventBuilder withType(String type) { + this.type = type; + return this; + } + + public ChangeViewInstanceRequestAuditEventBuilder withDisplayName(String displayName) { + this.displayName = displayName; + return this; + } + + public ChangeViewInstanceRequestAuditEventBuilder withVersion(String version) { + this.version = version; + return this; + } + } + + protected ChangeViewInstanceRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected ChangeViewInstanceRequestAuditEvent(ChangeViewInstanceRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link ChangeViewInstanceRequestAuditEvent} + * @return a builder instance + */ + public static ChangeViewInstanceRequestAuditEventBuilder builder() { + return new ChangeViewInstanceRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/f5148002/ambari-server/src/main/java/org/apache/ambari/server/audit/request/DeleteViewInstanceRequestAuditEvent.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/DeleteViewInstanceRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/DeleteViewInstanceRequestAuditEvent.java new file mode 100644 index 0000000..20d3234 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/DeleteViewInstanceRequestAuditEvent.java @@ -0,0 +1,91 @@ +/* + * 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; + +public class DeleteViewInstanceRequestAuditEvent extends RequestAuditEvent { + + public static class DeleteViewInstanceRequestAuditEventBuilder extends RequestAuditEventBuilder<DeleteViewInstanceRequestAuditEvent, DeleteViewInstanceRequestAuditEventBuilder> { + + private String name; + + private String type; + + private String version; + + public DeleteViewInstanceRequestAuditEventBuilder() { + super.withOperation("View deletion"); + } + + @Override + protected DeleteViewInstanceRequestAuditEvent newAuditEvent() { + return new DeleteViewInstanceRequestAuditEvent(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(", Type(") + .append(type) + .append("), Version(") + .append(version) + .append("), Name(") + .append(name) + .append(")"); + } + + public DeleteViewInstanceRequestAuditEventBuilder withName(String name) { + this.name = name; + return this; + } + + public DeleteViewInstanceRequestAuditEventBuilder withType(String type) { + this.type = type; + return this; + } + + public DeleteViewInstanceRequestAuditEventBuilder withVersion(String version) { + this.version = version; + return this; + } + } + + protected DeleteViewInstanceRequestAuditEvent() { + } + + /** + * {@inheritDoc} + */ + protected DeleteViewInstanceRequestAuditEvent(DeleteViewInstanceRequestAuditEventBuilder builder) { + super(builder); + } + + /** + * Returns an builder for {@link DeleteViewInstanceRequestAuditEvent} + * @return a builder instance + */ + public static DeleteViewInstanceRequestAuditEventBuilder builder() { + return new DeleteViewInstanceRequestAuditEventBuilder(); + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/f5148002/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ViewInstanceEventCreator.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ViewInstanceEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ViewInstanceEventCreator.java new file mode 100644 index 0000000..31e2c1f --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/ViewInstanceEventCreator.java @@ -0,0 +1,147 @@ +/* + * 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.ActivateUserRequestAuditEvent; +import org.apache.ambari.server.audit.request.AddViewInstanceRequestAuditEvent; +import org.apache.ambari.server.audit.request.AdminUserRequestAuditEvent; +import org.apache.ambari.server.audit.request.ChangeViewInstanceRequestAuditEvent; +import org.apache.ambari.server.audit.request.CreateUserRequestAuditEvent; +import org.apache.ambari.server.audit.request.DeleteUserRequestAuditEvent; +import org.apache.ambari.server.audit.request.DeleteViewInstanceRequestAuditEvent; +import org.apache.ambari.server.audit.request.RequestAuditEventCreator; +import org.apache.ambari.server.audit.request.UserPasswordChangeRequestAuditEvent; +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 view instance requests + * For resource type {@link Resource.Type#ViewInstance} + * and request types {@link Request.Type#POST}, {@link Request.Type#PUT} and {@link Request.Type#DELETE} + */ +public class ViewInstanceEventCreator 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.PUT); + 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.ViewInstance); + } + + /** + * {@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 AddViewInstanceRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withType(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "view_name"))) + .withVersion(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "version"))) + .withName(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "instance_name"))) + .withDisplayName(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "label"))) + .withDescription(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "description"))) + .build(); + + case PUT: + return ChangeViewInstanceRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withType(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "view_name"))) + .withVersion(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "version"))) + .withName(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "instance_name"))) + .withDisplayName(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "label"))) + .withDescription(getProperty(request, PropertyHelper.getPropertyId("ViewInstanceInfo", "description"))) + .build(); + + case DELETE: + return DeleteViewInstanceRequestAuditEvent.builder() + .withTimestamp(DateTime.now()) + .withRequestType(request.getRequestType()) + .withResultStatus(result.getStatus()) + .withUrl(request.getURI()) + .withRemoteIp(request.getRemoteAddress()) + .withUserName(username) + .withType(request.getResource().getKeyValueMap().get(Resource.Type.View)) + .withVersion(request.getResource().getKeyValueMap().get(Resource.Type.ViewVersion)) + .withName(request.getResource().getKeyValueMap().get(Resource.Type.ViewInstance)) + .build(); + + default: + return null; + } + } + + private String getProperty(Request request, String properyId) { + if(!request.getBody().getPropertySets().isEmpty()) { + return String.valueOf(request.getBody().getPropertySets().iterator().next().get(properyId)); + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/f5148002/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 d49f453..b5e1a48 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 @@ -56,6 +56,7 @@ import org.apache.ambari.server.audit.request.eventcreator.DefaultEventCreator; import org.apache.ambari.server.audit.request.eventcreator.ComponentEventCreator; import org.apache.ambari.server.audit.request.eventcreator.ServiceEventCreator; import org.apache.ambari.server.audit.request.eventcreator.UserEventCreator; +import org.apache.ambari.server.audit.request.eventcreator.ViewInstanceEventCreator; import org.apache.ambari.server.checks.AbstractCheckDescriptor; import org.apache.ambari.server.checks.UpgradeCheckRegistry; import org.apache.ambari.server.configuration.Configuration; @@ -412,6 +413,7 @@ public class ControllerModule extends AbstractModule { auditLogEventCreatorBinder.addBinding().to(BlueprintExportEventCreator.class); auditLogEventCreatorBinder.addBinding().to(ServiceConfigDownloadEventCreator.class); auditLogEventCreatorBinder.addBinding().to(BlueprintEventCreator.class); + auditLogEventCreatorBinder.addBinding().to(ViewInstanceEventCreator.class); bind(RequestAuditLogger.class).to(RequestAuditLoggerImpl.class); }
