Skip to content

Commit

Permalink
FAB-10322 HFCAClient needs timeout settings
Browse files Browse the repository at this point in the history
Change-Id: I155916557518d2392b241d44a05027ffd6ca208d
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed May 23, 2018
1 parent 8f6944a commit 413a242
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/hyperledger/fabric_ca/sdk/HFCAClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -173,6 +174,10 @@ public class HFCAClient {

private static final Config config = Config.getConfig(); // DO NOT REMOVE THIS IS NEEDED TO MAKE SURE WE FIRST LOAD CONFIG!!!

private static final int CONNECTION_REQUEST_TIMEOUT = config.getConnectionRequestTimeout();
private static final int CONNECT_TIMEOUT = config.getConnectTimeout();
private static final int SOCKET_TIMEOUT = config.getSocketTimeout();

private static final Log logger = LogFactory.getLog(HFCAClient.class);

static final String FABRIC_CA_REQPROP = "caname";
Expand Down Expand Up @@ -1088,6 +1093,7 @@ String httpPost(String url, String body, UsernamePasswordCredentials credentials
HttpClient client = httpClientBuilder.build();

HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(getRequestConfig());

AuthCache authCache = new BasicAuthCache();

Expand Down Expand Up @@ -1142,6 +1148,7 @@ String httpPost(String url, String body, UsernamePasswordCredentials credentials
JsonObject httpPost(String url, String body, User registrar) throws Exception {
String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), body);
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(getRequestConfig());
logger.debug(format("httpPost %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));

final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
Expand All @@ -1163,6 +1170,7 @@ JsonObject httpGet(String url, User registrar) throws Exception {
String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "");
url = getURL(url);
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(getRequestConfig());
logger.debug(format("httpGet %s, authHTTPCert: %s", url, authHTTPCert));

final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
Expand All @@ -1182,6 +1190,7 @@ JsonObject httpGet(String url, User registrar) throws Exception {
JsonObject httpPut(String url, String body, User registrar) throws Exception {
String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), body);
HttpPut httpPut = new HttpPut(url);
httpPut.setConfig(getRequestConfig());
logger.debug(format("httpPutt %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert));

final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
Expand All @@ -1202,6 +1211,7 @@ JsonObject httpPut(String url, String body, User registrar) throws Exception {
JsonObject httpDelete(String url, User registrar) throws Exception {
String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "");
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.setConfig(getRequestConfig());
logger.debug(format("httpPut %s, authHTTPCert: %s", url, authHTTPCert));

final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
Expand Down Expand Up @@ -1461,5 +1471,17 @@ String toJson(JsonObject toJsonFunc) {
return stringWriter.toString();
}

private RequestConfig getRequestConfig() {

RequestConfig.Builder ret = RequestConfig.custom();

ret.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT);
ret.setConnectTimeout(CONNECT_TIMEOUT);
ret.setSocketTimeout(SOCKET_TIMEOUT);

return ret.build();

}

}

69 changes: 53 additions & 16 deletions src/main/java/org/hyperledger/fabric_ca/sdk/helper/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,29 @@
public class Config {
private static final Log logger = LogFactory.getLog(Config.class);

private static final String BASE_PROP = "org.hyperledger.fabric_ca.sdk.";

private static final String DEFAULT_CONFIG = "config.properties";
public static final String ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION = "org.hyperledger.fabric.sdk.configuration";
public static final String SECURITY_LEVEL = "org.hyperledger.fabric.sdk.security_level";
public static final String HASH_ALGORITHM = "org.hyperledger.fabric.sdk.hash_algorithm";
public static final String CACERTS = "org.hyperledger.fabric.sdk.cacerts";
public static final String PROPOSAL_WAIT_TIME = "org.hyperledger.fabric.sdk.proposal.wait.time";

public static final String ASYMMETRIC_KEY_TYPE = "org.hyperledger.fabric.sdk.crypto.asymmetric_key_type";
public static final String KEY_AGREEMENT_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.key_agreement_algorithm";
public static final String SYMMETRIC_KEY_TYPE = "org.hyperledger.fabric.sdk.crypto.symmetric_key_type";
public static final String SYMMETRIC_KEY_BYTE_COUNT = "org.hyperledger.fabric.sdk.crypto.symmetric_key_byte_count";
public static final String SYMMETRIC_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.symmetric_algorithm";
public static final String MAC_KEY_BYTE_COUNT = "org.hyperledger.fabric.sdk.crypto.mac_key_byte_count";
public static final String CERTIFICATE_FORMAT = "org.hyperledger.fabric.sdk.crypto.certificate_format";
public static final String SIGNATURE_ALGORITHM = "org.hyperledger.fabric.sdk.crypto.default_signature_algorithm";
public static final String MAX_LOG_STRING_LENGTH = "org.hyperledger.fabric.sdk.log.stringlengthmax";
public static final String LOGGERLEVEL = "org.hyperledger.fabric_ca.sdk.loglevel"; // ORG_HYPERLEDGER_FABRIC_CA_SDK_LOGLEVEL=TRACE,DEBUG
public static final String ORG_HYPERLEDGER_FABRIC_SDK_CONFIGURATION = BASE_PROP + "configuration";
public static final String SECURITY_LEVEL = BASE_PROP + "security_level";
public static final String HASH_ALGORITHM = BASE_PROP + "hash_algorithm";
public static final String CACERTS = "cacerts";
public static final String PROPOSAL_WAIT_TIME = BASE_PROP + "proposal.wait.time";

public static final String ASYMMETRIC_KEY_TYPE = BASE_PROP + "crypto.asymmetric_key_type";
public static final String KEY_AGREEMENT_ALGORITHM = BASE_PROP + "crypto.key_agreement_algorithm";
public static final String SYMMETRIC_KEY_TYPE = BASE_PROP + "crypto.symmetric_key_type";
public static final String SYMMETRIC_KEY_BYTE_COUNT = BASE_PROP + "crypto.symmetric_key_byte_count";
public static final String SYMMETRIC_ALGORITHM = BASE_PROP + "crypto.symmetric_algorithm";
public static final String MAC_KEY_BYTE_COUNT = BASE_PROP + "crypto.mac_key_byte_count";
public static final String CERTIFICATE_FORMAT = BASE_PROP + "crypto.certificate_format";
public static final String SIGNATURE_ALGORITHM = BASE_PROP + "crypto.default_signature_algorithm";
public static final String MAX_LOG_STRING_LENGTH = BASE_PROP + "log.stringlengthmax";
public static final String LOGGERLEVEL = BASE_PROP + "loglevel"; // ORG_HYPERLEDGER_FABRIC_CA_SDK_LOGLEVEL=TRACE,DEBUG

public static final String CONNECTION_REQUEST_TIMEOUT = BASE_PROP + "connection.connection_request_timeout"; //ORG_HYPERLEDGER_FABRIC_CA_SDK_CONNECTION_CONNECTION_REQUEST_TIMEOUT
public static final String CONNECT_TIMEOUT = BASE_PROP + "connection.connect_timeout"; //ORG_HYPERLEDGER_FABRIC_CA_SDK_CONNECTION_CONNECT_TIMEOUT
public static final String SOCKET_TIMEOUT = BASE_PROP + "connection.socket_timeout"; //ORG_HYPERLEDGER_FABRIC_CA_SDK_CONNECTION_SOCKET_TIMEOUT

private static Config config;
private static final Properties sdkProperties = new Properties();
Expand Down Expand Up @@ -84,6 +90,11 @@ private Config() {
defaultProperty(SIGNATURE_ALGORITHM, "SHA256withECDSA");
defaultProperty(SECURITY_LEVEL, "256");
defaultProperty(HASH_ALGORITHM, "SHA2");

defaultProperty(CONNECTION_REQUEST_TIMEOUT, "-1");
defaultProperty(CONNECT_TIMEOUT, "-1");
defaultProperty(SOCKET_TIMEOUT, "-1");

// TODO remove this once we have implemented MSP and get the peer certs from the channel
defaultProperty(CACERTS, "/genesisblock/peercacert.pem");

Expand Down Expand Up @@ -256,4 +267,30 @@ public int maxLogStringLength() {
return Integer.parseInt(getProperty(MAX_LOG_STRING_LENGTH));
}

/**
* milliseconds used when requesting a connection.
*
* @return
*/

public int getConnectionRequestTimeout() {
return Integer.parseInt(getProperty(CONNECTION_REQUEST_TIMEOUT));
}

/**
* Determines the timeout in milliseconds until a connection is established.
* @return
*/
public int getConnectTimeout() {
return Integer.parseInt(getProperty(CONNECT_TIMEOUT));
}

/**
* Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data
* @return
*/
public int getSocketTimeout() {

return Integer.parseInt(getProperty(SOCKET_TIMEOUT));
}
}

0 comments on commit 413a242

Please sign in to comment.