1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.sandbox;
17 
18 import com.android.tradefed.config.ConfigurationException;
19 import com.android.tradefed.config.GlobalConfiguration;
20 import com.android.tradefed.config.IConfiguration;
21 import com.android.tradefed.config.NoOpConfigOptionValueTransformer;
22 import com.android.tradefed.config.proxy.AutomatedReporters;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.result.error.ErrorIdentifier;
25 import com.android.tradefed.result.error.InfraErrorIdentifier;
26 import com.android.tradefed.sandbox.SandboxConfigDump.DumpCmd;
27 import com.android.tradefed.util.CommandResult;
28 import com.android.tradefed.util.CommandStatus;
29 import com.android.tradefed.util.FileUtil;
30 import com.android.tradefed.util.IRunUtil;
31 import com.android.tradefed.util.IRunUtil.EnvPriority;
32 import com.android.tradefed.util.SystemUtil;
33 import com.android.tradefed.util.TimeUtil;
34 
35 import com.google.common.base.Strings;
36 
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Set;
42 
43 /** A utility class for managing {@link IConfiguration} when doing sandboxing. */
44 public class SandboxConfigUtil {
45 
46     private static final long DUMP_TIMEOUT = 5 * 60 * 1000; // 5 minutes
47 
48     // For backward compatibility
dumpConfigForVersion( String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)49     public static File dumpConfigForVersion(
50             String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)
51             throws SandboxConfigurationException, IOException {
52         return dumpConfigForVersion(classpath, runUtil, args, dump, globalConfig, false);
53     }
54 
55     /**
56      * Create a subprocess based on the Tf jars from any version, and dump the xml {@link
57      * IConfiguration} based on the command line args.
58      *
59      * @param classpath the classpath to use to run the sandbox.
60      * @param runUtil the {@link IRunUtil} to use to run the command.
61      * @param args the command line args.
62      * @param dump the {@link DumpCmd} driving some of the outputs.
63      * @param globalConfig the file describing the global configuration to be used.
64      * @param skipJavaCheck whether or not to skip the java version check
65      * @return A {@link File} containing the xml dump from the command line.
66      * @throws SandboxConfigurationException if the dump is not successful.
67      */
dumpConfigForVersion( String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig, boolean skipJavaCheck)68     public static File dumpConfigForVersion(
69             String classpath,
70             IRunUtil runUtil,
71             String[] args,
72             DumpCmd dump,
73             File globalConfig,
74             boolean skipJavaCheck)
75             throws SandboxConfigurationException, IOException {
76         if (Strings.isNullOrEmpty(classpath)) {
77             throw new SandboxConfigurationException(
78                     "Something went wrong with the sandbox setup, classpath was empty.",
79                     InfraErrorIdentifier.INTERNAL_CONFIG_ERROR);
80         }
81         runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE);
82         runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_SERVER_CONFIG_VARIABLE);
83         runUtil.unsetEnvVariable(AutomatedReporters.PROTO_REPORTING_PORT);
84         File destination = null;
85         try {
86             destination = FileUtil.createTempFile("config-container", ".xml");
87             if (globalConfig != null) {
88                 runUtil.setEnvVariable(
89                         GlobalConfiguration.GLOBAL_CONFIG_VARIABLE, globalConfig.getAbsolutePath());
90                 runUtil.setEnvVariablePriority(EnvPriority.SET);
91             }
92         } catch (IOException e) {
93             FileUtil.deleteFile(globalConfig);
94             FileUtil.deleteFile(destination);
95             throw e;
96         }
97         File tmpDir = FileUtil.createTempDir("config-dump-temp-dir");
98         CommandResult result = null;
99         try {
100             List<String> mCmdArgs = new ArrayList<>();
101             mCmdArgs.add(SystemUtil.getRunningJavaBinaryPath(skipJavaCheck).getAbsolutePath());
102             mCmdArgs.add(String.format("-Djava.io.tmpdir=%s", tmpDir.getAbsolutePath()));
103             mCmdArgs.add("-cp");
104             mCmdArgs.add(classpath);
105             mCmdArgs.add(SandboxConfigDump.class.getCanonicalName());
106             mCmdArgs.add(dump.toString());
107             mCmdArgs.add(destination.getAbsolutePath());
108             for (String arg : args) {
109                 mCmdArgs.add(arg);
110             }
111             result = runUtil.runTimedCmd(DUMP_TIMEOUT, mCmdArgs.toArray(new String[0]));
112             if (CommandStatus.SUCCESS.equals(result.getStatus())) {
113                 return destination;
114             }
115         } finally {
116             FileUtil.recursiveDelete(tmpDir);
117         }
118 
119         if (result.getStderr() != null && !result.getStderr().isEmpty()) {
120             CLog.d("stderr: %s\nstdout: %s", result.getStderr(), result.getStdout());
121         }
122 
123         FileUtil.deleteFile(destination);
124         // Do not delete the global configuration file here in this case, it might still be used.
125         String errorMessage = "Error when dumping the config.";
126         if (CommandStatus.TIMED_OUT.equals(result.getStatus())) {
127             errorMessage +=
128                     String.format(" Timed out after %s.", TimeUtil.formatElapsedTime(DUMP_TIMEOUT));
129         }
130         errorMessage += String.format(" stderr: %s", result.getStderr());
131         ErrorIdentifier error = InfraErrorIdentifier.INTERNAL_CONFIG_ERROR;
132         if (result.getStderr().contains(InfraErrorIdentifier.KEYSTORE_CONFIG_ERROR.name())) {
133             error = InfraErrorIdentifier.KEYSTORE_CONFIG_ERROR;
134         }
135         if (result.getStderr().contains(InfraErrorIdentifier.CLASS_NOT_FOUND.name())) {
136             error = InfraErrorIdentifier.CLASS_NOT_FOUND;
137         }
138         if (result.getStderr().contains(InfraErrorIdentifier.OPTION_CONFIGURATION_ERROR.name())) {
139             error = InfraErrorIdentifier.OPTION_CONFIGURATION_ERROR;
140         }
141         if (result.getStderr().contains(InfraErrorIdentifier.GCS_ERROR.name())) {
142             error = InfraErrorIdentifier.GCS_ERROR;
143         }
144         throw new SandboxConfigurationException(errorMessage, error);
145     }
146 
147     /**
148      * Create a subprocess based on the Tf jars from any version, and dump the xml {@link
149      * IConfiguration} based on the command line args.
150      *
151      * @param rootDir the directory containing all the jars from TF.
152      * @param runUtil the {@link IRunUtil} to use to run the command.
153      * @param args the command line args.
154      * @param dump the {@link DumpCmd} driving some of the outputs.
155      * @param globalConfig the file describing the global configuration to be used.
156      * @return A {@link File} containing the xml dump from the command line.
157      * @throws ConfigurationException if the dump is not successful.
158      */
dumpConfigForVersion( File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)159     public static File dumpConfigForVersion(
160             File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)
161             throws ConfigurationException, IOException {
162         // include all jars on the classpath
163         String classpath = "";
164         Set<String> jarFiles = FileUtil.findFiles(rootDir, ".*.jar");
165         classpath = String.join(":", jarFiles);
166         return dumpConfigForVersion(classpath, runUtil, args, dump, globalConfig, false);
167     }
168 
169     /** Create a global config with only the keystore to make it available in subprocess. */
dumpFilteredGlobalConfig(Set<String> exclusionPatterns)170     public static File dumpFilteredGlobalConfig(Set<String> exclusionPatterns) throws IOException {
171         return GlobalConfiguration.getInstance()
172                 .cloneConfigWithFilter(
173                         exclusionPatterns, new NoOpConfigOptionValueTransformer(), false);
174     }
175 }
176