Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 8bdcbe9

Browse files
committed
FABJ-421 HCAIdentity create not setting data
Change-Id: I6a62fdad6e1c60342e942d772457fa4de5770352 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 5122f10 commit 8bdcbe9

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

src/main/java/org/hyperledger/fabric_ca/sdk/HFCAIdentity.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616

1717
package org.hyperledger.fabric_ca.sdk;
1818

19-
import java.io.PrintWriter;
20-
import java.io.StringWriter;
2119
import java.util.ArrayList;
20+
import java.util.Arrays;
2221
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.Set;
2325

2426
import javax.json.Json;
2527
import javax.json.JsonArray;
2628
import javax.json.JsonArrayBuilder;
2729
import javax.json.JsonObject;
2830
import javax.json.JsonObjectBuilder;
29-
import javax.json.JsonWriter;
3031

3132
import org.apache.commons.logging.Log;
3233
import org.apache.commons.logging.LogFactory;
3334
import org.hyperledger.fabric.sdk.User;
3435
import org.hyperledger.fabric.sdk.helper.Utils;
35-
import org.hyperledger.fabric_ca.sdk.exception.AffiliationException;
3636
import org.hyperledger.fabric_ca.sdk.exception.HTTPException;
3737
import org.hyperledger.fabric_ca.sdk.exception.IdentityException;
3838
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
@@ -62,6 +62,8 @@ public class HFCAIdentity {
6262

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

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

146148
/**
147149
* Set affiliation of the identity
148-
* @param affiliation Affiliation name
149150
*
151+
* @param affiliation Affiliation name
150152
*/
151153
public void setAffiliation(String affiliation) {
152154
this.affiliation = affiliation;
153155
}
154156

155157
/**
156158
* Set affiliation of the identity
157-
* @param affiliation Affiliation name
158159
*
160+
* @param affiliation Affiliation name
159161
*/
160162
public void setAffiliation(HFCAAffiliation affiliation) {
161163
this.affiliation = affiliation.getName();
162164
}
165+
163166
/**
164167
* The attributes of the identity
165168
*
@@ -188,7 +191,7 @@ public boolean isDeleted() {
188191
*
189192
* @param registrar The identity of the registrar (i.e. who is performing the registration).
190193
* @return statusCode The HTTP status code in the response
191-
* @throws IdentityException if retrieving an identity fails.
194+
* @throws IdentityException if retrieving an identity fails.
192195
* @throws InvalidArgumentException Invalid (null) argument specified
193196
*/
194197

@@ -225,7 +228,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
225228
}
226229
this.deleted = false;
227230
return statusCode;
228-
} catch (HTTPException e) {
231+
} catch (HTTPException e) {
229232
String msg = format("[Code: %d] - Error while getting user '%s' from url '%s': %s", e.getStatusCode(), getEnrollmentId(), readIdURL, e.getMessage());
230233
IdentityException identityException = new IdentityException(msg, e);
231234
logger.error(msg);
@@ -244,7 +247,7 @@ public int read(User registrar) throws IdentityException, InvalidArgumentExcepti
244247
*
245248
* @param registrar The identity of the registrar (i.e. who is performing the registration).
246249
* @return statusCode The HTTP status code in the response
247-
* @throws IdentityException if creating an identity fails.
250+
* @throws IdentityException if creating an identity fails.
248251
* @throws InvalidArgumentException Invalid (null) argument specified
249252
*/
250253

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

286-
/**
289+
/**
287290
* update an identity
288291
*
289292
* @param registrar The identity of the registrar (i.e. who is performing the registration).
290293
* @return statusCode The HTTP status code in the response
291-
* @throws IdentityException if adding an identity fails.
294+
* @throws IdentityException if adding an identity fails.
292295
* @throws InvalidArgumentException Invalid (null) argument specified
293296
*/
294297

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

308-
String body = client.toJson(idToJsonObject());
311+
String body = client.toJson(idToJsonObject(filtredUpdateAttrNames));
309312
JsonObject result = client.httpPut(updateURL, body, registrar);
310313

311314
statusCode = result.getInt("statusCode");
@@ -332,7 +335,7 @@ public int update(User registrar) throws IdentityException, InvalidArgumentExcep
332335
*
333336
* @param registrar The identity of the registrar (i.e. who is performing the registration).
334337
* @return statusCode The HTTP status code in the response
335-
* @throws IdentityException if adding an identity fails.
338+
* @throws IdentityException if adding an identity fails.
336339
* @throws InvalidArgumentException Invalid (null) argument specified
337340
*/
338341

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

394397
// Convert the identity request to a JSON object
395398
private JsonObject idToJsonObject() {
399+
return idToJsonObject(Collections.emptySet());
400+
}
401+
402+
private JsonObject idToJsonObject(Set<String> filteredAttrs) {
396403
JsonObjectBuilder ob = Json.createObjectBuilder();
397404
ob.add("id", enrollmentID);
398405
ob.add("type", type);
@@ -404,7 +411,9 @@ private JsonObject idToJsonObject() {
404411
}
405412
JsonArrayBuilder ab = Json.createArrayBuilder();
406413
for (Attribute attr : attrs) {
407-
ab.add(attr.toJsonObject());
414+
if (!filteredAttrs.contains(attr.getName())) {
415+
ab.add(attr.toJsonObject());
416+
}
408417
}
409418
ob.add("attrs", ab.build());
410419
if (this.secret != null) {

src/test/java/org/hyperledger/fabric_ca/sdkintegration/HFCAClientIT.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.bouncycastle.cert.X509CRLHolder;
4242
import org.bouncycastle.openssl.PEMParser;
4343
import org.hyperledger.fabric.sdk.Enrollment;
44+
import org.hyperledger.fabric.sdk.User;
4445
import org.hyperledger.fabric.sdk.identity.IdemixEnrollment;
4546
import org.hyperledger.fabric.sdk.security.CryptoSuite;
4647
import org.hyperledger.fabric.sdk.testutils.TestConfig;
@@ -596,7 +597,7 @@ public void testCreateAndGetIdentity() throws Exception {
596597
}
597598

598599
HFCAIdentity ident = getIdentityReq("testuser1", HFCAClient.HFCA_TYPE_PEER);
599-
ident.create(admin);
600+
createSuccessfulHCAIdentity(ident, admin);
600601

601602
HFCAIdentity identGet = client.newHFCAIdentity(ident.getEnrollmentId());
602603
identGet.read(admin);
@@ -643,7 +644,7 @@ public void testGetAllIdentity() throws Exception {
643644
}
644645

645646
HFCAIdentity ident = getIdentityReq("testuser2", HFCAClient.HFCA_TYPE_CLIENT);
646-
ident.create(admin);
647+
createSuccessfulHCAIdentity(ident, admin);
647648

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

674675
HFCAIdentity ident = getIdentityReq("testuser3", HFCAClient.HFCA_TYPE_ORDERER);
675-
ident.create(admin);
676+
createSuccessfulHCAIdentity(ident, admin);
676677
assertEquals("Incorrect response for type", "orderer", ident.getType());
677678
assertNotEquals("Incorrect value for max enrollments", ident.getMaxEnrollments(), new Integer(5));
678679

@@ -700,7 +701,7 @@ public void testDeleteIdentity() throws Exception {
700701

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

703-
ident.create(admin);
704+
createSuccessfulHCAIdentity(ident, admin);
704705
ident.delete(admin);
705706

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

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

741-
ident.create(admin);
742+
createSuccessfulHCAIdentity(ident, admin);
742743
ident.delete(admin);
743744

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

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

771-
ident.create(admin2);
772+
createSuccessfulHCAIdentity(ident, admin2);
772773
ident.delete(admin2);
773774
}
774775

@@ -802,7 +803,7 @@ public void testGetAllAffiliation() throws Exception {
802803
int found = 0;
803804
for (HFCAAffiliation aff : resp.getChildren()) {
804805
for (Iterator<String> iter = expectedFirstLevelAffiliations.iterator(); iter.hasNext();
805-
) {
806+
) {
806807
String element = iter.next();
807808
if (aff.getName().equals(element)) {
808809
iter.remove();
@@ -858,21 +859,21 @@ public void testUpdateAffiliation() throws Exception {
858859

859860
HFCAIdentity ident = client.newHFCAIdentity("testuser_org4");
860861
ident.setAffiliation(aff.getName());
861-
ident.create(admin);
862+
createSuccessfulHCAIdentity(ident, admin);
862863

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

866867
HFCAIdentity ident2 = client.newHFCAIdentity("testuser_org4.dept1");
867868
ident2.setAffiliation("org4.dept1");
868-
ident2.create(admin);
869+
createSuccessfulHCAIdentity(ident2, admin);
869870

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

873874
HFCAIdentity ident3 = client.newHFCAIdentity("testuser_org4.dept1.team1");
874875
ident3.setAffiliation("org4.dept1.team1");
875-
ident3.create(admin);
876+
createSuccessfulHCAIdentity(ident3, admin);
876877

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

950951
HFCAIdentity ident = getIdentityReq("testorg1dept1", "client");
951952
ident.setAffiliation(aff.getName());
952-
ident.create(admin);
953+
createSuccessfulHCAIdentity(ident, admin);
953954

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

960+
private static int createSuccessfulHCAIdentity(HFCAIdentity ident, User user) throws InvalidArgumentException, IdentityException {
961+
962+
int rc = ident.create(user);
963+
assertTrue(rc < 400);
964+
assertNotNull(ident.getSecret());
965+
assertFalse(ident.getSecret().isEmpty());
966+
assertNotNull(ident.getEnrollmentId());
967+
assertFalse(ident.getEnrollmentId().isEmpty());
968+
assertNotNull(ident.getType());
969+
assertFalse(ident.getType().isEmpty());
970+
971+
return rc;
972+
}
973+
959974
// Tests deleting an affiliation
960975
@Test
961976
public void testDeleteAffiliation() throws Exception {
@@ -971,14 +986,15 @@ public void testDeleteAffiliation() throws Exception {
971986

972987
HFCAIdentity ident = client.newHFCAIdentity("testuser_org6");
973988
ident.setAffiliation("org6");
974-
ident.create(admin);
989+
createSuccessfulHCAIdentity(ident, admin);
990+
975991

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

979995
HFCAIdentity ident2 = client.newHFCAIdentity("testuser_org6.dept1");
980996
ident2.setAffiliation("org6.dept1");
981-
ident2.create(admin);
997+
createSuccessfulHCAIdentity(ident2, admin);
982998

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

10501066
HFCAIdentity ident = getIdentityReq("testorg1dept3", "client");
10511067
ident.setAffiliation("org1.dept3");
1052-
ident.create(admin);
1068+
createSuccessfulHCAIdentity(ident, admin);
10531069

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

1334-
Enrollment enrollment = client.idemixEnroll(user.getEnrollment(), "idemixMsp");
1350+
Enrollment enrollment = client.idemixEnroll(user.getEnrollment(), "idemixMsp");
13351351
assertNotNull(enrollment);
13361352
assertTrue(enrollment instanceof IdemixEnrollment);
13371353
}

0 commit comments

Comments
 (0)