Java Code Examples for org.apache.hadoop.security.authentication.client.AuthenticatedURL#Token

The following examples show how to use org.apache.hadoop.security.authentication.client.AuthenticatedURL#Token . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URLConnectionFactory.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a url with read and connect timeouts
 *
 * @param url
 *          URL to open
 * @param isSpnego
 *          whether the url should be authenticated via SPNEGO
 * @return URLConnection
 * @throws IOException
 * @throws AuthenticationException
 */
public URLConnection openConnection(URL url, boolean isSpnego)
    throws IOException, AuthenticationException {
  if (isSpnego) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("open AuthenticatedURL connection" + url);
    }
    UserGroupInformation.getCurrentUser().checkTGTAndReloginFromKeytab();
    final AuthenticatedURL.Token authToken = new AuthenticatedURL.Token();
    return new AuthenticatedURL(new KerberosUgiAuthenticator(),
        connConfigurator).openConnection(url, authToken);
  } else {
    if (LOG.isDebugEnabled()) {
      LOG.debug("open URL connection");
    }
    URLConnection connection = url.openConnection();
    if (connection instanceof HttpURLConnection) {
      connConfigurator.configure((HttpURLConnection) connection);
    }
    return connection;
  }
}
 
Example 2
Source File: KerberosAuthenticator2.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * Performs SPNEGO authentication against the specified URL.
 * <p/>
 * If a token is given it does a NOP and returns the given token.
 * <p/>
 * If no token is given, it will perform the SPNEGO authentication sequence
 * using an HTTP <code>OPTIONS</code> request.
 *
 * @param url the URl to authenticate against.
 * @param token the authentication token being used for the user.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication error occurred.
 */
public void authenticate(URL url, AuthenticatedURL.Token token)
        throws IOException, AuthenticationException {
    if (!token.isSet()) {
        this.url = url;
        base64 = new Base64(0);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod(AUTH_HTTP_METHOD);
        conn.connect();
        if (isNegotiate()) {
            doSpnegoSequence(token);
        } else {
            getFallBackAuthenticator().authenticate(url, token);
        }
    }
}
 
Example 3
Source File: KerberosWebHDFSConnection2.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
public KerberosWebHDFSConnection2(String httpfsUrl, String principal, String password)  {
        this.httpfsUrl = httpfsUrl;
        this.principal = principal;
        this.password = password;

        Configuration conf = new Configuration();
        conf.addResource("conf/hdfs-site.xml");
        conf.addResource("conf/core-site.xml");
        newToken = new AuthenticatedURL.Token();

        KerberosAuthenticator ka = new KerberosAuthenticator();
        ConnectionConfigurator connectionConfigurator = new SSLFactory(SSLFactory.Mode.CLIENT,conf);
        ka.setConnectionConfigurator(connectionConfigurator);

        try{
            URL url = new URL(httpfsUrl);
            ka.authenticate(url,newToken);
        }catch(Exception e){
            e.printStackTrace();
        }


         this.authenticatedURL = new AuthenticatedURL(ka,connectionConfigurator);
//        this.authenticatedURL = new AuthenticatedURL(
//                new KerberosAuthenticator2(principal, password));
    }
 
Example 4
Source File: WhoClient.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  try {
    if (args.length != 1) {
      System.err.println("Usage: <URL>");
      System.exit(-1);
    }
    AuthenticatedURL.Token token = new AuthenticatedURL.Token();
    URL url = new URL(args[0]);
    HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
    System.out.println();
    System.out.println("Token value: " + token);
    System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
    System.out.println();
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
      BufferedReader reader = new BufferedReader(
          new InputStreamReader(
              conn.getInputStream(), Charset.forName("UTF-8")));
      String line = reader.readLine();
      while (line != null) {
        System.out.println(line);
        line = reader.readLine();
      }
      reader.close();
    }
    System.out.println();
  }
  catch (Exception ex) {
    System.err.println("ERROR: " + ex.getMessage());
    System.exit(-1);
  }
}
 
Example 5
Source File: AnalyticJobGeneratorHadoop2.java    From dr-elephant with Apache License 2.0 5 votes vote down vote up
/**
 * Authenticate and update the token
 */
private void updateAuthToken() {
  if (_currentTime - _tokenUpdatedTime > TOKEN_UPDATE_INTERVAL) {
    logger.info("AnalysisProvider updating its Authenticate Token...");
    _token = new AuthenticatedURL.Token();
    _authenticatedURL = new AuthenticatedURL();
    _tokenUpdatedTime = _currentTime;
  }
}
 
Example 6
Source File: DelegationTokenAuthenticator.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void authenticate(URL url, AuthenticatedURL.Token token)
    throws IOException, AuthenticationException {
  if (!hasDelegationToken(url, token)) {
    authenticator.authenticate(url, token);
  }
}
 
Example 7
Source File: DelegationTokenAuthenticator.java    From big-c with Apache License 2.0 5 votes vote down vote up
private boolean hasDelegationToken(URL url, AuthenticatedURL.Token token) {
  boolean hasDt = false;
  if (token instanceof DelegationTokenAuthenticatedURL.Token) {
    hasDt = ((DelegationTokenAuthenticatedURL.Token) token).
        getDelegationToken() != null;
  }
  if (!hasDt) {
    String queryStr = url.getQuery();
    hasDt = (queryStr != null) && queryStr.contains(DELEGATION_PARAM + "=");
  }
  return hasDt;
}
 
Example 8
Source File: WhoClient.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  try {
    if (args.length != 1) {
      System.err.println("Usage: <URL>");
      System.exit(-1);
    }
    AuthenticatedURL.Token token = new AuthenticatedURL.Token();
    URL url = new URL(args[0]);
    HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
    System.out.println();
    System.out.println("Token value: " + token);
    System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
    System.out.println();
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
      BufferedReader reader = new BufferedReader(
          new InputStreamReader(
              conn.getInputStream(), Charset.forName("UTF-8")));
      String line = reader.readLine();
      while (line != null) {
        System.out.println(line);
        line = reader.readLine();
      }
      reader.close();
    }
    System.out.println();
  }
  catch (Exception ex) {
    System.err.println("ERROR: " + ex.getMessage());
    System.exit(-1);
  }
}
 
Example 9
Source File: Client.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Initiate client side Kerberos negotiation with the server.
 * @param method method to inject the authentication token into.
 * @param uri the String to parse as a URL.
 * @throws IOException if unknown protocol is found.
 */
private void negotiate(HttpUriRequest method, String uri) throws IOException {
  try {
    AuthenticatedURL.Token token = new AuthenticatedURL.Token();
    KerberosAuthenticator authenticator = new KerberosAuthenticator();
    authenticator.authenticate(new URL(uri), token);
    // Inject the obtained negotiated token in the method cookie
    injectToken(method, token);
  } catch (AuthenticationException e) {
    LOG.error("Failed to negotiate with the server.", e);
    throw new IOException(e);
  }
}
 
Example 10
Source File: KerberosWebHDFSConnection2.java    From Transwarp-Sample-Code with MIT License 5 votes vote down vote up
/**
     * <b>GETHOMEDIRECTORY</b>
     *
     * curl -i "http://<HOST>:<PORT>/webhdfs/v1/?op=GETHOMEDIRECTORY"
     *
     * @return
     * @throws MalformedURLException
     * @throws IOException
     * @throws AuthenticationException
     */
    public String getHomeDirectory() throws MalformedURLException, IOException,
            AuthenticationException {
//        ensureValidToken();
        Configuration conf = new Configuration();
        conf.addResource("conf/hdfs-site.xml");
        conf.addResource("conf/core-site.xml");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromPassword("hdfs", "123456");
        FileSystem fs = FileSystem.get(conf);
        System.out.println(fs.getDelegationToken("hdfs"));

        Token token0 = new AuthenticatedURL.Token("HAAEaGRmcwRoZGZzAIoBWOLlnNuKAVkG8iDbbwgU246eZ3EbfUsfNlF4F0xoew3LW3QSV0VCSERGUyBkZWxlZ2F0aW9uEDE3Mi4xNi4yLjk2OjgwMjA");
        System.out.println(token0.toString());
        System.out.println(fs.getDelegationToken("hdfs").encodeToUrlString());


        HttpURLConnection connection = authenticatedURL.openConnection(new URL(
                new URL(httpfsUrl), "/webhdfs/v1/?op=GETDELEGATIONTOKEN"), token);
        HttpURLConnection conn = authenticatedURL.openConnection(new URL(
                new URL(httpfsUrl), "/webhdfs/v1/?delegation=HAAEaGRmcwRoZGZzAIoBWOLlnNuKAVkG8iDbbwgU246eZ3EbfUsfNlF4F0xoew3LW3QSV0VCSERGUyBkZWxlZ2F0aW9uEDE3Mi4xNi4yLjk2OjgwMjA&op=GETHOMEDIRECTORY"), token0);


        conn.connect();
        connection.connect();
       String ss = result(connection,true);
        System.out.println(ss);
        String resp = result(conn, true);
        conn.disconnect();
        return resp;
    }
 
Example 11
Source File: ThreadContextMR2.java    From dr-elephant with Apache License 2.0 5 votes vote down vote up
@Override
public AuthenticatedURL.Token initialValue() {
    _LOCAL_LAST_UPDATED.set(System.currentTimeMillis());
    // Random an interval for each executor to avoid update token at the same time
    _LOCAL_UPDATE_INTERVAL.set(Statistics.MINUTE_IN_MS * 30 + RANDOM_GENERATOR.nextLong()
            % (3 * Statistics.MINUTE_IN_MS));
    logger.info("Executor " + _LOCAL_THREAD_ID.get() + " update interval " + _LOCAL_UPDATE_INTERVAL.get() * 1.0
            / Statistics.MINUTE_IN_MS);
    return new AuthenticatedURL.Token();
}
 
Example 12
Source File: Client.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that injects an authentication token to send with the method.
 * @param method method to inject the authentication token into.
 * @param token authentication token to inject.
 */
private void injectToken(HttpUriRequest method, AuthenticatedURL.Token token) {
  String t = token.toString();
  if (t != null) {
    if (!t.startsWith("\"")) {
      t = "\"" + t + "\"";
    }
    method.addHeader(COOKIE, AUTH_COOKIE_EQ + t);
  }
}
 
Example 13
Source File: DelegationTokenAuthenticator.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void authenticate(URL url, AuthenticatedURL.Token token)
    throws IOException, AuthenticationException {
  if (!hasDelegationToken(url, token)) {
    authenticator.authenticate(url, token);
  }
}
 
Example 14
Source File: DelegationTokenAuthenticator.java    From big-c with Apache License 2.0 3 votes vote down vote up
/**
 * Renews a delegation token from the server end-point using the
 * configured <code>Authenticator</code> for authentication.
 *
 * @param url the URL to renew the delegation token from. Only HTTP/S URLs are
 * supported.
 * @param token the authentication token with the Delegation Token to renew.
 * @param doAsUser the user to do as, which will be the token owner.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
public long renewDelegationToken(URL url,
    AuthenticatedURL.Token token,
    Token<AbstractDelegationTokenIdentifier> dToken, String doAsUser)
    throws IOException, AuthenticationException {
  Map json = doDelegationTokenOperation(url, token,
      DelegationTokenOperation.RENEWDELEGATIONTOKEN, null, dToken, true,
      doAsUser);
  return (Long) json.get(RENEW_DELEGATION_TOKEN_JSON);
}
 
Example 15
Source File: DelegationTokenAuthenticator.java    From hadoop with Apache License 2.0 3 votes vote down vote up
/**
 * Cancels a delegation token from the server end-point. It does not require
 * being authenticated by the configured <code>Authenticator</code>.
 *
 * @param url the URL to cancel the delegation token from. Only HTTP/S URLs
 * are supported.
 * @param token the authentication token with the Delegation Token to cancel.
 * @throws IOException if an IO error occurred.
 */
public void cancelDelegationToken(URL url,
    AuthenticatedURL.Token token,
    Token<AbstractDelegationTokenIdentifier> dToken)
    throws IOException {
  cancelDelegationToken(url, token, dToken, null);
}
 
Example 16
Source File: DelegationTokenAuthenticator.java    From big-c with Apache License 2.0 3 votes vote down vote up
/**
 * Cancels a delegation token from the server end-point. It does not require
 * being authenticated by the configured <code>Authenticator</code>.
 *
 * @param url the URL to cancel the delegation token from. Only HTTP/S URLs
 * are supported.
 * @param token the authentication token with the Delegation Token to cancel.
 * @throws IOException if an IO error occurred.
 */
public void cancelDelegationToken(URL url,
    AuthenticatedURL.Token token,
    Token<AbstractDelegationTokenIdentifier> dToken)
    throws IOException {
  cancelDelegationToken(url, token, dToken, null);
}
 
Example 17
Source File: DelegationTokenAuthenticator.java    From hadoop with Apache License 2.0 3 votes vote down vote up
/**
 * Renews a delegation token from the server end-point using the
 * configured <code>Authenticator</code> for authentication.
 *
 * @param url the URL to renew the delegation token from. Only HTTP/S URLs are
 * supported.
 * @param token the authentication token with the Delegation Token to renew.
 * @param doAsUser the user to do as, which will be the token owner.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
public long renewDelegationToken(URL url,
    AuthenticatedURL.Token token,
    Token<AbstractDelegationTokenIdentifier> dToken, String doAsUser)
    throws IOException, AuthenticationException {
  Map json = doDelegationTokenOperation(url, token,
      DelegationTokenOperation.RENEWDELEGATIONTOKEN, null, dToken, true,
      doAsUser);
  return (Long) json.get(RENEW_DELEGATION_TOKEN_JSON);
}
 
Example 18
Source File: DelegationTokenAuthenticatedURL.java    From big-c with Apache License 2.0 3 votes vote down vote up
/**
 * Returns an authenticated {@link HttpURLConnection}, it uses a Delegation
 * Token only if the given auth token is an instance of {@link Token} and
 * it contains a Delegation Token, otherwise use the configured
 * {@link DelegationTokenAuthenticator} to authenticate the connection.
 *
 * @param url the URL to connect to. Only HTTP/S URLs are supported.
 * @param token the authentication token being used for the user.
 * @return an authenticated {@link HttpURLConnection}.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
@Override
public HttpURLConnection openConnection(URL url, AuthenticatedURL.Token token)
    throws IOException, AuthenticationException {
  return (token instanceof Token) ? openConnection(url, (Token) token)
                                  : super.openConnection(url ,token);
}
 
Example 19
Source File: DelegationTokenAuthenticator.java    From big-c with Apache License 2.0 3 votes vote down vote up
/**
 * Renews a delegation token from the server end-point using the
 * configured <code>Authenticator</code> for authentication.
 *
 * @param url the URL to renew the delegation token from. Only HTTP/S URLs are
 * supported.
 * @param token the authentication token with the Delegation Token to renew.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
public long renewDelegationToken(URL url,
    AuthenticatedURL.Token token,
    Token<AbstractDelegationTokenIdentifier> dToken)
    throws IOException, AuthenticationException {
  return renewDelegationToken(url, token, dToken, null);
}
 
Example 20
Source File: DelegationTokenAuthenticator.java    From hadoop with Apache License 2.0 2 votes vote down vote up
/**
 * Requests a delegation token using the configured <code>Authenticator</code>
 * for authentication.
 *
 * @param url the URL to get the delegation token from. Only HTTP/S URLs are
 * supported.
 * @param token the authentication token being used for the user where the
 * Delegation token will be stored.
 * @param renewer the renewer user.
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
public Token<AbstractDelegationTokenIdentifier> getDelegationToken(URL url,
    AuthenticatedURL.Token token, String renewer)
    throws IOException, AuthenticationException {
 return getDelegationToken(url, token, renewer, null);
}