EnableHlsTaskRunner extends BaseHlsTaskRunner { public String performTask(String taskParameters) { EnableStreamingTaskParameters taskParams = EnableStreamingTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); boolean secure = taskParams.isSecure(); List<String> allowedOrigins = taskParams.getAllowedOrigins(); log.info("Performing " + TASK_NAME + " task on space " + spaceId + ". Secure streaming set to " + secure); String bucketName = unwrappedS3Provider.getBucketName(spaceId); String domainName = null; String distId = null; String oaIdentityId = getOriginAccessId(); EnableStreamingTaskResult taskResult = new EnableStreamingTaskResult(); DistributionSummary existingDist = getExistingDistribution(bucketName); if (existingDist != null) { boolean existingSecure = !existingDist.getDefaultCacheBehavior().getTrustedSigners().getItems().isEmpty(); if ((secure && !existingSecure) || (!secure && existingSecure)) { throw new UnsupportedTaskException(TASK_NAME, "The space " + spaceId + " is already configured to stream as " + (secure ? "OPEN" : "SECURE") + " and cannot be updated to stream as " + (secure ? "SECURE" : "OPEN") + ". To do this, you must first execute the " + StorageTaskConstants.DELETE_HLS_TASK_NAME + " task."); } distId = existingDist.getId(); if (!existingDist.isEnabled()) { setDistributionState(distId, true); } domainName = existingDist.getDomainName(); } else { S3OriginConfig s3OriginConfig = new S3OriginConfig() .withOriginAccessIdentity(S3_ORIGIN_OAI_PREFIX + oaIdentityId); Origin s3Origin = new Origin().withDomainName(bucketName + S3_ORIGIN_SUFFIX) .withS3OriginConfig(s3OriginConfig) .withId("S3-" + bucketName); TrustedSigners signers = new TrustedSigners(); if (secure) { signers.setItems(Collections.singletonList(cfAccountId)); signers.setEnabled(true); signers.setQuantity(1); } else { signers.setEnabled(false); signers.setQuantity(0); } DefaultCacheBehavior defaultCacheBehavior = new DefaultCacheBehavior(); defaultCacheBehavior.setTrustedSigners(signers); defaultCacheBehavior.setViewerProtocolPolicy(ViewerProtocolPolicy.RedirectToHttps); defaultCacheBehavior.setAllowedMethods( new AllowedMethods().withItems(Method.GET, Method.HEAD, Method.OPTIONS) .withQuantity(3)); defaultCacheBehavior.setForwardedValues( new ForwardedValues() .withQueryString(false) .withCookies(new CookiePreference().withForward(ItemSelection.None)) .withHeaders(new Headers().withItems("Origin", "Access-Control-Request-Headers", "Access-Control-Request-Method") .withQuantity(3))); defaultCacheBehavior.setMinTTL(0l); defaultCacheBehavior.setTargetOriginId(s3Origin.getId()); Origins origins; CacheBehaviors cacheBehaviors = new CacheBehaviors(); if (secure) { CustomOriginConfig cookiesOriginConfig = new CustomOriginConfig() .withOriginProtocolPolicy(OriginProtocolPolicy.HttpsOnly) .withHTTPPort(80) .withHTTPSPort(443); String getCookiesPath = "/durastore/aux"; String cookiesOriginId = "Custom origin - " + dcHost + getCookiesPath; Origin cookiesOrigin = new Origin().withDomainName(dcHost) .withOriginPath(getCookiesPath) .withId(cookiesOriginId) .withCustomOriginConfig(cookiesOriginConfig); origins = new Origins().withItems(s3Origin, cookiesOrigin).withQuantity(2); CookiePreference cookiePreference = new CookiePreference().withForward(ItemSelection.All); CacheBehavior cookiesCacheBehavior = new CacheBehavior() .withPathPattern("/cookies") .withTargetOriginId(cookiesOriginId) .withViewerProtocolPolicy(ViewerProtocolPolicy.RedirectToHttps) .withAllowedMethods(new AllowedMethods().withItems(Method.GET, Method.HEAD).withQuantity(2)) .withForwardedValues(new ForwardedValues().withQueryString(true).withCookies(cookiePreference)) .withTrustedSigners(new TrustedSigners().withEnabled(false).withQuantity(0)) .withMinTTL(0l); cacheBehaviors = cacheBehaviors.withItems(cookiesCacheBehavior).withQuantity(1); } else { origins = new Origins().withItems(s3Origin).withQuantity(1); } DistributionConfig distributionConfig = new DistributionConfig() .withCallerReference("" + System.currentTimeMillis()) .withOrigins(origins) .withEnabled(true) .withComment("HLS streaming for space: " + spaceId) .withDefaultCacheBehavior(defaultCacheBehavior); if (secure) { distributionConfig.setCacheBehaviors(cacheBehaviors); } Distribution dist = cfClient.createDistribution( new CreateDistributionRequest(distributionConfig)).getDistribution(); domainName = dist.getDomainName(); } setBucketAccessPolicy(bucketName, oaIdentityId); setCorsPolicy(bucketName, allowedOrigins, dcHost); Map<String, String> spaceProps = s3Provider.getSpaceProperties(spaceId); spaceProps.put(HLS_STREAMING_HOST_PROP, domainName); spaceProps.put(HLS_STREAMING_TYPE_PROP, secure ? STREAMING_TYPE.SECURE.name() : STREAMING_TYPE.OPEN.name()); unwrappedS3Provider.setNewSpaceProperties(spaceId, spaceProps); taskResult.setResult(TASK_NAME + " task completed successfully"); taskResult.setStreamingHost(domainName); String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; } EnableHlsTaskRunner(StorageProvider s3Provider,
                               S3StorageProvider unwrappedS3Provider,
                               AmazonS3 s3Client,
                               AmazonCloudFrontClient cfClient,
                               String cfAccountId,
                               String dcHost); String getName(); String performTask(String taskParameters);  }
@Test public void testPerformTaskSwapDistType() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); expectExistingOriginAccessId(); expectExistingSecureDistribution(true); EnableHlsTaskRunner runner = new EnableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient, cfAccountId, dcHost); replayMocks(); EnableStreamingTaskParameters taskParams = new EnableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setSecure(false); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (UnsupportedTaskException e) { assertNotNull(e); } }
@Test public void testPerformTaskOpenEnable() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); expectExistingOriginAccessId(); expectExistingOpenDistribution(false); Capture<UpdateDistributionRequest> updateDistRequestCapture = expectSetDistributionState(); expectSetBucketPolicy(); Capture<BucketCrossOriginConfiguration> corsConfigCapture = expectSetCorsPolicy(); Capture<Map<String, String>> spacePropsCapture = expectSetBucketTags(); EnableHlsTaskRunner runner = new EnableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient, cfAccountId, dcHost); replayMocks(); EnableStreamingTaskParameters taskParams = new EnableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setSecure(false); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); EnableStreamingTaskResult taskResult = EnableStreamingTaskResult.deserialize(results); assertNotNull(taskResult); assertEquals(domainName, taskResult.getStreamingHost()); UpdateDistributionRequest updateDistRequest = updateDistRequestCapture.getValue(); assertTrue(updateDistRequest.getDistributionConfig().getEnabled()); BucketCrossOriginConfiguration corsConfig = corsConfigCapture.getValue(); assertEquals(1, corsConfig.getRules().size()); CORSRule corsRule = corsConfig.getRules().get(0); assertEquals(1, corsRule.getAllowedOrigins().size()); assertEquals("https: assertEquals(2, corsRule.getAllowedMethods().size()); assertEquals(1, corsRule.getAllowedHeaders().size()); assertEquals("*", corsRule.getAllowedHeaders().get(0)); Map<String, String> spaceProps = spacePropsCapture.getValue(); assertEquals(domainName, spaceProps.get(HLS_STREAMING_HOST_PROP)); assertEquals("OPEN", spaceProps.get(HLS_STREAMING_TYPE_PROP)); }
@Test public void testPerformTaskNoChangesNeeded() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); expectExistingOriginAccessId(); expectExistingOpenDistribution(true); expectSetBucketPolicy(); Capture<BucketCrossOriginConfiguration> corsConfigCapture = expectSetCorsPolicy(); Capture<Map<String, String>> spacePropsCapture = expectSetBucketTags(); EnableHlsTaskRunner runner = new EnableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient, cfAccountId, dcHost); replayMocks(); EnableStreamingTaskParameters taskParams = new EnableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setSecure(false); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); EnableStreamingTaskResult taskResult = EnableStreamingTaskResult.deserialize(results); assertNotNull(taskResult); assertEquals(domainName, taskResult.getStreamingHost()); BucketCrossOriginConfiguration corsConfig = corsConfigCapture.getValue(); assertEquals(1, corsConfig.getRules().size()); CORSRule corsRule = corsConfig.getRules().get(0); assertEquals(1, corsRule.getAllowedOrigins().size()); assertEquals("https: assertEquals(2, corsRule.getAllowedMethods().size()); assertEquals(1, corsRule.getAllowedHeaders().size()); assertEquals("*", corsRule.getAllowedHeaders().get(0)); Map<String, String> spaceProps = spacePropsCapture.getValue(); assertEquals(domainName, spaceProps.get(HLS_STREAMING_HOST_PROP)); assertEquals("OPEN", spaceProps.get(HLS_STREAMING_TYPE_PROP)); }
@Test public void testPerformTaskOpenSuccess() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); expectNewOriginAccessId(); Capture<CreateDistributionRequest> createDistRequestCapture = expectNewDistribution(); expectSetBucketPolicy(); Capture<BucketCrossOriginConfiguration> corsConfigCapture = expectSetCorsPolicy(); Capture<Map<String, String>> spacePropsCapture = expectSetBucketTags(); EnableHlsTaskRunner runner = new EnableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient, cfAccountId, dcHost); replayMocks(); EnableStreamingTaskParameters taskParams = new EnableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setSecure(false); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); EnableStreamingTaskResult taskResult = EnableStreamingTaskResult.deserialize(results); assertNotNull(taskResult); assertEquals(taskResult.getStreamingHost(), domainName); DistributionConfig distConfig = createDistRequestCapture.getValue().getDistributionConfig(); assertTrue(distConfig.getEnabled()); assertNull(distConfig.getCacheBehaviors()); DefaultCacheBehavior cacheBehavior = distConfig.getDefaultCacheBehavior(); assertFalse(cacheBehavior.getTrustedSigners().getEnabled()); assertEquals(new Integer(0), cacheBehavior.getTrustedSigners().getQuantity()); assertEquals(0, cacheBehavior.getTrustedSigners().getItems().size()); assertEquals(new Integer(3), cacheBehavior.getAllowedMethods().getQuantity()); assertEquals(3, cacheBehavior.getAllowedMethods().getItems().size()); assertEquals(new Integer(3), cacheBehavior.getForwardedValues().getHeaders().getQuantity()); assertEquals(3, cacheBehavior.getForwardedValues().getHeaders().getItems().size()); assertFalse(cacheBehavior.getForwardedValues().getQueryString()); assertEquals("none", cacheBehavior.getForwardedValues().getCookies().getForward()); assertEquals(new Integer(1), distConfig.getOrigins().getQuantity()); assertEquals(1, distConfig.getOrigins().getItems().size()); assertTrue(distConfig.getOrigins().getItems().get(0).getDomainName().startsWith(bucketName)); BucketCrossOriginConfiguration corsConfig = corsConfigCapture.getValue(); assertEquals(1, corsConfig.getRules().size()); CORSRule corsRule = corsConfig.getRules().get(0); assertEquals(1, corsRule.getAllowedOrigins().size()); assertEquals("https: assertEquals(2, corsRule.getAllowedMethods().size()); assertEquals(1, corsRule.getAllowedHeaders().size()); assertEquals("*", corsRule.getAllowedHeaders().get(0)); Map<String, String> spaceProps = spacePropsCapture.getValue(); assertEquals(domainName, spaceProps.get(HLS_STREAMING_HOST_PROP)); assertEquals("OPEN", spaceProps.get(HLS_STREAMING_TYPE_PROP)); }
@Test public void testPerformTaskSecureSuccess() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); expectExistingOriginAccessId(); Capture<CreateDistributionRequest> createDistRequestCapture = expectNewDistribution(); expectSetBucketPolicy(); Capture<BucketCrossOriginConfiguration> corsConfigCapture = expectSetCorsPolicy(); Capture<Map<String, String>> spacePropsCapture = expectSetBucketTags(); EnableHlsTaskRunner runner = new EnableHlsTaskRunner(s3Provider, unwrappedS3Provider, s3Client, cfClient, cfAccountId, dcHost); replayMocks(); EnableStreamingTaskParameters taskParams = new EnableStreamingTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setSecure(true); List<String> allowedOrigins = new ArrayList<>(); String corsOrigin0 = "https: String corsOrigin1 = "http: allowedOrigins.add(corsOrigin0); allowedOrigins.add(corsOrigin1); taskParams.setAllowedOrigins(allowedOrigins); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); EnableStreamingTaskResult taskResult = EnableStreamingTaskResult.deserialize(results); assertNotNull(taskResult); assertEquals(taskResult.getStreamingHost(), domainName); DistributionConfig distConfig = createDistRequestCapture.getValue().getDistributionConfig(); assertTrue(distConfig.getEnabled()); assertEquals(new Integer(1), distConfig.getCacheBehaviors().getQuantity()); assertEquals(1, distConfig.getCacheBehaviors().getItems().size()); CacheBehavior altCacheBehavior = distConfig.getCacheBehaviors().getItems().get(0); assertEquals("/cookies", altCacheBehavior.getPathPattern()); assertTrue(altCacheBehavior.getTargetOriginId().contains(dcHost)); assertEquals(new Integer(2), altCacheBehavior.getAllowedMethods().getQuantity()); assertEquals(2, altCacheBehavior.getAllowedMethods().getItems().size()); assertTrue(altCacheBehavior.getForwardedValues().getQueryString()); assertEquals("all", altCacheBehavior.getForwardedValues().getCookies().getForward()); assertFalse(altCacheBehavior.getTrustedSigners().getEnabled()); DefaultCacheBehavior cacheBehavior = distConfig.getDefaultCacheBehavior(); assertTrue(cacheBehavior.getTrustedSigners().getEnabled()); assertEquals(new Integer(1), cacheBehavior.getTrustedSigners().getQuantity()); assertEquals(1, cacheBehavior.getTrustedSigners().getItems().size()); assertEquals(new Integer(3), cacheBehavior.getAllowedMethods().getQuantity()); assertEquals(3, cacheBehavior.getAllowedMethods().getItems().size()); assertEquals(new Integer(3), cacheBehavior.getForwardedValues().getHeaders().getQuantity()); assertEquals(3, cacheBehavior.getForwardedValues().getHeaders().getItems().size()); assertFalse(cacheBehavior.getForwardedValues().getQueryString()); assertEquals("none", cacheBehavior.getForwardedValues().getCookies().getForward()); assertEquals(new Integer(2), distConfig.getOrigins().getQuantity()); assertEquals(2, distConfig.getOrigins().getItems().size()); assertTrue(distConfig.getOrigins().getItems().get(0).getDomainName().startsWith(bucketName)); assertEquals("/durastore/aux", distConfig.getOrigins().getItems().get(1).getOriginPath()); BucketCrossOriginConfiguration corsConfig = corsConfigCapture.getValue(); assertEquals(3, corsConfig.getRules().size()); CORSRule corsRule0 = corsConfig.getRules().get(0); assertEquals(1, corsRule0.getAllowedOrigins().size()); assertEquals(corsOrigin0, corsRule0.getAllowedOrigins().get(0)); assertEquals(2, corsRule0.getAllowedMethods().size()); assertEquals(1, corsRule0.getAllowedHeaders().size()); assertEquals("*", corsRule0.getAllowedHeaders().get(0)); CORSRule corsRule1 = corsConfig.getRules().get(1); assertEquals(1, corsRule1.getAllowedOrigins().size()); assertEquals(corsOrigin1, corsRule1.getAllowedOrigins().get(0)); assertEquals(2, corsRule1.getAllowedMethods().size()); assertEquals(1, corsRule1.getAllowedHeaders().size()); assertEquals("*", corsRule1.getAllowedHeaders().get(0)); CORSRule corsRule2 = corsConfig.getRules().get(2); assertEquals(1, corsRule2.getAllowedOrigins().size()); assertEquals("https: assertEquals(2, corsRule2.getAllowedMethods().size()); assertEquals(1, corsRule2.getAllowedHeaders().size()); assertEquals("*", corsRule2.getAllowedHeaders().get(0)); Map<String, String> spaceProps = spacePropsCapture.getValue(); assertEquals(domainName, spaceProps.get(HLS_STREAMING_HOST_PROP)); assertEquals("SECURE", spaceProps.get(HLS_STREAMING_TYPE_PROP)); }