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