View Javadoc

1   package gov.bnl.gums.configuration;
2   
3   import gov.bnl.gums.GUMS;
4   import gov.bnl.gums.GridUser;
5   import gov.bnl.gums.account.AccountMapper;
6   import gov.bnl.gums.account.GroupAccountMapper;
7   import gov.bnl.gums.groupToAccount.GroupToAccountMapping;
8   import gov.bnl.gums.hostToGroup.CertificateHostToGroupMapping;
9   import gov.bnl.gums.hostToGroup.HostToGroupMapping;
10  import gov.bnl.gums.persistence.PersistenceFactory;
11  import gov.bnl.gums.userGroup.ManualUserGroup;
12  import gov.bnl.gums.userGroup.UserGroup;
13  import gov.bnl.gums.userGroup.VOMSUserGroup;
14  import gov.bnl.gums.userGroup.VomsServer;
15  
16  import java.io.File;
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import javax.xml.transform.*;
22  import javax.xml.transform.sax.*;
23  import javax.xml.transform.stream.*;
24  
25  import org.apache.commons.digester.Digester;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.xml.sax.helpers.XMLReaderFactory;
29  import org.xml.sax.XMLReader;
30  import org.xml.sax.InputSource;
31  
32  /**
33   *
34   * @author Jay Packard
35   */
36  public class ConfigurationTransform {
37  	static private Log log = LogFactory.getLog(FileConfigurationStore.class);
38  	static private Log gumsResourceAdminLog = LogFactory.getLog(GUMS.resourceAdminLog);
39  	
40  	/**
41  	 * Transforms gums.config 1.1 to 1.2
42  	 * 
43  	 * @param configFile
44  	 * @param transformFile
45  	 */
46  	static public Configuration doTransform(String configPath, String transformPath) {
47          log.trace("Transforming configuration file '" + configPath + "' using transform '" + transformPath);
48  		
49  		try {
50  	    	String configFileTemp = configPath + "~";
51          	
52  	        XMLReader reader = XMLReaderFactory.createXMLReader();
53  	        Source source = new SAXSource(reader, new InputSource(configPath));
54  
55  	        StreamResult result = new StreamResult(configFileTemp);
56  	        Source style = new StreamSource(transformPath);
57  
58  	        TransformerFactory transFactory = TransformerFactory.newInstance();
59  	        Transformer trans = transFactory.newTransformer(style);
60  	        
61  	        trans.transform(source, result);
62  	        
63          	// Reload it to get rid of duplicates that the transform couldn't handle
64          	// as well as to clean up the formatting
65          	Digester digester = ConfigurationToolkit.retrieveDigester();
66          	Configuration configuration = new Configuration();
67              digester.push(configuration);
68              digester.parse("file://"+configFileTemp);
69              
70              new File(configFileTemp).delete();
71              
72              // Clean up VOMS server names
73              Iterator it = new ArrayList(configuration.getVomsServers().keySet()).iterator();
74              while (it.hasNext()) {
75              	String name = (String)it.next();
76              	String origName = new String(name);
77              	if (name.startsWith("http")) {
78              		name = name.replaceAll("^http.*://","");
79              		name = name.replaceAll(".*/voms/","");
80              		name = name.replaceAll(".*/edg-voms-admin/","");
81              		name = name.replaceAll("[:|/].*","");
82              		name = name.toLowerCase();
83              		if (configuration.getVomsServer(name)!=null) {
84  	            		int count = 1;
85  	            		while (configuration.getVomsServer(name + Integer.toString(count)) != null) 
86  	            			count++;
87              			name += Integer.toString(count);
88              		}
89              		VomsServer vo = configuration.getVomsServer(origName);
90              		if (vo!=null) {
91  	            		vo.setName(name);
92  	            		configuration.removeVomsServer(origName);
93  	            		configuration.addVomsServer(vo);
94  	            		Iterator it2 = configuration.getUserGroups().values().iterator();
95  	            		while (it2.hasNext()) {
96  	            			UserGroup userGroup = (UserGroup)it2.next();
97  	            			if (userGroup instanceof VOMSUserGroup && ((VOMSUserGroup)userGroup).getVomsServer().equals(origName))
98  	            				((VOMSUserGroup)userGroup).setVomsServer(name);
99  	            		}
100             		}
101             	}
102             }
103             
104             // Clean up account mapper names
105             it = new ArrayList(configuration.getAccountMappers().keySet()).iterator();
106             while (it.hasNext()) {
107             	String name = (String)it.next();
108             	String origName = new String(name);
109             	if (name.indexOf("://")!=-1) {
110             		name = name.replaceAll(".*://","");
111             		name = name.replaceAll(".*/dc=","");
112             		name = name.replaceAll("dc=","");
113             		name = name.toLowerCase();
114             		if (configuration.getAccountMapper(name)!=null) {
115 	            		int count = 1;
116 	            		while (configuration.getAccountMapper(name + Integer.toString(count)) != null) 
117 	            			count++;
118             			name += Integer.toString(count);
119             		}
120             		AccountMapper accountMapper = configuration.getAccountMapper(origName);
121             		if (accountMapper!=null) {
122             			accountMapper.setName(name);
123 	            		configuration.removeAccountMapper(origName);
124 	            		configuration.addAccountMapper(accountMapper);
125 	            		Iterator it2 = configuration.getGroupToAccountMappings().values().iterator();
126 	            		while (it2.hasNext()) {
127 	            			GroupToAccountMapping groupToAccountMapping = (GroupToAccountMapping)it2.next();
128 	            			Iterator it3 = groupToAccountMapping.getAccountMappers().iterator();
129 	            			int index = 0;
130 	            			while (it3.hasNext()) {
131 	            				String str = (String)it3.next();
132 	            				if (str.equals(origName)) {
133 	            					groupToAccountMapping.getAccountMappers().remove(index);
134 	            					groupToAccountMapping.getAccountMappers().add(index, name);
135 	            					it3 = groupToAccountMapping.getAccountMappers().iterator();
136 	            					index = 0;
137 	            				}
138 	            				else
139 	            					index++;
140 	            			}
141 	            		}
142             		}
143             	}
144             }
145        
146             // Insert GIP probe
147             PersistenceFactory persistenceFactory = configuration.getPersistenceFactory("mysql");
148             if (persistenceFactory==null && configuration.getPersistenceFactories().size()>0)
149             	persistenceFactory = (PersistenceFactory)configuration.getPersistenceFactories().values().iterator().next();
150             if (persistenceFactory != null) {
151             	// Add UserGroup
152             	UserGroup userGroup = configuration.getUserGroup("gums-test");
153             	if (userGroup==null || !(userGroup instanceof ManualUserGroup)) {
154                 	int index = 1;
155             		while (configuration.getUserGroup("gums-test"+(index==1?"":Integer.toString(index)))!=null)
156             			index++;
157             		userGroup = new ManualUserGroup(configuration, "gums-test"+(index==1?"":Integer.toString(index)));
158             		userGroup.setDescription("Testing GUMS-status with GIP Probe");
159             		((ManualUserGroup)userGroup).setPersistenceFactory(persistenceFactory.getName());
160             		configuration.addUserGroup(userGroup);
161             	}
162             	
163             	// Add member to usergroup's database
164             	GridUser user = new GridUser();
165         		user.setCertificateDN("/GIP-GUMS-Probe-Identity");
166             	if(((ManualUserGroup)userGroup).getMemberList().indexOf(user)==-1) {
167             		((ManualUserGroup)userGroup).addMember(user);
168             	}
169             	
170             	// Add AccountMapper
171             	AccountMapper accountMapper = configuration.getAccountMapper("gums-test");
172             	if (accountMapper==null || !(accountMapper instanceof GroupAccountMapper) || !((GroupAccountMapper)accountMapper).getAccountName().equals("GumsTestUserMappingSuccessful")) {
173                 	int index = 1;
174             		while (configuration.getAccountMapper("gums-test"+(index==1?"":Integer.toString(index)))!=null)
175             			index++;
176             		accountMapper = new GroupAccountMapper(configuration, "gums-test"+(index==1?"":Integer.toString(index)));
177             		accountMapper.setDescription("Testing GUMS-status with GIP Probe");
178             		((GroupAccountMapper)accountMapper).setAccountName("GumsTestUserMappingSuccessful");
179             		configuration.addAccountMapper(accountMapper);
180             	}
181             	
182             	// Add GroupToAccountMapping
183             	GroupToAccountMapping g2aMapping = configuration.getGroupToAccountMapping("gums-test");
184             	if (g2aMapping==null) {
185                 	int index = 1;
186             		while (configuration.getGroupToAccountMapping("gums-test"+(index==1?"":Integer.toString(index)))!=null)
187             			index++;
188             		g2aMapping = new GroupToAccountMapping(configuration, "gums-test"+(index==1?"":Integer.toString(index)));
189             		g2aMapping.setDescription("Testing GUMS-status with GIP Probe");
190             		configuration.addGroupToAccountMapping(g2aMapping);
191             	}
192             	if (g2aMapping.getAccountMappers().indexOf(accountMapper.getName())==-1)
193             		g2aMapping.addAccountMapper(accountMapper.getName());
194             	if (g2aMapping.getUserGroups().indexOf(userGroup.getName())==-1)
195             		g2aMapping.addUserGroup(userGroup.getName());     
196             	
197            		// add or alter HostToGroupMapping
198            		String domainName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
199             	if (domainName!=null && domainName.indexOf(".")!=-1)
200             		domainName = domainName.substring(domainName.indexOf("."),domainName.length());
201            		String cn = "*/?*" + (domainName!=null?domainName:".localdomain");
202            		List h2gMappings = configuration.getHostToGroupMappings();
203            		boolean foundCn = false;
204            		for (int i=0; i<h2gMappings.size(); i++) {
205            			// add groupToAccountMapping to each hostToGroupMapping
206            			HostToGroupMapping h2gMapping = (HostToGroupMapping)h2gMappings.get(i);
207            			h2gMapping.addGroupToAccountMapping(g2aMapping.getName()); 
208            			if (h2gMapping.getName().indexOf(cn)!=-1)
209            				foundCn = true;
210            		}
211             	if (!foundCn) {
212             		// create a new hostToGroupMapping
213             		HostToGroupMapping h2gMapping = new CertificateHostToGroupMapping(configuration);
214             		h2gMapping.setDescription("Testing GUMS-status with GIP Probe");
215             		((CertificateHostToGroupMapping)h2gMapping).setCn(cn);
216             		configuration.addHostToGroupMapping(h2gMapping);
217             		h2gMapping.addGroupToAccountMapping(g2aMapping.getName()); 
218             	}
219             }
220             
221             return configuration;
222 	     } catch (Exception e) {
223 	        gumsResourceAdminLog.fatal("Could not convert older version of gums.config: " + e.getMessage());
224 	        log.info("Could not convert older version of gums.config", e);
225 	        throw new RuntimeException("Could not convert older version of gums.config");	    	 
226 	     } 
227 	}
228 
229 }