@Override public void init(String serviceName, String appId, String configPropertyPrefix) { if (LOG.isDebugEnabled()) { LOG.debug("==> RangerAdminJersey2RESTClient.init(" + configPropertyPrefix + ")"); } _serviceName = serviceName; _pluginId = _utils.getPluginId(serviceName, appId); _baseUrl = _utils.getPolicyRestUrl(configPropertyPrefix); _sslConfigFileName = _utils.getSsslConfigFileName(configPropertyPrefix); _isSSL = _utils.isSsl(_baseUrl); _restClientConnTimeOutMs = RangerConfiguration.getInstance() .getInt(configPropertyPrefix + ".policy.rest.client.connection.timeoutMs", 120 * 1000); _restClientReadTimeOutMs = RangerConfiguration.getInstance() .getInt(configPropertyPrefix + ".policy.rest.client.read.timeoutMs", 30 * 1000); LOG.info( "Init params: " + String.format( "Base URL[%s], SSL Congig filename[%s], ServiceName=[%s]", _baseUrl, _sslConfigFileName, _serviceName)); _client = getClient(); _client.property(ClientProperties.CONNECT_TIMEOUT, _restClientConnTimeOutMs); _client.property(ClientProperties.READ_TIMEOUT, _restClientReadTimeOutMs); if (LOG.isDebugEnabled()) { LOG.debug( "<== RangerAdminJersey2RESTClient.init(" + configPropertyPrefix + "): " + _client.toString()); } }
@Override public void revokeAccess(GrantRevokeRequest request) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("==> RangerAdminRESTClient.grantAccess(" + request + ")"); } String url = _utils.getUrlForRevokeAccess(_baseUrl, _serviceName); Response response = _client .target(url) .queryParam(RangerRESTUtils.REST_PARAM_PLUGIN_ID, _pluginId) .request(MediaType.APPLICATION_JSON_TYPE) .get(); int httpResponseCode = response == null ? -1 : response.getStatus(); switch (httpResponseCode) { case -1: LOG.warn( "Unexpected: Null response from policy server while grating access! Returning null!"); throw new Exception("unknown error!"); case 200: LOG.debug("grantAccess() suceeded: HTTP status=" + httpResponseCode); break; case 401: throw new AccessControlException(); default: String body = response.readEntity(String.class); String message = String.format( "Unexpected: Received status[%d] with body[%s] form url[%s]", httpResponseCode, body, url); LOG.warn(message); throw new Exception("HTTP status: " + httpResponseCode); } if (LOG.isDebugEnabled()) { LOG.debug("<== RangerAdminRESTClient.grantAccess(" + request + ")"); } }
@Override public ServicePolicies getServicePoliciesIfUpdated(long lastKnownVersion) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug( "==> RangerAdminJersey2RESTClient.getServicePoliciesIfUpdated(" + lastKnownVersion + ")"); } ServicePolicies servicePolicies = null; String url = _utils.getUrlForPolicyUpdate(_baseUrl, _serviceName); try { Response response = _client .target(url) .queryParam( RangerRESTUtils.REST_PARAM_LAST_KNOWN_POLICY_VERSION, Long.toString(lastKnownVersion)) .queryParam(RangerRESTUtils.REST_PARAM_PLUGIN_ID, _pluginId) .request(MediaType.APPLICATION_JSON_TYPE) .get(); int httpResponseCode = response == null ? -1 : response.getStatus(); String body = null; switch (httpResponseCode) { case 200: body = response.readEntity(String.class); if (LOG.isDebugEnabled()) { LOG.debug("Response from 200 server: " + body); } Gson gson = getGson(); servicePolicies = gson.fromJson(body, ServicePolicies.class); if (LOG.isDebugEnabled()) { LOG.debug("Deserialized response to: " + servicePolicies); } break; case 304: LOG.debug("Got response: 304. Ok. Returning null"); break; case -1: LOG.warn( "Unexpected: Null response from policy server while trying to get policies! Returning null!"); break; default: body = response.readEntity(String.class); LOG.warn( String.format( "Unexpected: Received status[%d] with body[%s] form url[%s]", httpResponseCode, body, url)); break; } if (LOG.isDebugEnabled()) { LOG.debug( "<== RangerAdminJersey2RESTClient.getServicePoliciesIfUpdated(" + lastKnownVersion + "): " + servicePolicies); } return servicePolicies; } catch (Exception ex) { LOG.error( "Failed getting policies from server. url=" + url + ", pluginId=" + _pluginId + ", lastKnownVersion=" + lastKnownVersion); throw ex; } }