mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
15.36.2016 2502ac715d947766bd3e2aa700bdfd21c6059b9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008 Sun Microsystems, Inc.
 * Portions Copyright 2011-2016 ForgeRock AS.
 */
package org.opends.server.admin;
 
import static org.opends.server.util.ServerConstants.*;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.Schema;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
 
public class ValidateConfigDefinitionsTest extends DirectoryServerTestCase {
 
  @BeforeClass
  public void startServer() throws Exception {
    TestCaseUtils.startServer();
  }
 
  /** Returns all AbstractManagedObjectDefinition objects that are defined in. */
  @DataProvider
  public Object[][] enumrateManageObjectDefns() throws Exception {
    TopCfgDefn topCfgDefn = TopCfgDefn.getInstance();
    List<AbstractManagedObjectDefinition<?,?>> allCfgDefns = new ArrayList<>(topCfgDefn.getAllChildren());
 
    Object[][] params = new Object[allCfgDefns.size()][];
    for (int i = 0; i < params.length; i++) {
      params[i] = new Object[]{allCfgDefns.get(i)};
    }
 
    return params;
  }
 
  /** Exceptions to config objects having a different objectclass. */
  private static final List<String> CLASS_OBJECT_CLASS_EXCEPTIONS =
          Arrays.asList(
                  "org.opends.server.admin.std.meta.RootCfgDefn",
                  "org.opends.server.admin.std.meta.GlobalCfgDefn"
          );
 
 
  @Test(dataProvider="enumrateManageObjectDefns")
  public void validateConfigObjectDefinitions(AbstractManagedObjectDefinition<?, ?> objectDef) {
    String objName = objectDef.getName();
    StringBuilder errors = new StringBuilder();
    Collection<PropertyDefinition<?>> allDefinitions =
            objectDef.getAllPropertyDefinitions();
 
    LDAPProfile ldapProfile = LDAPProfile.getInstance();
    String ldapObjectclassName = ldapProfile.getObjectClass(objectDef);
    if (ldapObjectclassName == null) {
      errors.append("There is no objectclass definition for configuration object " + objName);
    } else {
      String expectedObjectClass = "ds-cfg-" + objName;
      if (!ldapObjectclassName.equals(expectedObjectClass) &&
          !CLASS_OBJECT_CLASS_EXCEPTIONS.contains(objectDef.getClass().getName())) {
        errors.append("For config object " + objName +
                ", the LDAP objectclass must be " + expectedObjectClass +
                " instead of " + ldapObjectclassName).append(EOL + EOL);
      }
    }
    ObjectClass configObjectClass = DirectoryServer.getSchema().getObjectClass(ldapObjectclassName.toLowerCase());;
 
    for (PropertyDefinition<?> propDef: allDefinitions) {
      validatePropertyDefinition(objectDef, configObjectClass, propDef, errors);
    }
 
    Assert.assertEquals(errors.length(), 0,
        "The configuration definition for " + objectDef.getName() + " has the following problems: " + EOL + errors);
  }
 
  /** Exceptions to properties ending in -class being exactly 'java-class'. */
  private static final List<String> CLASS_PROPERTY_EXCEPTIONS =
          Arrays.asList(
                  // e.g. "prop-name-ending-with-class"
          );
 
  /** Exceptions to properties ending in -enabled being exactly 'enabled'. */
  private static final List<String> ENABLED_PROPERTY_EXCEPTIONS =
          Arrays.asList(
                  "index-filter-analyzer-enabled",
                  "subordinate-indexes-enabled"
                  // e.g. "prop-name-ending-with-enabled"
          );
 
  /** Exceptions to properties not starting with the name of their config object. */
  private static final List<String> OBJECT_PREFIX_PROPERTY_EXCEPTIONS =
          Arrays.asList(
                  "backend-id",
                  "plugin-type",
                  "replication-server-id",
                  "network-group-id",
                  "workflow-id",
                  "workflow-element-id",
                  "workflow-element"
                  // e.g. "prop-name-starting-with-object-prefix"
          );
 
 
  private void validatePropertyDefinition(AbstractManagedObjectDefinition<?, ?> objectDef,
                                          ObjectClass configObjectClass,
                                          PropertyDefinition<?> propDef,
                                          StringBuilder errors) {
    String objName = objectDef.getName();
    String propName = propDef.getName();
 
    // We want class properties to be exactly java-class
    if (propName.endsWith("-class") &&
        !propName.equals("java-class") &&
        !CLASS_PROPERTY_EXCEPTIONS.contains(propName))
    {
      errors.append("The " + propName + " property on config object " + objName +
              " should probably be java-class.  If not, then add " +
              propName + " to the CLASS_PROPERTY_EXCEPTIONS array in " +
              ValidateConfigDefinitionsTest.class.getName() + " to suppress" +
              " this warning.");
    }
 
    // We want enabled properties to be exactly enabled
    if (propName.endsWith("-enabled") && !ENABLED_PROPERTY_EXCEPTIONS.contains(propName))
    {
      errors.append("The " + propName + " property on config object " + objName +
              " should probably be just 'enabled'.  If not, then add " +
              propName + " to the ENABLED_PROPERTY_EXCEPTIONS array in " +
              ValidateConfigDefinitionsTest.class.getName() + " to suppress" +
              " this warning.");
    }
 
    // It's redundant for properties to be prefixed with the name of their objecty
    if (propName.startsWith(objName) && !propName.equals(objName) &&
            !OBJECT_PREFIX_PROPERTY_EXCEPTIONS.contains(propName))
    {
      errors.append("The " + propName + " property on config object " + objName +
              " should not be prefixed with the name of the config object because" +
              " this is redundant.  If you disagree, then add " +
              propName + " to the OBJECT_PREFIX_PROPERTY_EXCEPTIONS array in " +
              ValidateConfigDefinitionsTest.class.getName() + " to suppress" +
              " this warning.");
    }
 
 
    LDAPProfile ldapProfile = LDAPProfile.getInstance();
    String ldapAttrName = ldapProfile.getAttributeName(objectDef, propDef);
 
    // LDAP attribute name is consistent with the property name
    String expectedLdapAttr = "ds-cfg-" + propName;
    if (!ldapAttrName.equals(expectedLdapAttr)) {
      errors.append("For the " + propName + " property on config object " + objName +
              ", the LDAP attribute must be " + expectedLdapAttr + " instead of " + ldapAttrName).append(EOL + EOL);
    }
 
 
    Schema schema = DirectoryServer.getSchema();
 
    // LDAP attribute exists
    if (!schema.hasAttributeType(ldapAttrName))
    {
      errors.append(propName + " property on config object " + objName + " is declared" +
               " to use ldap attribute " + ldapAttrName + ", but this attribute is not in the schema ").append(EOL + EOL);
    } else {
      AttributeType attrType = schema.getAttributeType(ldapAttrName);
 
      // LDAP attribute is multivalued if the property is multivalued
      if (propDef.hasOption(PropertyOption.MULTI_VALUED) && attrType.isSingleValue()) {
        errors.append(propName + " property on config object " + objName + " is declared" +
                 " as multi-valued, but the corresponding ldap attribute " + ldapAttrName +
                 " is declared as single-valued.").append(EOL + EOL);
      }
 
      if (configObjectClass != null) {
        // If it's mandatory in the schema, it must be mandatory on the config property
        Set<AttributeType> mandatoryAttributes = configObjectClass.getRequiredAttributeChain();
        if (mandatoryAttributes.contains(attrType) && !propDef.hasOption(PropertyOption.MANDATORY)) {
          errors.append(propName + " property on config object " + objName + " is not declared" +
                   " as mandatory even though the corresponding ldap attribute " + ldapAttrName +
                   " is declared as mandatory in the schema.").append(EOL + EOL);
        }
 
        Set<AttributeType> allowedAttributes = new HashSet<>(mandatoryAttributes);
        allowedAttributes.addAll(configObjectClass.getOptionalAttributeChain());
        if (!allowedAttributes.contains(attrType)) {
          errors.append(propName + " property on config object " + objName + " has" +
                   " the corresponding ldap attribute " + ldapAttrName +
                   ", but this attribute is not an allowed attribute on the configuration " +
                   " object's objectclass " + configObjectClass.getNameOrOID()).append(EOL + EOL);
        }
      }
    }
  }
}