GetHlsSignedCookiesUrlTaskRunner extends BaseHlsTaskRunner { public String performTask(String taskParameters) { GetSignedCookiesUrlTaskParameters taskParams = GetSignedCookiesUrlTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); String ipAddress = taskParams.getIpAddress(); int minutesToExpire = taskParams.getMinutesToExpire(); if (minutesToExpire <= 0) { minutesToExpire = DEFAULT_MINUTES_TO_EXPIRE; } String redirectUrl = taskParams.getRedirectUrl(); log.info("Performing " + TASK_NAME + " task with parameters: spaceId=" + spaceId + ", minutesToExpire=" + minutesToExpire + ", ipAddress=" + ipAddress + ", redirectUrl=" + redirectUrl); String bucketName = unwrappedS3Provider.getBucketName(spaceId); checkThatStreamingServiceIsEnabled(spaceId, TASK_NAME); DistributionSummary 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(); Calendar expireCalendar = Calendar.getInstance(); expireCalendar.add(Calendar.MINUTE, minutesToExpire); Map<String, String> signedCookies = new HashMap<>(); try { File cfKeyPathFile = getCfKeyPathFile(this.cfKeyPath); CloudFrontCookieSigner.CookiesForCustomPolicy cookies = CloudFrontCookieSigner.getCookiesForCustomPolicy( SignerUtils.Protocol.https, domainName, cfKeyPathFile, "*", cfKeyId, expireCalendar.getTime(), null, ipAddress); signedCookies.put(cookies.getPolicy().getKey(), cookies.getPolicy().getValue()); signedCookies.put(cookies.getSignature().getKey(), cookies.getSignature().getValue()); signedCookies.put(cookies.getKeyPairId().getKey(), cookies.getKeyPairId().getValue()); } catch (InvalidKeySpecException | IOException e) { throw new RuntimeException("Error encountered attempting to create signed cookies in task " + TASK_NAME + ": " + e.getMessage(), e); } String token = storeCookies(signedCookies, domainName, redirectUrl); GetSignedCookiesUrlTaskResult taskResult = new GetSignedCookiesUrlTaskResult(); taskResult.setSignedCookiesUrl("https: String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; } GetHlsSignedCookiesUrlTaskRunner(StorageProvider s3Provider,
                                            S3StorageProvider unwrappedS3Provider,
                                            AmazonCloudFrontClient cfClient,
                                            StringDataStoreFactory dataStoreFactory,
                                            String cfKeyId,
                                            String cfKeyPath); String getName(); String performTask(String taskParameters); static final int DEFAULT_MINUTES_TO_EXPIRE; }
@Test public void testPerformTaskNoDistributionDomain() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); EasyMock.expect(s3Provider.getSpaceProperties(spaceId)) .andReturn(new HashMap<>()); GetHlsSignedCookiesUrlTaskRunner runner = new GetHlsSignedCookiesUrlTaskRunner(s3Provider, unwrappedS3Provider, cfClient, dataStoreFactory, cfKeyId, cfKeyPath); replayMocks(); try { runner.performTask(null); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } GetSignedCookiesUrlTaskParameters taskParams = new GetSignedCookiesUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setRedirectUrl(redirectUrl); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } }
@Test public void testPerformTaskNoDistribution() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); Map<String, String> props = new HashMap<>(); props.put(HLS_STREAMING_HOST_PROP, domainName); EasyMock.expect(s3Provider.getSpaceProperties(spaceId)) .andReturn(props); ListDistributionsResult distSummaryResult = new ListDistributionsResult().withDistributionList(new DistributionList()); EasyMock.expect(cfClient.listDistributions(EasyMock.isA(ListDistributionsRequest.class))) .andReturn(distSummaryResult); GetHlsSignedCookiesUrlTaskRunner runner = new GetHlsSignedCookiesUrlTaskRunner(s3Provider, unwrappedS3Provider, cfClient, dataStoreFactory, cfKeyId, cfKeyPath); replayMocks(); GetSignedCookiesUrlTaskParameters taskParams = new GetSignedCookiesUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setRedirectUrl(redirectUrl); try { runner.performTask(taskParams.serialize()); fail("Exception expected"); } catch (Exception expected) { assertNotNull(expected); } }
@Test public void testPerformTaskSuccess() { EasyMock.expect(unwrappedS3Provider.getBucketName(EasyMock.isA(String.class))) .andReturn(bucketName); Map<String, String> props = new HashMap<>(); props.put(HLS_STREAMING_HOST_PROP, domainName); EasyMock.expect(s3Provider.getSpaceProperties(spaceId)) .andReturn(props); cfClientExpectValidDistribution(cfClient); String token = "abc123"; EasyMock.expect(dataStoreFactory.create(Constants.HIDDEN_COOKIE_SPACE)) .andReturn(dataStore); Capture<String> cookiesDataCapture = Capture.newInstance(); EasyMock.expect(dataStore.storeData(EasyMock.capture(cookiesDataCapture))) .andReturn(token); GetHlsSignedCookiesUrlTaskRunner runner = new GetHlsSignedCookiesUrlTaskRunner(s3Provider, unwrappedS3Provider, cfClient, dataStoreFactory, cfKeyId, cfKeyPath); replayMocks(); GetSignedCookiesUrlTaskParameters taskParams = new GetSignedCookiesUrlTaskParameters(); taskParams.setSpaceId(spaceId); taskParams.setMinutesToExpire(100); taskParams.setIpAddress("1.2.3.4"); taskParams.setRedirectUrl(redirectUrl); String results = runner.performTask(taskParams.serialize()); assertNotNull(results); GetSignedCookiesUrlTaskResult taskResult = GetSignedCookiesUrlTaskResult.deserialize(results); assertNotNull(taskResult); String cookieUrl = taskResult.getSignedCookiesUrl(); assertNotNull(cookieUrl); assertEquals("https: String cookiesData = cookiesDataCapture.getValue(); SignedCookieData signedCookieData = SignedCookieData.deserialize(cookiesData); assertNotNull(signedCookieData); assertEquals(domainName, signedCookieData.getStreamingHost()); assertEquals(redirectUrl, signedCookieData.getRedirectUrl()); Map<String, String> signedCookies = signedCookieData.getSignedCookies(); assertNotNull(signedCookies); assertNotNull(signedCookies.get("CloudFront-Policy")); assertNotNull(signedCookies.get("CloudFront-Signature")); assertNotNull(signedCookies.get("CloudFront-Key-Pair-Id")); }