Example #1
0
            /// <exception cref="System.Exception"/>
            public Void Call()
            {
                Uri url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=GETHOMEDIRECTORY"
                                  );
                AuthenticatedURL aUrl = new AuthenticatedURL();

                AuthenticatedURL.Token aToken = new AuthenticatedURL.Token();
                HttpURLConnection      conn   = aUrl.OpenConnection(url, aToken);

                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpOk);
                return(null);
            }
Example #2
0
            /// <exception cref="System.Exception"/>
            public Void Call()
            {
                //get delegation token doing SPNEGO authentication
                Uri url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=GETDELEGATIONTOKEN"
                                  );
                AuthenticatedURL aUrl = new AuthenticatedURL();

                AuthenticatedURL.Token aToken = new AuthenticatedURL.Token();
                HttpURLConnection      conn   = aUrl.OpenConnection(url, aToken);

                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpOk);
                JSONObject json = (JSONObject) new JSONParser().Parse(new InputStreamReader(conn.GetInputStream
                                                                                                ()));

                json = (JSONObject)json[DelegationTokenAuthenticator.DelegationTokenJson];
                string tokenStr = (string)json[DelegationTokenAuthenticator.DelegationTokenUrlStringJson
                                  ];

                //access httpfs using the delegation token
                url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=GETHOMEDIRECTORY&delegation="
                              + tokenStr);
                conn = (HttpURLConnection)url.OpenConnection();
                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpOk);
                //try to renew the delegation token without SPNEGO credentials
                url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=RENEWDELEGATIONTOKEN&token="
                              + tokenStr);
                conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod("PUT");
                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpUnauthorized
                                                );
                //renew the delegation token with SPNEGO credentials
                url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=RENEWDELEGATIONTOKEN&token="
                              + tokenStr);
                conn = aUrl.OpenConnection(url, aToken);
                conn.SetRequestMethod("PUT");
                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpOk);
                //cancel delegation token, no need for SPNEGO credentials
                url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=CANCELDELEGATIONTOKEN&token="
                              + tokenStr);
                conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod("PUT");
                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpOk);
                //try to access httpfs with the canceled delegation token
                url = new Uri(TestJettyHelper.GetJettyURL(), "/webhdfs/v1/?op=GETHOMEDIRECTORY&delegation="
                              + tokenStr);
                conn = (HttpURLConnection)url.OpenConnection();
                NUnit.Framework.Assert.AreEqual(conn.GetResponseCode(), HttpURLConnection.HttpUnauthorized
                                                );
                return(null);
            }
Example #3
0
 public static void Main(string[] args)
 {
     try
     {
         if (args.Length != 1)
         {
             System.Console.Error.WriteLine("Usage: <URL>");
             System.Environment.Exit(-1);
         }
         AuthenticatedURL.Token token = new AuthenticatedURL.Token();
         Uri url = new Uri(args[0]);
         HttpURLConnection conn = new AuthenticatedURL().OpenConnection(url, token);
         System.Console.Out.WriteLine();
         System.Console.Out.WriteLine("Token value: " + token);
         System.Console.Out.WriteLine("Status code: " + conn.GetResponseCode() + " " + conn
                                      .GetResponseMessage());
         System.Console.Out.WriteLine();
         if (conn.GetResponseCode() == HttpURLConnection.HttpOk)
         {
             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.GetInputStream
                                                                                  (), Extensions.GetEncoding("UTF-8")));
             string line = reader.ReadLine();
             while (line != null)
             {
                 System.Console.Out.WriteLine(line);
                 line = reader.ReadLine();
             }
             reader.Close();
         }
         System.Console.Out.WriteLine();
     }
     catch (Exception ex)
     {
         System.Console.Error.WriteLine("ERROR: " + ex.Message);
         System.Environment.Exit(-1);
     }
 }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException
        ///     "/>
        private IDictionary DoDelegationTokenOperation <_T0>(Uri url, AuthenticatedURL.Token
                                                             token, DelegationTokenAuthenticator.DelegationTokenOperation operation, string
                                                             renewer, Org.Apache.Hadoop.Security.Token.Token <_T0> dToken, bool hasResponse, string
                                                             doAsUser)
            where _T0 : TokenIdentifier
        {
            IDictionary ret = null;
            IDictionary <string, string> @params = new Dictionary <string, string>();

            @params[OpParam] = operation.ToString();
            if (renewer != null)
            {
                @params[RenewerParam] = renewer;
            }
            if (dToken != null)
            {
                @params[TokenParam] = dToken.EncodeToUrlString();
            }
            // proxyuser
            if (doAsUser != null)
            {
                @params[DelegationTokenAuthenticatedURL.DoAs] = URLEncoder.Encode(doAsUser, "UTF-8"
                                                                                  );
            }
            string        urlStr    = url.ToExternalForm();
            StringBuilder sb        = new StringBuilder(urlStr);
            string        separator = (urlStr.Contains("?")) ? "&" : "?";

            foreach (KeyValuePair <string, string> entry in @params)
            {
                sb.Append(separator).Append(entry.Key).Append("=").Append(URLEncoder.Encode(entry
                                                                                            .Value, "UTF8"));
                separator = "&";
            }
            url = new Uri(sb.ToString());
            AuthenticatedURL  aUrl = new AuthenticatedURL(this, connConfigurator);
            HttpURLConnection conn = aUrl.OpenConnection(url, token);

            conn.SetRequestMethod(operation.GetHttpMethod());
            HttpExceptionUtils.ValidateResponse(conn, HttpURLConnection.HttpOk);
            if (hasResponse)
            {
                string contentType = conn.GetHeaderField(ContentType);
                contentType = (contentType != null) ? StringUtils.ToLowerCase(contentType) : null;
                if (contentType != null && contentType.Contains(ApplicationJsonMime))
                {
                    try
                    {
                        ObjectMapper mapper = new ObjectMapper();
                        ret = mapper.ReadValue <IDictionary>(conn.GetInputStream());
                    }
                    catch (Exception ex)
                    {
                        throw new AuthenticationException(string.Format("'%s' did not handle the '%s' delegation token operation: %s"
                                                                        , url.GetAuthority(), operation, ex.Message), ex);
                    }
                }
                else
                {
                    throw new AuthenticationException(string.Format("'%s' did not " + "respond with JSON to the '%s' delegation token operation"
                                                                    , url.GetAuthority(), operation));
                }
            }
            return(ret);
        }
Example #5
0
        /// <exception cref="System.IO.IOException"/>
        private T Call <T>(HttpURLConnection conn, IDictionary jsonOutput, int expectedResponse
                           , int authRetryCount)
        {
            System.Type klass = typeof(T);
            T           ret   = null;

            try
            {
                if (jsonOutput != null)
                {
                    WriteJson(jsonOutput, conn.GetOutputStream());
                }
            }
            catch (IOException ex)
            {
                conn.GetInputStream().Close();
                throw;
            }
            if ((conn.GetResponseCode() == HttpURLConnection.HttpForbidden && (conn.GetResponseMessage
                                                                                   ().Equals(AnonymousRequestsDisallowed) || conn.GetResponseMessage().Contains(InvalidSignature
                                                                                                                                                                ))) || conn.GetResponseCode() == HttpURLConnection.HttpUnauthorized)
            {
                // Ideally, this should happen only when there is an Authentication
                // failure. Unfortunately, the AuthenticationFilter returns 403 when it
                // cannot authenticate (Since a 401 requires Server to send
                // WWW-Authenticate header as well)..
                this.authToken = new DelegationTokenAuthenticatedURL.Token();
                if (authRetryCount > 0)
                {
                    string contentType   = conn.GetRequestProperty(ContentType);
                    string requestMethod = conn.GetRequestMethod();
                    Uri    url           = conn.GetURL();
                    conn = CreateConnection(url, requestMethod);
                    conn.SetRequestProperty(ContentType, contentType);
                    return(Call(conn, jsonOutput, expectedResponse, klass, authRetryCount - 1));
                }
            }
            try
            {
                AuthenticatedURL.ExtractToken(conn, authToken);
            }
            catch (AuthenticationException)
            {
            }
            // Ignore the AuthExceptions.. since we are just using the method to
            // extract and set the authToken.. (Workaround till we actually fix
            // AuthenticatedURL properly to set authToken post initialization)
            HttpExceptionUtils.ValidateResponse(conn, expectedResponse);
            if (Runtime.EqualsIgnoreCase(ApplicationJsonMime, conn.GetContentType()) &&
                klass != null)
            {
                ObjectMapper mapper = new ObjectMapper();
                InputStream  @is    = null;
                try
                {
                    @is = conn.GetInputStream();
                    ret = mapper.ReadValue(@is, klass);
                }
                catch (IOException ex)
                {
                    if (@is != null)
                    {
                        @is.Close();
                    }
                    throw;
                }
                finally
                {
                    if (@is != null)
                    {
                        @is.Close();
                    }
                }
            }
            return(ret);
        }