Skip to content

Commit 13acdd3

Browse files
Support loading LSC files for selected VOs (#49)
1 parent 4ee09d9 commit 13acdd3

File tree

3 files changed

+102
-77
lines changed

3 files changed

+102
-77
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SPDX-License-Identifier: Apache-2.0
1010

1111
<groupId>org.italiangrid</groupId>
1212
<artifactId>voms-api-java</artifactId>
13-
<version>3.3.6</version>
13+
<version>3.3.7-SNAPSHOT</version>
1414
<packaging>jar</packaging>
1515

1616
<name>voms-api-java</name>

src/main/java/org/italiangrid/voms/store/impl/DefaultVOMSTrustStore.java

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,45 @@
3535

3636
/**
3737
*
38-
* The default implementation for the VOMS trust store. This implementation
39-
* <b>does not</b> refresh the trust information on a periodic basis. For an
40-
* updating trust store see {@link DefaultUpdatingVOMSTrustStore}.
38+
* The default implementation for the VOMS trust store. This implementation <b>does not</b> refresh
39+
* the trust information on a periodic basis. For an updating trust store see
40+
* {@link DefaultUpdatingVOMSTrustStore}.
4141
*
4242
* @author Andrea Ceccanti
4343
*
4444
*/
4545
public class DefaultVOMSTrustStore implements VOMSTrustStore {
4646

4747
/**
48-
* The default directory where local VOMS trust information is rooted:
49-
* {@value #DEFAULT_VOMS_DIR}
48+
* The default directory where local VOMS trust information is rooted: {@value #DEFAULT_VOMS_DIR}
5049
**/
5150
public static final String DEFAULT_VOMS_DIR = "/etc/grid-security/vomsdir";
5251

5352
/**
54-
* The filename suffix used to match certificates in the VOMS local trust
55-
* directories
53+
* The filename suffix used to match certificates in the VOMS local trust directories
5654
**/
5755
public static final String CERTIFICATE_FILENAME_SUFFIX = ".pem";
5856

5957
/**
60-
* The filename suffix used to match LSC files in the VOMS local trust
61-
* directories
58+
* The filename suffix used to match LSC files in the VOMS local trust directories
6259
**/
6360
public static final String LSC_FILENAME_SUFFIX = ".lsc";
6461

6562
/**
66-
* The list of local trusted directories that is searched for trust
67-
* information (certs or LSC files)
63+
* The list of local trusted directories that is searched for trust information (certs or LSC
64+
* files)
6865
**/
6966
private final List<String> localTrustedDirs;
7067

7168
/** Map of local parsed AA certificates keyed by certificate subject hash **/
72-
private Map<String, X509Certificate> localAACertificatesByHash = new HashMap<String, X509Certificate>();
69+
private Map<String, X509Certificate> localAACertificatesByHash =
70+
new HashMap<String, X509Certificate>();
7371

7472
/** The set of local parsed LSC information keyed by VO **/
7573
private Map<String, Set<LSCInfo>> localLSCInfo = new HashMap<String, Set<LSCInfo>>();
7674

7775
/**
78-
* The trust store status listener that will be notified of changes in this
79-
* trust store
76+
* The trust store status listener that will be notified of changes in this trust store
8077
**/
8178
private VOMSTrustStoreStatusListener listener;
8279

@@ -92,12 +89,12 @@ public class DefaultVOMSTrustStore implements VOMSTrustStore {
9289
/** A lock to guard the setting of the status listener **/
9390
protected final Object listenerLock = new Object();
9491

92+
private final List<String> voNames;
93+
9594
/**
96-
* Builds a list of trusted directories containing only
97-
* {@link #DEFAULT_VOMS_DIR}.
95+
* Builds a list of trusted directories containing only {@link #DEFAULT_VOMS_DIR}.
9896
*
99-
* @return a list of default trusted directory containing the
100-
* {@link #DEFAULT_VOMS_DIR}
97+
* @return a list of default trusted directory containing the {@link #DEFAULT_VOMS_DIR}
10198
**/
10299
protected static List<String> buildDefaultTrustedDirs() {
103100

@@ -108,24 +105,27 @@ protected static List<String> buildDefaultTrustedDirs() {
108105

109106
/**
110107
*
111-
* @param localTrustDirs
112-
* a non-null list of local trust directories
113-
* @param listener
114-
* the {@link VOMSTrustStoreStatusListener} to use for this trust
115-
* store
116-
* @throws IllegalArgumentException
117-
* when the list passed as argument is null
108+
* @param localTrustDirs a non-null list of local trust directories
109+
* @param listener the {@link VOMSTrustStoreStatusListener} to use for this trust store
110+
* @throws IllegalArgumentException when the list passed as argument is null
118111
*
119112
*/
120-
public DefaultVOMSTrustStore(List<String> localTrustDirs,
121-
VOMSTrustStoreStatusListener listener) {
113+
public DefaultVOMSTrustStore(List<String> localTrustDirs, VOMSTrustStoreStatusListener listener) {
114+
115+
this(localTrustDirs, null, listener);
116+
}
117+
118+
public DefaultVOMSTrustStore(List<String> localTrustDirs, List<String> voNames,
119+
VOMSTrustStoreStatusListener listener) {
122120

123-
if (localTrustDirs == null)
121+
if (localTrustDirs == null) {
124122
throw new IllegalArgumentException(
125-
"Please provide a non-null list of local trust directories!");
123+
"Please provide a non-null list of local trust directories!");
124+
}
126125

127126
this.localTrustedDirs = localTrustDirs;
128127
this.listener = listener;
128+
this.voNames = voNames;
129129
loadTrustInformation();
130130
}
131131

@@ -142,8 +142,7 @@ public DefaultVOMSTrustStore(List<String> localTrustDirs) {
142142
/**
143143
* Default constructor.
144144
*
145-
* Sets the local trusted directories to the default of
146-
* {@value #DEFAULT_VOMS_DIR}.
145+
* Sets the local trusted directories to the default of {@value #DEFAULT_VOMS_DIR}.
147146
*
148147
*
149148
*/
@@ -167,8 +166,8 @@ public List<X509Certificate> getLocalAACertificates() {
167166
read.lock();
168167

169168
try {
170-
return Collections.unmodifiableList(new ArrayList<X509Certificate>(
171-
localAACertificatesByHash.values()));
169+
return Collections
170+
.unmodifiableList(new ArrayList<X509Certificate>(localAACertificatesByHash.values()));
172171
} finally {
173172
read.unlock();
174173
}
@@ -200,9 +199,8 @@ public LSCInfo getLSC(String voName, String hostname) {
200199
}
201200

202201
/**
203-
* Loads all the certificates in the local directory. Only files with the
204-
* extension matching the {@link #CERTIFICATE_FILENAME_PATTERN} are
205-
* considered.
202+
* Loads all the certificates in the local directory. Only files with the extension matching the
203+
* {@link #CERTIFICATE_FILENAME_PATTERN} are considered.
206204
*
207205
* @param directory
208206
*/
@@ -228,8 +226,8 @@ public boolean accept(File dir, String name) {
228226
}
229227

230228
/**
231-
* Loads a VOMS AA certificate from a given file and stores this certificate
232-
* in the local map of trusted VOMS AA certificate.
229+
* Loads a VOMS AA certificate from a given file and stores this certificate in the local map of
230+
* trusted VOMS AA certificate.
233231
*
234232
* @param file
235233
*/
@@ -239,8 +237,8 @@ private void loadCertificateFromFile(File file) {
239237

240238
try {
241239

242-
X509Certificate aaCert = CertificateUtils.loadCertificate(
243-
new FileInputStream(file), Encoding.PEM);
240+
X509Certificate aaCert =
241+
CertificateUtils.loadCertificate(new FileInputStream(file), Encoding.PEM);
244242

245243
// Get certificate subject hash, using the CANL implementation for CA
246244
// files
@@ -254,9 +252,9 @@ private void loadCertificateFromFile(File file) {
254252
}
255253

256254
} catch (IOException e) {
257-
String errorMessage = String.format(
258-
"Error parsing VOMS trusted certificate from %s. Reason: %s",
259-
file.getAbsolutePath(), e.getMessage());
255+
String errorMessage =
256+
String.format("Error parsing VOMS trusted certificate from %s. Reason: %s",
257+
file.getAbsolutePath(), e.getMessage());
260258
throw new VOMSError(errorMessage, e);
261259
}
262260

@@ -299,8 +297,7 @@ public boolean accept(File dir, String name) {
299297
// In the VOMS trust anchor structure, LSC files are named as
300298
// <hostname>.lsc where hostname
301299
// is the name of host where the VOMS AA is running
302-
String hostname = lscFileName.substring(0,
303-
lscFileName.indexOf(LSC_FILENAME_SUFFIX));
300+
String hostname = lscFileName.substring(0, lscFileName.indexOf(LSC_FILENAME_SUFFIX));
304301

305302
LSCInfo info = null;
306303

@@ -322,46 +319,43 @@ public boolean accept(File dir, String name) {
322319
}
323320

324321
/**
325-
* Performs basic sanity checks performed on a file supposed to hold a VOMS AA
326-
* certificate.
322+
* Performs basic sanity checks performed on a file supposed to hold a VOMS AA certificate.
327323
*
328324
* @param certFile
329325
*/
330326
private void certificateFileSanityChecks(File certFile) {
331327

332328
if (!certFile.exists())
333-
throw new VOMSError("Local VOMS trusted certificate does not exist:"
334-
+ certFile.getAbsolutePath());
329+
throw new VOMSError(
330+
"Local VOMS trusted certificate does not exist:" + certFile.getAbsolutePath());
335331

336332
if (!certFile.canRead())
337-
throw new VOMSError("Local VOMS trusted certificate is not readable:"
338-
+ certFile.getAbsolutePath());
333+
throw new VOMSError(
334+
"Local VOMS trusted certificate is not readable:" + certFile.getAbsolutePath());
339335

340336
}
341337

342338
/**
343-
* Performs basic sanity checks on a directory that is supposed to contain
344-
* VOMS AA certificates and LSC files.
339+
* Performs basic sanity checks on a directory that is supposed to contain VOMS AA certificates
340+
* and LSC files.
345341
*
346342
* @param directory
347343
*/
348344
private void directorySanityChecks(File directory) {
349345

350346
if (!directory.exists())
351-
throw new VOMSError("Local trust directory does not exists:"
352-
+ directory.getAbsolutePath());
347+
throw new VOMSError("Local trust directory does not exists:" + directory.getAbsolutePath());
353348

354349
if (!directory.isDirectory())
355-
throw new VOMSError("Local trust directory is not a directory:"
356-
+ directory.getAbsolutePath());
350+
throw new VOMSError(
351+
"Local trust directory is not a directory:" + directory.getAbsolutePath());
357352

358353
if (!directory.canRead())
359-
throw new VOMSError("Local trust directory is not readable:"
360-
+ directory.getAbsolutePath());
354+
throw new VOMSError("Local trust directory is not readable:" + directory.getAbsolutePath());
361355

362356
if (!directory.canExecute())
363-
throw new VOMSError("Local trust directory is not traversable:"
364-
+ directory.getAbsolutePath());
357+
throw new VOMSError(
358+
"Local trust directory is not traversable:" + directory.getAbsolutePath());
365359

366360
}
367361

@@ -380,7 +374,7 @@ public void loadTrustInformation() {
380374

381375
if (localTrustedDirs.isEmpty()) {
382376
throw new VOMSError(
383-
"No local trust directory was specified for this trust store. Please provide at least one path where LSC and VOMS service certificates will be searched for.");
377+
"No local trust directory was specified for this trust store. Please provide at least one path where LSC and VOMS service certificates will be searched for.");
384378
}
385379

386380
cleanupStores();
@@ -404,8 +398,11 @@ public boolean accept(File pathname) {
404398
});
405399

406400
for (File voDir : voDirs) {
407-
loadLSCFromDirectory(voDir);
408-
loadCertificatesFromDirectory(voDir);
401+
402+
if (voNames == null || voNames.contains(voDir.getName())) {
403+
loadLSCFromDirectory(voDir);
404+
loadCertificatesFromDirectory(voDir);
405+
}
409406
}
410407
}
411408

src/test/java/org/italiangrid/voms/test/TestDefaultVOMSTrustStore.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.italiangrid.voms.test;
99

1010
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertNull;
1113
import static org.junit.Assert.assertTrue;
1214

1315
import java.io.FileInputStream;
@@ -20,6 +22,7 @@
2022

2123
import org.italiangrid.voms.VOMSError;
2224
import org.italiangrid.voms.store.impl.DefaultVOMSTrustStore;
25+
import org.italiangrid.voms.util.NullListener;
2326
import org.junit.Test;
2427

2528
import eu.emi.security.authn.x509.impl.CertificateUtils;
@@ -34,17 +37,16 @@ public class TestDefaultVOMSTrustStore {
3437
@Test(expected = VOMSError.class)
3538
public void testEmptyTrustDirsFailure() {
3639

37-
@SuppressWarnings({ "unused", "unchecked" })
38-
DefaultVOMSTrustStore store = new DefaultVOMSTrustStore(
39-
Collections.EMPTY_LIST);
40+
@SuppressWarnings({"unused", "unchecked"})
41+
DefaultVOMSTrustStore store = new DefaultVOMSTrustStore(Collections.EMPTY_LIST);
4042

4143
}
4244

4345
@Test(expected = VOMSError.class)
4446
public void testNonExistentTrustDirsFailure() {
4547

46-
List<String> trustDirs = Arrays.asList(new String[] { "/etc/do/not/exist",
47-
"/etc/grid-security/vomsdir" });
48+
List<String> trustDirs =
49+
Arrays.asList(new String[] {"/etc/do/not/exist", "/etc/grid-security/vomsdir"});
4850

4951
@SuppressWarnings("unused")
5052
DefaultVOMSTrustStore store = new DefaultVOMSTrustStore(trustDirs);
@@ -75,22 +77,48 @@ public void testEmptyTrustDir() {
7577
}
7678

7779
@Test
78-
public void testCertificateParsing() throws FileNotFoundException,
79-
IOException {
80+
public void testCertificateParsing() throws FileNotFoundException, IOException {
8081

8182
String vomsDir = "src/test/resources/vomsdir";
8283
String certFileName = "src/test/resources/vomsdir/test-host.cnaf.infn.it.pem";
83-
X509Certificate cert = CertificateUtils.loadCertificate(
84-
new FileInputStream(certFileName), Encoding.PEM);
84+
X509Certificate cert =
85+
CertificateUtils.loadCertificate(new FileInputStream(certFileName), Encoding.PEM);
8586

86-
List<String> trustDirs = Arrays.asList(new String[] { vomsDir });
87+
List<String> trustDirs = Arrays.asList(new String[] {vomsDir});
8788

8889
DefaultVOMSTrustStore store = new DefaultVOMSTrustStore(trustDirs);
8990

9091
assertEquals(1, store.getLocalAACertificates().size());
9192

92-
assertTrue(cert.getSubjectX500Principal().equals(
93-
store.getLocalAACertificates().get(0).getSubjectX500Principal()));
93+
assertTrue(cert.getSubjectX500Principal()
94+
.equals(store.getLocalAACertificates().get(0).getSubjectX500Principal()));
95+
}
96+
97+
@Test
98+
public void testAllLSCInStore() {
99+
100+
List<String> trustDirs = Arrays.asList("src/test/resources/vomsdir");
101+
102+
DefaultVOMSTrustStore store = new DefaultVOMSTrustStore(trustDirs, NullListener.INSTANCE);
103+
104+
assertNotNull(store.getLSC("test.vo", "test-host.cnaf.infn.it"));
105+
assertNotNull(store.getLSC("test.vo", "test-multichain.cnaf.infn.it"));
106+
assertNotNull(store.getLSC("test.vo.1", "wilco.cnaf.infn.it"));
107+
108+
}
109+
110+
@Test
111+
public void testLSCForVoInStore() {
112+
113+
List<String> trustDirs = Arrays.asList("src/test/resources/vomsdir");
114+
115+
DefaultVOMSTrustStore store =
116+
new DefaultVOMSTrustStore(trustDirs, Arrays.asList("test.vo"), NullListener.INSTANCE);
117+
118+
assertNotNull(store.getLSC("test.vo", "test-host.cnaf.infn.it"));
119+
assertNotNull(store.getLSC("test.vo", "test-multichain.cnaf.infn.it"));
120+
assertNull(store.getLSC("test.vo.1", "wilco.cnaf.infn.it"));
121+
94122
}
95123

96124
public void testUpdatingVOMSTrustStore() {

0 commit comments

Comments
 (0)