Exemplo n.º 1
0
 private List<Object> getValuesSorted(final ConnectorObject resource, final String field) {
   final Attribute value = AttributeUtil.find(field, resource.getAttributes());
   if (value == null || value.getValue() == null || value.getValue().isEmpty()) {
     return Collections.emptyList();
   } else if (value.getValue().size() > 1) {
     List<Object> results = new ArrayList<Object>(value.getValue());
     Collections.sort(results, VALUE_COMPARATOR);
     return results;
   } else {
     return value.getValue();
   }
 }
Exemplo n.º 2
0
 private Map<String, MdObjectAttribute> getAttributeChanges(Set<Attribute> attributes) {
   Map<String, MdObjectAttribute> changes = new HashMap<String, MdObjectAttribute>();
   for (Attribute attr : attributes) {
     if (!attr.getValue().isEmpty()) {
       MdObjectAttribute a =
           new MdObjectAttribute(
               attr.getName(), MetadataState.LOCAL, attr.getValue().iterator().next().toString());
       changes.put(attr.getName(), a);
     } else {
       MdObjectAttribute a = new MdObjectAttribute(attr.getName(), MetadataState.LOCAL, null);
       changes.put(attr.getName(), a);
     }
   }
   return changes;
 }
Exemplo n.º 3
0
  @Override
  public SolarisEntry buildAccountEntry(String username, Set<NativeAttribute> attrsToGet) {
    /**
     * bunch of boolean flags says if the command is needed to be launched (based on attributes to
     * get)
     */
    boolean isLogins = LoginsCommand.isLoginsRequired(attrsToGet);
    boolean isProfiles = attrsToGet.contains(NativeAttribute.PROFILES);
    boolean isAuths = attrsToGet.contains(NativeAttribute.AUTHS);
    boolean isLast = attrsToGet.contains(NativeAttribute.LAST_LOGIN);
    boolean isRoles = attrsToGet.contains(NativeAttribute.ROLES);

    // if (conn.isNis()) {
    // return buildNISUser(username);
    // }
    SolarisEntry.Builder entryBuilder =
        new SolarisEntry.Builder(username).addAttr(NativeAttribute.NAME, username);

    // we need to execute Logins command always, to figure out if the user
    // exists at all.
    SolarisEntry loginsEntry = LoginsCommand.getAttributesFor(username, conn);

    // Null indicates that the user was not found.
    if (loginsEntry == null) {
      return null;
    }

    if (isLogins) {
      entryBuilder.addAllAttributesFrom(loginsEntry);
    }
    if (isProfiles) {
      final Attribute profiles = ProfilesCommand.getProfilesAttributeFor(username, conn);
      entryBuilder.addAttr(NativeAttribute.PROFILES, profiles.getValue());
    }
    if (isAuths) {
      final Attribute auths = AuthsCommand.getAuthsAttributeFor(username, conn);
      entryBuilder.addAttr(NativeAttribute.AUTHS, auths.getValue());
    }
    if (isLast) {
      final Attribute last = LastCommand.getLastAttributeFor(username, conn);
      entryBuilder.addAttr(NativeAttribute.LAST_LOGIN, last.getValue());
    }
    if (isRoles) {
      final Attribute roles = RolesCommand.getRolesAttributeFor(username, conn);
      entryBuilder.addAttr(NativeAttribute.ROLES, roles.getValue());
    }
    return entryBuilder.build();
  }
 private void displayUser(ConnectorObject user) {
   Set<Attribute> attributes = user.getAttributes();
   for (Attribute attribute : attributes) {
     System.out.println(attribute.getName());
     List<Object> values = attribute.getValue();
     for (Object value : values) {
       System.out.println("    " + value.getClass().getName() + ":" + value);
     }
   }
 }
Exemplo n.º 5
0
  private Uid doUpdate() throws IOException, JSchException {

    if (uid == null || StringUtil.isBlank(uid.getUidValue())) {
      throw new IllegalArgumentException("No Uid attribute provided in the attributes");
    }

    LOG.info("Update user: "******"Wrong object class");
    }

    if (objectClass.equals(ObjectClass.ACCOUNT)) {
      if (!EvaluateCommandsResultOutput.evaluateUserOrGroupExists(
          unixConnection.execute(
              UnixConnector.getCommandGenerator().userExists(uid.getUidValue())))) {
        throw new ConnectorException("User " + uid + " do not exists");
      }
      for (Attribute attr : attrs) {
        if (attr.is(Name.NAME) || attr.is(Uid.NAME)) {
          newUserName = (String) attr.getValue().get(0);
        } else if (attr.is(OperationalAttributes.PASSWORD_NAME)) {
          password = Utilities.getPlainPassword((GuardedString) attr.getValue().get(0));
        } else if (attr.is(OperationalAttributes.ENABLE_NAME)) {
          status = Boolean.parseBoolean(attr.getValue().get(0).toString());
        } else if (attr.is(configuration.getCommentAttribute())) {
          comment = attr.getValue().get(0).toString();
        } else if (attr.is(configuration.getShellAttribute())) {
          shell = (String) attr.getValue().get(0).toString();
        } else if (attr.is(configuration.getHomeDirectoryAttribute())) {
          homeDirectory = (String) attr.getValue().get(0).toString();
        }
      }
      unixConnection.execute(
          UnixConnector.getCommandGenerator()
              .updateUser(
                  uid.getUidValue(), newUserName, password, status, comment, shell, homeDirectory));
      //            unixConnection.execute("mv /home/" + uid.getUidValue() + " /home/" +
      // newUserName);
      unixConnection.execute(
          UnixConnector.getCommandGenerator().moveHomeDirectory(uid.getUidValue(), newUserName));
      if (!status) {
        unixConnection.execute(UnixConnector.getCommandGenerator().lockUser(uid.getUidValue()));
      } else {
        unixConnection.execute(UnixConnector.getCommandGenerator().unlockUser(uid.getUidValue()));
      }
      if (StringUtil.isNotBlank(newUserName) && StringUtil.isNotEmpty(newUserName)) {
        unixConnection.execute(
            UnixConnector.getCommandGenerator().updateGroup(uid.getUidValue(), newUserName));
      }
    } else if (objectClass.equals(ObjectClass.GROUP)) {
      if (!EvaluateCommandsResultOutput.evaluateUserOrGroupExists(
          unixConnection.execute(UnixConnector.getCommandGenerator().groupExists(newUserName)))) {
        throw new ConnectorException("Group do not exists");
      }
      unixConnection.execute(
          UnixConnector.getCommandGenerator().updateGroup(uid.getUidValue(), newUserName));
    }
    return uid;
  }
Exemplo n.º 6
0
  @Override
  public void serialize(
      final Attribute source, final JsonGenerator jgen, final SerializerProvider sp)
      throws IOException {

    jgen.writeStartObject();

    jgen.writeStringField("name", source.getName());

    jgen.writeFieldName("value");
    if (source.getValue() == null) {
      jgen.writeNull();
    } else {
      jgen.writeStartArray();
      for (Object value : source.getValue()) {
        if (value == null) {
          jgen.writeNull();
        } else if (value instanceof GuardedString) {
          jgen.writeObject(value);
        } else if (value instanceof Integer) {
          jgen.writeNumber((Integer) value);
        } else if (value instanceof Long) {
          jgen.writeNumber((Long) value);
        } else if (value instanceof Double) {
          jgen.writeNumber((Double) value);
        } else if (value instanceof Boolean) {
          jgen.writeBoolean((Boolean) value);
        } else if (value instanceof byte[]) {
          jgen.writeString(BYTE_ARRAY_PREFIX + Base64.encode((byte[]) value) + BYTE_ARRAY_SUFFIX);
        } else {
          jgen.writeString(value.toString());
        }
      }
      jgen.writeEndArray();
    }

    jgen.writeEndObject();
  }
  @Transactional(readOnly = true)
  @Override
  public void before(final PropagationTask task, final ConnectorObject beforeObj) {
    super.before(task, beforeObj);

    Provision provision = task.getResource().getProvision(anyTypeDAO.findGroup());
    if (AnyTypeKind.USER == task.getAnyTypeKind() && provision.getMapping() != null) {
      User user = userDAO.find(task.getAnyKey());
      if (user != null) {
        List<String> groupConnObjectLinks = new ArrayList<>();
        for (Group group : userDAO.findAllGroups(user)) {
          if (group.getResourceNames().contains(task.getResource().getKey())
              && StringUtils.isNotBlank(provision.getMapping().getConnObjectLink())) {

            LOG.debug("Evaluating connObjectLink for {}", group);

            JexlContext jexlContext = new MapContext();
            JexlUtils.addFieldsToContext(group, jexlContext);
            JexlUtils.addPlainAttrsToContext(group.getPlainAttrs(), jexlContext);
            JexlUtils.addDerAttrsToContext(group, jexlContext);

            String groupConnObjectLinkLink =
                JexlUtils.evaluate(provision.getMapping().getConnObjectLink(), jexlContext);
            LOG.debug("ConnObjectLink for {} is '{}'", group, groupConnObjectLinkLink);
            if (StringUtils.isNotBlank(groupConnObjectLinkLink)) {
              groupConnObjectLinks.add(groupConnObjectLinkLink);
            }
          }
        }
        LOG.debug("Group connObjectLinks to propagate for membership: {}", groupConnObjectLinks);

        Set<Attribute> attributes = new HashSet<>(task.getAttributes());

        Set<String> groups = new HashSet<>(groupConnObjectLinks);
        Attribute ldapGroups = AttributeUtil.find(getGroupMembershipAttrName(), attributes);

        if (ldapGroups != null) {
          for (Object obj : ldapGroups.getValue()) {
            groups.add(obj.toString());
          }
        }

        attributes.add(AttributeBuilder.build(getGroupMembershipAttrName(), groups));
        task.setAttributes(attributes);
      }
    } else {
      LOG.debug("Not about user, or group mapping missing for resource: not doing anything");
    }
  }
Exemplo n.º 8
0
  /**
   * build user based on the content given.
   *
   * @param loginsLine
   * @param lastLoginLine
   * @return the build user.
   */
  private SolarisEntry buildUser(String username, String loginsLine, String lastLoginLine) {
    if (lastLoginLine == null) {
      return LoginsCommand.getEntry(loginsLine, username);
    } else {
      SolarisEntry.Builder entryBuilder =
          new SolarisEntry.Builder(username).addAttr(NativeAttribute.NAME, username);
      // logins
      SolarisEntry entry = LoginsCommand.getEntry(loginsLine, username);
      entryBuilder.addAllAttributesFrom(entry);

      // last
      Attribute attribute = LastCommand.parseOutput(username, lastLoginLine);
      entryBuilder.addAttr(NativeAttribute.LAST_LOGIN, attribute.getValue());

      return entryBuilder.build();
    }
  }
 static <O, T> List<T> castList(Attribute attr, Class<T> resultType) {
   if (attr == null) {
     return Collections.emptyList();
   }
   return castList(attr.getValue(), resultType);
 }
Exemplo n.º 10
0
  private Map<VirSchema, List<String>> getValues(final Any<?> any, final Set<VirSchema> schemas) {
    Collection<? extends ExternalResource> ownedResources =
        anyUtilsFactory.getInstance(any).getAllResources(any);

    Map<VirSchema, List<String>> result = new HashMap<>();

    Map<Provision, Set<VirSchema>> toRead = new HashMap<>();

    for (VirSchema schema : schemas) {
      if (ownedResources.contains(schema.getProvision().getResource())) {
        VirAttrCacheValue virAttrCacheValue =
            virAttrCache.get(any.getType().getKey(), any.getKey(), schema.getKey());

        if (virAttrCache.isValidEntry(virAttrCacheValue)) {
          LOG.debug("Values for {} found in cache: {}", schema, virAttrCacheValue);
          result.put(schema, virAttrCacheValue.getValues());
        } else {
          Set<VirSchema> schemasToRead = toRead.get(schema.getProvision());
          if (schemasToRead == null) {
            schemasToRead = new HashSet<>();
            toRead.put(schema.getProvision(), schemasToRead);
          }
          schemasToRead.add(schema);
        }
      } else {
        LOG.debug(
            "Not considering {} since {} is not assigned to {}",
            schema,
            any,
            schema.getProvision().getResource());
      }
    }

    for (Map.Entry<Provision, Set<VirSchema>> entry : toRead.entrySet()) {
      LOG.debug("About to read from {}: {}", entry.getKey(), entry.getValue());

      String connObjectKey =
          MappingUtils.getConnObjectKeyItem(entry.getKey()) == null
              ? null
              : mappingManager.getConnObjectKeyValue(any, entry.getKey());
      if (StringUtils.isBlank(connObjectKey)) {
        LOG.error("No ConnObjectKey found for {}, ignoring...", entry.getKey());
      } else {
        Set<MappingItem> linkingMappingItems = new HashSet<>();
        for (VirSchema schema : entry.getValue()) {
          linkingMappingItems.add(schema.asLinkingMappingItem());
        }

        Connector connector = connFactory.getConnector(entry.getKey().getResource());
        try {
          ConnectorObject connectorObject =
              connector.getObject(
                  entry.getKey().getObjectClass(),
                  new Uid(connObjectKey),
                  MappingUtils.buildOperationOptions(linkingMappingItems.iterator()));

          if (connectorObject == null) {
            LOG.debug("No read from {} about {}", entry.getKey(), connObjectKey);
          } else {
            for (VirSchema schema : entry.getValue()) {
              Attribute attr = connectorObject.getAttributeByName(schema.getExtAttrName());
              if (attr != null) {
                VirAttrCacheValue virAttrCacheValue = new VirAttrCacheValue();
                virAttrCacheValue.setValues(attr.getValue());
                virAttrCache.put(
                    any.getType().getKey(), any.getKey(), schema.getKey(), virAttrCacheValue);
                LOG.debug("Values for {} set in cache: {}", schema, virAttrCacheValue);

                result.put(schema, virAttrCacheValue.getValues());
              }
            }
          }
        } catch (Exception e) {
          LOG.error("Error reading from {}", entry.getKey(), e);
        }
      }
    }

    return result;
  }