GetSignedUrlTaskRunner extends BaseStreamingTaskRunner { public String performTask(String taskParameters) { GetSignedUrlTaskParameters taskParams = GetSignedUrlTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); String contentId = taskParams.getContentId(); String resourcePrefix = taskParams.getResourcePrefix(); String ipAddress = taskParams.getIpAddress(); int minutesToExpire = taskParams.getMinutesToExpire(); if (minutesToExpire <= 0) { minutesToExpire = DEFAULT_MINUTES_TO_EXPIRE; } log.info("Performing " + TASK_NAME + " task with parameters: spaceId=" + spaceId + ", contentId=" + contentId + ", resourcePrefix=" + resourcePrefix + ", minutesToExpire=" + minutesToExpire + ", ipAddress=" + ipAddress); String bucketName = unwrappedS3Provider.getBucketName(spaceId); GetSignedUrlTaskResult taskResult = new GetSignedUrlTaskResult(); checkThatStreamingServiceIsEnabled(spaceId, TASK_NAME); StreamingDistributionSummary existingDist = getExistingDistribution(bucketName); if (null == existingDist) { throw new UnsupportedTaskException(TASK_NAME, "The " + TASK_NAME + " task can only be used after a space " + "has been configured to enable secure streaming. Use " + StorageTaskConstants.ENABLE_STREAMING_TASK_NAME + " to enable secure streaming on this space."); } String domainName = existingDist.getDomainName(); if (existingDist.getTrustedSigners().getItems().isEmpty()) { throw new UnsupportedTaskException(TASK_NAME, "The " + TASK_NAME + " task cannot be used to request a " + "stream from an open distribution. Use " + StorageTaskConstants.GET_URL_TASK_NAME + " instead."); } if (null == resourcePrefix) { resourcePrefix = ""; } Calendar expireCalendar = Calendar.getInstance(); expireCalendar.add(Calendar.MINUTE, minutesToExpire); try { File cfKeyPathFile = getCfKeyPathFile(this.cfKeyPath); String signedUrl = CloudFrontUrlSigner.getSignedURLWithCustomPolicy( SignerUtils.Protocol.rtmp, domainName, cfKeyPathFile, contentId, cfKeyId, expireCalendar.getTime(), null, ipAddress); taskResult.setSignedUrl("rtmp: resourcePrefix + signedUrl); } catch (InvalidKeySpecException | IOException e) { throw new RuntimeException("Error encountered attempting to sign URL for" + " task " + TASK_NAME + ": " + e.getMessage(), e); } String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; } GetSignedUrlTaskRunner(StorageProvider s3Provider,
                                  S3StorageProvider unwrappedS3Provider,
                                  AmazonCloudFrontClient cfClient,
                                  String cfKeyId,
                                  String cfKeyPath); String getName(); String performTask(String taskParameters); static final int DEFAULT_MINUTES_TO_EXPIRE; }
@Test public void testPerformTask1a() throws Exception { GetSignedUrlTaskRunner runner = createRunner(createMockStorageProvider(), createMockUnwrappedS3StorageProvider(), createMockS3ClientV1(), createMockCFClientV4(true)); try { runner.performTask(null); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } GetSignedUrlTaskParameters taskParams = new GetSignedUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setContentId(contentId); String results = runner.performTask(taskParams.serialize()); String signedUrl = GetSignedUrlTaskResult.deserialize(results).getSignedUrl(); assertTrue(signedUrl.startsWith("rtmp: assertTrue(signedUrl.contains("Policy=")); assertTrue(signedUrl.contains("Signature=")); assertTrue(signedUrl.contains("Key-Pair-Id=")); }
@Test public void testPerformTask1b() throws Exception { GetSignedUrlTaskRunner runner = createRunner(createMockStorageProvider(), createMockUnwrappedS3StorageProvider(), createMockS3ClientV1(), createMockCFClientV4(true)); String prefix = "prefix:"; GetSignedUrlTaskParameters taskParams = new GetSignedUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setContentId(contentId); taskParams.setResourcePrefix(prefix); taskParams.setIpAddress("1.2.3.4/32"); taskParams.setMinutesToExpire(10); String results = runner.performTask(taskParams.serialize()); String signedUrl = GetSignedUrlTaskResult.deserialize(results).getSignedUrl(); assertTrue(signedUrl.startsWith("rtmp: prefix + contentId)); assertTrue(signedUrl.contains("Policy=")); assertTrue(signedUrl.contains("Signature=")); assertTrue(signedUrl.contains("Key-Pair-Id=")); }
@Test public void testPerformTask2() throws Exception { GetSignedUrlTaskRunner runner = createRunner(createMockStorageProvider(), createMockUnwrappedS3StorageProvider(), createMockS3ClientV1(), createMockCFClientV3()); GetSignedUrlTaskParameters taskParams = new GetSignedUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setContentId(contentId); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (UnsupportedTaskException e) { assertTrue(e.getMessage() .contains(StorageTaskConstants.ENABLE_STREAMING_TASK_NAME)); } }
@Test public void testPerformTask3() throws Exception { GetSignedUrlTaskRunner runner = createRunner(createMockStorageProvider(), createMockUnwrappedS3StorageProvider(), createMockS3ClientV1(), createMockCFClientV4(false)); GetSignedUrlTaskParameters taskParams = new GetSignedUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setContentId(contentId); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (UnsupportedTaskException e) { assertTrue(e.getMessage() .contains(StorageTaskConstants.GET_URL_TASK_NAME)); } }
@Test public void testPerformTask4() throws Exception { BaseStreamingTaskRunner runner = createRunner(createMockStorageProvider(new HashMap<String, String>(), true), createMockUnwrappedS3StorageProvider(), createMockS3ClientV1(), createMockCFClientV1()); GetUrlTaskParameters taskParams = new GetUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setContentId(contentId); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (UnsupportedTaskException e) { assertTrue(e.getMessage() .contains(StorageTaskConstants.ENABLE_STREAMING_TASK_NAME)); } }