001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.hadoop.hdfs; 020 021 import java.io.FileInputStream; 022 import java.io.IOException; 023 import java.net.HttpURLConnection; 024 import java.net.InetSocketAddress; 025 import java.net.URI; 026 import java.net.URL; 027 import java.security.KeyStore; 028 import java.security.cert.X509Certificate; 029 030 import javax.net.ssl.HostnameVerifier; 031 import javax.net.ssl.HttpsURLConnection; 032 import javax.net.ssl.KeyManager; 033 import javax.net.ssl.KeyManagerFactory; 034 import javax.net.ssl.SSLContext; 035 import javax.net.ssl.SSLSession; 036 import javax.net.ssl.TrustManager; 037 import javax.net.ssl.TrustManagerFactory; 038 import javax.net.ssl.X509TrustManager; 039 040 import org.apache.hadoop.classification.InterfaceAudience; 041 import org.apache.hadoop.classification.InterfaceStability; 042 import org.apache.hadoop.conf.Configuration; 043 import org.apache.hadoop.hdfs.web.URLUtils; 044 import org.apache.hadoop.util.Time; 045 046 /** 047 * An implementation of a protocol for accessing filesystems over HTTPS. The 048 * following implementation provides a limited, read-only interface to a 049 * filesystem over HTTPS. 050 * 051 * @see org.apache.hadoop.hdfs.server.namenode.ListPathsServlet 052 * @see org.apache.hadoop.hdfs.server.namenode.FileDataServlet 053 */ 054 @InterfaceAudience.Private 055 @InterfaceStability.Evolving 056 public class HsftpFileSystem extends HftpFileSystem { 057 058 private static final long MM_SECONDS_PER_DAY = 1000 * 60 * 60 * 24; 059 private volatile int ExpWarnDays = 0; 060 061 /** 062 * Return the protocol scheme for the FileSystem. 063 * <p/> 064 * 065 * @return <code>hsftp</code> 066 */ 067 @Override 068 public String getScheme() { 069 return "hsftp"; 070 } 071 072 @Override 073 public void initialize(URI name, Configuration conf) throws IOException { 074 super.initialize(name, conf); 075 setupSsl(conf); 076 ExpWarnDays = conf.getInt("ssl.expiration.warn.days", 30); 077 } 078 079 /** 080 * Set up SSL resources 081 * 082 * @throws IOException 083 */ 084 private static void setupSsl(Configuration conf) throws IOException { 085 Configuration sslConf = new HdfsConfiguration(false); 086 sslConf.addResource(conf.get(DFSConfigKeys.DFS_CLIENT_HTTPS_KEYSTORE_RESOURCE_KEY, 087 DFSConfigKeys.DFS_CLIENT_HTTPS_KEYSTORE_RESOURCE_DEFAULT)); 088 FileInputStream fis = null; 089 try { 090 SSLContext sc = SSLContext.getInstance("SSL"); 091 KeyManager[] kms = null; 092 TrustManager[] tms = null; 093 if (sslConf.get("ssl.client.keystore.location") != null) { 094 // initialize default key manager with keystore file and pass 095 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 096 KeyStore ks = KeyStore.getInstance(sslConf.get( 097 "ssl.client.keystore.type", "JKS")); 098 char[] ksPass = sslConf.get("ssl.client.keystore.password", "changeit") 099 .toCharArray(); 100 fis = new FileInputStream(sslConf.get("ssl.client.keystore.location", 101 "keystore.jks")); 102 ks.load(fis, ksPass); 103 kmf.init(ks, sslConf.get("ssl.client.keystore.keypassword", "changeit") 104 .toCharArray()); 105 kms = kmf.getKeyManagers(); 106 fis.close(); 107 fis = null; 108 } 109 // initialize default trust manager with truststore file and pass 110 if (sslConf.getBoolean("ssl.client.do.not.authenticate.server", false)) { 111 // by pass trustmanager validation 112 tms = new DummyTrustManager[] { new DummyTrustManager() }; 113 } else { 114 TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); 115 KeyStore ts = KeyStore.getInstance(sslConf.get( 116 "ssl.client.truststore.type", "JKS")); 117 char[] tsPass = sslConf.get("ssl.client.truststore.password", 118 "changeit").toCharArray(); 119 fis = new FileInputStream(sslConf.get("ssl.client.truststore.location", 120 "truststore.jks")); 121 ts.load(fis, tsPass); 122 tmf.init(ts); 123 tms = tmf.getTrustManagers(); 124 } 125 sc.init(kms, tms, new java.security.SecureRandom()); 126 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 127 } catch (Exception e) { 128 throw new IOException("Could not initialize SSLContext", e); 129 } finally { 130 if (fis != null) { 131 fis.close(); 132 } 133 } 134 } 135 136 @Override 137 protected int getDefaultPort() { 138 return getDefaultSecurePort(); 139 } 140 141 @Override 142 protected InetSocketAddress getNamenodeSecureAddr(URI uri) { 143 return getNamenodeAddr(uri); 144 } 145 146 @Override 147 protected URI getNamenodeUri(URI uri) { 148 return getNamenodeSecureUri(uri); 149 } 150 151 @Override 152 protected HttpURLConnection openConnection(String path, String query) 153 throws IOException { 154 query = addDelegationTokenParam(query); 155 final URL url = new URL("https", nnUri.getHost(), 156 nnUri.getPort(), path + '?' + query); 157 HttpsURLConnection conn = (HttpsURLConnection)URLUtils.openConnection(url); 158 // bypass hostname verification 159 conn.setHostnameVerifier(new DummyHostnameVerifier()); 160 conn.setRequestMethod("GET"); 161 conn.connect(); 162 163 // check cert expiration date 164 final int warnDays = ExpWarnDays; 165 if (warnDays > 0) { // make sure only check once 166 ExpWarnDays = 0; 167 long expTimeThreshold = warnDays * MM_SECONDS_PER_DAY + Time.now(); 168 X509Certificate[] clientCerts = (X509Certificate[]) conn 169 .getLocalCertificates(); 170 if (clientCerts != null) { 171 for (X509Certificate cert : clientCerts) { 172 long expTime = cert.getNotAfter().getTime(); 173 if (expTime < expTimeThreshold) { 174 StringBuilder sb = new StringBuilder(); 175 sb.append("\n Client certificate " 176 + cert.getSubjectX500Principal().getName()); 177 int dayOffSet = (int) ((expTime - Time.now()) / MM_SECONDS_PER_DAY); 178 sb.append(" have " + dayOffSet + " days to expire"); 179 LOG.warn(sb.toString()); 180 } 181 } 182 } 183 } 184 return (HttpURLConnection) conn; 185 } 186 187 /** 188 * Dummy hostname verifier that is used to bypass hostname checking 189 */ 190 protected static class DummyHostnameVerifier implements HostnameVerifier { 191 @Override 192 public boolean verify(String hostname, SSLSession session) { 193 return true; 194 } 195 } 196 197 /** 198 * Dummy trustmanager that is used to trust all server certificates 199 */ 200 protected static class DummyTrustManager implements X509TrustManager { 201 @Override 202 public void checkClientTrusted(X509Certificate[] chain, String authType) { 203 } 204 205 @Override 206 public void checkServerTrusted(X509Certificate[] chain, String authType) { 207 } 208 209 @Override 210 public X509Certificate[] getAcceptedIssuers() { 211 return null; 212 } 213 } 214 215 }