001/*
002 * Copyright 2008-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2015-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.unboundidds.tasks;
037
038
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.Date;
043import java.util.LinkedHashMap;
044import java.util.List;
045import java.util.Map;
046
047import com.unboundid.ldap.sdk.Attribute;
048import com.unboundid.ldap.sdk.Entry;
049import com.unboundid.util.NotMutable;
050import com.unboundid.util.StaticUtils;
051import com.unboundid.util.ThreadSafety;
052import com.unboundid.util.ThreadSafetyLevel;
053
054import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
055
056
057
058/**
059 * This class defines a Directory Server task that can be used to cause the
060 * server to enter lockdown mode, in which case it will only allow requests
061 * from users with the lockdown-mode privilege.  Lockdown mode is intended to
062 * allow administrators to perform operations with the server online but without
063 * worrying about the impact that those operations may have on other users.  In
064 * In some special cases, the server may place itself in lockdown mode as a
065 * defense mechanism rather than risking the exposure of sensitive information.
066 * For example, if the server detects any malformed access control rules at
067 * startup, then it will place itself in lockdown mode rather than attempt to
068 * run without that rule in effect since it could have been intended to prevent
069 * unauthorized users from accessing certain data.
070 * <BR>
071 * <BLOCKQUOTE>
072 *   <B>NOTE:</B>  This class, and other classes within the
073 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
074 *   supported for use against Ping Identity, UnboundID, and
075 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
076 *   for proprietary functionality or for external specifications that are not
077 *   considered stable or mature enough to be guaranteed to work in an
078 *   interoperable way with other types of LDAP servers.
079 * </BLOCKQUOTE>
080 * <BR>
081 * The enter lockdown mode task does not have any task-specific properties.  See
082 * the {@link LeaveLockdownModeTask} class for the corresponding mechanism to
083 * bring the server out of lockdown mode.
084 */
085@NotMutable()
086@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
087public final class EnterLockdownModeTask
088       extends Task
089{
090  /**
091   * The fully-qualified name of the Java class that is used for the enter
092   * lockdown mode task.
093   */
094  static final String ENTER_LOCKDOWN_MODE_TASK_CLASS =
095       "com.unboundid.directory.server.tasks.EnterLockdownModeTask";
096
097
098
099  /**
100   * The name of the attribute used to specify the reason for putting the server
101   * into lockdown mode.
102   */
103  private static final String ATTR_ENTER_LOCKDOWN_REASON =
104       "ds-task-enter-lockdown-reason";
105
106
107
108  /**
109   * The task property for the enter-lockdown reason.
110   */
111  private static final TaskProperty PROPERTY_ENTER_LOCKDOWN_REASON =
112       new TaskProperty(ATTR_ENTER_LOCKDOWN_REASON,
113                        INFO_DISPLAY_NAME_ENTER_LOCKDOWN_REASON.get(),
114                        INFO_DESCRIPTION_ENTER_LOCKDOWN_REASON.get(),
115                        String.class, false, false, false);
116
117
118
119  /**
120   * The name of the object class used in enter-lockdown-mode task entries.
121   */
122  private static final String OC_ENTER_LOCKDOWN_MODE_TASK =
123      "ds-task-enter-lockdown-mode";
124
125
126
127  /**
128   * The serial version UID for this serializable class.
129   */
130  private static final long serialVersionUID = -4104020769734351458L;
131
132
133
134  // The reason for entering lockdown mode.
135  private final String reason;
136
137
138
139  /**
140   * Creates a new uninitialized enter lockdown mode task instance which should
141   * only be used for obtaining general information about this task, including
142   * the task name, description, and supported properties.  Attempts to use a
143   * task created with this constructor for any other reason will likely fail.
144   */
145  public EnterLockdownModeTask()
146  {
147    reason = null;
148  }
149
150
151
152  /**
153   * Creates a new enter lockdown mode task with the specified task ID.
154   *
155   * @param  taskID  The task ID to use for this task.  If it is {@code null}
156   *                 then a UUID will be generated for use as the task ID.
157   */
158  public EnterLockdownModeTask(final String taskID)
159  {
160    this(taskID, null);
161  }
162
163
164
165  /**
166   * Creates a new enter lockdown mode task with the specified task ID.
167   *
168   * @param  taskID  The task ID to use for this task.  If it is {@code null}
169   *                 then a UUID will be generated for use as the task ID.
170   * @param  reason  The user-specified reason for entering lockdown mode. This
171   *                 may be {@code null}.
172   */
173  public EnterLockdownModeTask(final String taskID, final String reason)
174  {
175    this(taskID, reason, null, null, null, null, null);
176  }
177
178
179
180  /**
181   * Creates a new enter lockdown mode task with the provided information.
182   *
183   * @param  taskID                  The task ID to use for this task.  If it is
184   *                                 {@code null} then a UUID will be generated
185   *                                 for use as the task ID.
186   * @param  scheduledStartTime      The time that this task should start
187   *                                 running.
188   * @param  dependencyIDs           The list of task IDs that will be required
189   *                                 to complete before this task will be
190   *                                 eligible to start.
191   * @param  failedDependencyAction  Indicates what action should be taken if
192   *                                 any of the dependencies for this task do
193   *                                 not complete successfully.
194   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
195   *                                 that should be notified when this task
196   *                                 completes.
197   * @param  notifyOnError           The list of e-mail addresses of individuals
198   *                                 that should be notified if this task does
199   *                                 not complete successfully.
200   */
201  public EnterLockdownModeTask(final String taskID,
202              final Date scheduledStartTime, final List<String> dependencyIDs,
203              final FailedDependencyAction failedDependencyAction,
204              final List<String> notifyOnCompletion,
205              final List<String> notifyOnError)
206  {
207    this(taskID, null, scheduledStartTime, dependencyIDs,
208         failedDependencyAction, notifyOnCompletion, notifyOnError);
209  }
210
211
212
213  /**
214   * Creates a new enter lockdown mode task with the provided information.
215   *
216   * @param  taskID                  The task ID to use for this task.  If it is
217   *                                 {@code null} then a UUID will be generated
218   *                                 for use as the task ID.
219   * @param  reason                  The user-specified reason for entering
220   *                                 lockdown mode. This may be {@code null}.
221   * @param  scheduledStartTime      The time that this task should start
222   *                                 running.
223   * @param  dependencyIDs           The list of task IDs that will be required
224   *                                 to complete before this task will be
225   *                                 eligible to start.
226   * @param  failedDependencyAction  Indicates what action should be taken if
227   *                                 any of the dependencies for this task do
228   *                                 not complete successfully.
229   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
230   *                                 that should be notified when this task
231   *                                 completes.
232   * @param  notifyOnError           The list of e-mail addresses of individuals
233   *                                 that should be notified if this task does
234   *                                 not complete successfully.
235   */
236  public EnterLockdownModeTask(final String taskID, final String reason,
237              final Date scheduledStartTime, final List<String> dependencyIDs,
238              final FailedDependencyAction failedDependencyAction,
239              final List<String> notifyOnCompletion,
240              final List<String> notifyOnError)
241  {
242    this(taskID, reason, scheduledStartTime, dependencyIDs,
243         failedDependencyAction, null, notifyOnCompletion, null,
244         notifyOnError, null, null, null);
245  }
246
247
248
249  /**
250   * Creates a new enter lockdown mode task with the provided information.
251   *
252   * @param  taskID                  The task ID to use for this task.  If it is
253   *                                 {@code null} then a UUID will be generated
254   *                                 for use as the task ID.
255   * @param  reason                  The user-specified reason for entering
256   *                                 lockdown mode. This may be {@code null}.
257   * @param  scheduledStartTime      The time that this task should start
258   *                                 running.
259   * @param  dependencyIDs           The list of task IDs that will be required
260   *                                 to complete before this task will be
261   *                                 eligible to start.
262   * @param  failedDependencyAction  Indicates what action should be taken if
263   *                                 any of the dependencies for this task do
264   *                                 not complete successfully.
265   * @param  notifyOnStart           The list of e-mail addresses of individuals
266   *                                 that should be notified when this task
267   *                                 starts running.
268   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
269   *                                 that should be notified when this task
270   *                                 completes.
271   * @param  notifyOnSuccess         The list of e-mail addresses of individuals
272   *                                 that should be notified if this task
273   *                                 completes successfully.
274   * @param  notifyOnError           The list of e-mail addresses of individuals
275   *                                 that should be notified if this task does
276   *                                 not complete successfully.
277   * @param  alertOnStart            Indicates whether the server should send an
278   *                                 alert notification when this task starts.
279   * @param  alertOnSuccess          Indicates whether the server should send an
280   *                                 alert notification if this task completes
281   *                                 successfully.
282   * @param  alertOnError            Indicates whether the server should send an
283   *                                 alert notification if this task fails to
284   *                                 complete successfully.
285   */
286  public EnterLockdownModeTask(final String taskID, final String reason,
287              final Date scheduledStartTime, final List<String> dependencyIDs,
288              final FailedDependencyAction failedDependencyAction,
289              final List<String> notifyOnStart,
290              final List<String> notifyOnCompletion,
291              final List<String> notifyOnSuccess,
292              final List<String> notifyOnError, final Boolean alertOnStart,
293              final Boolean alertOnSuccess, final Boolean alertOnError)
294  {
295    super(taskID, ENTER_LOCKDOWN_MODE_TASK_CLASS, scheduledStartTime,
296          dependencyIDs, failedDependencyAction, notifyOnStart,
297         notifyOnCompletion, notifyOnSuccess,  notifyOnError, alertOnStart,
298         alertOnSuccess, alertOnError);
299
300    this.reason = reason;
301  }
302
303
304
305  /**
306   * Creates a new enter lockdown mode task from the provided entry.
307   *
308   * @param  entry  The entry to use to create this enter lockdown mode task.
309   *
310   * @throws  TaskException  If the provided entry cannot be parsed as an enter
311   *                         lockdown mode task entry.
312   */
313  public EnterLockdownModeTask(final Entry entry)
314         throws TaskException
315  {
316    super(entry);
317
318    // Get the "reason" string if it is present.
319    reason = entry.getAttributeValue(ATTR_ENTER_LOCKDOWN_REASON);
320  }
321
322
323
324  /**
325   * Creates a new enter lockdown mode task from the provided set of task
326   * properties.
327   *
328   * @param  properties  The set of task properties and their corresponding
329   *                     values to use for the task.  It must not be
330   *                     {@code null}.
331   *
332   * @throws  TaskException  If the provided set of properties cannot be used to
333   *                         create a valid enter lockdown mode task.
334   */
335  public EnterLockdownModeTask(final Map<TaskProperty,List<Object>> properties)
336         throws TaskException
337  {
338    super(ENTER_LOCKDOWN_MODE_TASK_CLASS, properties);
339
340    String r = null;
341    for (final Map.Entry<TaskProperty,List<Object>> entry :
342            properties.entrySet())
343    {
344      final TaskProperty p = entry.getKey();
345      final String attrName = p.getAttributeName();
346      final List<Object> values = entry.getValue();
347
348      if (attrName.equalsIgnoreCase(ATTR_ENTER_LOCKDOWN_REASON))
349      {
350        r = parseString(p, values, null);
351        break;
352      }
353    }
354
355    reason = r;
356  }
357
358
359
360  /**
361   * Retrieves the user-specified reason why the server is entering lockdown
362   * mode.
363   *
364   * @return  The reason the server is entering lockdown mode, or {@code null}
365   *          if none was specified.
366   */
367  public String getReason()
368  {
369    return reason;
370  }
371
372
373
374  /**
375   * {@inheritDoc}
376   */
377  @Override()
378  public String getTaskName()
379  {
380    return INFO_TASK_NAME_ENTER_LOCKDOWN_MODE.get();
381  }
382
383
384
385  /**
386   * {@inheritDoc}
387   */
388  @Override()
389  public String getTaskDescription()
390  {
391    return INFO_TASK_DESCRIPTION_ENTER_LOCKDOWN_MODE.get();
392  }
393
394
395
396  /**
397   * {@inheritDoc}
398   */
399  @Override()
400  protected List<String> getAdditionalObjectClasses()
401  {
402    return Collections.singletonList(OC_ENTER_LOCKDOWN_MODE_TASK);
403  }
404
405
406
407  /**
408   * {@inheritDoc}
409   */
410  @Override()
411  protected List<Attribute> getAdditionalAttributes()
412  {
413    final ArrayList<Attribute> attrs = new ArrayList<>(1);
414    if (reason != null)
415    {
416      attrs.add(new Attribute(ATTR_ENTER_LOCKDOWN_REASON, reason));
417    }
418    return attrs;
419  }
420
421
422
423  /**
424   * {@inheritDoc}
425   */
426  @Override()
427  public List<TaskProperty> getTaskSpecificProperties()
428  {
429    final List<TaskProperty> propList =
430              Collections.singletonList(PROPERTY_ENTER_LOCKDOWN_REASON);
431
432    return Collections.unmodifiableList(propList);
433  }
434
435
436
437  /**
438   * {@inheritDoc}
439   */
440  @Override()
441  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
442  {
443    final LinkedHashMap<TaskProperty,List<Object>> props =
444         new LinkedHashMap<>(StaticUtils.computeMapCapacity(10));
445
446    if (reason != null)
447    {
448      props.put(PROPERTY_ENTER_LOCKDOWN_REASON,
449              Collections.<Object>singletonList(reason));
450    }
451
452    props.putAll(super.getTaskPropertyValues());
453    return Collections.unmodifiableMap(props);
454  }
455}