use of com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule in project ldapsdk by pingidentity.
the class ScrambleAttributeTransformation method scrambleAttribute.
/**
* Creates a copy of the provided attribute with its values scrambled if
* appropriate.
*
* @param a The attribute to scramble.
*
* @return A copy of the provided attribute with its values scrambled, or
* the original attribute if no scrambling should be performed.
*/
@Nullable()
public Attribute scrambleAttribute(@NotNull final Attribute a) {
if ((a == null) || (a.size() == 0)) {
return a;
}
final String baseName = StaticUtils.toLowerCase(a.getBaseName());
final MatchingRule matchingRule = attributes.get(baseName);
if (matchingRule == null) {
return a;
}
if (matchingRule instanceof BooleanMatchingRule) {
// results. We will just pick boolean values at random.
if (a.size() == 1) {
return new Attribute(a.getName(), schema, ThreadLocalRandom.get().nextBoolean() ? "TRUE" : "FALSE");
} else {
// regardless of how many values the provided attribute actually had.
return new Attribute(a.getName(), schema, "TRUE", "FALSE");
}
} else if (matchingRule instanceof DistinguishedNameMatchingRule) {
final String[] originalValues = a.getValues();
final String[] scrambledValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
try {
scrambledValues[i] = scrambleDN(new DN(originalValues[i])).toString();
} catch (final Exception e) {
Debug.debugException(e);
scrambledValues[i] = scrambleString(originalValues[i]);
}
}
return new Attribute(a.getName(), schema, scrambledValues);
} else if (matchingRule instanceof GeneralizedTimeMatchingRule) {
final String[] originalValues = a.getValues();
final String[] scrambledValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
scrambledValues[i] = scrambleGeneralizedTime(originalValues[i]);
}
return new Attribute(a.getName(), schema, scrambledValues);
} else if ((matchingRule instanceof IntegerMatchingRule) || (matchingRule instanceof NumericStringMatchingRule) || (matchingRule instanceof TelephoneNumberMatchingRule)) {
final String[] originalValues = a.getValues();
final String[] scrambledValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
scrambledValues[i] = scrambleNumericValue(originalValues[i]);
}
return new Attribute(a.getName(), schema, scrambledValues);
} else if (matchingRule instanceof OctetStringMatchingRule) {
// If the target attribute is userPassword, then treat it like an encoded
// password.
final byte[][] originalValues = a.getValueByteArrays();
final byte[][] scrambledValues = new byte[originalValues.length][];
for (int i = 0; i < originalValues.length; i++) {
if (baseName.equals("userpassword") || baseName.equals("2.5.4.35")) {
scrambledValues[i] = StaticUtils.getBytes(scrambleEncodedPassword(StaticUtils.toUTF8String(originalValues[i])));
} else {
scrambledValues[i] = scrambleBinaryValue(originalValues[i]);
}
}
return new Attribute(a.getName(), schema, scrambledValues);
} else {
final String[] originalValues = a.getValues();
final String[] scrambledValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
if (baseName.equals("userpassword") || baseName.equals("2.5.4.35") || baseName.equals("authpassword") || baseName.equals("1.3.6.1.4.1.4203.1.3.4")) {
scrambledValues[i] = scrambleEncodedPassword(originalValues[i]);
} else if (originalValues[i].startsWith("{") && originalValues[i].endsWith("}")) {
scrambledValues[i] = scrambleJSONObject(originalValues[i]);
} else {
scrambledValues[i] = scrambleString(originalValues[i]);
}
}
return new Attribute(a.getName(), schema, scrambledValues);
}
}
use of com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule in project ldapsdk by pingidentity.
the class RedactAttributeTransformation method transformEntry.
/**
* {@inheritDoc}
*/
@Override()
@Nullable()
public Entry transformEntry(@NotNull final Entry e) {
if (e == null) {
return null;
}
// If we should process entry DNs, then see if the DN contains any of the
// target attributes.
final String newDN;
if (redactDNAttributes) {
newDN = redactDN(e.getDN());
} else {
newDN = e.getDN();
}
// Create a copy of the entry with all appropriate attributes redacted.
final Collection<Attribute> originalAttributes = e.getAttributes();
final ArrayList<Attribute> newAttributes = new ArrayList<>(originalAttributes.size());
for (final Attribute a : originalAttributes) {
final String baseName = StaticUtils.toLowerCase(a.getBaseName());
if (attributes.contains(baseName)) {
if (preserveValueCount && (a.size() > 1)) {
final ASN1OctetString[] values = new ASN1OctetString[a.size()];
for (int i = 0; i < values.length; i++) {
values[i] = new ASN1OctetString("***REDACTED" + (i + 1) + "***");
}
newAttributes.add(new Attribute(a.getName(), values));
} else {
newAttributes.add(new Attribute(a.getName(), "***REDACTED***"));
}
} else if (redactDNAttributes && (schema != null) && (MatchingRule.selectEqualityMatchingRule(baseName, schema) instanceof DistinguishedNameMatchingRule)) {
final String[] originalValues = a.getValues();
final String[] newValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
newValues[i] = redactDN(originalValues[i]);
}
newAttributes.add(new Attribute(a.getName(), schema, newValues));
} else {
newAttributes.add(a);
}
}
return new Entry(newDN, schema, newAttributes);
}
use of com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule in project ldapsdk by pingidentity.
the class RedactAttributeTransformation method transformChangeRecord.
/**
* {@inheritDoc}
*/
@Override()
@Nullable()
public LDIFChangeRecord transformChangeRecord(@NotNull final LDIFChangeRecord r) {
if (r == null) {
return null;
}
// entry.
if (r instanceof LDIFAddChangeRecord) {
final LDIFAddChangeRecord addRecord = (LDIFAddChangeRecord) r;
return new LDIFAddChangeRecord(transformEntry(addRecord.getEntryToAdd()), addRecord.getControls());
}
// that we might need to redact.
if (r instanceof LDIFDeleteChangeRecord) {
if (redactDNAttributes) {
final LDIFDeleteChangeRecord deleteRecord = (LDIFDeleteChangeRecord) r;
return new LDIFDeleteChangeRecord(redactDN(deleteRecord.getDN()), deleteRecord.getControls());
} else {
return r;
}
}
// If it's a modify change record, then redact all appropriate values.
if (r instanceof LDIFModifyChangeRecord) {
final LDIFModifyChangeRecord modifyRecord = (LDIFModifyChangeRecord) r;
final String newDN;
if (redactDNAttributes) {
newDN = redactDN(modifyRecord.getDN());
} else {
newDN = modifyRecord.getDN();
}
final Modification[] originalMods = modifyRecord.getModifications();
final Modification[] newMods = new Modification[originalMods.length];
for (int i = 0; i < originalMods.length; i++) {
// If the modification doesn't have any values, then just use the
// original modification.
final Modification m = originalMods[i];
if (!m.hasValue()) {
newMods[i] = m;
continue;
}
// See if the modification targets an attribute that we should redact.
// If not, then see if the attribute has a DN syntax.
final String attrName = StaticUtils.toLowerCase(Attribute.getBaseName(m.getAttributeName()));
if (!attributes.contains(attrName)) {
if (redactDNAttributes && (schema != null) && (MatchingRule.selectEqualityMatchingRule(attrName, schema) instanceof DistinguishedNameMatchingRule)) {
final String[] originalValues = m.getValues();
final String[] newValues = new String[originalValues.length];
for (int j = 0; j < originalValues.length; j++) {
newValues[j] = redactDN(originalValues[j]);
}
newMods[i] = new Modification(m.getModificationType(), m.getAttributeName(), newValues);
} else {
newMods[i] = m;
}
continue;
}
// Get the original values. If there's only one of them, or if we
// shouldn't preserve the original number of values, then just create a
// modification with a single value. Otherwise, create a modification
// with the appropriate number of values.
final ASN1OctetString[] originalValues = m.getRawValues();
if (preserveValueCount && (originalValues.length > 1)) {
final ASN1OctetString[] newValues = new ASN1OctetString[originalValues.length];
for (int j = 0; j < originalValues.length; j++) {
newValues[j] = new ASN1OctetString("***REDACTED" + (j + 1) + "***");
}
newMods[i] = new Modification(m.getModificationType(), m.getAttributeName(), newValues);
} else {
newMods[i] = new Modification(m.getModificationType(), m.getAttributeName(), "***REDACTED***");
}
}
return new LDIFModifyChangeRecord(newDN, newMods, modifyRecord.getControls());
}
// superior DN contain anything that we might need to redact.
if (r instanceof LDIFModifyDNChangeRecord) {
if (redactDNAttributes) {
final LDIFModifyDNChangeRecord modDNRecord = (LDIFModifyDNChangeRecord) r;
return new LDIFModifyDNChangeRecord(redactDN(modDNRecord.getDN()), redactDN(modDNRecord.getNewRDN()), modDNRecord.deleteOldRDN(), redactDN(modDNRecord.getNewSuperiorDN()), modDNRecord.getControls());
} else {
return r;
}
}
// We should never get here.
return r;
}
use of com.unboundid.ldap.matchingrules.DistinguishedNameMatchingRule in project ldapsdk by pingidentity.
the class RenameAttributeTransformation method transformEntry.
/**
* {@inheritDoc}
*/
@Override()
@Nullable()
public Entry transformEntry(@NotNull final Entry e) {
if (e == null) {
return null;
}
final String newDN;
if (renameInDNs) {
newDN = replaceDN(e.getDN());
} else {
newDN = e.getDN();
}
// Iterate through the attributes in the entry and make any appropriate name
// replacements.
final Collection<Attribute> originalAttributes = e.getAttributes();
final ArrayList<Attribute> newAttributes = new ArrayList<>(originalAttributes.size());
for (final Attribute a : originalAttributes) {
// Determine if we we should rename this attribute.
final String newName;
final String baseName = StaticUtils.toLowerCase(a.getBaseName());
if (baseSourceNames.contains(baseName)) {
if (a.hasOptions()) {
final StringBuilder buffer = new StringBuilder();
buffer.append(baseTargetName);
for (final String option : a.getOptions()) {
buffer.append(';');
buffer.append(option);
}
newName = buffer.toString();
} else {
newName = baseTargetName;
}
} else {
newName = a.getName();
}
// If we should rename attributes in entry DNs, then see if this
// attribute has a DN syntax and if so then process its values.
final String[] newValues;
if (renameInDNs && (schema != null) && (MatchingRule.selectEqualityMatchingRule(baseName, schema) instanceof DistinguishedNameMatchingRule)) {
final String[] originalValues = a.getValues();
newValues = new String[originalValues.length];
for (int i = 0; i < originalValues.length; i++) {
newValues[i] = replaceDN(originalValues[i]);
}
} else {
newValues = a.getValues();
}
newAttributes.add(new Attribute(newName, schema, newValues));
}
return new Entry(newDN, newAttributes);
}
Aggregations