Search in sources :

Example 96 with URL

use of java.net.URL in project hadoop by apache.

the class KMSClientProvider method reencryptEncryptedKey.

@Override
public EncryptedKeyVersion reencryptEncryptedKey(EncryptedKeyVersion ekv) throws IOException, GeneralSecurityException {
    checkNotNull(ekv.getEncryptionKeyVersionName(), "versionName");
    checkNotNull(ekv.getEncryptedKeyIv(), "iv");
    checkNotNull(ekv.getEncryptedKeyVersion(), "encryptedKey");
    Preconditions.checkArgument(ekv.getEncryptedKeyVersion().getVersionName().equals(KeyProviderCryptoExtension.EEK), "encryptedKey version name must be '%s', is '%s'", KeyProviderCryptoExtension.EEK, ekv.getEncryptedKeyVersion().getVersionName());
    final Map<String, String> params = new HashMap<>();
    params.put(KMSRESTConstants.EEK_OP, KMSRESTConstants.EEK_REENCRYPT);
    final Map<String, Object> jsonPayload = new HashMap<>();
    jsonPayload.put(KMSRESTConstants.NAME_FIELD, ekv.getEncryptionKeyName());
    jsonPayload.put(KMSRESTConstants.IV_FIELD, Base64.encodeBase64String(ekv.getEncryptedKeyIv()));
    jsonPayload.put(KMSRESTConstants.MATERIAL_FIELD, Base64.encodeBase64String(ekv.getEncryptedKeyVersion().getMaterial()));
    final URL url = createURL(KMSRESTConstants.KEY_VERSION_RESOURCE, ekv.getEncryptionKeyVersionName(), KMSRESTConstants.EEK_SUB_RESOURCE, params);
    final HttpURLConnection conn = createConnection(url, HTTP_POST);
    conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON_MIME);
    final Map response = call(conn, jsonPayload, HttpURLConnection.HTTP_OK, Map.class);
    return parseJSONEncKeyVersion(ekv.getEncryptionKeyName(), response);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL)

Example 97 with URL

use of java.net.URL in project hadoop by apache.

the class KMSClientProvider method getKeysMetadata.

@Override
@SuppressWarnings("unchecked")
public Metadata[] getKeysMetadata(String... keyNames) throws IOException {
    List<Metadata> keysMetadata = new ArrayList<Metadata>();
    List<String[]> keySets = createKeySets(keyNames);
    for (String[] keySet : keySets) {
        if (keyNames.length > 0) {
            Map<String, Object> queryStr = new HashMap<String, Object>();
            queryStr.put(KMSRESTConstants.KEY, keySet);
            URL url = createURL(KMSRESTConstants.KEYS_METADATA_RESOURCE, null, null, queryStr);
            HttpURLConnection conn = createConnection(url, HTTP_GET);
            List<Map> list = call(conn, null, HttpURLConnection.HTTP_OK, List.class);
            for (Map map : list) {
                keysMetadata.add(parseJSONMetadata(map));
            }
        }
    }
    return keysMetadata.toArray(new Metadata[keysMetadata.size()]);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL) HttpURLConnection(java.net.HttpURLConnection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 98 with URL

use of java.net.URL in project hadoop by apache.

the class KMSClientProvider method getDelegationTokenService.

private Text getDelegationTokenService() throws IOException {
    URL url = new URL(kmsUrl);
    InetSocketAddress addr = new InetSocketAddress(url.getHost(), url.getPort());
    Text dtService = SecurityUtil.buildTokenService(addr);
    return dtService;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Text(org.apache.hadoop.io.Text) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL)

Example 99 with URL

use of java.net.URL in project hadoop by apache.

the class KMSClientProvider method call.

private <T> T call(HttpURLConnection conn, Map jsonOutput, int expectedResponse, Class<T> klass, int authRetryCount) throws IOException {
    T ret = null;
    try {
        if (jsonOutput != null) {
            writeJson(jsonOutput, conn.getOutputStream());
        }
    } catch (IOException ex) {
        IOUtils.closeStream(conn.getInputStream());
        throw ex;
    }
    if ((conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN && (conn.getResponseMessage().equals(ANONYMOUS_REQUESTS_DISALLOWED) || conn.getResponseMessage().contains(INVALID_SIGNATURE))) || conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
        // 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)..
        KMSClientProvider.this.authToken = new DelegationTokenAuthenticatedURL.Token();
        if (authRetryCount > 0) {
            String contentType = conn.getRequestProperty(CONTENT_TYPE);
            String requestMethod = conn.getRequestMethod();
            URL url = conn.getURL();
            conn = createConnection(url, requestMethod);
            conn.setRequestProperty(CONTENT_TYPE, contentType);
            return call(conn, jsonOutput, expectedResponse, klass, authRetryCount - 1);
        }
    }
    try {
        AuthenticatedURL.extractToken(conn, authToken);
    } catch (AuthenticationException e) {
    // 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 (conn.getContentType() != null && conn.getContentType().trim().toLowerCase().startsWith(APPLICATION_JSON_MIME) && klass != null) {
        ObjectMapper mapper = new ObjectMapper();
        InputStream is = null;
        try {
            is = conn.getInputStream();
            ret = mapper.readValue(is, klass);
        } finally {
            IOUtils.closeStream(is);
        }
    }
    return ret;
}
Also used : DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 100 with URL

use of java.net.URL in project hadoop by apache.

the class KMSClientProvider method getKeyVersions.

@Override
public List<KeyVersion> getKeyVersions(String name) throws IOException {
    checkNotEmpty(name, "name");
    URL url = createURL(KMSRESTConstants.KEY_RESOURCE, name, KMSRESTConstants.VERSIONS_SUB_RESOURCE, null);
    HttpURLConnection conn = createConnection(url, HTTP_GET);
    List response = call(conn, null, HttpURLConnection.HTTP_OK, List.class);
    List<KeyVersion> versions = null;
    if (!response.isEmpty()) {
        versions = new ArrayList<KeyVersion>();
        for (Object obj : response) {
            versions.add(parseJSONKeyVersion((Map) obj));
        }
    }
    return versions;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) EncryptedKeyVersion(org.apache.hadoop.crypto.key.KeyProviderCryptoExtension.EncryptedKeyVersion) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) DelegationTokenAuthenticatedURL(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL)

Aggregations

URL (java.net.URL)8112 IOException (java.io.IOException)2006 Test (org.junit.Test)1653 File (java.io.File)1638 MalformedURLException (java.net.MalformedURLException)1165 HttpURLConnection (java.net.HttpURLConnection)1030 InputStream (java.io.InputStream)1028 ArrayList (java.util.ArrayList)633 URLConnection (java.net.URLConnection)515 InputStreamReader (java.io.InputStreamReader)473 URLClassLoader (java.net.URLClassLoader)451 BufferedReader (java.io.BufferedReader)390 HashMap (java.util.HashMap)361 URISyntaxException (java.net.URISyntaxException)286 URI (java.net.URI)269 Map (java.util.Map)259 FileInputStream (java.io.FileInputStream)227 List (java.util.List)205 FileOutputStream (java.io.FileOutputStream)194 OutputStream (java.io.OutputStream)194