@Override public String createEntityExtractUrl(DataStore store, String installPath, ImageFormat format, DataObject dataObject) { SwiftTO swiftTO = (SwiftTO)store.getTO(); String tempKey = UUID.randomUUID().toString(); boolean result = SwiftUtil.setTempKey(swiftTO, tempKey); if (!result) { String errMsg = "Unable to set Temp-Key: " + tempKey; s_logger.error(errMsg); throw new CloudRuntimeException(errMsg); } String containerName = SwiftUtil.getContainerName(dataObject.getType().toString(), dataObject.getId()); String objectName = installPath.split("\\/")[1]; // Get extract url expiration interval set in global configuration (in seconds) int urlExpirationInterval = Integer.parseInt(_configDao.getValue(Config.ExtractURLExpirationInterval.toString())); URL swiftUrl = SwiftUtil.generateTempUrl(swiftTO, containerName, objectName, tempKey, urlExpirationInterval); if (swiftUrl != null) { s_logger.debug("Swift temp-url: " + swiftUrl.toString()); return swiftUrl.toString(); } throw new CloudRuntimeException("Unable to create extraction URL"); }
public static URL generateTempUrl(SwiftClientCfg cfg, String container, String object, String tempKey, int urlExpirationInterval) { int currentTime = (int) (System.currentTimeMillis() / 1000L); int expirationSeconds = currentTime + urlExpirationInterval; try { URL endpoint = new URL(cfg.getEndPoint()); String method = "GET"; String path = String.format("/v1/AUTH_%s/%s/%s", cfg.getAccount(), container, object); //sign the request String hmacBody = String.format("%s\n%d\n%s", method, expirationSeconds, path); String signature = calculateRFC2104HMAC(hmacBody, tempKey); path += String.format("?temp_url_sig=%s&temp_url_expires=%d", signature, expirationSeconds); //generate the temp url URL tempUrl = new URL(endpoint.getProtocol(), endpoint.getHost(), endpoint.getPort(), path); return tempUrl; } catch (Exception e) { logger.error(e.getMessage()); throw new CloudRuntimeException(e.getMessage()); } }
protected static String getSwiftObjectCmd(SwiftClientCfg cfg, String swiftCliPath, String operation,String container, String objectName) { String cmd = getSwiftCmd(cfg,swiftCliPath, operation) +" "+ container+" "+objectName; if(StringUtils.isNotBlank(cfg.getStoragePolicy()) && supportsStoragePolicies(operation)){ return cmd + String.format(WITH_STORAGE_POLICY, cfg.getStoragePolicy()); } return cmd; }
public static boolean deleteObject(SwiftClientCfg cfg, String path) { Script command = new Script("/bin/bash", logger); command.add("-c"); String[] paths = splitSwiftPath(path); if (paths == null) { return false; } String container = paths[0]; String objectName = paths[1]; command.add(getSwiftObjectCmd(cfg, getSwiftCLIPath(), "delete", container, objectName)); OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); command.execute(parser); return true; }
public static boolean postMeta(SwiftClientCfg cfg, String container, String object, Map<String, String> metas) { Script command = new Script("/bin/bash", logger); command.add("-c"); command.add(getSwiftObjectCmd(cfg, getSwiftCLIPath(),"post", container, object) + getMeta(metas)); OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser(); String result = command.execute(parser); if (result != null) { throw new CloudRuntimeException("Failed to post meta" + result); } return true; }
String containerName = SwiftUtil.getContainerName(destData.getObjectType().toString(), pathId); String swiftPath = SwiftUtil.putObject(swift, srcFile, containerName, srcFile.getName());
@Test public void testGetUploadCmdWithStoragePolicy() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\""; assertThat(uploadCmd, is(equalTo(expected))); }
public static String putObject(SwiftClientCfg cfg, File srcFile, String container, String fileName) { if (fileName == null) { fileName = srcFile.getName(); } Script command = new Script("/bin/bash", logger); command.add("-c"); command.add(String.format(CD_SRC, srcFile.getParent())+getUploadObjectCommand(cfg, getSwiftCLIPath(), container,fileName, srcFile.length())); OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); String result = command.execute(parser); if (result != null) { throw new CloudRuntimeException("Failed to upload file: " + result); } if (parser.getLines() != null) { String[] lines = parser.getLines().split("\\n"); for (String line : lines) { if (line.contains("Errno") || line.contains("failed") || line.contains("not found")) { throw new CloudRuntimeException("Failed to upload file: " + Arrays.toString(lines)); } } } return container + File.separator + srcFile.getName(); }
public static String[] list(SwiftClientCfg swift, String container, String rFilename) { StringBuilder swiftCmdBuilder = new StringBuilder(); swiftCmdBuilder.append(getSwiftContainerCmd(swift, getSwiftCLIPath(), "list", container)); if (rFilename != null) { swiftCmdBuilder.append(" -p "); swiftCmdBuilder.append(rFilename); } Script command = new Script("/bin/bash", logger); command.add("-c"); command.add(swiftCmdBuilder.toString()); OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser(); String result = command.execute(parser); if (result == null && parser.getLines() != null && !parser.getLines().equalsIgnoreCase("")) { return parser.getLines().split("\\n"); } else { if (result != null) { String errMsg = "swiftList failed , err=" + result; logger.debug("Failed to list " + errMsg); } else { String errMsg = "swiftList failed, no lines returns"; logger.debug("Failed to list " + errMsg); } } return new String[0]; }
command.add(getSwiftObjectCmd(cfg, getSwiftCLIPath(), "download", container, srcPath)+" -o " + destFilePath);
@Test public void testGetContainerName(){ String inputType = "Template"; long inputId = 45; String output = SwiftUtil.getContainerName(inputType, inputId); String expected = "T-45"; assertEquals(expected, output); }
@Test public void testGetSwiftContainerCmd() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn(null); String containerCmd = SwiftUtil.getSwiftContainerCmd(cfg, "swift", "list", "T-123"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword list T-123"; assertThat(containerCmd, is(equalTo(expected))); }
protected static String getUploadObjectCommand(SwiftClientCfg cfg, String swiftCliPath, String container, String objectName, long size) { String cmd = getSwiftObjectCmd(cfg, swiftCliPath, "upload", container, objectName); if(size > SWIFT_MAX_SIZE){ return cmd + WITH_SEGMENTS; } return cmd; }
static String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); return toHexString(mac.doFinal(data.getBytes())); }
protected static String getSwiftContainerCmd(SwiftClientCfg cfg, String swiftCliPath, String operation, String container) { return getSwiftCmd(cfg,swiftCliPath, operation) +" "+ container; }
@Test public void testGenerateTempUrl() { SwiftUtil.SwiftClientCfg cfg = Mockito.mock(SwiftUtil.SwiftClientCfg.class); when(cfg.getEndPoint()).thenReturn("http://localhost:8080/v1/"); when(cfg.getAccount()).thenReturn("test"); String container = "testContainer"; String object = "testObject"; String tempKey = "testKey"; int urlExpirationInterval = 3600; String expected = "http://localhost:8080/v1/AUTH_test/testContainer/testObject"; URL output = SwiftUtil.generateTempUrl(cfg, container, object, tempKey, urlExpirationInterval); assertTrue(output.toString().contains(expected)); }
@Test public void testSplitSwiftPath(){ String input = "container" + File.separator + "object"; String[] output = SwiftUtil.splitSwiftPath(input); String[] expected = {"container", "object"}; assertArrayEquals(expected, output); }
@Test public void testGetUploadCmdWithSegmentsAndStoragePolicy() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn("policy1"); String uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 5368709121L); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd --storage-policy \"policy1\" -S 5368709120"; assertThat(uploadCmd, is(equalTo(expected))); }
@Test public void testListContainerCmdWithStoragePolicyButNotSupportedByOperation() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn("policy1"); String uploadCmd = SwiftUtil.getSwiftContainerCmd(cfg, "swift", "list", "T-1"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword list T-1"; assertThat(uploadCmd, is(equalTo(expected))); }
@Test public void testGetSwiftObjectCmd() { SwiftClientCfg cfg = mock(SwiftClientCfg.class); given(cfg.getEndPoint()).willReturn("swift.endpoint"); given(cfg.getAccount()).willReturn("cs"); given(cfg.getUserName()).willReturn("sec-storage"); given(cfg.getKey()).willReturn("mypassword"); given(cfg.getStoragePolicy()).willReturn(null); String objectCmd = SwiftUtil.getSwiftObjectCmd(cfg, "swift", "delete", "T-123", "template.vhd"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword delete T-123 template.vhd"; assertThat(objectCmd, is(equalTo(expected))); }