Search in sources :

Example 6 with InvalidArgumentException

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;
    }
}
Also used : InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 7 with InvalidArgumentException

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;
    }
}
Also used : KeyPair(java.security.KeyPair) Base64(java.util.Base64) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) JsonObject(javax.json.JsonObject) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) URISyntaxException(java.net.URISyntaxException) RegistrationException(org.hyperledger.fabric_ca.sdk.exception.RegistrationException) KeyStoreException(java.security.KeyStoreException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) GenerateCRLException(org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException) KeyManagementException(java.security.KeyManagementException) IdentityException(org.hyperledger.fabric_ca.sdk.exception.IdentityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EnrollmentException(org.hyperledger.fabric_ca.sdk.exception.EnrollmentException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RevocationException(org.hyperledger.fabric_ca.sdk.exception.RevocationException) ParseException(org.apache.http.ParseException) MalformedURLException(java.net.MalformedURLException) InfoException(org.hyperledger.fabric_ca.sdk.exception.InfoException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsonArray(javax.json.JsonArray) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

Example 8 with InvalidArgumentException

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;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 9 with InvalidArgumentException

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;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Example 10 with InvalidArgumentException

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;
    }
}
Also used : AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) InvalidArgumentException(org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException) HTTPException(org.hyperledger.fabric_ca.sdk.exception.HTTPException) AffiliationException(org.hyperledger.fabric_ca.sdk.exception.AffiliationException)

Aggregations

AffiliationException (org.hyperledger.fabric_ca.sdk.exception.AffiliationException)18 HTTPException (org.hyperledger.fabric_ca.sdk.exception.HTTPException)18 InvalidArgumentException (org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException)18 JsonObject (javax.json.JsonObject)17 IdentityException (org.hyperledger.fabric_ca.sdk.exception.IdentityException)14 IOException (java.io.IOException)10 MalformedURLException (java.net.MalformedURLException)10 URISyntaxException (java.net.URISyntaxException)10 KeyManagementException (java.security.KeyManagementException)10 KeyStoreException (java.security.KeyStoreException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 CertificateException (java.security.cert.CertificateException)10 ParseException (org.apache.http.ParseException)10 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)10 EnrollmentException (org.hyperledger.fabric_ca.sdk.exception.EnrollmentException)10 GenerateCRLException (org.hyperledger.fabric_ca.sdk.exception.GenerateCRLException)10 InfoException (org.hyperledger.fabric_ca.sdk.exception.InfoException)10 RegistrationException (org.hyperledger.fabric_ca.sdk.exception.RegistrationException)10 RevocationException (org.hyperledger.fabric_ca.sdk.exception.RevocationException)10