This is an automated email from the ASF dual-hosted git repository. ofuks pushed a commit to branch DLAB-1571 in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git
commit 1a24f7e5d1db436147c4de9f2401b32d085b1626 Author: Oleh Fuks <[email protected]> AuthorDate: Mon Mar 2 11:52:11 2020 +0200 Added some changes --- services/billing-gcp/pom.xml | 20 +++++++ .../epam/dlab/billing/gcp/conf/SecurityConfig.java | 65 ++++++++++++++++++++++ .../billing-gcp/src/main/resources/application.yml | 15 ++++- .../dlab/backendapi/resources/BillingResource.java | 9 +-- .../service/impl/BillingServiceImplNew.java | 10 ++-- 5 files changed, 107 insertions(+), 12 deletions(-) diff --git a/services/billing-gcp/pom.xml b/services/billing-gcp/pom.xml index 6ae8114..e562089 100644 --- a/services/billing-gcp/pom.xml +++ b/services/billing-gcp/pom.xml @@ -39,6 +39,13 @@ <type>pom</type> <scope>import</scope> </dependency> + <dependency> + <groupId>org.keycloak.bom</groupId> + <artifactId>keycloak-adapter-bom</artifactId> + <version>4.8.3.Final</version> + <type>pom</type> + <scope>import</scope> + </dependency> </dependencies> </dependencyManagement> @@ -57,6 +64,19 @@ <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-security</artifactId> + </dependency> + <dependency> + <groupId>org.keycloak</groupId> + <artifactId>keycloak-spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.keycloak</groupId> + <artifactId>keycloak-spring-security-adapter</artifactId> + <version>4.8.3.Final</version> + </dependency> + <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> diff --git a/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/conf/SecurityConfig.java b/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/conf/SecurityConfig.java new file mode 100644 index 0000000..7e2d5d7 --- /dev/null +++ b/services/billing-gcp/src/main/java/com/epam/dlab/billing/gcp/conf/SecurityConfig.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.epam.dlab.billing.gcp.conf; + +import org.keycloak.adapters.KeycloakConfigResolver; +import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver; +import org.keycloak.adapters.springsecurity.KeycloakConfiguration; +import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider; +import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper; +import org.springframework.security.core.session.SessionRegistryImpl; +import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; +import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; + +@KeycloakConfiguration +class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) { + KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider(); + keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper()); + auth.authenticationProvider(keycloakAuthenticationProvider); + } + + @Bean + public KeycloakConfigResolver KeycloakConfigResolver() { + return new KeycloakSpringBootConfigResolver(); + } + + @Bean + @Override + protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { + return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + super.configure(http); + http + .anonymous().disable() + .authorizeRequests() + .antMatchers("/*").authenticated(); + } +} \ No newline at end of file diff --git a/services/billing-gcp/src/main/resources/application.yml b/services/billing-gcp/src/main/resources/application.yml index aae4af9..cfd2b36 100644 --- a/services/billing-gcp/src/main/resources/application.yml +++ b/services/billing-gcp/src/main/resources/application.yml @@ -1,4 +1,6 @@ spring: + main: + allow-bean-definition-overriding: true data: mongodb: username: admin @@ -24,5 +26,16 @@ server.ssl.key-alias: billing logging: file: /var/opt/dlab/log/ssn/billing.log level: + org: + springframework: + security: DEBUG com: - epam: trace \ No newline at end of file + epam: trace + +keycloak: + bearer-only: true + realm: DLAB_bhliva + resource: sss + credentials.secret: cf5a484b-039b-4161-8707-ad65c0f25962 + ssl-required: none + auth-server-url: http://52.11.45.11:8080/auth \ No newline at end of file diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/BillingResource.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/BillingResource.java index c87f7b2..b6b7932 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/BillingResource.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/resources/BillingResource.java @@ -30,7 +30,6 @@ import org.bson.Document; import javax.validation.Valid; import javax.validation.constraints.NotNull; import javax.ws.rs.Consumes; -import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -58,14 +57,12 @@ public class BillingResource { return billingService.getBillingReport(userInfo, formDTO); } - @GET + @POST @Path("/report2") @Produces(MediaType.APPLICATION_JSON) - public Response getBillingReport2( -// @Auth UserInfo userInfo, - @Valid @NotNull BillingFilter filter + public Response getBillingReport2(@Auth UserInfo userInfo, @Valid @NotNull BillingFilter filter ) { - return Response.ok(billingServiceNew.getBillingReport(null, filter)).build(); + return Response.ok(billingServiceNew.getBillingReport(userInfo, filter)).build(); } @POST diff --git a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplNew.java b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplNew.java index 1eb2e67..19413bf 100644 --- a/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplNew.java +++ b/services/self-service/src/main/java/com/epam/dlab/backendapi/service/impl/BillingServiceImplNew.java @@ -97,7 +97,7 @@ public class BillingServiceImplNew implements BillingServiceNew { .collect(Collectors.toMap(BillingReportDTO::getDlabId, b -> b)); log.debug("Billable resources are: {}", billableResources); - List<BillingReportDTO> billingReport = getRemoteBillingData() + List<BillingReportDTO> billingReport = getRemoteBillingData(userInfo) .stream() .filter(getBillingDataFilter(filter)) .map(bd -> toBillingData(bd, getOrDefault(billableResources, bd.getTag()))) @@ -118,11 +118,11 @@ public class BillingServiceImplNew implements BillingServiceNew { return billableResources.getOrDefault(tag, BillingReportDTO.builder().dlabId(tag).build()); } - private List<BillingData> getRemoteBillingData() { + private List<BillingData> getRemoteBillingData(UserInfo userInfo) { List<EndpointDTO> endpoints = endpointService.getEndpoints(); ExecutorService executor = Executors.newFixedThreadPool(endpoints.size()); List<Callable<List<BillingData>>> callableTasks = new ArrayList<>(); - endpoints.forEach(e -> callableTasks.add(getTask(getBillingUrl(e.getUrl())))); + endpoints.forEach(e -> callableTasks.add(getTask(userInfo, getBillingUrl(e.getUrl())))); List<BillingData> billingData; try { @@ -166,8 +166,8 @@ public class BillingServiceImplNew implements BillingServiceNew { .toString(); } - private Callable<List<BillingData>> getTask(String url) { - return () -> provisioningService.get(url, new GenericType<List<BillingData>>() { + private Callable<List<BillingData>> getTask(UserInfo userInfo, String url) { + return () -> provisioningService.get(url, userInfo.getAccessToken(), new GenericType<List<BillingData>>() { }); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
