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

Commit 8ccc1a7

Browse files
committed
FAB-5827 Support attrib. req. for enrollment.
Change-Id: Ibf57b250b43ed796da1178231bdf06d53476d978 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 7f6dc28 commit 8ccc1a7

File tree

3 files changed

+127
-19
lines changed

3 files changed

+127
-19
lines changed

src/main/java/org/hyperledger/fabric/sdk/Endpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Endpoint {
7979
if (properties != null) {
8080
if ("grpcs".equals(protocol)) {
8181
if (properties.containsKey("pemFile") && properties.containsKey("pemBytes")) {
82-
throw new RuntimeException("Properties \"pemBytes\" and \"pemBytes\" can not be both set.");
82+
throw new RuntimeException("Properties \"pemBytes\" and \"pemFile\" can not be both set.");
8383
}
8484
if (properties.containsKey("pemFile")) {
8585
Path path = Paths.get(properties.getProperty("pemFile"));

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

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import java.security.KeyPair;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.HashMap;
23+
import java.util.Map;
2224

2325
import javax.json.Json;
2426
import javax.json.JsonArrayBuilder;
2527
import javax.json.JsonObject;
2628
import javax.json.JsonObjectBuilder;
2729
import javax.json.JsonWriter;
2830

31+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
32+
2933
/**
3034
* An enrollment request is information required to enroll the user with member service.
3135
*/
@@ -34,16 +38,18 @@ public class EnrollmentRequest {
3438
// A PEM-encoded string containing the CSR (Certificate Signing Request) based on PKCS #10
3539
private String csr;
3640
// Comma-separated list of host names to associate with the certificate
37-
private Collection<String> hosts;
41+
private Collection<String> hosts = new ArrayList<String>();
3842
// Name of the signing profile to use when issuing the certificate
39-
private String profile;
43+
private String profile = null;
4044
// Label used in HSM operations
41-
private String label;
45+
private String label = null;
4246
// Key pair for generating certification request
43-
private KeyPair keypair;
47+
private KeyPair keypair = null;
4448
// The Certificate Authority's name
4549
private String caName;
4650

51+
// Attribute requests. added v1.1
52+
private Map<String, AttrReq> attrreqs = new HashMap<>();
4753
/**
4854
* The certificate signing request if it's not supplied it will be generated.
4955
*
@@ -56,11 +62,7 @@ public void setCsr(String csr) {
5662

5763
// Constructor
5864
public EnrollmentRequest() {
59-
this.csr = null;
60-
this.hosts = new ArrayList<String>();
61-
this.profile = null;
62-
this.label = null;
63-
this.keypair = null;
65+
6466
}
6567

6668
String getCsr() {
@@ -75,8 +77,6 @@ String getCsr() {
7577
* @param keypair Keypair used to sign or create the certificate if needed.
7678
*/
7779
public EnrollmentRequest(String profile, String label, KeyPair keypair) {
78-
this.csr = null;
79-
this.hosts = new ArrayList<String>();
8080
this.profile = profile;
8181
this.label = label;
8282
this.keypair = keypair;
@@ -125,7 +125,7 @@ public void setLabel(String label) {
125125
}
126126

127127
public Collection<String> getHosts() {
128-
return hosts;
128+
return new ArrayList<>(hosts);
129129
}
130130

131131
public void addHost(String host) {
@@ -136,7 +136,7 @@ public void addHost(String host) {
136136
String toJson() {
137137
StringWriter stringWriter = new StringWriter();
138138
JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter));
139-
jsonWriter.writeObject(this.toJsonObject());
139+
jsonWriter.writeObject(toJsonObject());
140140
jsonWriter.close();
141141
return stringWriter.toString();
142142
}
@@ -162,6 +162,44 @@ private JsonObject toJsonObject() {
162162
factory.add(HFCAClient.FABRIC_CA_REQPROP, caName);
163163
}
164164
factory.add("certificate_request", csr);
165+
166+
if (!attrreqs.isEmpty()) {
167+
JsonArrayBuilder ab = Json.createArrayBuilder();
168+
for (AttrReq attrReq : attrreqs.values()) {
169+
JsonObjectBuilder i = Json.createObjectBuilder();
170+
i.add("name", attrReq.name);
171+
if (attrReq.require != null) {
172+
i.add("require", attrReq.require);
173+
}
174+
ab.add(i);
175+
176+
}
177+
factory.add("attr_reqs", ab.build());
178+
}
179+
165180
return factory.build();
166181
}
182+
183+
public AttrReq addAttrReq(String name) throws InvalidArgumentException {
184+
if (name == null || name.isEmpty()) {
185+
throw new InvalidArgumentException("name may not be null or empty.");
186+
187+
}
188+
return new AttrReq(name);
189+
}
190+
191+
public class AttrReq {
192+
final String name;
193+
Boolean require = null;
194+
195+
AttrReq(String name) {
196+
this.name = name;
197+
attrreqs.put(name, this);
198+
}
199+
200+
public AttrReq setRequire(boolean require) {
201+
this.require = require;
202+
return this;
203+
}
204+
}
167205
}

src/test/java/org/hyperledger/fabric_ca/sdk/EnrollmentRequestTest.java

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@
1515
package org.hyperledger.fabric_ca.sdk;
1616

1717
import java.security.KeyPair;
18+
19+
import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException;
1820
import org.junit.Assert;
21+
import org.junit.Rule;
1922
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
25+
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertTrue;
2028

2129
public class EnrollmentRequestTest {
30+
@Rule
31+
public ExpectedException thrown = ExpectedException.none();
32+
2233
private static final String caName = "certsInc";
2334
private static final String csr = "11436845810603";
2435
private static final String profile = "test profile";
@@ -31,7 +42,7 @@ public void testNewInstanceEmpty() {
3142
try {
3243
EnrollmentRequest testEnrollReq = new EnrollmentRequest();
3344
Assert.assertNull(testEnrollReq.getCsr());
34-
Assert.assertTrue(testEnrollReq.getHosts().isEmpty());
45+
assertTrue(testEnrollReq.getHosts().isEmpty());
3546
Assert.assertNull(testEnrollReq.getProfile());
3647
Assert.assertNull(testEnrollReq.getLabel());
3748
Assert.assertNull(testEnrollReq.getKeyPair());
@@ -47,7 +58,7 @@ public void testNewInstanceParms() {
4758
try {
4859
EnrollmentRequest testEnrollReq = new EnrollmentRequest(profile, label, keyPair);
4960
Assert.assertNull(testEnrollReq.getCsr());
50-
Assert.assertTrue(testEnrollReq.getHosts().isEmpty());
61+
assertTrue(testEnrollReq.getHosts().isEmpty());
5162
Assert.assertEquals(testEnrollReq.getProfile(), profile);
5263
Assert.assertEquals(testEnrollReq.getLabel(), label);
5364
Assert.assertNull(testEnrollReq.getKeyPair());
@@ -65,13 +76,13 @@ public void testEnrollReqSetGet() {
6576
testEnrollReq.addHost("d.com");
6677
testEnrollReq.setCsr(csr);
6778
testEnrollReq.setCSR(csr); // Unsure why there are two methods that
68-
// set csr
79+
// set csr
6980
testEnrollReq.setProfile(profile);
7081
testEnrollReq.setLabel(label);
7182
testEnrollReq.setKeyPair(null);
7283
testEnrollReq.setCAName(caName);
7384
Assert.assertEquals(testEnrollReq.getCsr(), csr);
74-
Assert.assertTrue(testEnrollReq.getHosts().contains("d.com"));
85+
assertTrue(testEnrollReq.getHosts().contains("d.com"));
7586
Assert.assertEquals(testEnrollReq.getProfile(), profile);
7687
Assert.assertEquals(testEnrollReq.getLabel(), label);
7788
Assert.assertNull(testEnrollReq.getKeyPair());
@@ -94,10 +105,69 @@ public void testEnrollReqToJson() {
94105
testEnrollReq.setKeyPair(null);
95106
testEnrollReq.setCAName(caName);
96107

97-
Assert.assertTrue(testEnrollReq.toJson().contains(csr));
108+
assertTrue(testEnrollReq.toJson().contains(csr));
98109

99110
} catch (Exception e) {
100111
Assert.fail("Unexpected Exception " + e.getMessage());
101112
}
102113
}
114+
115+
@Test
116+
public void testEnrollReqToJsonAttr() throws Exception {
117+
118+
EnrollmentRequest testEnrollReq = new EnrollmentRequest();
119+
testEnrollReq.addHost("d.com");
120+
testEnrollReq.setCsr(csr);
121+
testEnrollReq.setProfile(profile);
122+
testEnrollReq.setLabel(label);
123+
testEnrollReq.setKeyPair(null);
124+
testEnrollReq.setCAName(caName);
125+
testEnrollReq.addAttrReq("foo");
126+
testEnrollReq.addAttrReq("foorequired").setRequire(true);
127+
testEnrollReq.addAttrReq("foofalse").setRequire(false);
128+
129+
String s = testEnrollReq.toJson();
130+
assertNotNull(s);
131+
assertTrue(s.contains("\"attr_reqs\":["));
132+
assertTrue(s.contains("\"name\":\"foorequired\",\"require\":true"));
133+
assertTrue(s.contains("\"name\":\"foofalse\",\"require\":false"));
134+
135+
}
136+
137+
@Test
138+
public void testEnrollReqToJsonAttrNotThere() throws Exception {
139+
140+
EnrollmentRequest testEnrollReq = new EnrollmentRequest();
141+
testEnrollReq.addHost("d.com");
142+
testEnrollReq.setCsr(csr);
143+
testEnrollReq.setProfile(profile);
144+
testEnrollReq.setLabel(label);
145+
testEnrollReq.setKeyPair(null);
146+
testEnrollReq.setCAName(caName);
147+
148+
String s = testEnrollReq.toJson();
149+
assertNotNull(s);
150+
assertFalse(s.contains("\"attr_reqs\":["));
151+
}
152+
153+
@Test
154+
public void testEnrollReqToJsonAttrNullName() throws Exception {
155+
thrown.expect(InvalidArgumentException.class);
156+
thrown.expectMessage("name may not be null or empty.");
157+
158+
EnrollmentRequest testEnrollReq = new EnrollmentRequest();
159+
testEnrollReq.addAttrReq(null);
160+
161+
}
162+
163+
@Test
164+
public void testEnrollReqToJsonAttrEmptyName() throws Exception {
165+
thrown.expect(InvalidArgumentException.class);
166+
thrown.expectMessage("name may not be null or empty.");
167+
168+
EnrollmentRequest testEnrollReq = new EnrollmentRequest();
169+
testEnrollReq.addAttrReq("");
170+
171+
}
172+
103173
}

0 commit comments

Comments
 (0)