View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.secure;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Arrays;
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.jasypt.util.text.BasicTextEncryptor;
24  import org.junit.Test;
25  import org.kuali.common.util.Assert;
26  import org.kuali.common.util.LocationUtils;
27  import org.kuali.common.util.SimpleFormatter;
28  import org.kuali.common.util.Str;
29  import org.kuali.common.util.UnixCmds;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public class DefaultSecureChannelTest {
34  
35  	private static final Logger logger = LoggerFactory.getLogger(DefaultSecureChannelTest.class);
36  	SimpleFormatter formatter = new SimpleFormatter();
37  
38  	protected SecureChannel getSecureChannel() {
39  		String privateKeyString = LocationUtils.toString("/Users/jeffcaddel/.ssh/ole-key.pem");
40  		BasicTextEncryptor bte = new BasicTextEncryptor();
41  		bte.setPassword("1423Morgan");
42  		String s = bte.encrypt(privateKeyString);
43  		System.out.println(s);
44  		DefaultSecureChannel channel = new DefaultSecureChannel();
45  		channel.setUsername("root");
46  		channel.setHostname("env7.ole.kuali.org");
47  		channel.setStrictHostKeyChecking(false);
48  		channel.setIncludeDefaultPrivateKeyLocations(false);
49  		channel.setUseConfigFile(false);
50  		channel.setPrivateKeyStrings(Arrays.asList(privateKeyString));
51  		return channel;
52  	}
53  
54  	// @Test
55  	public void testExec() {
56  		try {
57  			UnixCmds cmds = new UnixCmds();
58  			SecureChannel channel = getSecureChannel();
59  			channel.open();
60  			show(channel.executeCommand(cmds.rmrf("/root/x")));
61  			show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/forced-shutdown.sh")));
62  			show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/cleanup.sh")));
63  			show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/startup.sh")));
64  			show(channel.executeCommand(cmds.mkdirp("/home/tomcat/x/y/z/foo")));
65  			show(channel.executeCommand("ls -la > /home/tomcat/x/y/z/foo.sh"));
66  			show(channel.executeCommand("cat", "foo\nbar"));
67  			show(channel.executeCommand(cmds.chmod("755", "/home/tomcat/x/y/z/foo.sh")));
68  			show(channel.executeCommand(cmds.chownr("tomcat", "tomcat", "/home/tomcat/x")));
69  			channel.close();
70  		} catch (Exception e) {
71  			e.printStackTrace();
72  		}
73  	}
74  
75  	protected void show(Result result) throws IOException {
76  		int exitValue = result.getExitValue();
77  		if (exitValue != 0) {
78  			logger.info("Exit value = {}", result.getExitValue());
79  		}
80  		if (result.getStdin() == null) {
81  			Object[] args = { result.getCommand(), formatter.getTime(result.getElapsed()) };
82  			logger.info("[{}] - {}", args);
83  		} else {
84  			Object[] args = { result.getCommand(), Str.flatten(result.getStdin(), "\\r", "\\n"), formatter.getTime(result.getElapsed()) };
85  			logger.info("[{}] < [{}] - {}", args);
86  		}
87  		String[] stdout = StringUtils.split(result.getStdout(), '\n');
88  		for (String line : stdout) {
89  			logger.info(line);
90  		}
91  		String[] stderr = StringUtils.split(result.getStderr(), '\n');
92  		for (String line : stderr) {
93  			logger.info(line);
94  		}
95  	}
96  
97  	// @Test
98  	public void testGetWorkingDirectory() {
99  		SecureChannel channel = null;
100 		try {
101 			channel = getSecureChannel();
102 			channel.open();
103 			RemoteFile file = channel.getWorkingDirectory();
104 			logger.info(file.getAbsolutePath());
105 		} catch (Exception e) {
106 			e.printStackTrace();
107 		} finally {
108 			ChannelUtils.closeQuietly(channel);
109 		}
110 	}
111 
112 	// @Test
113 	public void testRoundTrip() {
114 		SecureChannel channel = null;
115 		try {
116 			String absolutePath = "/root/x/y/z/hello.txt";
117 			File localSrc = new File("/tmp/sftp/src.txt");
118 			File localDst = new File("/tmp/sftp/dst.txt");
119 			channel = getSecureChannel();
120 			channel.open();
121 			RemoteFile remote = channel.getMetaData(absolutePath);
122 			channel.copyFile(localSrc, remote);
123 			Assert.isTrue(channel.exists(absolutePath));
124 			Assert.isFalse(channel.isDirectory(absolutePath));
125 			channel.copyFile(remote, localDst);
126 			channel.deleteFile(absolutePath);
127 			Assert.isTrue(channel.exists(absolutePath));
128 		} catch (Exception e) {
129 			e.printStackTrace();
130 		} finally {
131 			ChannelUtils.closeQuietly(channel);
132 		}
133 	}
134 
135 	@Test
136 	public void test1() {
137 		try {
138 			String filename = "/home/.kuali";
139 			File file = new File(filename);
140 			String url = LocationUtils.getCanonicalURLString(file);
141 			logger.info(url);
142 			String path = LocationUtils.getNormalizedAbsolutePath(filename);
143 			logger.info(path);
144 		} catch (Exception e) {
145 			e.printStackTrace();
146 		}
147 	}
148 
149 }