001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 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.Arrays;
027import java.util.Collections;
028import java.util.Date;
029import java.util.LinkedHashMap;
030import java.util.List;
031import java.util.Map;
032
033import com.unboundid.ldap.sdk.Attribute;
034import com.unboundid.ldap.sdk.Entry;
035import com.unboundid.util.NotMutable;
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 Alcatel-Lucent 8661
060 *   server products.  These classes provide support for proprietary
061 *   functionality or for external specifications that are not considered stable
062 *   or mature enough to be guaranteed to work in an interoperable way with
063 *   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    super(taskID, ENTER_LOCKDOWN_MODE_TASK_CLASS, scheduledStartTime,
228          dependencyIDs, failedDependencyAction, notifyOnCompletion,
229          notifyOnError);
230
231    this.reason = reason;
232  }
233
234
235
236  /**
237   * Creates a new enter lockdown mode task from the provided entry.
238   *
239   * @param  entry  The entry to use to create this enter lockdown mode task.
240   *
241   * @throws  TaskException  If the provided entry cannot be parsed as an enter
242   *                         lockdown mode task entry.
243   */
244  public EnterLockdownModeTask(final Entry entry)
245         throws TaskException
246  {
247    super(entry);
248
249    // Get the "reason" string if it is present.
250    reason = entry.getAttributeValue(ATTR_ENTER_LOCKDOWN_REASON);
251  }
252
253
254
255  /**
256   * Creates a new enter lockdown mode task from the provided set of task
257   * properties.
258   *
259   * @param  properties  The set of task properties and their corresponding
260   *                     values to use for the task.  It must not be
261   *                     {@code null}.
262   *
263   * @throws  TaskException  If the provided set of properties cannot be used to
264   *                         create a valid enter lockdown mode task.
265   */
266  public EnterLockdownModeTask(final Map<TaskProperty,List<Object>> properties)
267         throws TaskException
268  {
269    super(ENTER_LOCKDOWN_MODE_TASK_CLASS, properties);
270
271    String r = null;
272    for (final Map.Entry<TaskProperty,List<Object>> entry :
273            properties.entrySet())
274    {
275      final TaskProperty p = entry.getKey();
276      final String attrName = p.getAttributeName();
277      final List<Object> values = entry.getValue();
278
279      if (attrName.equalsIgnoreCase(ATTR_ENTER_LOCKDOWN_REASON))
280      {
281        r = parseString(p, values, null);
282        break;
283      }
284    }
285
286    reason = r;
287  }
288
289
290
291  /**
292   * Retrieves the user-specified reason why the server is entering lockdown
293   * mode.
294   *
295   * @return  The reason the server is entering lockdown mode, or {@code null}
296   *          if none was specified.
297   */
298  public String getReason()
299  {
300    return reason;
301  }
302
303
304
305  /**
306   * {@inheritDoc}
307   */
308  @Override()
309  public String getTaskName()
310  {
311    return INFO_TASK_NAME_ENTER_LOCKDOWN_MODE.get();
312  }
313
314
315
316  /**
317   * {@inheritDoc}
318   */
319  @Override()
320  public String getTaskDescription()
321  {
322    return INFO_TASK_DESCRIPTION_ENTER_LOCKDOWN_MODE.get();
323  }
324
325
326
327  /**
328   * {@inheritDoc}
329   */
330  @Override()
331  protected List<String> getAdditionalObjectClasses()
332  {
333    return Arrays.asList(OC_ENTER_LOCKDOWN_MODE_TASK);
334  }
335
336
337
338  /**
339   * {@inheritDoc}
340   */
341  @Override()
342  protected List<Attribute> getAdditionalAttributes()
343  {
344    final ArrayList<Attribute> attrs = new ArrayList<Attribute>(1);
345    if(reason != null)
346    {
347      attrs.add(new Attribute(ATTR_ENTER_LOCKDOWN_REASON, reason));
348    }
349    return attrs;
350  }
351
352
353
354  /**
355   * {@inheritDoc}
356   */
357  @Override()
358  public List<TaskProperty> getTaskSpecificProperties()
359  {
360    final List<TaskProperty> propList =
361              Arrays.asList(PROPERTY_ENTER_LOCKDOWN_REASON);
362
363    return Collections.unmodifiableList(propList);
364  }
365
366
367
368  /**
369   * {@inheritDoc}
370   */
371  @Override()
372  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
373  {
374    final LinkedHashMap<TaskProperty,List<Object>> props =
375         new LinkedHashMap<TaskProperty,List<Object>>();
376
377    if (reason != null)
378    {
379      props.put(PROPERTY_ENTER_LOCKDOWN_REASON,
380              Collections.<Object>unmodifiableList(Arrays.asList(reason)));
381    }
382
383    props.putAll(super.getTaskPropertyValues());
384    return Collections.unmodifiableMap(props);
385  }
386}