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 getSwiftCmd(SwiftClientCfg cfg, String swiftCli, String operation){ return String.format(SWIFT_CMD, swiftCli,cfg.getEndPoint(),cfg.getAccount(),cfg.getUserName(),cfg.getKey(),operation); }
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; }
@Test public void testListContainerCmdWithoutStoragePolicy() { 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 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 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 testGetUploadCmdWithSegmentsBecauseOfSize() { 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 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 -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 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))); }
@Test public void testGetSwiftCmd() { 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 cmd = SwiftUtil.getSwiftCmd(cfg, "swift", "stat"); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword stat"; assertThat(cmd, is(equalTo(expected))); }
@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 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))); }
@Test public void testGetUploadCmd() { 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 uploadCmd = SwiftUtil.getUploadObjectCommand(cfg, "swift", "T-1", "template.vhd", 1024); String expected = "/usr/bin/python swift -A swift.endpoint -U cs:sec-storage -K mypassword upload T-1 template.vhd"; 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))); }