Skip to content

Commit

Permalink
FABJ-421 HCAIdentity create not setting data
Browse files Browse the repository at this point in the history
Change-Id: I6a62fdad6e1c60342e942d772457fa4de5770352
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Mar 14, 2019
1 parent 5122f10 commit 8bdcbe9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
39 changes: 24 additions & 15 deletions src/main/java/org/hyperledger/fabric_ca/sdk/HFCAIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

package org.hyperledger.fabric_ca.sdk;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric_ca.sdk.exception.AffiliationException;
import org.hyperledger.fabric_ca.sdk.exception.HTTPException;
import org.hyperledger.fabric_ca.sdk.exception.IdentityException;
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
Expand Down Expand Up @@ -62,6 +62,8 @@ public class HFCAIdentity {

static final String HFCA_IDENTITY = HFCAClient.HFCA_CONTEXT_ROOT + "identities";
private static final Log logger = LogFactory.getLog(HFCAIdentity.class);
//These attributes can not be modified with REST put request.
private static final Set<String> filtredUpdateAttrNames = new HashSet<>(Arrays.asList("hf.EnrollmentID", "hf.Type", "hf.Affiliation"));

HFCAIdentity(String enrollmentID, HFCAClient client) throws InvalidArgumentException {
if (Utils.isNullOrEmpty(enrollmentID)) {
Expand Down Expand Up @@ -145,21 +147,22 @@ public String getAffiliation() {

/**
* Set affiliation of the identity
* @param affiliation Affiliation name
*
* @param affiliation Affiliation name
*/
public void setAffiliation(String affiliation) {
this.affiliation = affiliation;
}

/**
* Set affiliation of the identity
* @param affiliation Affiliation name
*
* @param affiliation Affiliation name
*/
public void setAffiliation(HFCAAffiliation affiliation) {
this.affiliation = affiliation.getName();
}

/**
* The attributes of the identity
*
Expand Down Expand Up @@ -188,7 +191,7 @@ public boolean isDeleted() {
*
* @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 retrieving an identity fails.
* @throws IdentityException if retrieving an identity fails.
* @throws InvalidArgumentException Invalid (null) argument specified
*/

Expand Down Expand Up @@ -225,7 +228,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
}
this.deleted = false;
return statusCode;
} catch (HTTPException e) {
} catch (HTTPException e) {
String msg = format("[Code: %d] - Error while getting user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), readIdURL, e.getMessage());
IdentityException identityException = new IdentityException(msg, e);
logger.error(msg);
Expand All @@ -244,7 +247,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
*
* @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 IdentityException if creating an identity fails.
* @throws InvalidArgumentException Invalid (null) argument specified
*/

Expand All @@ -264,7 +267,7 @@ public int create(User registrar) throws IdentityException, InvalidArgumentExcep
String body = client.toJson(idToJsonObject());
JsonObject result = client.httpPost(createURL, body, registrar);
statusCode = result.getInt("statusCode");
if (statusCode >= 400) {
if (statusCode < 400) {
getHFCAIdentity(result);
logger.debug(format("identity url: %s, registrar: %s done.", createURL, registrar));
}
Expand All @@ -283,12 +286,12 @@ public int create(User registrar) throws IdentityException, InvalidArgumentExcep
}
}

/**
/**
* update 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 adding an identity fails.
* @throws IdentityException if adding an identity fails.
* @throws InvalidArgumentException Invalid (null) argument specified
*/

Expand All @@ -305,7 +308,7 @@ public int update(User registrar) throws IdentityException, InvalidArgumentExcep
updateURL = client.getURL(HFCA_IDENTITY + "/" + getEnrollmentId());
logger.debug(format("identity url: %s, registrar: %s", updateURL, registrar.getName()));

String body = client.toJson(idToJsonObject());
String body = client.toJson(idToJsonObject(filtredUpdateAttrNames));
JsonObject result = client.httpPut(updateURL, body, registrar);

statusCode = result.getInt("statusCode");
Expand All @@ -332,7 +335,7 @@ public int update(User registrar) throws IdentityException, InvalidArgumentExcep
*
* @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 adding an identity fails.
* @throws IdentityException if adding an identity fails.
* @throws InvalidArgumentException Invalid (null) argument specified
*/

Expand Down Expand Up @@ -393,6 +396,10 @@ private void getHFCAIdentity(JsonObject result) {

// Convert the identity request to a JSON object
private JsonObject idToJsonObject() {
return idToJsonObject(Collections.emptySet());
}

private JsonObject idToJsonObject(Set<String> filteredAttrs) {
JsonObjectBuilder ob = Json.createObjectBuilder();
ob.add("id", enrollmentID);
ob.add("type", type);
Expand All @@ -404,7 +411,9 @@ private JsonObject idToJsonObject() {
}
JsonArrayBuilder ab = Json.createArrayBuilder();
for (Attribute attr : attrs) {
ab.add(attr.toJsonObject());
if (!filteredAttrs.contains(attr.getName())) {
ab.add(attr.toJsonObject());
}
}
ob.add("attrs", ab.build());
if (this.secret != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.bouncycastle.cert.X509CRLHolder;
import org.bouncycastle.openssl.PEMParser;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.identity.IdemixEnrollment;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.testutils.TestConfig;
Expand Down Expand Up @@ -596,7 +597,7 @@ public void testCreateAndGetIdentity() throws Exception {
}

HFCAIdentity ident = getIdentityReq("testuser1", HFCAClient.HFCA_TYPE_PEER);
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);

HFCAIdentity identGet = client.newHFCAIdentity(ident.getEnrollmentId());
identGet.read(admin);
Expand Down Expand Up @@ -643,7 +644,7 @@ public void testGetAllIdentity() throws Exception {
}

HFCAIdentity ident = getIdentityReq("testuser2", HFCAClient.HFCA_TYPE_CLIENT);
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);

Collection<HFCAIdentity> foundIdentities = client.getHFCAIdentities(admin);
String[] expectedIdenities = new String[] {"testuser2", "admin"};
Expand Down Expand Up @@ -672,7 +673,7 @@ public void testModifyIdentity() throws Exception {
}

HFCAIdentity ident = getIdentityReq("testuser3", HFCAClient.HFCA_TYPE_ORDERER);
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);
assertEquals("Incorrect response for type", "orderer", ident.getType());
assertNotEquals("Incorrect value for max enrollments", ident.getMaxEnrollments(), new Integer(5));

Expand Down Expand Up @@ -700,7 +701,7 @@ public void testDeleteIdentity() throws Exception {

HFCAIdentity ident = client.newHFCAIdentity(user.getName());

ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);
ident.delete(admin);

ident.read(admin);
Expand Down Expand Up @@ -738,7 +739,7 @@ public void testDeleteIdentityFailSecondDelete() throws Exception {

HFCAIdentity ident = client.newHFCAIdentity("deletedUser2");

ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);
ident.delete(admin);

ident.delete(admin);
Expand Down Expand Up @@ -768,7 +769,7 @@ public void testDeleteIdentityNotAllowed() throws Exception {

HFCAIdentity ident = client2.newHFCAIdentity(user.getName());

ident.create(admin2);
createSuccessfulHCAIdentity(ident, admin2);
ident.delete(admin2);
}

Expand Down Expand Up @@ -802,7 +803,7 @@ public void testGetAllAffiliation() throws Exception {
int found = 0;
for (HFCAAffiliation aff : resp.getChildren()) {
for (Iterator<String> iter = expectedFirstLevelAffiliations.iterator(); iter.hasNext();
) {
) {
String element = iter.next();
if (aff.getName().equals(element)) {
iter.remove();
Expand Down Expand Up @@ -858,21 +859,21 @@ public void testUpdateAffiliation() throws Exception {

HFCAIdentity ident = client.newHFCAIdentity("testuser_org4");
ident.setAffiliation(aff.getName());
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);

HFCAAffiliation aff2 = client.newHFCAAffiliation("org4.dept1");
aff2.create(admin);

HFCAIdentity ident2 = client.newHFCAIdentity("testuser_org4.dept1");
ident2.setAffiliation("org4.dept1");
ident2.create(admin);
createSuccessfulHCAIdentity(ident2, admin);

HFCAAffiliation aff3 = client.newHFCAAffiliation("org4.dept1.team1");
aff3.create(admin);

HFCAIdentity ident3 = client.newHFCAIdentity("testuser_org4.dept1.team1");
ident3.setAffiliation("org4.dept1.team1");
ident3.create(admin);
createSuccessfulHCAIdentity(ident3, admin);

aff.setUpdateName("org5");
// Set force option to true, since their identities associated with affiliations
Expand Down Expand Up @@ -949,13 +950,27 @@ public void testUpdateAffiliationInvalid() throws Exception {

HFCAIdentity ident = getIdentityReq("testorg1dept1", "client");
ident.setAffiliation(aff.getName());
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);

aff.setUpdateName("org1.dept2");
HFCAAffiliationResp resp = aff.update(admin);
assertEquals("Incorrect status code", new Integer(400), new Integer(resp.getStatusCode()));
}

private static int createSuccessfulHCAIdentity(HFCAIdentity ident, User user) throws InvalidArgumentException, IdentityException {

int rc = ident.create(user);
assertTrue(rc < 400);
assertNotNull(ident.getSecret());
assertFalse(ident.getSecret().isEmpty());
assertNotNull(ident.getEnrollmentId());
assertFalse(ident.getEnrollmentId().isEmpty());
assertNotNull(ident.getType());
assertFalse(ident.getType().isEmpty());

return rc;
}

// Tests deleting an affiliation
@Test
public void testDeleteAffiliation() throws Exception {
Expand All @@ -971,14 +986,15 @@ public void testDeleteAffiliation() throws Exception {

HFCAIdentity ident = client.newHFCAIdentity("testuser_org6");
ident.setAffiliation("org6");
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);


HFCAAffiliation aff2 = client.newHFCAAffiliation("org6.dept1");
aff2.create(admin);

HFCAIdentity ident2 = client.newHFCAIdentity("testuser_org6.dept1");
ident2.setAffiliation("org6.dept1");
ident2.create(admin);
createSuccessfulHCAIdentity(ident2, admin);

HFCAAffiliationResp resp = aff.delete(admin, true);
int idCount = 0;
Expand Down Expand Up @@ -1049,7 +1065,7 @@ public void testForceDeleteAffiliationInvalid() throws Exception {

HFCAIdentity ident = getIdentityReq("testorg1dept3", "client");
ident.setAffiliation("org1.dept3");
ident.create(admin);
createSuccessfulHCAIdentity(ident, admin);

HFCAAffiliationResp resp = aff.delete(admin);
assertEquals("Incorrect status code", new Integer(401), new Integer(resp.getStatusCode()));
Expand Down Expand Up @@ -1331,7 +1347,7 @@ public void testGetIdemixCred() throws Exception {
user.setEnrollmentSecret(client.register(rr, admin));
user.setEnrollment(client.enroll(user.getName(), user.getEnrollmentSecret()));

Enrollment enrollment = client.idemixEnroll(user.getEnrollment(), "idemixMsp");
Enrollment enrollment = client.idemixEnroll(user.getEnrollment(), "idemixMsp");
assertNotNull(enrollment);
assertTrue(enrollment instanceof IdemixEnrollment);
}
Expand Down

0 comments on commit 8bdcbe9

Please sign in to comment.