1 /* 2 * Copyright (c) 2018 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.api; 18 19 import com.android.vts.entity.TestSuiteFileEntity; 20 import com.android.vts.entity.TestSuiteResultEntity; 21 import com.android.vts.proto.TestSuiteResultMessageProto; 22 import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 23 import com.google.api.client.http.javanet.NetHttpTransport; 24 import com.google.api.client.json.jackson.JacksonFactory; 25 import com.google.api.services.oauth2.Oauth2; 26 import com.google.api.services.oauth2.model.Tokeninfo; 27 import com.google.gson.Gson; 28 import com.googlecode.objectify.Key; 29 import org.apache.commons.codec.binary.Base64; 30 31 import javax.servlet.http.HttpServletRequest; 32 import javax.servlet.http.HttpServletResponse; 33 import java.io.IOException; 34 import java.util.HashMap; 35 import java.util.Map; 36 import java.util.logging.Level; 37 import java.util.logging.Logger; 38 import java.util.stream.Collectors; 39 import javax.servlet.ServletConfig; 40 import javax.servlet.ServletException; 41 42 /** REST endpoint for posting test suite data to the Dashboard. */ 43 public class TestSuiteResultRestServlet extends BaseApiServlet { 44 private static String SERVICE_CLIENT_ID; 45 private static final String SERVICE_NAME = "VTS Dashboard"; 46 private static final Logger logger = 47 Logger.getLogger(TestSuiteResultRestServlet.class.getName()); 48 49 @Override init(ServletConfig cfg)50 public void init(ServletConfig cfg) throws ServletException { 51 super.init(cfg); 52 53 SERVICE_CLIENT_ID = systemConfigProp.getProperty("appengine.serviceClientID"); 54 } 55 56 @Override doPost(HttpServletRequest request, HttpServletResponse response)57 public void doPost(HttpServletRequest request, HttpServletResponse response) 58 throws IOException { 59 // Retrieve the params 60 TestSuiteResultMessageProto.TestSuiteResultMessage testSuiteResultMessage; 61 try { 62 String payload = request.getReader().lines().collect(Collectors.joining()); 63 byte[] value = Base64.decodeBase64(payload); 64 testSuiteResultMessage = 65 TestSuiteResultMessageProto.TestSuiteResultMessage.parseFrom(value); 66 } catch (IOException e) { 67 response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 68 logger.log(Level.WARNING, "Invalid proto: " + e.getLocalizedMessage()); 69 return; 70 } 71 72 Map<String, Object> resultMap = new HashMap<>(); 73 // Verify service account access token. 74 if (testSuiteResultMessage.hasAccessToken()) { 75 String accessToken = testSuiteResultMessage.getAccessToken(); 76 logger.log(Level.INFO, "accessToken => " + accessToken); 77 GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken); 78 Oauth2 oauth2 = 79 new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential) 80 .setApplicationName(SERVICE_NAME) 81 .build(); 82 Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken(accessToken).execute(); 83 84 if (tokenInfo.getIssuedTo().equals(SERVICE_CLIENT_ID)) { 85 String filePath = "suite_result/2019/04/06/132343.bin"; 86 Key<TestSuiteFileEntity> testSuiteFileParent = 87 Key.create(TestSuiteFileEntity.class, filePath); 88 TestSuiteFileEntity newTestSuiteFileEntity = new TestSuiteFileEntity(filePath); 89 TestSuiteResultEntity testSuiteResultEntity = 90 new TestSuiteResultEntity( 91 testSuiteFileParent, 92 testSuiteResultMessage.getStartTime(), 93 testSuiteResultMessage.getEndTime(), 94 testSuiteResultMessage.getTestType(), 95 testSuiteResultMessage.getBootSuccess(), 96 testSuiteResultMessage.getResultPath(), 97 testSuiteResultMessage.getInfraLogPath(), 98 testSuiteResultMessage.getHostName(), 99 testSuiteResultMessage.getSuitePlan(), 100 testSuiteResultMessage.getSuiteVersion(), 101 testSuiteResultMessage.getSuiteName(), 102 testSuiteResultMessage.getSuiteBuildNumber(), 103 testSuiteResultMessage.getModulesDone(), 104 testSuiteResultMessage.getModulesTotal(), 105 testSuiteResultMessage.getBranch(), 106 testSuiteResultMessage.getTarget(), 107 testSuiteResultMessage.getBuildId(), 108 testSuiteResultMessage.getBuildSystemFingerprint(), 109 testSuiteResultMessage.getBuildVendorFingerprint(), 110 testSuiteResultMessage.getPassedTestCaseCount(), 111 testSuiteResultMessage.getFailedTestCaseCount()); 112 113 testSuiteResultEntity.save(newTestSuiteFileEntity); 114 resultMap.put("result", "successfully saved!"); 115 } else { 116 logger.log(Level.WARNING, "service_client_id didn't match!"); 117 logger.log(Level.INFO, "SERVICE_CLIENT_ID => " + tokenInfo.getIssuedTo()); 118 resultMap.put("result", "Wrong Service Client ID!"); 119 } 120 } else { 121 logger.log(Level.WARNING, "postMessage do not contain any accessToken!"); 122 resultMap.put("result", "Access Token Missing!"); 123 } 124 String json = new Gson().toJson(resultMap); 125 response.setStatus(HttpServletResponse.SC_OK); 126 response.setContentType("application/json"); 127 response.setCharacterEncoding("UTF-8"); 128 response.getWriter().write(json); 129 } 130 } 131