Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLAT-10933: Generate direct url to packager with auth header for inte… #9451

Merged
merged 4 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions alpha/apps/kaltura/lib/request/kNetworkUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class kNetworkUtils
{
const KALTURA_AUTH_HEADER = 'HTTP_X_KALTURA_AUTH';
const DEFAULT_AUTH_HEADER_VERSION = 1;
/**
* @return bool
* @throws Exception
Expand All @@ -28,15 +29,14 @@ public static function isAuthenticatedURI()
$version = $parts[0];
$timestamp = $parts[1];
$expectedSignature = $parts[2];

$url = ltrim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')), '/');
$secret = kConf::get('vod_packager_secure_token','local', null);
if (!$secret)

$actualSignature = self::calculateSignature($version, $timestamp, $url);
if(!$actualSignature)
{
KalturaLog::warning("Missing authentication_secret in configuration");
return false;
}

$actualSignature = base64_encode(hash_hmac('sha256', "$version,$timestamp,$url", $secret, true));
KalturaLog::debug("Actual Signature [$actualSignature] - Expected Signature [$expectedSignature]" );
if ( $actualSignature !== $expectedSignature)
{
Expand All @@ -46,4 +46,17 @@ public static function isAuthenticatedURI()

return true;
}

public static function calculateSignature($version, $timestamp, $url)
{
$secret = kConf::get('vod_packager_authentication_secret','local', null);
if (!$secret)
{
KalturaLog::warning("Missing authentication_secret in configuration");
return '';
}

KalturaLog::debug("Calculating signature for version [$version], time [$timestamp], url [$url]");
return base64_encode(hash_hmac('sha256', "$version,$timestamp,$url", $secret, true));
}
}
41 changes: 24 additions & 17 deletions alpha/lib/model/FileSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,6 @@ public function getStatusAsString()
return (isset($this->statusMap[$this->getStatus()])) ? $this->statusMap[$this->getStatus()] : "Unknown";
}

protected function getServeUrl($entryId)
{
$dynamicAttributes = DeliveryProfileDynamicAttributes::init($this->getDc(), $entryId, PlaybackProtocol::HTTP, infraRequestUtils::getProtocol());
$flavorAsset = assetPeer::retrieveById($this->getObjectId());
$dynamicAttributes->setFlavorAssets(array($flavorAsset));
$urlManager = DeliveryProfilePeer::getRemoteDeliveryByStorageId($dynamicAttributes);
$urlManager->setDynamicAttributes($dynamicAttributes);
$url = $urlManager->buildServeFlavors();
return ($url[0]['urlPrefix'] . $url[0]['url']);
}

public function getExternalUrl($entryId, $format = PlaybackProtocol::HTTP)
{
$storage = StorageProfilePeer::retrieveByPK($this->getDc());
Expand All @@ -304,28 +293,46 @@ public function getExternalUrl($entryId, $format = PlaybackProtocol::HTTP)
{
return null;
}

$kalturaPeriodicStorage = false;
if(in_array($this->getDc(), kStorageExporter::getPeriodicStorageIdsByPartner($this->getPartnerId())))
{
return $this->getServeUrl($entryId);
$kalturaPeriodicStorage = true;
}

$urlManager = DeliveryProfilePeer::getRemoteDeliveryByStorageId(DeliveryProfileDynamicAttributes::init($this->getDc(), $entryId, PlaybackProtocol::HTTP, infraRequestUtils::getProtocol()));
if(is_null($urlManager) && infraRequestUtils::getProtocol() != 'http')
if(is_null($urlManager) && infraRequestUtils::getProtocol() != 'http' && !$kalturaPeriodicStorage)
$urlManager = DeliveryProfilePeer::getRemoteDeliveryByStorageId(DeliveryProfileDynamicAttributes::init($this->getDc(), $entryId));
if(is_null($urlManager))
return null;

$url = $urlManager->getFileSyncUrl($this);
$baseUrl = $urlManager->getUrl();


if($kalturaPeriodicStorage)
{
$url = '/direct' . $url;
$authParams = $this->addKalturaAuthParams($url);
$url .= $authParams;
}

$url = ltrim($url, "/");
if (strpos($url, "://") === false){
if (strpos($url, "://") === false)
{
$url = rtrim($baseUrl, "/") . "/".$url ;
}

return $url;
}


protected function addKalturaAuthParams($url)
{
$version = kNetworkUtils::DEFAULT_AUTH_HEADER_VERSION;
$timestamp = time();
$signature = kNetworkUtils::calculateSignature($version, $timestamp, $url);

return '?kaltura_auth=' . $version . ',' . $timestamp . ',' . $signature;
}

/* (non-PHPdoc)
* @see BaseFileSync::preUpdate()
*/
Expand Down