Skip to content

Commit

Permalink
Merge pull request #240 from sivanag1974/dev
Browse files Browse the repository at this point in the history
Workload details changed
  • Loading branch information
RickyS2 authored Feb 22, 2020
2 parents 1a08742 + 6e3cc1c commit 08c4937
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,58 @@

package com.tmobile.cso.vault.api.service;

import com.google.common.collect.ImmutableMap;
import com.google.gson.*;
import com.tmobile.cso.vault.api.exception.LogMessage;
import com.tmobile.cso.vault.api.model.*;
import com.tmobile.cso.vault.api.utils.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tmobile.cso.vault.api.exception.LogMessage;
import com.tmobile.cso.vault.api.model.UserDetails;
import com.tmobile.cso.vault.api.model.WorkloadAppDetails;
import com.tmobile.cso.vault.api.process.RestProcessor;
import com.tmobile.cso.vault.api.utils.JSONUtil;
import com.tmobile.cso.vault.api.utils.ThreadLocalContext;

@Component
public class WorkloadDetailsService {

@Value("${workload.endpoint}")
private String workloadEndpoint;

@Value("${workload.endpoint.token}")
private String workloadEndpointToken;

@Autowired
RestProcessor restprocessor;

private static Logger log = LogManager.getLogger(WorkloadDetailsService.class);

/**
Expand All @@ -55,51 +80,65 @@ public class WorkloadDetailsService {
public ResponseEntity<String> getWorkloadDetails(String token, UserDetails userDetails) {
String api = workloadEndpoint;
List<WorkloadAppDetails> workloadAppDetailsList = new ArrayList<>();

workloadAppDetailsList.add(new WorkloadAppDetails(WorkloadAppDetails.APP_NAME_OTHER, WorkloadAppDetails.APP_TAG_OTHER, WorkloadAppDetails.APP_ID_OTHER));
// get first response

JsonObject response = getApiResponse(api);
JsonArray results = response.getAsJsonObject("data").getAsJsonArray("summary");
String pagination = response.getAsJsonObject("data").get("paginationURL").getAsString();
Integer total = response.getAsJsonObject("data").get("total").getAsInt();
Integer maxResults = response.getAsJsonObject("data").get("maxResults").getAsInt();

// call each pagination and populate results json
if (total > maxResults) {
for (int index = 1; index <= (total / maxResults); index++) {
response = getApiResponse(api + "?" + pagination);
results.addAll(response.getAsJsonObject("data").getAsJsonArray("summary"));
if (results.size() < total) {
pagination = response.getAsJsonObject("data").get("paginationURL").getAsString();
if (response != null) {
JsonArray results = response.getAsJsonArray("items");
// iterate json array to populate WorkloadAppDetails list
for(JsonElement jsonElement: results) {
if (jsonElement.getAsJsonObject() != null) {
JsonObject metadata = jsonElement.getAsJsonObject().getAsJsonObject("metadata");
JsonObject spec = jsonElement.getAsJsonObject().getAsJsonObject("spec");
WorkloadAppDetails workloadAppDetails = new WorkloadAppDetails();
if (spec != null) {
workloadAppDetails.setAppName((spec.get("summary").isJsonNull()?"":spec.get("summary").getAsString()));
workloadAppDetails.setAppID((spec.get("id").isJsonNull()?"":spec.get("id").getAsString()));
workloadAppDetails.setAppTag((spec.get("id").isJsonNull()?"":spec.get("id").getAsString()));
workloadAppDetailsList.add(workloadAppDetails);
}

}
}
}

// iterate json array to populate WorkloadAppDetails list
for(JsonElement jsonElement: results) {
JsonObject summary = jsonElement.getAsJsonObject();
WorkloadAppDetails workloadAppDetails = new WorkloadAppDetails();
workloadAppDetails.setAppID((summary.get("appID").isJsonNull()?"":summary.get("appID").getAsString()));
workloadAppDetails.setAppName((summary.get("appName").isJsonNull()?"":summary.get("appName").getAsString()));
workloadAppDetails.setAppTag((summary.get("appTag").isJsonNull()?"":summary.get("appTag").getAsString()));
workloadAppDetailsList.add(workloadAppDetails);
}
// Add a default
workloadAppDetailsList.add(new WorkloadAppDetails(WorkloadAppDetails.APP_NAME_OTHER, WorkloadAppDetails.APP_TAG_OTHER, WorkloadAppDetails.APP_ID_OTHER));
return ResponseEntity.status(HttpStatus.OK).body(JSONUtil.getJSON(workloadAppDetailsList));
}


/**
* To get response from Workload endpoint
* @param api
* @return
*/
private JsonObject getApiResponse(String api) {
private JsonObject getApiResponse(String api) {
JsonParser jsonParser = new JsonParser();
Gson gson = new Gson();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpClient httpClient =null;
try {

httpClient = HttpClientBuilder.create().setSSLHostnameVerifier(
NoopHostnameVerifier.INSTANCE).
setSSLContext(
new SSLContextBuilder().loadTrustMaterial(null,new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build()
).setRedirectStrategy(new LaxRedirectStrategy()).build();


} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e1) {
// TODO Auto-generated catch block
log.debug(e1.getMessage());
}

//HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet getRequest = new HttpGet(api);
getRequest.addHeader("accept", "application/json");

getRequest.addHeader("Authorization",workloadEndpointToken);
String output = "";
StringBuffer jsonResponse = new StringBuffer();

Expand All @@ -108,7 +147,6 @@ private JsonObject getApiResponse(String api) {
if (apiResponse.getStatusLine().getStatusCode() != 200) {
return null;
}

BufferedReader br = new BufferedReader(new InputStreamReader((apiResponse.getEntity().getContent())));
while ((output = br.readLine()) != null) {
jsonResponse.append(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@RestController
@CrossOrigin
@Api( description = "Get Workload details", position = 14, hidden = true)
@Api( description = "Get Workload details", position = 14)
public class WorkloadDetailsController {

@Autowired
Expand All @@ -40,7 +40,7 @@ public class WorkloadDetailsController {
* @param token
* @return
*/
@ApiOperation(value = "${WorkloadDetailsController.getApprolesFromCwm.value}", notes = "${WorkloadDetailsController.getApprolesFromCwm.notes}", hidden = true)
@ApiOperation(value = "${WorkloadDetailsController.getApprolesFromCwm.value}", notes = "${WorkloadDetailsController.getApprolesFromCwm.notes}")
@GetMapping(value="/v2/serviceaccounts/cwm/approles", produces="application/json")
public ResponseEntity<String> getWorkloadDetails(HttpServletRequest request, @RequestHeader(value="vault-token") String token){
UserDetails userDetails = (UserDetails) ((HttpServletRequest) request).getAttribute("UserDetails");
Expand Down
1 change: 1 addition & 0 deletions tvaultapi/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ spring.mail.host=email.smtp
spring.mail.port=123

workload.endpoint=
workload.endpoint.token=

ad.notification.fromemail=
ad.notification.mail.subject=Onboarding Service account %s is successful
Expand Down

0 comments on commit 08c4937

Please sign in to comment.