use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HFCAIdentity method create.
/**
* create an identity
*
* @param registrar The identity of the registrar (i.e. who is performing the registration).
* @return statusCode The HTTP status code in the response
* @throws IdentityException if creating an identity fails.
* @throws InvalidArgumentException Invalid (null) argument specified
*/
public int create(User registrar) throws IdentityException, InvalidArgumentException {
if (this.deleted) {
throw new IdentityException("Identity has been deleted");
}
if (registrar == null) {
throw new InvalidArgumentException("Registrar should be a valid member");
}
String createURL = "";
try {
createURL = client.getURL(HFCA_IDENTITY);
logger.debug(format("identity url: %s, registrar: %s", createURL, registrar.getName()));
String body = client.toJson(idToJsonObject());
JsonObject result = client.httpPost(createURL, body, registrar);
statusCode = result.getInt("statusCode");
if (statusCode >= 400) {
getHFCAIdentity(result);
logger.debug(format("identity url: %s, registrar: %s done.", createURL, registrar));
}
this.deleted = false;
return statusCode;
} catch (HTTPException e) {
String msg = format("[Code: %d] - Error while creating user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), createURL, e.getMessage());
IdentityException identityException = new IdentityException(msg, e);
logger.error(msg);
throw identityException;
} catch (Exception e) {
String msg = format("Error while creating user '%s' from url '%s': %s", getEnrollmentId(), createURL, e.getMessage());
IdentityException identityException = new IdentityException(msg, e);
logger.error(msg);
throw identityException;
}
}
use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HFCAClient method enroll.
/**
* Enroll the user with member service
*
* @param user Identity name to enroll
* @param secret Secret returned via registration
* @param req Enrollment request with the following fields: hosts, profile, csr, label, keypair
* @return enrollment
* @throws EnrollmentException
* @throws InvalidArgumentException
*/
public Enrollment enroll(String user, String secret, EnrollmentRequest req) throws EnrollmentException, InvalidArgumentException {
logger.debug(format("url:%s enroll user: %s", url, user));
if (Utils.isNullOrEmpty(user)) {
throw new InvalidArgumentException("enrollment user is not set");
}
if (Utils.isNullOrEmpty(secret)) {
throw new InvalidArgumentException("enrollment secret is not set");
}
if (cryptoSuite == null) {
throw new InvalidArgumentException("Crypto primitives not set.");
}
setUpSSL();
try {
String pem = req.getCsr();
KeyPair keypair = req.getKeyPair();
if (null != pem && keypair == null) {
throw new InvalidArgumentException("If certificate signing request is supplied the key pair needs to be supplied too.");
}
if (keypair == null) {
logger.debug("[HFCAClient.enroll] Generating keys...");
// generate ECDSA keys: signing and encryption keys
keypair = cryptoSuite.keyGen();
logger.debug("[HFCAClient.enroll] Generating keys...done!");
}
if (pem == null) {
String csr = cryptoSuite.generateCertificationRequest(user, keypair);
req.setCSR(csr);
}
if (caName != null && !caName.isEmpty()) {
req.setCAName(caName);
}
String body = req.toJson();
String responseBody = httpPost(url + HFCA_ENROLL, body, new UsernamePasswordCredentials(user, secret));
logger.debug("response:" + responseBody);
JsonReader reader = Json.createReader(new StringReader(responseBody));
JsonObject jsonst = (JsonObject) reader.read();
boolean success = jsonst.getBoolean("success");
logger.debug(format("[HFCAClient] enroll success:[%s]", success));
if (!success) {
throw new EnrollmentException(format("FabricCA failed enrollment for user %s response success is false.", user));
}
JsonObject result = jsonst.getJsonObject("result");
if (result == null) {
throw new EnrollmentException(format("FabricCA failed enrollment for user %s - response did not contain a result", user));
}
Base64.Decoder b64dec = Base64.getDecoder();
String signedPem = new String(b64dec.decode(result.getString("Cert").getBytes(UTF_8)));
logger.debug(format("[HFCAClient] enroll returned pem:[%s]", signedPem));
JsonArray messages = jsonst.getJsonArray("messages");
if (messages != null && !messages.isEmpty()) {
JsonObject jo = messages.getJsonObject(0);
String message = format("Enroll request response message [code %d]: %s", jo.getInt("code"), jo.getString("message"));
logger.info(message);
}
logger.debug("Enrollment done.");
return new HFCAEnrollment(keypair, signedPem);
} catch (EnrollmentException ee) {
logger.error(format("url:%s, user:%s error:%s", url, user, ee.getMessage()), ee);
throw ee;
} catch (Exception e) {
EnrollmentException ee = new EnrollmentException(format("Url:%s, Failed to enroll user %s ", url, user), e);
logger.error(e.getMessage(), e);
throw ee;
}
}
use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HFCAAffiliation method delete.
/**
* delete an affiliation
*
* @param registrar The identity of the registrar (i.e. who is performing the registration).
* @param force Forces the deletion of affiliation
* @return Response of request
* @throws AffiliationException if deleting an affiliation fails.
* @throws InvalidArgumentException
*/
public HFCAAffiliationResp delete(User registrar, boolean force) throws AffiliationException, InvalidArgumentException {
if (this.deleted) {
throw new AffiliationException("Affiliation has been deleted");
}
if (registrar == null) {
throw new InvalidArgumentException("Registrar should be a valid member");
}
String deleteURL = "";
try {
Map<String, String> queryParm = new HashMap<String, String>();
queryParm.put("force", String.valueOf(force));
deleteURL = client.getURL(HFCA_AFFILIATION + "/" + this.name, queryParm);
logger.debug(format("affiliation url: %s, registrar: %s", deleteURL, registrar.getName()));
JsonObject result = client.httpDelete(deleteURL, registrar);
logger.debug(format("identity url: %s, registrar: %s done.", deleteURL, registrar));
this.deleted = true;
return getResponse(result);
} catch (HTTPException e) {
String msg = format("[Code: %d] - Error while deleting affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, deleteURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
} catch (Exception e) {
String msg = format("Error while deleting affiliation %s url: %s %s ", this.name, deleteURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
}
}
use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HFCAAffiliation method read.
/**
* gets a specific affiliation
*
* @param registrar The identity of the registrar
* @return Returns response
* @throws AffiliationException if getting an affiliation fails.
* @throws InvalidArgumentException
*/
public int read(User registrar) throws AffiliationException, InvalidArgumentException {
if (registrar == null) {
throw new InvalidArgumentException("Registrar should be a valid member");
}
String readAffURL = "";
try {
readAffURL = HFCA_AFFILIATION + "/" + name;
logger.debug(format("affiliation url: %s, registrar: %s", readAffURL, registrar.getName()));
JsonObject result = client.httpGet(readAffURL, registrar);
logger.debug(format("affiliation url: %s, registrar: %s done.", readAffURL, registrar));
HFCAAffiliationResp resp = getResponse(result);
this.childHFCAAffiliations = resp.getChildren();
this.identities = resp.getIdentities();
this.deleted = false;
return resp.statusCode;
} catch (HTTPException e) {
String msg = format("[Code: %d] - Error while getting affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, readAffURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
} catch (Exception e) {
String msg = format("Error while getting affiliation %s url: %s %s ", this.name, readAffURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
}
}
use of org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException in project fabric-sdk-java by hyperledger.
the class HFCAAffiliation method update.
/**
* update an affiliation
*
* @param registrar The identity of the registrar (i.e. who is performing the registration).
* @param force Forces updating of child affiliations
* @return Response of request
* @throws AffiliationException If updating an affiliation fails
* @throws InvalidArgumentException
*/
public HFCAAffiliationResp update(User registrar, boolean force) throws AffiliationException, InvalidArgumentException {
if (this.deleted) {
throw new AffiliationException("Affiliation has been deleted");
}
if (registrar == null) {
throw new InvalidArgumentException("Registrar should be a valid member");
}
if (Utils.isNullOrEmpty(name)) {
throw new InvalidArgumentException("Affiliation name cannot be null or empty");
}
String updateURL = "";
try {
Map<String, String> queryParm = new HashMap<String, String>();
queryParm.put("force", String.valueOf(force));
updateURL = client.getURL(HFCA_AFFILIATION + "/" + this.name, queryParm);
logger.debug(format("affiliation url: %s, registrar: %s", updateURL, registrar.getName()));
String body = client.toJson(affToJsonObject());
JsonObject result = client.httpPut(updateURL, body, registrar);
generateResponse(result);
logger.debug(format("identity url: %s, registrar: %s done.", updateURL, registrar));
HFCAAffiliationResp resp = getResponse(result);
this.childHFCAAffiliations = resp.childHFCAAffiliations;
this.identities = resp.identities;
return getResponse(result);
} catch (HTTPException e) {
String msg = format("[Code: %d] - Error while updating affiliation '%s' from url '%s': %s", e.getStatusCode(), this.name, updateURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
} catch (Exception e) {
String msg = format("Error while updating affiliation %s url: %s %s ", this.name, updateURL, e.getMessage());
AffiliationException affiliationException = new AffiliationException(msg, e);
logger.error(msg);
throw affiliationException;
}
}
Aggregations