001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util.secure;
017
018 import java.io.File;
019 import java.io.IOException;
020 import java.util.Arrays;
021
022 import org.apache.commons.lang3.StringUtils;
023 import org.jasypt.util.text.BasicTextEncryptor;
024 import org.junit.Test;
025 import org.kuali.common.util.Assert;
026 import org.kuali.common.util.FormatUtils;
027 import org.kuali.common.util.LocationUtils;
028 import org.kuali.common.util.Str;
029 import org.kuali.common.util.UnixCmds;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class DefaultSecureChannelTest {
034
035 private static final Logger logger = LoggerFactory.getLogger(DefaultSecureChannelTest.class);
036
037 protected SecureChannel getSecureChannel() {
038 String privateKeyString = LocationUtils.toString("/Users/jeffcaddel/.ssh/ole-key.pem");
039 BasicTextEncryptor bte = new BasicTextEncryptor();
040 bte.setPassword("1423Morgan");
041 String s = bte.encrypt(privateKeyString);
042 System.out.println(s);
043 DefaultSecureChannel channel = new DefaultSecureChannel();
044 channel.setUsername("root");
045 channel.setHostname("env7.ole.kuali.org");
046 channel.setStrictHostKeyChecking(false);
047 channel.setIncludeDefaultPrivateKeyLocations(false);
048 channel.setUseConfigFile(false);
049 channel.setPrivateKeyStrings(Arrays.asList(privateKeyString));
050 return channel;
051 }
052
053 // @Test
054 public void testExec() {
055 try {
056 UnixCmds cmds = new UnixCmds();
057 SecureChannel channel = getSecureChannel();
058 channel.open();
059 show(channel.executeCommand(cmds.rmrf("/root/x")));
060 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/forced-shutdown.sh")));
061 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/cleanup.sh")));
062 show(channel.executeCommand(cmds.su("tomcat", "/usr/local/tomcat/bin/startup.sh")));
063 show(channel.executeCommand(cmds.mkdirp("/home/tomcat/x/y/z/foo")));
064 show(channel.executeCommand("ls -la > /home/tomcat/x/y/z/foo.sh"));
065 show(channel.executeCommand("cat", "foo\nbar"));
066 show(channel.executeCommand(cmds.chmod("755", "/home/tomcat/x/y/z/foo.sh")));
067 show(channel.executeCommand(cmds.chownr("tomcat", "tomcat", "/home/tomcat/x")));
068 channel.close();
069 } catch (Exception e) {
070 e.printStackTrace();
071 }
072 }
073
074 protected void show(Result result) throws IOException {
075 int exitValue = result.getExitValue();
076 if (exitValue != 0) {
077 logger.info("Exit value = {}", result.getExitValue());
078 }
079 if (result.getStdin() == null) {
080 Object[] args = { result.getCommand(), FormatUtils.getTime(result.getElapsed()) };
081 logger.info("[{}] - {}", args);
082 } else {
083 Object[] args = { result.getCommand(), Str.flatten(result.getStdin(), "\\r", "\\n"), FormatUtils.getTime(result.getElapsed()) };
084 logger.info("[{}] < [{}] - {}", args);
085 }
086 String[] stdout = StringUtils.split(result.getStdout(), '\n');
087 for (String line : stdout) {
088 logger.info(line);
089 }
090 String[] stderr = StringUtils.split(result.getStderr(), '\n');
091 for (String line : stderr) {
092 logger.info(line);
093 }
094 }
095
096 // @Test
097 public void testGetWorkingDirectory() {
098 SecureChannel channel = null;
099 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 // @Test
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 }