Skip to content

Commit

Permalink
OP-20978: Feature: New Gate Api for fetching userDetails along with c…
Browse files Browse the repository at this point in the history
…loudAccounts (#432)
  • Loading branch information
emanipravallika authored Oct 12, 2023
1 parent 51ce8e3 commit ffc4933
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docker_build/gate.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
opsmx:
baseUrl: http://oes-api:8085
baseUrl: http://localhost:8085
enabled: true
autopilot:
baseUrl: http://localhost:8090
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<String> userRoles = new ArrayList<>();
private List<Object> cloudAccounts = new ArrayList<>();
}

0 comments on commit ffc4933

Please sign in to comment.