diff --git a/docker_build/gate.yml b/docker_build/gate.yml index 9d71b84e80..634730f228 100644 --- a/docker_build/gate.yml +++ b/docker_build/gate.yml @@ -1,6 +1,6 @@ services: opsmx: - baseUrl: http://oes-api:8085 + baseUrl: http://localhost:8085 enabled: true autopilot: baseUrl: http://localhost:8090 diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AuthController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AuthController.groovy index 353aba1e03..5eb316e41f 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AuthController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/AuthController.groovy @@ -18,11 +18,12 @@ package com.netflix.spinnaker.gate.controllers import com.netflix.spinnaker.gate.security.SpinnakerUser import com.netflix.spinnaker.gate.services.PermissionService +import com.netflix.spinnaker.gate.services.UserInfoService +import com.netflix.spinnaker.gate.services.internal.OpsmxOesService import com.netflix.spinnaker.security.AuthenticatedRequest import com.netflix.spinnaker.security.User import groovy.util.logging.Slf4j import io.swagger.annotations.ApiOperation -import org.apache.commons.lang3.exception.ExceptionUtils import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.security.access.prepost.PreAuthorize @@ -58,6 +59,12 @@ class AuthController { @Autowired PermissionService permissionService + @Autowired + UserInfoService userInfoService + + @Autowired + OpsmxOesService opsmxOesService + @Autowired AuthController(@Value('${services.deck.base-url:}') URL deckBaseUrl, @Value('${services.deck.redirect-host-pattern:#{null}}') String redirectHostPattern) { @@ -160,4 +167,19 @@ class AuthController { AuthenticatedRequest.getSpinnakerUser().orElse("anonymous") ) } + + @ApiOperation(value = "Get user Details with cloudAccounts") + @RequestMapping(value = "/userInfo", method = RequestMethod.GET) + Object userInfo(@ApiIgnore @SpinnakerUser User user) { + if (!user) { + throw new Exception("UnAuthorized User") + } + def fiatRoles = permissionService.getRoles(user.username)?.collect{ it.name } + if (fiatRoles) { + user.roles = fiatRoles + } + def response = opsmxOesService.getOesResponse5( + "accountsConfig", "v3", "spinnaker", "cloudProviderAccount", false, false) + return userInfoService.getAllInfoOfUser(user, response) + } } diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/UserInfoService.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/UserInfoService.groovy new file mode 100644 index 0000000000..1ef04bcfa1 --- /dev/null +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/services/UserInfoService.groovy @@ -0,0 +1,60 @@ +/* + * Copyright 2023 Netflix, Inc. + * + * 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 com.netflix.spinnaker.gate.services + +import com.google.gson.Gson +import com.google.gson.JsonParser +import com.netflix.spinnaker.security.User +import com.opsmx.spinnaker.gate.model.UserInfoDetailsModel +import groovy.util.logging.Slf4j +import org.springframework.stereotype.Service + +@Slf4j +@Service +class UserInfoService { + + Gson gson = new Gson() + + Object getAllInfoOfUser(User user, Object response) throws Exception { + + UserInfoDetailsModel userInfoDetails = new UserInfoDetailsModel() + try { + log.info("CloudProviderAccounts response from oes service: {}", response) + def inputStr = gson.toJson(response) + def extractedCloudAccounts = JsonParser.parseString(inputStr).getAsJsonArray() + + def cloudAccounts = extractedCloudAccounts.collect { accountJson -> + def accountType = accountJson.getAsJsonPrimitive("accountType").getAsString() + def name = accountJson.getAsJsonPrimitive("name").getAsString() + def cloudAccount = [cloudProvider: accountType, accountName: name] + cloudAccount + } + log.info("Extracted cloudAccounts for user: {}", cloudAccounts) + + userInfoDetails.cloudAccounts = cloudAccounts + userInfoDetails.userName = user.username + userInfoDetails.firstName = user.firstName + userInfoDetails.lastName = user.lastName + userInfoDetails.userMailId = user.email + userInfoDetails.userRoles = user.roles + + } catch (Exception e) { + e.printStackTrace() + } + return userInfoDetails + } +} diff --git a/gate-web/src/main/java/com/opsmx/spinnaker/gate/model/UserInfoDetailsModel.java b/gate-web/src/main/java/com/opsmx/spinnaker/gate/model/UserInfoDetailsModel.java new file mode 100644 index 0000000000..d44d7a6268 --- /dev/null +++ b/gate-web/src/main/java/com/opsmx/spinnaker/gate/model/UserInfoDetailsModel.java @@ -0,0 +1,36 @@ +/* + * Copyright 2023 Netflix, Inc. + * + * 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 com.opsmx.spinnaker.gate.model; + +import com.fasterxml.jackson.annotation.JsonInclude; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import lombok.Data; + +@Data +@JsonInclude +public class UserInfoDetailsModel { + + private String userName; + private String firstName; + private String lastName; + private String userMailId; + + private Collection userRoles = new ArrayList<>(); + private List cloudAccounts = new ArrayList<>(); +}