1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.FormatUtils;
27 import org.kuali.common.util.LocationUtils;
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
37 protected SecureChannel getSecureChannel() {
38 String privateKeyString = LocationUtils.toString("/Users/jeffcaddel/.ssh/ole-key.pem");
39 BasicTextEncryptor bte = new BasicTextEncryptor();
40 bte.setPassword("1423Morgan");
41 String s = bte.encrypt(privateKeyString);
42 System.out.println(s);
43 DefaultSecureChannel channel = new DefaultSecureChannel();
44 channel.setUsername("root");
45 channel.setHostname("env7.ole.kuali.org");
46 channel.setStrictHostKeyChecking(false);
47 channel.setIncludeDefaultPrivateKeyLocations(false);
48 channel.setUseConfigFile(false);
49 channel.setPrivateKeyStrings(Arrays.asList(privateKeyString));
50 return channel;
51 }
52
53
54 public void testExec() {
55 try {
56 UnixCmds cmds = new UnixCmds();
57 SecureChannel channel = getSecureChannel();
58 channel.open();
59 show(channel.executeCommand(cmds.rmrf("/root/x")));
60 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/forced-shutdown.sh")));
61 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/cleanup.sh")));
62 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/startup.sh")));
63 show(channel.executeCommand(cmds.mkdirp("/home/tomcat/x/y/z/foo")));
64 show(channel.executeCommand("ls -la > /home/tomcat/x/y/z/foo.sh"));
65 show(channel.executeCommand("cat", "foo\nbar"));
66 show(channel.executeCommand(cmds.chmod("755", "/home/tomcat/x/y/z/foo.sh")));
67 show(channel.executeCommand(cmds.chownr("tomcat", "tomcat", "/home/tomcat/x")));
68 channel.close();
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73
74 protected void show(Result result) throws IOException {
75 int exitValue = result.getExitValue();
76 if (exitValue != 0) {
77 logger.info("Exit value = {}", result.getExitValue());
78 }
79 if (result.getStdin() == null) {
80 Object[] args = { result.getCommand(), FormatUtils.getTime(result.getElapsed()) };
81 logger.info("[{}] - {}", args);
82 } else {
83 Object[] args = { result.getCommand(), Str.flatten(result.getStdin(), "\\r", "\\n"), FormatUtils.getTime(result.getElapsed()) };
84 logger.info("[{}] < [{}] - {}", args);
85 }
86 String[] stdout = StringUtils.split(result.getStdout(), '\n');
87 for (String line : stdout) {
88 logger.info(line);
89 }
90 String[] stderr = StringUtils.split(result.getStderr(), '\n');
91 for (String line : stderr) {
92 logger.info(line);
93 }
94 }
95
96
97 public void testGetWorkingDirectory() {
98 SecureChannel channel = null;
99 try {
100 channel = getSecureChannel();
101 channel.open();
102 RemoteFile file = channel.getWorkingDirectory();
103 logger.info(file.getAbsolutePath());
104 } catch (Exception e) {
105 e.printStackTrace();
106 } finally {
107 ChannelUtils.closeQuietly(channel);
108 }
109 }
110
111
112 public void testRoundTrip() {
113 SecureChannel channel = null;
114 try {
115 String absolutePath = "/root/x/y/z/hello.txt";
116 File localSrc = new File("/tmp/sftp/src.txt");
117 File localDst = new File("/tmp/sftp/dst.txt");
118 channel = getSecureChannel();
119 channel.open();
120 RemoteFile remote = channel.getMetaData(absolutePath);
121 channel.copyFile(localSrc, remote);
122 Assert.isTrue(channel.exists(absolutePath));
123 Assert.isFalse(channel.isDirectory(absolutePath));
124 channel.copyFile(remote, localDst);
125 channel.deleteFile(absolutePath);
126 Assert.isTrue(channel.exists(absolutePath));
127 } catch (Exception e) {
128 e.printStackTrace();
129 } finally {
130 ChannelUtils.closeQuietly(channel);
131 }
132 }
133
134 @Test
135 public void test1() {
136 try {
137 String filename = "/home/.kuali";
138 File file = new File(filename);
139 String url = LocationUtils.getCanonicalURLString(file);
140 logger.info(url);
141 String path = LocationUtils.getNormalizedAbsolutePath(filename);
142 logger.info(path);
143 } catch (Exception e) {
144 e.printStackTrace();
145 }
146 }
147
148 }