Search in sources :

Example 1 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class SearchesTest method setUp.

@Before
public void setUp() throws Exception {
    when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(INDEX_RANGES);
    metricRegistry = new MetricRegistry();
    searches = new Searches(new Configuration(), indexRangeService, client, metricRegistry, streamService, mock(Indices.class));
}
Also used : Configuration(org.graylog2.Configuration) MetricRegistry(com.codahale.metrics.MetricRegistry) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) Before(org.junit.Before)

Example 2 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class LdapUserAuthenticator method updateFromLdap.

private void updateFromLdap(User user, LdapEntry userEntry, LdapSettings ldapSettings, String username) {
    final String displayNameAttribute = ldapSettings.getDisplayNameAttribute();
    final String fullName = firstNonNull(userEntry.get(displayNameAttribute), username);
    user.setName(username);
    user.setFullName(fullName);
    user.setExternal(true);
    if (user.getTimeZone() == null) {
        user.setTimeZone(rootTimeZone);
    }
    final String email = userEntry.getEmail();
    if (isNullOrEmpty(email)) {
        LOG.debug("No email address found for user {} in LDAP. Using {}@localhost", username, username);
        user.setEmail(username + "@localhost");
    } else {
        user.setEmail(email);
    }
    // TODO This is a crude hack until we have a proper way to distinguish LDAP users from normal users
    if (isNullOrEmpty(user.getHashedPassword())) {
        ((UserImpl) user).setHashedPassword("User synced from LDAP.");
    }
    // map ldap groups to user roles, if the mapping is present
    final Set<String> translatedRoleIds = Sets.newHashSet(Sets.union(Sets.newHashSet(ldapSettings.getDefaultGroupId()), ldapSettings.getAdditionalDefaultGroupIds()));
    if (!userEntry.getGroups().isEmpty()) {
        // ldap search returned groups, these always override the ones set on the user
        try {
            final Map<String, Role> roleNameToRole = roleService.loadAllLowercaseNameMap();
            for (String ldapGroupName : userEntry.getGroups()) {
                final String roleName = ldapSettings.getGroupMapping().get(ldapGroupName);
                if (roleName == null) {
                    LOG.debug("User {}: No group mapping for ldap group <{}>", username, ldapGroupName);
                    continue;
                }
                final Role role = roleNameToRole.get(roleName.toLowerCase(Locale.ENGLISH));
                if (role != null) {
                    LOG.debug("User {}: Mapping ldap group <{}> to role <{}>", username, ldapGroupName, role.getName());
                    translatedRoleIds.add(role.getId());
                } else {
                    LOG.warn("User {}: No role found for ldap group <{}>", username, ldapGroupName);
                }
            }
        } catch (NotFoundException e) {
            LOG.error("Unable to load user roles", e);
        }
    } else if (ldapSettings.getGroupMapping().isEmpty() || ldapSettings.getGroupSearchBase().isEmpty() || ldapSettings.getGroupSearchPattern().isEmpty() || ldapSettings.getGroupIdAttribute().isEmpty()) {
        // no group mapping or configuration set, we'll leave the previously set groups alone on sync
        // when first creating the user these will be empty
        translatedRoleIds.addAll(user.getRoleIds());
    }
    user.setRoleIds(translatedRoleIds);
    // preserve the raw permissions (the ones without the synthetic self-edit permissions or the "*" admin one)
    user.setPermissions(user.getPermissions());
}
Also used : Role(org.graylog2.shared.users.Role) UserImpl(org.graylog2.users.UserImpl) NotFoundException(org.graylog2.database.NotFoundException)

Example 3 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class LdapResource method testLdapConfiguration.

@POST
@Timed
@RequiresPermissions(RestPermissions.LDAP_EDIT)
@ApiOperation("Test LDAP Configuration")
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NoAuditEvent("only used to test LDAP configuration")
public LdapTestConfigResponse testLdapConfiguration(@ApiParam(name = "Configuration to test", required = true) @Valid @NotNull LdapTestConfigRequest request) {
    final LdapConnectionConfig config = new LdapConnectionConfig();
    final URI ldapUri = request.ldapUri();
    config.setLdapHost(ldapUri.getHost());
    config.setLdapPort(ldapUri.getPort());
    config.setUseSsl(ldapUri.getScheme().startsWith("ldaps"));
    config.setUseTls(request.useStartTls());
    if (request.trustAllCertificates()) {
        config.setTrustManagers(new TrustAllX509TrustManager());
    }
    if (!isNullOrEmpty(request.systemUsername()) && !isNullOrEmpty(request.systemPassword())) {
        config.setName(request.systemUsername());
        config.setCredentials(request.systemPassword());
    }
    LdapNetworkConnection connection = null;
    try {
        try {
            connection = ldapConnector.connect(config);
        } catch (LdapException e) {
            return LdapTestConfigResponse.create(false, false, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet(), e.getMessage());
        }
        if (null == connection) {
            return LdapTestConfigResponse.create(false, false, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet(), "Could not connect to LDAP server");
        }
        boolean connected = connection.isConnected();
        boolean systemAuthenticated = connection.isAuthenticated();
        // the web interface allows testing the connection only, in that case we can bail out early.
        if (request.testConnectOnly()) {
            return LdapTestConfigResponse.create(connected, systemAuthenticated, false, Collections.<String, String>emptyMap(), Collections.<String>emptySet());
        }
        String userPrincipalName = null;
        boolean loginAuthenticated = false;
        Map<String, String> entryMap = Collections.emptyMap();
        String exception = null;
        Set<String> groups = Collections.emptySet();
        try {
            final LdapEntry entry = ldapConnector.search(connection, request.searchBase(), request.searchPattern(), "*", request.principal(), request.activeDirectory(), request.groupSearchBase(), request.groupIdAttribute(), request.groupSearchPattern());
            if (entry != null) {
                userPrincipalName = entry.getBindPrincipal();
                entryMap = entry.getAttributes();
                groups = entry.getGroups();
            }
        } catch (CursorException | LdapException e) {
            exception = e.getMessage();
        }
        try {
            loginAuthenticated = ldapConnector.authenticate(connection, userPrincipalName, request.password());
        } catch (Exception e) {
            exception = e.getMessage();
        }
        return LdapTestConfigResponse.create(connected, systemAuthenticated, loginAuthenticated, entryMap, groups, exception);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                LOG.warn("Unable to close LDAP connection.", e);
            }
        }
    }
}
Also used : LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapEntry(org.graylog2.shared.security.ldap.LdapEntry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) IOException(java.io.IOException) TrustAllX509TrustManager(org.graylog2.security.TrustAllX509TrustManager) URI(java.net.URI) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) ValidationException(org.graylog2.plugin.database.ValidationException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 4 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class RotationStrategyResource method config.

@PUT
@Path("config")
@Consumes(MediaType.APPLICATION_JSON)
@Timed
@ApiOperation(value = "Configuration of the current rotation strategy", notes = "This resource stores the configuration of the currently used rotation strategy.")
@AuditEvent(type = AuditEventTypes.ES_INDEX_ROTATION_STRATEGY_UPDATE)
public RotationStrategySummary config(@ApiParam(value = "The description of the rotation strategy and its configuration", required = true) @Valid @NotNull RotationStrategySummary rotationStrategySummary) {
    if (!rotationStrategies.containsKey(rotationStrategySummary.strategy())) {
        throw new NotFoundException("Couldn't find rotation strategy for given type " + rotationStrategySummary.strategy());
    }
    final IndexManagementConfig oldConfig = clusterConfigService.get(IndexManagementConfig.class);
    if (oldConfig == null) {
        throw new InternalServerErrorException("Couldn't retrieve index management configuration");
    }
    final IndexManagementConfig indexManagementConfig = IndexManagementConfig.create(rotationStrategySummary.strategy(), oldConfig.retentionStrategy());
    clusterConfigService.write(rotationStrategySummary.config());
    clusterConfigService.write(indexManagementConfig);
    return rotationStrategySummary;
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IndexManagementConfig(org.graylog2.indexer.management.IndexManagementConfig) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 5 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class RotationStrategyResource method config.

@GET
@Path("config")
@Timed
@ApiOperation(value = "Configuration of the current rotation strategy", notes = "This resource returns the configuration of the currently used rotation strategy.")
public RotationStrategySummary config() {
    final IndexManagementConfig indexManagementConfig = clusterConfigService.get(IndexManagementConfig.class);
    if (indexManagementConfig == null) {
        throw new InternalServerErrorException("Couldn't retrieve index management configuration");
    }
    final String strategyName = indexManagementConfig.rotationStrategy();
    final Provider<RotationStrategy> provider = rotationStrategies.get(strategyName);
    if (provider == null) {
        throw new InternalServerErrorException("Couldn't retrieve rotation strategy provider");
    }
    final RotationStrategy rotationStrategy = provider.get();
    @SuppressWarnings("unchecked") final Class<RotationStrategyConfig> configClass = (Class<RotationStrategyConfig>) rotationStrategy.configurationClass();
    final RotationStrategyConfig config = clusterConfigService.get(configClass);
    return RotationStrategySummary.create(strategyName, config);
}
Also used : RotationStrategy(org.graylog2.plugin.indexer.rotation.RotationStrategy) RotationStrategyConfig(org.graylog2.plugin.indexer.rotation.RotationStrategyConfig) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) IndexManagementConfig(org.graylog2.indexer.management.IndexManagementConfig) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Test (org.junit.Test)67 Configuration (org.graylog2.plugin.configuration.Configuration)46 Configuration (org.apache.commons.configuration2.Configuration)38 ApiOperation (io.swagger.annotations.ApiOperation)31 Timed (com.codahale.metrics.annotation.Timed)23 AuditEvent (org.graylog2.audit.jersey.AuditEvent)23 Path (javax.ws.rs.Path)22 BadRequestException (javax.ws.rs.BadRequestException)21 MessageInput (org.graylog2.plugin.inputs.MessageInput)18 File (java.io.File)17 ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)17 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)15 Stream (org.graylog2.plugin.streams.Stream)15 MidpointConfiguration (com.evolveum.midpoint.common.configuration.api.MidpointConfiguration)14 List (java.util.List)14 DateTime (org.joda.time.DateTime)14 Produces (javax.ws.rs.Produces)13 Configuration (org.graylog2.Configuration)13 ApiResponses (io.swagger.annotations.ApiResponses)12 IOException (java.io.IOException)12