| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.stores; |
| 16 |
|
| 17 |
import org.globus.gsi.provider.SigningPolicyStore; |
| 18 |
import org.globus.gsi.provider.SigningPolicyStoreException; |
| 19 |
import org.globus.gsi.provider.SigningPolicyStoreParameters; |
| 20 |
|
| 21 |
import org.apache.commons.logging.LogFactory; |
| 22 |
|
| 23 |
import org.apache.commons.logging.Log; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.net.URI; |
| 27 |
import java.security.InvalidAlgorithmParameterException; |
| 28 |
import java.util.Collection; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.Map; |
| 31 |
|
| 32 |
|
| 33 |
import javax.security.auth.x500.X500Principal; |
| 34 |
|
| 35 |
import org.globus.gsi.SigningPolicy; |
| 36 |
import org.globus.gsi.util.CertificateIOUtil; |
| 37 |
|
| 38 |
import org.globus.util.GlobusResource; |
| 39 |
import org.globus.util.GlobusPathMatchingResourcePatternResolver; |
| 40 |
|
| 41 |
/** |
| 42 |
* FILL ME |
| 43 |
* |
| 44 |
* @author [email protected] |
| 45 |
*/ |
| 46 |
public class ResourceSigningPolicyStore implements SigningPolicyStore { |
| 47 |
|
| 48 |
private Map<URI, ResourceSigningPolicy> signingPolicyFileMap = new HashMap<URI, ResourceSigningPolicy>(); |
| 49 |
private Map<String, SigningPolicy> policyMap = new HashMap<String, SigningPolicy>(); |
| 50 |
private ResourceSigningPolicyStoreParameters parameters; |
| 51 |
private final static Log logger = LogFactory.getLog(ResourceSigningPolicyStore.class.getCanonicalName()); |
| 52 |
private final Map<String, Long> invalidPoliciesCache = new HashMap<String, Long>(); |
| 53 |
private final Map<String, Long> validPoliciesCache = new HashMap<String, Long>(); |
| 54 |
private final static long CACHE_TIME_MILLIS = 3600*1000; |
| 55 |
|
| 56 |
/** |
| 57 |
* Please use the {@link Stores} class to generate Key/Cert stores |
| 58 |
*/ |
| 59 |
public ResourceSigningPolicyStore(SigningPolicyStoreParameters param) throws InvalidAlgorithmParameterException { |
| 60 |
if (param == null) { |
| 61 |
throw new IllegalArgumentException(); |
| 62 |
} |
| 63 |
|
| 64 |
if (!(param instanceof ResourceSigningPolicyStoreParameters)) { |
| 65 |
throw new InvalidAlgorithmParameterException(); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
this.parameters = (ResourceSigningPolicyStoreParameters) param; |
| 70 |
} |
| 71 |
|
| 72 |
public synchronized SigningPolicy getSigningPolicy(X500Principal caPrincipal) throws SigningPolicyStoreException { |
| 73 |
|
| 74 |
if (caPrincipal == null) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
String caPrincipalName = caPrincipal.getName(); |
| 78 |
long now = System.currentTimeMillis(); |
| 79 |
String hash = CertificateIOUtil.nameHash(caPrincipal); |
| 80 |
|
| 81 |
Long validCacheTime = validPoliciesCache.get(caPrincipalName); |
| 82 |
Long invalidCacheTime = invalidPoliciesCache.get(caPrincipalName); |
| 83 |
|
| 84 |
if ((invalidCacheTime != null) && (now - invalidCacheTime < 10*CACHE_TIME_MILLIS)) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
if ((validCacheTime == null) || (now - validCacheTime >= CACHE_TIME_MILLIS) || !this.policyMap.containsKey(caPrincipalName)) { |
| 88 |
loadPolicy(hash, caPrincipalName); |
| 89 |
} |
| 90 |
return this.policyMap.get(caPrincipalName); |
| 91 |
} |
| 92 |
|
| 93 |
private synchronized void loadPolicy(String hash, String caPrincipalName) throws SigningPolicyStoreException { |
| 94 |
|
| 95 |
String locations = this.parameters.getTrustRootLocations(); |
| 96 |
GlobusResource[] resources = new GlobusPathMatchingResourcePatternResolver(). |
| 97 |
getResources(locations); |
| 98 |
long now = System.currentTimeMillis(); |
| 99 |
boolean found_policy = false; |
| 100 |
|
| 101 |
// Optimization: If we find a hash for this CA, only process that. |
| 102 |
// Otherwise, we will process all policies. |
| 103 |
for (GlobusResource resource : resources) { |
| 104 |
String filename = resource.getFilename(); |
| 105 |
if (!filename.startsWith(hash)) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
if (loadSigningPolicy(resource, policyMap, signingPolicyFileMap, now)) { |
| 110 |
found_policy = true; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (!found_policy) { |
| 115 |
// Poor-man's implementation. Note it is much more expensive than a hashed directory |
| 116 |
for (GlobusResource resource : resources) { |
| 117 |
loadSigningPolicy(resource, policyMap, signingPolicyFileMap, now); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (!validPoliciesCache.containsKey(caPrincipalName)) { |
| 122 |
invalidPoliciesCache.put(caPrincipalName, now); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
private boolean loadSigningPolicy( |
| 128 |
GlobusResource policyResource, Map<String, SigningPolicy> policyMapToLoad, |
| 129 |
Map<URI, ResourceSigningPolicy> currentPolicyFileMap, long currentMillis) { |
| 130 |
|
| 131 |
String filename = policyResource.getFilename(); |
| 132 |
long now = currentMillis; |
| 133 |
boolean loaded = false; |
| 134 |
|
| 135 |
Long invalidCacheTime = invalidPoliciesCache.get(filename); |
| 136 |
if ((invalidCacheTime != null) && (now - invalidCacheTime < 10 * CACHE_TIME_MILLIS)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
try { |
| 141 |
URI uri; |
| 142 |
if (!policyResource.isReadable()) { |
| 143 |
throw new SigningPolicyStoreException("file is not readable"); |
| 144 |
} |
| 145 |
try { |
| 146 |
uri = policyResource.getURI(); |
| 147 |
} catch (IOException e) { |
| 148 |
throw new SigningPolicyStoreException(e); |
| 149 |
} |
| 150 |
|
| 151 |
ResourceSigningPolicy filePolicy = this.signingPolicyFileMap.get(uri); |
| 152 |
if (filePolicy == null) { |
| 153 |
try { |
| 154 |
filePolicy = new ResourceSigningPolicy(policyResource); |
| 155 |
} catch (ResourceStoreException e) { |
| 156 |
throw new SigningPolicyStoreException(e); |
| 157 |
} |
| 158 |
} |
| 159 |
Collection<SigningPolicy> policies = filePolicy.getSigningPolicies(); |
| 160 |
|
| 161 |
currentPolicyFileMap.put(uri, filePolicy); |
| 162 |
if (policies != null) { |
| 163 |
for (SigningPolicy policy : policies) { |
| 164 |
X500Principal caPrincipal = policy.getCASubjectDN(); |
| 165 |
policyMapToLoad.put(caPrincipal.getName(), policy); |
| 166 |
validPoliciesCache.put(caPrincipal.getName(), now); |
| 167 |
} |
| 168 |
} |
| 169 |
loaded = true; |
| 170 |
} catch (SigningPolicyStoreException e) { |
| 171 |
if ((invalidCacheTime == null) || (now - invalidCacheTime >= 10 * CACHE_TIME_MILLIS)) { |
| 172 |
logger.warn("Failed to load signing policy: " + filename + " : " + e.getMessage()); |
| 173 |
logger.debug("Failed to load signing policy: " + filename, e); |
| 174 |
invalidPoliciesCache.put(filename, now); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return loaded; |
| 179 |
} |
| 180 |
} |