Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
feat(settings): move constants.json config to database
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGeraud committed May 4, 2018
1 parent 55b2c87 commit 8613552
Show file tree
Hide file tree
Showing 20 changed files with 1,130 additions and 57 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.management.model.annotations;

import io.gravitee.management.model.parameters.Key;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ParameterKey {
Key value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.management.model.parameters;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
public enum Key {
COMPANY_NAME("company.name"),

PORTAL_TITLE("portal.title"),
PORTAL_ENTRYPOINT("portal.entrypoint"),
PORTAL_APIKEY_HEADER("portal.apikey.header"),
PORTAL_SUPPORT("portal.support.enabled"),
PORTAL_RATING("portal.rating.enabled"),
PORTAL_DEVMODE("portal.devMode.enabled"),
PORTAL_USERCREATION("portal.userCreation.enabled"),
PORTAL_ANALYTICS_TRACKINGID("portal.analytics.trackingId"),
PORTAL_APIS_TILESMODE("portal.apis.tilesMode.enabled"),
PORTAL_DASHBOARD_WIDGETS("portal.dashboard.widgets"),

MANAGEMENT_TITLE("management.title"),

THEME_NAME("theme.name"),
THEME_LOGO("theme.logo"),
THEME_LOADER("theme.loader"),
THEME_CSS("theme.css"),

AUTHENTICATION_FORCELOGIN("authentication.forceLogin.enabled"),
AUTHENTICATION_LOCALLOGIN("authentication.localLogin.enabled"),
AUTHENTICATION_GOOGLE_CLIENTID("authentication.google.clientId"),
AUTHENTICATION_GITHUB_CLIENTID("authentication.github.clientId"),
AUTHENTICATION_OAUTH2_CLIENTID("authentication.oauth2.clientId"),
AUTHENTICATION_OAUTH2_NAME("authentication.oauth2.name"),
AUTHENTICATION_OAUTH2_COLOR("authentication.oauth2.color"),
AUTHENTICATION_OAUTH2_AUTHORIZATION_ENDPOINT("authentication.oauth2.authorization.endpoint"),
AUTHENTICATION_OAUTH2_USER_LOGOUT_ENDPOINT("authentication.oauth2.user.logout.endpoint"),
AUTHENTICATION_OAUTH2_SCOPE("authentication.oauth2.scopes"),

SCHEDULER_TASKS("scheduler.tasks"),
SCHEDULER_NOTIFICATIONS("scheduler.notifications"),

DOCUMENTATION_URL("documentation.url")
;

String key;

Key(String key) {
this.key = key;
}

public String key() {
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public enum PortalPermission implements Permission {
DOCUMENTATION("DOCUMENTATION", 1100),
APPLICATION( "APPLICATION", 1200),
VIEW( "VIEW", 1300),
TOP_APIS( "TOP_APIS", 1400);
TOP_APIS( "TOP_APIS", 1400),
SETTINGS( "SETTINGS", 1500);

String name;
int mask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum RolePermission {
PORTAL_APPLICATION (RoleScope.PORTAL, PortalPermission.APPLICATION),
PORTAL_VIEW (RoleScope.PORTAL, PortalPermission.VIEW),
PORTAL_TOP_APIS (RoleScope.PORTAL, PortalPermission.TOP_APIS),
PORTAL_SETTINGS (RoleScope.PORTAL, PortalPermission.SETTINGS),

API_DEFINITION (RoleScope.API, ApiPermission.DEFINITION),
API_PLAN (RoleScope.API, ApiPermission.PLAN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.gravitee.repository.management.model.Parameter;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -34,6 +35,11 @@ public Optional<Parameter> findById(String s) throws TechnicalException {
return target.findById(s);
}

@Override
public List<Parameter> findAll(List<String> keys) throws TechnicalException {
return target.findAll(keys);
}

@Override
public Parameter create(Parameter item) throws TechnicalException {
return target.create(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.gravitee.management.rest.resource.search.SearchResource;
import io.gravitee.management.security.authentication.AuthenticationProvider;
import io.gravitee.management.security.authentication.AuthenticationProviderManager;
import io.gravitee.management.service.ParameterService;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;
Expand Down Expand Up @@ -107,10 +108,9 @@ private void registerAuthenticationEndpoint(String provider, Class resource) {
Optional<AuthenticationProvider> socialProvider = authenticationProviderManager.findIdentityProviderByType(provider);
if (socialProvider.isPresent()) {
Map<String, Object> configuration = socialProvider.get().configuration();
String clientId = (String) configuration.get("clientId");
String clientSecret = (String) configuration.get("clientSecret");

if (clientId != null && !clientId.isEmpty() && clientSecret != null && !clientSecret.isEmpty()) {
if (clientSecret != null && !clientSecret.isEmpty()) {
register(resource);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
*/
package io.gravitee.management.rest.resource;

import io.gravitee.common.http.MediaType;
import io.gravitee.management.model.PortalConfigEntity;
import io.gravitee.management.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.ws.rs.Path;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

/**
* Defines the REST resources to manage Portal.
Expand All @@ -31,9 +39,32 @@
@Api(tags = {"Portal"})
public class PortalResource {

@Inject
ConfigService configService;

@Context
private ResourceContext resourceContext;


@GET
@Produces(MediaType.APPLICATION_JSON)
public PortalConfigEntity getConfig() {
return configService.getPortalConfig();
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Save the portal configuration")
public Response savePortalConfig(
@ApiParam(name = "config", required = true) @NotNull PortalConfigEntity portalConfigEntity) {
configService.save(portalConfigEntity);
return Response
.ok()
.entity(portalConfigEntity)
.build();
}

@Path("pages")
public PortalPagesResource getPortalPagesResource() {
return resourceContext.getResource(PortalPagesResource.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import io.gravitee.management.security.authentication.AuthenticationProvider;
import io.gravitee.management.security.authentication.AuthenticationProviderManager;
import io.gravitee.management.security.config.BasicSecurityConfigurerAdapter;
import io.gravitee.management.service.ParameterService;
import io.gravitee.management.service.utils.EnvironmentUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;

import javax.inject.Inject;
import java.util.*;

/**
Expand All @@ -36,6 +38,8 @@ public class AuthenticationProviderManagerImpl implements AuthenticationProvider
@Autowired
private ConfigurableEnvironment environment;

@Autowired
ParameterService parameterService;
private static final Logger LOGGER = LoggerFactory.getLogger(BasicSecurityConfigurerAdapter.class);

private static final Set<String> OAUTH2_AUTHENTICATION_PROVIDERS = new HashSet(Arrays.asList("google", "github", "oauth2"));
Expand All @@ -59,7 +63,7 @@ public Optional<AuthenticationProvider> findIdentityProviderByType(String type)
.findFirst();
}

private Map<String, Object> getConfiguration(AuthenticationProvider provider) {
private Map<String, Object> getEnvironmentConfiguration(AuthenticationProvider provider) {
String prefix = "security.providers[" + provider.index() + "].";
Map<String, Object> properties = EnvironmentUtils.getPropertiesStartingWith(environment, prefix);
Map<String, Object> unprefixedProperties = new HashMap<>(properties.size());
Expand All @@ -68,6 +72,16 @@ private Map<String, Object> getConfiguration(AuthenticationProvider provider) {
return unprefixedProperties;
}

private Map<String, Object> getDatabaseConfiguration(AuthenticationProvider provider) {
if (OAUTH2_AUTHENTICATION_PROVIDERS.contains(provider.type())) {
List<String> values = parameterService.findAll("authentication." + provider.type() + ".clientId");
if (values != null && !values.isEmpty()) {
return Collections.singletonMap("clientId", values.get(0));
}
}
return Collections.emptyMap();
}

private void loadAuthenticationProviders() {
LOGGER.debug("Looking for authentication providers...");
identityProviders = new ArrayList<>();
Expand All @@ -86,7 +100,9 @@ private void loadAuthenticationProviders() {
provider = new DefaultAuthenticationProvider(type, idx);
}

provider.setConfiguration(getConfiguration(provider));
Map<String, Object> config = getEnvironmentConfiguration(provider);
config.putAll(getDatabaseConfiguration(provider));
provider.setConfiguration(config);
identityProviders.add(provider);
LOGGER.debug("\tAuthentication provider [{}] has been defined", type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.management.service;

import io.gravitee.management.model.PortalConfigEntity;

/**
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
public interface ConfigService {
PortalConfigEntity getPortalConfig();
void save(PortalConfigEntity portalConfigEntity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@
import io.gravitee.repository.management.model.Parameter;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* @author Azize ELAMRANI (azize at graviteesource.com)
* @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
* @author GraviteeSource Team
*/
public interface ParameterService {


boolean findAsBoolean(String key);
List<String> findAll(String key);
Map<String, List<String>> findAll(List<String> keys);

<T> List<T> findAll(String key, Function<String, T> mapper);
<T> Map<String, List<T>> findAll(List<String> keys, Function<String, T> mapper);

<T> List<T> findAll(String key, Function<String, T> mapper, Predicate<String> filter);
<T> Map<String, List<T>> findAll(List<String> keys, Function<String, T> mapper, Predicate<String> filter);

Parameter create(String key, String value);

Expand Down
Loading

0 comments on commit 8613552

Please sign in to comment.