You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by st...@apache.org on 2016/03/24 13:08:43 UTC

[11/51] [abbrv] ambari git commit: Alert group event creator

Alert group event creator


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/85f26fa5
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/85f26fa5
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/85f26fa5

Branch: refs/heads/audit_logging
Commit: 85f26fa5eed6c55cbea2019785feb8887985ab51
Parents: 3149884
Author: Daniel Gergely <dg...@hortonworks.com>
Authored: Mon Feb 22 14:01:05 2016 +0100
Committer: Toader, Sebastian <st...@hortonworks.com>
Committed: Thu Mar 24 13:06:46 2016 +0100

----------------------------------------------------------------------
 .../event/AddAlertGroupRequestAuditEvent.java   |  96 ++++++++++++
 .../ChangeAlertGroupRequestAuditEvent.java      |  96 ++++++++++++
 .../DeleteAlertGroupRequestAuditEvent.java      |  75 +++++++++
 .../eventcreator/AlertGroupEventCreator.java    | 157 +++++++++++++++++++
 .../server/controller/ControllerModule.java     |   2 +
 5 files changed, 426 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/85f26fa5/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertGroupRequestAuditEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertGroupRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertGroupRequestAuditEvent.java
new file mode 100644
index 0000000..46a2273
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/AddAlertGroupRequestAuditEvent.java
@@ -0,0 +1,96 @@
+/*
+ * 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 AddAlertGroupRequestAuditEvent extends RequestAuditEvent {
+
+  public static class AddAlertGroupRequestAuditEventBuilder extends RequestAuditEventBuilder<AddAlertGroupRequestAuditEvent, AddAlertGroupRequestAuditEventBuilder> {
+
+    private String name;
+
+    private List<String> definitionIds;
+
+    private List<String> notificationIds;
+
+    public AddAlertGroupRequestAuditEventBuilder() {
+      super.withOperation("Alert group addition");
+    }
+
+    @Override
+    protected AddAlertGroupRequestAuditEvent newAuditEvent() {
+      return new AddAlertGroupRequestAuditEvent(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(", Alert group name(")
+        .append(name)
+        .append("), Definition IDs(")
+        .append(StringUtils.join(definitionIds, ", "))
+        .append("), Notification IDs(")
+        .append(StringUtils.join(notificationIds, ", "))
+        .append(")");
+    }
+
+    public AddAlertGroupRequestAuditEventBuilder withName(String name) {
+      this.name = name;
+      return this;
+    }
+
+    public AddAlertGroupRequestAuditEventBuilder withDefinitionIds(List<String> ids) {
+      this.definitionIds = ids;
+      return this;
+    }
+
+    public AddAlertGroupRequestAuditEventBuilder withNotificationIds(List<String> ids) {
+      this.notificationIds = ids;
+      return this;
+    }
+  }
+
+  protected AddAlertGroupRequestAuditEvent() {
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  protected AddAlertGroupRequestAuditEvent(AddAlertGroupRequestAuditEventBuilder builder) {
+    super(builder);
+  }
+
+  /**
+   * Returns an builder for {@link AddAlertGroupRequestAuditEvent}
+   * @return a builder instance
+   */
+  public static AddAlertGroupRequestAuditEventBuilder builder() {
+    return new AddAlertGroupRequestAuditEventBuilder();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/85f26fa5/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertGroupRequestAuditEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertGroupRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertGroupRequestAuditEvent.java
new file mode 100644
index 0000000..621eff6
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/ChangeAlertGroupRequestAuditEvent.java
@@ -0,0 +1,96 @@
+/*
+ * 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 ChangeAlertGroupRequestAuditEvent extends RequestAuditEvent {
+
+  public static class ChangeAlertGroupRequestAuditEventBuilder extends RequestAuditEventBuilder<ChangeAlertGroupRequestAuditEvent, ChangeAlertGroupRequestAuditEventBuilder> {
+
+    private String name;
+
+    private List<String> definitionIds;
+
+    private List<String> notificationIds;
+
+    public ChangeAlertGroupRequestAuditEventBuilder() {
+      super.withOperation("Alert group change");
+    }
+
+    @Override
+    protected ChangeAlertGroupRequestAuditEvent newAuditEvent() {
+      return new ChangeAlertGroupRequestAuditEvent(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(", Alert group name(")
+        .append(name)
+        .append("), Definition IDs(")
+        .append(StringUtils.join(definitionIds, ", "))
+        .append("), Notification IDs(")
+        .append(StringUtils.join(notificationIds, ", "))
+        .append(")");
+    }
+
+    public ChangeAlertGroupRequestAuditEventBuilder withName(String name) {
+      this.name = name;
+      return this;
+    }
+
+    public ChangeAlertGroupRequestAuditEventBuilder withDefinitionIds(List<String> ids) {
+      this.definitionIds = ids;
+      return this;
+    }
+
+    public ChangeAlertGroupRequestAuditEventBuilder withNotificationIds(List<String> ids) {
+      this.notificationIds = ids;
+      return this;
+    }
+  }
+
+  protected ChangeAlertGroupRequestAuditEvent() {
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  protected ChangeAlertGroupRequestAuditEvent(ChangeAlertGroupRequestAuditEventBuilder builder) {
+    super(builder);
+  }
+
+  /**
+   * Returns an builder for {@link ChangeAlertGroupRequestAuditEvent}
+   * @return a builder instance
+   */
+  public static ChangeAlertGroupRequestAuditEventBuilder builder() {
+    return new ChangeAlertGroupRequestAuditEventBuilder();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/85f26fa5/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertGroupRequestAuditEvent.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertGroupRequestAuditEvent.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertGroupRequestAuditEvent.java
new file mode 100644
index 0000000..95b223d
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/event/DeleteAlertGroupRequestAuditEvent.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 DeleteAlertGroupRequestAuditEvent extends RequestAuditEvent {
+
+  public static class DeleteAlertGroupRequestAuditEventBuilder extends RequestAuditEventBuilder<DeleteAlertGroupRequestAuditEvent, DeleteAlertGroupRequestAuditEventBuilder> {
+
+    private String id;
+
+    public DeleteAlertGroupRequestAuditEventBuilder() {
+      super.withOperation("Alert group removal");
+    }
+
+    @Override
+    protected DeleteAlertGroupRequestAuditEvent newAuditEvent() {
+      return new DeleteAlertGroupRequestAuditEvent(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(", Alert group ID(")
+        .append(id)
+        .append(")");
+    }
+
+    public DeleteAlertGroupRequestAuditEventBuilder withId(String id) {
+      this.id = id;
+      return this;
+    }
+  }
+
+  protected DeleteAlertGroupRequestAuditEvent() {
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  protected DeleteAlertGroupRequestAuditEvent(DeleteAlertGroupRequestAuditEventBuilder builder) {
+    super(builder);
+  }
+
+  /**
+   * Returns an builder for {@link DeleteAlertGroupRequestAuditEvent}
+   * @return a builder instance
+   */
+  public static DeleteAlertGroupRequestAuditEventBuilder builder() {
+    return new DeleteAlertGroupRequestAuditEventBuilder();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/85f26fa5/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertGroupEventCreator.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertGroupEventCreator.java b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertGroupEventCreator.java
new file mode 100644
index 0000000..a34e2d6
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/AlertGroupEventCreator.java
@@ -0,0 +1,157 @@
+/*
+ * 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.LinkedList;
+import java.util.List;
+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.request.RequestAuditEventCreator;
+import org.apache.ambari.server.audit.request.event.AddAlertGroupRequestAuditEvent;
+import org.apache.ambari.server.audit.request.event.AddUserToGroupRequestAuditEvent;
+import org.apache.ambari.server.audit.request.event.ChangeAlertGroupRequestAuditEvent;
+import org.apache.ambari.server.audit.request.event.DeleteAlertGroupRequestAuditEvent;
+import org.apache.ambari.server.audit.request.event.MembershipChangeRequestAuditEvent;
+import org.apache.ambari.server.audit.request.event.RemoveUserFromGroupRequestAuditEvent;
+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 group requests
+ * For resource type {@link Resource.Type#AlertGroup}
+ * and request types {@link Request.Type#POST}, {@link Request.Type#PUT} and {@link Request.Type#DELETE}
+ */
+public class AlertGroupEventCreator 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.AlertGroup);
+  }
+
+  /**
+   * {@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 AddAlertGroupRequestAuditEvent.builder()
+          .withTimestamp(DateTime.now())
+          .withRequestType(request.getRequestType())
+          .withResultStatus(result.getStatus())
+          .withUrl(request.getURI())
+          .withRemoteIp(request.getRemoteAddress())
+          .withUserName(username)
+          .withName(getName(request))
+          .withDefinitionIds(getDefinitionIds(request))
+          .withNotificationIds(getNotificationIds(request))
+          .build();
+      case PUT:
+        return ChangeAlertGroupRequestAuditEvent.builder()
+          .withTimestamp(DateTime.now())
+          .withRequestType(request.getRequestType())
+          .withResultStatus(result.getStatus())
+          .withUrl(request.getURI())
+          .withRemoteIp(request.getRemoteAddress())
+          .withUserName(username)
+          .withName(getName(request))
+          .withDefinitionIds(getDefinitionIds(request))
+          .withNotificationIds(getNotificationIds(request))
+          .build();
+      case DELETE:
+        return DeleteAlertGroupRequestAuditEvent.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.AlertGroup))
+          .build();
+      default:
+        return null;
+    }
+  }
+
+  private String getName(Request request) {
+    if(!request.getBody().getNamedPropertySets().isEmpty()) {
+      return String.valueOf(request.getBody().getNamedPropertySets().iterator().next().getProperties().get(PropertyHelper.getPropertyId("AlertGroup","name")));
+    }
+    return null;
+  }
+
+  private List<String> getDefinitionIds(Request request) {
+    if(!request.getBody().getNamedPropertySets().isEmpty()) {
+      List<String> list = (List<String>) request.getBody().getNamedPropertySets().iterator().next().getProperties().get(PropertyHelper.getPropertyId("AlertGroup", "definitions"));
+      if (list != null) {
+        return list;
+      }
+    }
+    return Collections.emptyList();
+  }
+
+  private List<String> getNotificationIds(Request request) {
+    if(!request.getBody().getNamedPropertySets().isEmpty()) {
+      List<String> list = (List<String>) request.getBody().getNamedPropertySets().iterator().next().getProperties().get(PropertyHelper.getPropertyId("AlertGroup", "targets"));
+      if (list != null) {
+        return list;
+      }
+    }
+    return Collections.emptyList();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/85f26fa5/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 2e03d29..f47ffec 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
@@ -44,6 +44,7 @@ import org.apache.ambari.server.actionmanager.StageFactoryImpl;
 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.BlueprintEventCreator;
 import org.apache.ambari.server.audit.request.eventcreator.BlueprintExportEventCreator;
 import org.apache.ambari.server.audit.request.eventcreator.PrivilegeEventCreator;
@@ -420,6 +421,7 @@ public class ControllerModule extends AbstractModule {
     auditLogEventCreatorBinder.addBinding().to(ViewPrivilegeEventCreator.class);
     auditLogEventCreatorBinder.addBinding().to(RepositoryEventCreator.class);
     auditLogEventCreatorBinder.addBinding().to(RepositoryVersionEventCreator.class);
+    auditLogEventCreatorBinder.addBinding().to(AlertGroupEventCreator.class);
 
     bind(RequestAuditLogger.class).to(RequestAuditLoggerImpl.class);
   }