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    package org.apache.oozie.client;
019    
020    import java.io.BufferedReader;
021    import java.io.File;
022    import java.io.FileReader;
023    import java.io.FileWriter;
024    import java.io.IOException;
025    import java.io.Writer;
026    import java.net.HttpURLConnection;
027    import java.net.URL;
028    import java.util.Date;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
033    import org.apache.hadoop.security.authentication.client.AuthenticationException;
034    import org.apache.hadoop.security.authentication.client.Authenticator;
035    import org.apache.hadoop.security.authentication.client.KerberosAuthenticator;
036    import org.apache.hadoop.security.authentication.client.PseudoAuthenticator;
037    
038    /**
039     * This subclass of {@link XOozieClient} supports Kerberos HTTP SPNEGO and simple authentication.
040     */
041    public class AuthOozieClient extends XOozieClient {
042    
043        /**
044         * Java system property to specify a custom Authenticator implementation.
045         */
046        public static final String AUTHENTICATOR_CLASS_SYS_PROP = "authenticator.class";
047    
048        /**
049         * Java system property that, if set the authentication token will be cached in the user home directory in a hidden
050         * file <code>.oozie-auth-token</code> with user read/write permissions only.
051         */
052        public static final String USE_AUTH_TOKEN_CACHE_SYS_PROP = "oozie.auth.token.cache";
053    
054        /**
055         * File constant that defines the location of the authentication token cache file.
056         * <p/>
057         * It resolves to <code>${user.home}/.oozie-auth-token</code>.
058         */
059        public static final File AUTH_TOKEN_CACHE_FILE = new File(System.getProperty("user.home"), ".oozie-auth-token");
060    
061        public static enum AuthType {
062            KERBEROS, SIMPLE
063        }
064    
065        private String authOption = null;
066    
067        /**
068         * Create an instance of the AuthOozieClient.
069         *
070         * @param oozieUrl the Oozie URL
071         */
072        public AuthOozieClient(String oozieUrl) {
073            this(oozieUrl, null);
074        }
075    
076        /**
077         * Create an instance of the AuthOozieClient.
078         *
079         * @param oozieUrl the Oozie URL
080         * @param authOption the auth option
081         */
082        public AuthOozieClient(String oozieUrl, String authOption) {
083            super(oozieUrl);
084            this.authOption = authOption;
085        }
086    
087        /**
088         * Create an authenticated connection to the Oozie server.
089         * <p/>
090         * It uses Hadoop-auth client authentication which by default supports
091         * Kerberos HTTP SPNEGO, Pseudo/Simple and anonymous.
092         * <p/>
093         * if the Java system property {@link #USE_AUTH_TOKEN_CACHE_SYS_PROP} is set to true Hadoop-auth
094         * authentication token will be cached/used in/from the '.oozie-auth-token' file in the user
095         * home directory.
096         *
097         * @param url the URL to open a HTTP connection to.
098         * @param method the HTTP method for the HTTP connection.
099         * @return an authenticated connection to the Oozie server.
100         * @throws IOException if an IO error occurred.
101         * @throws OozieClientException if an oozie client error occurred.
102         */
103        @Override
104        protected HttpURLConnection createConnection(URL url, String method) throws IOException, OozieClientException {
105            boolean useAuthFile = System.getProperty(USE_AUTH_TOKEN_CACHE_SYS_PROP, "false").equalsIgnoreCase("true");
106            AuthenticatedURL.Token readToken = new AuthenticatedURL.Token();
107            AuthenticatedURL.Token currentToken = new AuthenticatedURL.Token();
108    
109            if (useAuthFile) {
110                readToken = readAuthToken();
111                if (readToken != null) {
112                    currentToken = new AuthenticatedURL.Token(readToken.toString());
113                }
114            }
115    
116            if (currentToken.isSet()) {
117                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
118                conn.setRequestMethod("OPTIONS");
119                AuthenticatedURL.injectToken(conn, currentToken);
120                if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED ||
121                        isCookieExpired(conn.getRequestProperty("Cookie"))) {
122                    AUTH_TOKEN_CACHE_FILE.delete();
123                    currentToken = new AuthenticatedURL.Token();
124                }
125            }
126    
127            if (!currentToken.isSet()) {
128                Authenticator authenticator = getAuthenticator();
129                try {
130                    HttpURLConnection conn = new AuthenticatedURL(authenticator).openConnection(url, currentToken);
131                    if (conn.getRequestProperty("Cookie") == null) {
132                        authOption = "SIMPLE";
133                        new AuthenticatedURL(getAuthenticator()).openConnection(url, currentToken);
134                    }
135                }
136                catch (AuthenticationException ex) {
137                    AUTH_TOKEN_CACHE_FILE.delete();
138                    throw new OozieClientException(OozieClientException.AUTHENTICATION,
139                                                   "Could not authenticate, " + ex.getMessage(), ex);
140                }
141            }
142            if (useAuthFile && currentToken.isSet() && !currentToken.equals(readToken)) {
143                writeAuthToken(currentToken);
144            }
145            HttpURLConnection conn = super.createConnection(url, method);
146            AuthenticatedURL.injectToken(conn, currentToken);
147    
148            return conn;
149        }
150    
151    
152        protected boolean isCookieExpired(String cookie) {
153            if (cookie == null || cookie.isEmpty()) {
154                return true;
155            }
156            String [] cookieParts = cookie.split("&");
157            String expired = "";
158            for (String s: cookieParts) {
159                if (s.startsWith("e=")) {
160                    expired = s;
161                    break;
162                }
163            }
164    
165            if (expired.isEmpty()) {
166                return true;
167            }
168    
169            long expirationTime = 0;
170            try {
171                expirationTime = Long.parseLong(expired.split("=")[1]);
172            }
173            catch (NumberFormatException ex) {
174                return true;
175            }
176            return expirationTime < new Date().getTime();
177        }
178        /**
179         * Read a authentication token cached in the user home directory.
180         * <p/>
181         *
182         * @return the authentication token cached in the user home directory, NULL if none.
183         */
184        protected AuthenticatedURL.Token readAuthToken() {
185            AuthenticatedURL.Token authToken = null;
186            if (AUTH_TOKEN_CACHE_FILE.exists()) {
187                try {
188                    BufferedReader reader = new BufferedReader(new FileReader(AUTH_TOKEN_CACHE_FILE));
189                    String line = reader.readLine();
190                    reader.close();
191                    if (line != null) {
192                        authToken = new AuthenticatedURL.Token(line);
193                    }
194                }
195                catch (IOException ex) {
196                    //NOP
197                }
198            }
199            return authToken;
200        }
201    
202        /**
203         * Write the current authentication token to the user home directory.authOption
204         * <p/>
205         * The file is written with user only read/write permissions.
206         * <p/>
207         * If the file cannot be updated or the user only ready/write permissions cannot be set the file is deleted.
208         *
209         * @param authToken the authentication token to cache.
210         */
211        protected void writeAuthToken(AuthenticatedURL.Token authToken) {
212            try {
213                Writer writer = new FileWriter(AUTH_TOKEN_CACHE_FILE);
214                writer.write(authToken.toString());
215                writer.close();
216                // sets read-write permissions to owner only
217                AUTH_TOKEN_CACHE_FILE.setReadable(false, false);
218                AUTH_TOKEN_CACHE_FILE.setReadable(true, true);
219                AUTH_TOKEN_CACHE_FILE.setWritable(true, true);
220            }
221            catch (IOException ioe) {
222                // if case of any error we just delete the cache, if user-only
223                // write permissions are not properly set a security exception
224                // is thrown and the file will be deleted.
225                AUTH_TOKEN_CACHE_FILE.delete();
226            }
227        }
228    
229        /**
230         * Return the Hadoop-auth Authenticator to use.
231         * <p/>
232         * It first looks for value of command line option 'auth', if not set it continues to check
233         * {@link #AUTHENTICATOR_CLASS_SYS_PROP} Java system property for Authenticator.
234         * <p/>
235         * It the value of the {@link #AUTHENTICATOR_CLASS_SYS_PROP} is not set it uses
236         * Hadoop-auth <code>KerberosAuthenticator</code> which supports both Kerberos HTTP SPNEGO and Pseudo/simple
237         * authentication.
238         *
239         * @return the Authenticator to use, <code>NULL</code> if none.
240         *
241         * @throws OozieClientException thrown if the authenticator could not be instantiated.
242         */
243        protected Authenticator getAuthenticator() throws OozieClientException {
244            if (authOption != null) {
245                try {
246                    Class<? extends Authenticator> authClass = getAuthenticators().get(authOption.toUpperCase());
247                    if (authClass == null) {
248                        throw new OozieClientException(OozieClientException.AUTHENTICATION,
249                                "Authenticator class not found [" + authClass + "]");
250                    }
251                    return authClass.newInstance();
252                }
253                catch (IllegalArgumentException iae) {
254                    throw new OozieClientException(OozieClientException.AUTHENTICATION, "Invalid options provided for auth: " + authOption
255                            + ", (" + AuthType.KERBEROS + " or " + AuthType.SIMPLE + " expected.)");
256                }
257                catch (InstantiationException ex) {
258                    throw new OozieClientException(OozieClientException.AUTHENTICATION,
259                            "Could not instantiate Authenticator for option [" + authOption + "], " +
260                            ex.getMessage(), ex);
261                }
262                catch (IllegalAccessException ex) {
263                    throw new OozieClientException(OozieClientException.AUTHENTICATION,
264                            "Could not instantiate Authenticator for option [" + authOption + "], " +
265                            ex.getMessage(), ex);
266                }
267    
268            }
269    
270            String className = System.getProperty(AUTHENTICATOR_CLASS_SYS_PROP, KerberosAuthenticator.class.getName());
271            if (className != null) {
272                try {
273                    ClassLoader cl = Thread.currentThread().getContextClassLoader();
274                    Class<? extends Object> klass = (cl != null) ? cl.loadClass(className) :
275                        getClass().getClassLoader().loadClass(className);
276                    if (klass == null) {
277                        throw new OozieClientException(OozieClientException.AUTHENTICATION,
278                                "Authenticator class not found [" + className + "]");
279                    }
280                    return (Authenticator) klass.newInstance();
281                }
282                catch (Exception ex) {
283                    throw new OozieClientException(OozieClientException.AUTHENTICATION,
284                                                   "Could not instantiate Authenticator [" + className + "], " +
285                                                   ex.getMessage(), ex);
286                }
287            }
288            else {
289                throw new OozieClientException(OozieClientException.AUTHENTICATION,
290                                               "Authenticator class not found [" + className + "]");
291            }
292        }
293    
294        /**
295         * Get the map for classes of Authenticator.
296         * Default values are:
297         * null -> KerberosAuthenticator
298         * SIMPLE -> PseudoAuthenticator
299         * KERBEROS -> KerberosAuthenticator
300         *
301         * @return the map for classes of Authenticator
302         * @throws OozieClientException
303         */
304        protected Map<String, Class<? extends Authenticator>> getAuthenticators() {
305            Map<String, Class<? extends Authenticator>> authClasses = new HashMap<String, Class<? extends Authenticator>>();
306            authClasses.put(AuthType.KERBEROS.toString(), KerberosAuthenticator.class);
307            authClasses.put(AuthType.SIMPLE.toString(), PseudoAuthenticator.class);
308            authClasses.put(null, KerberosAuthenticator.class);
309            return authClasses;
310        }
311    
312        /**
313         * Get authOption
314         *
315         * @return the authOption
316         */
317        public String getAuthOption() {
318            return authOption;
319        }
320    
321    }