From c2acf6a3b044e82dbd62ca9ab1db4d990a64483a Mon Sep 17 00:00:00 2001 From: madhusudhan Date: Wed, 12 Aug 2020 18:21:45 +0530 Subject: [PATCH] Api for group call --- docker_build/gate.yml | 1 + .../OpsmxPlatformController.groovy | 10 +++++++ .../controllers/TokenAuthController.groovy | 30 +++++++++++++++++-- .../internal/OpsmxPlatformService.groovy | 7 +++++ .../spinnaker/gate/util/OesRestApi.java | 18 +++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 gate-web/src/main/java/com/netflix/spinnaker/gate/util/OesRestApi.java diff --git a/docker_build/gate.yml b/docker_build/gate.yml index d8770216d4..c5fe1609ea 100644 --- a/docker_build/gate.yml +++ b/docker_build/gate.yml @@ -7,6 +7,7 @@ services: enabled: true platform: baseUrl: http://localhost:8095 + groupPath: /platformservice/v1/users/{username}/userGroups/importAndCache enabled: true security: basic: diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/OpsmxPlatformController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/OpsmxPlatformController.groovy index 465cf1f0a4..8ecb4a5503 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/OpsmxPlatformController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/OpsmxPlatformController.groovy @@ -191,4 +191,14 @@ class OpsmxPlatformController { return opsmxPlatformService.updatePlatformResponse1(version, type, source, data) } + @ApiOperation(value = "Endpoint for platform rest services") + @RequestMapping(value = "/{version}/{type}/{source}/{source1}/{source2}", method = RequestMethod.PUT) + Object updatePlatformResponse2(@PathVariable("version") String version, + @PathVariable("type") String type, + @PathVariable("source") String source, + @PathVariable("source1") String source1, + @PathVariable("source2") String source2) { + + return opsmxPlatformService.updatePlatformResponse2(version, type, source, source1, source2) + } } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/TokenAuthController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/TokenAuthController.groovy index 667724fa9f..ec03b9c73e 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/TokenAuthController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/TokenAuthController.groovy @@ -4,9 +4,12 @@ import com.netflix.spinnaker.gate.config.AuthenticationRequest import com.netflix.spinnaker.gate.config.AuthenticationResponse import com.netflix.spinnaker.gate.config.JwtUtil import com.netflix.spinnaker.gate.services.UserDataService +import com.netflix.spinnaker.gate.util.OesRestApi import io.swagger.annotations.ApiOperation import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression +import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.BadCredentialsException @@ -30,6 +33,15 @@ class TokenAuthController { @Autowired AuthenticationManager authenticationManager + @Value('${services.platform.enabled:false}') + boolean isPlatformEnabled; + + @Value('${services.platform.baseUrl:null}') + String url; + + @Value('${services.platform.groupPath:null}') + String apiPath; + @ApiOperation(value = "New Login for Jwt") @RequestMapping(value = "/login", method = RequestMethod.POST) public ResponseEntity authenticateUser(@RequestBody AuthenticationRequest authenticationRequest ) { @@ -45,9 +57,21 @@ class TokenAuthController { final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername()); - final String jwt = jwtTokenUtil.generateToken(userDetails); - - return ResponseEntity.ok(new AuthenticationResponse(jwt)); + if (isPlatformEnabled) { + String path = apiPath.replace("{username}",userDetails.getUsername()); + boolean isSuccessful = OesRestApi.initiateUserGroupInPlatform(url+path); + if (isSuccessful) { + final String jwt = jwtTokenUtil.generateToken(userDetails); + return ResponseEntity.ok(new AuthenticationResponse(jwt)); + } + else { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + else { + final String jwt = jwtTokenUtil.generateToken(userDetails); + return ResponseEntity.ok(new AuthenticationResponse(jwt)); + } } } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/OpsmxPlatformService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/OpsmxPlatformService.groovy index a8e5c2bdd5..743b6d14cd 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/OpsmxPlatformService.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/internal/OpsmxPlatformService.groovy @@ -106,4 +106,11 @@ interface OpsmxPlatformService { @Path('source') String source, @Body Object data) + @PUT("/platformservice/{version}/{type}/{source}/{source1}/{source2}") + Object updatePlatformResponse2(@Path('version') String version, + @Path('type') String type, + @Path('source') String source, + @Path('source1') String source1, + @Path('source2') String source2) + } diff --git a/gate-web/src/main/java/com/netflix/spinnaker/gate/util/OesRestApi.java b/gate-web/src/main/java/com/netflix/spinnaker/gate/util/OesRestApi.java new file mode 100644 index 0000000000..2c8d30ace5 --- /dev/null +++ b/gate-web/src/main/java/com/netflix/spinnaker/gate/util/OesRestApi.java @@ -0,0 +1,18 @@ +package com.netflix.spinnaker.gate.util; + +import java.io.IOException; +import okhttp3.*; + +public class OesRestApi { + private static OkHttpClient httpClient = new OkHttpClient(); + + public static boolean initiateUserGroupInPlatform(String url) throws IOException { + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(JSON, ""); + Request request = new Request.Builder().url(url).put(body).build(); + + try (Response response = httpClient.newCall(request).execute()) { + return response.isSuccessful(); + } + } +}