| 
23 | 23 | import com.sun.jersey.api.client.ClientResponse;  | 
24 | 24 | import com.sun.jersey.api.client.WebResource;  | 
25 | 25 | import com.sun.jersey.api.client.WebResource.Builder;  | 
 | 26 | +import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;  | 
 | 27 | +import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;  | 
26 | 28 | import org.apache.commons.cli.CommandLine;  | 
27 | 29 | import org.apache.commons.cli.GnuParser;  | 
28 | 30 | import org.apache.commons.cli.MissingArgumentException;  | 
 | 
31 | 33 | import org.apache.hadoop.classification.InterfaceStability.Unstable;  | 
32 | 34 | import org.apache.hadoop.conf.Configuration;  | 
33 | 35 | import org.apache.hadoop.conf.Configured;  | 
 | 36 | +import org.apache.hadoop.security.authentication.client.AuthenticatedURL;  | 
 | 37 | +import org.apache.hadoop.security.ssl.SSLFactory;  | 
34 | 38 | import org.apache.hadoop.security.UserGroupInformation;  | 
35 | 39 | import org.apache.hadoop.util.Tool;  | 
36 | 40 | import org.apache.hadoop.yarn.conf.YarnConfiguration;  | 
 | 
41 | 45 | 
 
  | 
42 | 46 | import javax.ws.rs.core.MediaType;  | 
43 | 47 | import javax.ws.rs.core.Response.Status;  | 
 | 48 | +import java.io.IOException;  | 
 | 49 | +import java.net.HttpURLConnection;  | 
 | 50 | +import java.net.URL;  | 
44 | 51 | import java.util.ArrayList;  | 
45 | 52 | import java.util.Arrays;  | 
46 | 53 | import java.util.HashMap;  | 
@@ -156,7 +163,12 @@ public int run(String[] args) throws Exception {  | 
156 | 163 |   @VisibleForTesting  | 
157 | 164 |   int formatSchedulerConf(String webAppAddress, WebResource resource)  | 
158 | 165 |       throws Exception {  | 
159 |  | -    Client webServiceClient = Client.create();  | 
 | 166 | +    Configuration conf = getConf();  | 
 | 167 | +    SSLFactory clientSslFactory = null;  | 
 | 168 | +    if (YarnConfiguration.useHttps(conf)) {  | 
 | 169 | +      clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);  | 
 | 170 | +    }  | 
 | 171 | +    Client webServiceClient = createWebServiceClient(clientSslFactory);  | 
160 | 172 |     ClientResponse response = null;  | 
161 | 173 |     resource = (resource != null) ? resource :  | 
162 | 174 |         webServiceClient.resource(webAppAddress);  | 
@@ -194,14 +206,24 @@ int formatSchedulerConf(String webAppAddress, WebResource resource)  | 
194 | 206 |       if (response != null) {  | 
195 | 207 |         response.close();  | 
196 | 208 |       }  | 
197 |  | -      webServiceClient.destroy();  | 
 | 209 | +      if (webServiceClient != null) {  | 
 | 210 | +        webServiceClient.destroy();  | 
 | 211 | +      }  | 
 | 212 | +      if (clientSslFactory != null) {  | 
 | 213 | +        clientSslFactory.destroy();  | 
 | 214 | +      }  | 
198 | 215 |     }  | 
199 | 216 |   }  | 
200 | 217 | 
 
  | 
201 | 218 |   @VisibleForTesting  | 
202 | 219 |   int updateSchedulerConfOnRMNode(String webAppAddress,  | 
203 | 220 |       SchedConfUpdateInfo updateInfo) throws Exception {  | 
204 |  | -    Client webServiceClient = Client.create();  | 
 | 221 | +    Configuration conf = getConf();  | 
 | 222 | +    SSLFactory clientSslFactory = null;  | 
 | 223 | +    if (YarnConfiguration.useHttps(conf)) {  | 
 | 224 | +      clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);  | 
 | 225 | +    }  | 
 | 226 | +    Client webServiceClient = createWebServiceClient(clientSslFactory);  | 
205 | 227 |     ClientResponse response = null;  | 
206 | 228 |     WebResource resource = webServiceClient.resource(webAppAddress);  | 
207 | 229 | 
 
  | 
@@ -236,10 +258,42 @@ int updateSchedulerConfOnRMNode(String webAppAddress,  | 
236 | 258 |       if (response != null) {  | 
237 | 259 |         response.close();  | 
238 | 260 |       }  | 
239 |  | -      webServiceClient.destroy();  | 
 | 261 | +      if (webServiceClient != null) {  | 
 | 262 | +        webServiceClient.destroy();  | 
 | 263 | +      }  | 
 | 264 | +      if (clientSslFactory != null) {  | 
 | 265 | +        clientSslFactory.destroy();  | 
 | 266 | +      }  | 
240 | 267 |     }  | 
241 | 268 |   }  | 
242 | 269 | 
 
  | 
 | 270 | +  private Client createWebServiceClient(SSLFactory clientSslFactory) {  | 
 | 271 | +    Client webServiceClient = new Client(new URLConnectionClientHandler(  | 
 | 272 | +        new HttpURLConnectionFactory() {  | 
 | 273 | +        @Override  | 
 | 274 | +        public HttpURLConnection getHttpURLConnection(URL url)  | 
 | 275 | +            throws IOException {  | 
 | 276 | +          AuthenticatedURL.Token token = new AuthenticatedURL.Token();  | 
 | 277 | +          AuthenticatedURL aUrl;  | 
 | 278 | +          HttpURLConnection conn = null;  | 
 | 279 | +          try {  | 
 | 280 | +            if (clientSslFactory != null) {  | 
 | 281 | +              clientSslFactory.init();  | 
 | 282 | +              aUrl = new AuthenticatedURL(null, clientSslFactory);  | 
 | 283 | +            } else {  | 
 | 284 | +              aUrl = new AuthenticatedURL();  | 
 | 285 | +            }  | 
 | 286 | +            conn = aUrl.openConnection(url, token);  | 
 | 287 | +          } catch (Exception e) {  | 
 | 288 | +            throw new IOException(e);  | 
 | 289 | +          }  | 
 | 290 | +          return conn;  | 
 | 291 | +        }  | 
 | 292 | +      }));  | 
 | 293 | +    webServiceClient.setChunkedEncodingSize(null);  | 
 | 294 | +    return webServiceClient;  | 
 | 295 | +  }  | 
 | 296 | + | 
243 | 297 | 
 
  | 
244 | 298 |   @VisibleForTesting  | 
245 | 299 |   void addQueues(String args, SchedConfUpdateInfo updateInfo) {  | 
 | 
0 commit comments