Skip to content

Commit

Permalink
[Task] Create and publish Sub Resource Integrity (SRI) hashes for the…
Browse files Browse the repository at this point in the history
… generated scripts #1621 (#1622)
  • Loading branch information
MSNev committed Aug 3, 2021
1 parent 975060a commit 5d8757b
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 135 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -178,6 +178,8 @@ dist-esm/
browser/
types/
dist/
# Don't commit the sub resource integrity generated files
**/*.integrity.json

# Grunt
.tscache
Expand Down
3 changes: 2 additions & 1 deletion AISKU/package.json
Expand Up @@ -14,14 +14,15 @@
},
"scripts": {
"clean": "rm -rfv browser types dist-esm",
"build": "npm run build:esm && npm run build:browser && npm run dtsgen",
"build": "npm run build:esm && npm run build:browser && npm run sri && npm run dtsgen",
"build:esm": "grunt aisku",
"build:browser": "rollup -c rollup.config.js",
"build:snippet": "grunt snippetvnext",
"rebuild": "npm run build",
"test": "grunt aiskutests",
"lint": "tslint -p tsconfig.json",
"dtsgen": "api-extractor run --local && node ../scripts/dtsgen.js 'Microsoft.ApplicationInsights'",
"sri": "node ../tools/subResourceIntegrity/generateIntegrityFile.js",
"nightwatch:chrome": "nightwatch -c Tests/nightwatch/nightwatch.json Tests/nightwatch/run_nightwatch.js --env chrome",
"nightwatch:firefox": "nightwatch -c Tests/nightwatch/nightwatch.json Tests/nightwatch/run_nightwatch.js --env firefox",
"nightwatch:edge": "nightwatch -c Tests/nightwatch/nightwatch.json Tests/nightwatch/run_nightwatch.js --env edge",
Expand Down
23 changes: 19 additions & 4 deletions AISKU/scripts/listCdnVersions.ps1
Expand Up @@ -15,6 +15,12 @@ param (

$metaSdkVer = "aijssdkver"
$metaSdkSrc = "aijssdksrc"
$jsContentType = "text/javascript; charset=utf-8";
$contentTypeMap = @{
"js" = $jsContentType;
"map" = "application/json";
"json" = "application/json";
};

$global:hasErrors = $false
$global:sasToken = $sasToken
Expand Down Expand Up @@ -373,19 +379,28 @@ Function ValidateAccess
Function GetVersion(
[string] $name
) {
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js)(?:\.map)?)$'
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js|integrity\.json)(?:\.map)?)$'
$match = ($name | select-string $regMatch -AllMatches).matches
$contentType = $jsContentType

if ($null -eq $match) {
return $null
}

$ext = $match.groups[6].value
$tokens = $ext.split(".")
if ($tokens.length -gt 0) {
$theExt = $tokens[$tokens.Count - 1]
$contentType = $contentTypeMap[$theExt]
}

[hashtable]$return = @{}
$return.path = $match.groups[1].value
$return.prefix = $match.groups[2].value
$return.ver = $match.groups[3].value
$return.verType = $match.groups[5].value
$return.ext = $match.groups[6].value
$return.contentType = $contentType

return $return
}
Expand Down Expand Up @@ -559,15 +574,15 @@ Function ListVersions(

if ($paths.ContainsKey($thePath) -ne $true) {
$paths[$thePath] = 1
$value = "{0,-20}" -f $thePath
$value = "{0,-28}" -f $thePath
$pathList = "$pathList$value "
} else {
$paths[$thePath] = ($paths[$thePath] + 1)
}
}

foreach ($thePath in $paths.Keys | Sort-Object) {
Log $(" - {1,-40} ({0})" -f $paths[$thePath],$thePath)
Log $(" - {1,-48} ({0})" -f $paths[$thePath],$thePath)
}

#Log $("v{0,-8} ({1,2}) - {2}" -f $key,$($fileList.Count),$pathList.Trim())
Expand Down Expand Up @@ -599,7 +614,7 @@ Function ListVersions(
$cacheControl = $cacheControl -replace "immutable","im"
$cacheControl = $cacheControl -replace ", "," "

Log $(" - {0,-44}{3,-13}{1,6:N1} Kb {2:yyyy-MM-dd HH:mm:ss} {4,10} {5}" -f $($blob.ICloudBlob.Container.Name + "/" + $blob.Name),($blob.Length/1kb),$blob.LastModified,$sdkVersion,$cacheControl,$metaTags)
Log $(" - {0,-48}{3,-13}{1,6:N1} Kb {2:yyyy-MM-dd HH:mm:ss} {4,10} {5}" -f $($blob.ICloudBlob.Container.Name + "/" + $blob.Name),($blob.Length/1kb),$blob.LastModified,$sdkVersion,$cacheControl,$metaTags)
}
}
}
Expand Down
39 changes: 32 additions & 7 deletions AISKU/scripts/publishReleaseToCdn.ps1
Expand Up @@ -13,6 +13,14 @@ param (

$metaSdkVer = "aijssdkver"

$cacheControl1Year = "public, max-age=31536000, immutable, no-transform";
$jsContentType = "text/javascript; charset=utf-8";
$contentTypeMap = @{
"js" = $jsContentType;
"map" = "application/json";
"json" = "application/json";
};

$global:hasErrors = $false
$global:cacheValue = $null
$global:sasToken = $sasToken
Expand Down Expand Up @@ -227,13 +235,18 @@ Function CheckLogin
Function AddReleaseFile(
$files,
[string] $releaseDir,
[string] $name
[string] $name,
[boolean] $optional = $false
) {
$sourcePath = (Join-Path $releaseDir -ChildPath ($name))

if (-Not (Test-Path $sourcePath)) {
Log-Warning "Missing expected source file '$sourcePath'";
exit
if ($false -eq $optional) {
Log-Warning "Missing expected source file '$sourcePath'";
exit
} else {
return
}
}

Log " - $sourcePath"
Expand Down Expand Up @@ -272,6 +285,7 @@ Function GetReleaseFiles
$files = New-Object 'system.collections.generic.dictionary[string,string]'

Log "Adding files";
AddReleaseFile $files $jsSdkSrcDir "ai.$version.integrity.json" $true
AddReleaseFile $files $jsSdkSrcDir "ai.$version.js"
AddReleaseFile $files $jsSdkSrcDir "ai.$version.js.map"
AddReleaseFile $files $jsSdkSrcDir "ai.$version.min.js"
Expand All @@ -291,19 +305,28 @@ Function GetReleaseFiles
Function GetVersion(
[string] $name
) {
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+\.\d+\.\d+(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js)(?:\.map)?)$'
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js|integrity\.json)(?:\.map)?)$'
$match = ($name | select-string $regMatch -AllMatches).matches
$contentType = $jsContentType

if ($null -eq $match) {
return $null
}

$ext = $match.groups[6].value
$tokens = $ext.split(".")
if ($tokens.length -gt 0) {
$theExt = $tokens[$tokens.Count - 1]
$contentType = $contentTypeMap[$theExt]
}

[hashtable]$return = @{}
$return.path = $match.groups[1].value
$return.prefix = $match.groups[2].value
$return.ver = $match.groups[3].value
$return.verType = $match.groups[5].value
$return.ext = $match.groups[6].value
$return.contentType = $contentType

return $return
}
Expand Down Expand Up @@ -379,6 +402,11 @@ Function PublishFiles(
$metadata[$metaSdkVer] = $version.ver
}

$contentType = $jsContentType
if ($null -ne $version.contentType) {
$contentType = $version.contentType
}

$newBlob = $null
$blob = Get-AzureStorageBlob -Container $storageContainer -Blob ($blobPrefix + $name) -Context $azureContext -ErrorAction SilentlyContinue
if ($null -ne $blob -and $blob.Count -ne 0) {
Expand Down Expand Up @@ -570,9 +598,6 @@ if ([string]::IsNullOrWhiteSpace($jsSdkDir) -eq $true) {
$fileTimeStamp = ((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmss")
$logFile = "$logDir\publishReleaseCdnLog_$fileTimeStamp.txt"

$cacheControl1Year = "public, max-age=31536000, immutable";
$contentType = "text/javascript; charset=utf-8";

Log-Params

# You will need to at least have the AzureRM module installed
Expand Down
29 changes: 22 additions & 7 deletions AISKU/scripts/setActiveCdnVersion.ps1
Expand Up @@ -15,6 +15,13 @@ param (

$metaSdkVer = "aijssdkver"
$metaSdkSrc = "aijssdksrc"
$cacheControl30Min = "public, max-age=1800, immutable, no-transform";
$jsContentType = "text/javascript; charset=utf-8";
$contentTypeMap = @{
"js" = $jsContentType;
"map" = "application/json";
"json" = "application/json";
};

$global:hasErrors = $false
$global:sasToken = $sasToken
Expand Down Expand Up @@ -373,19 +380,28 @@ Function ValidateAccess
Function GetVersion(
[string] $name
) {
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+\.\d+\.\d+(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js)(?:\.map)?)$'
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[^\.]+)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js|integrity\.json)(?:\.map)?)$'
$match = ($name | select-string $regMatch -AllMatches).matches
$contentType = $jsContentType

if ($null -eq $match) {
return $null
}

$ext = $match.groups[6].value
$tokens = $ext.split(".")
if ($tokens.length -gt 0) {
$theExt = $tokens[$tokens.Count - 1]
$contentType = $contentTypeMap[$theExt]
}

[hashtable]$return = @{}
$return.path = $match.groups[1].value
$return.prefix = $match.groups[2].value
$return.ver = $match.groups[3].value
$return.verType = $match.groups[4].value
$return.ext = $match.groups[5].value
$return.verType = $match.groups[5].value
$return.ext = $match.groups[6].value
$return.contentType = $contentType

return $return
}
Expand Down Expand Up @@ -680,9 +696,6 @@ if (!(Test-Path -Path $logDir)) {
$fileTimeStamp = ((get-date).ToUniversalTime()).ToString("yyyyMMddThhmmss")
$logFile = "$logDir\setActiveCdnVersionLog_$fileTimeStamp.txt"

$cacheControl30Min = "public, max-age=1800, immutable";
$contentType = "text/javascript; charset=utf-8";

Log-Params
Validate-Params

Expand Down Expand Up @@ -718,7 +731,9 @@ if ($files.ContainsKey($activeVersion) -ne $true) {
Log-Failure "Version [$activeVersion] does not appear to be deployed to [$container]"
} elseif ($files[$activeVersion].Count -ne 4 -and # Prior to 2.5.8
$files[$activeVersion].Count -ne 8 -and # Since 2.5.8
$files[$activeVersion].Count -ne 12) { # Since 2.5.8
$files[$activeVersion].Count -ne 9 -and # Since 2.6.5
$files[$activeVersion].Count -ne 12 -and # Since 2.5.8
$files[$activeVersion].Count -ne 13) { # Since 2.6.5
Log-Failure "Version [$activeVersion] does not fully deployed to [$container] -- only found [$($files[$activeVersion].Count)] file(s)"
}

Expand Down
5 changes: 3 additions & 2 deletions AISKULight/package.json
Expand Up @@ -9,13 +9,14 @@
"types": "types/index.d.ts",
"sideEffects": false,
"scripts": {
"build": "npm run build:esm && npm run build:browser && npm run dtsgen",
"build": "npm run build:esm && npm run build:browser && npm run sri && npm run dtsgen",
"build:esm": "grunt aiskulite",
"build:browser": "rollup -c rollup.config.js",
"rebuild": "npm run build",
"test": "echo 'No tests'",
"lint": "tslint -p tsconfig.json",
"dtsgen": "api-extractor run --local && node ../scripts/dtsgen.js 'Microsoft.ApplicationInsights'"
"dtsgen": "api-extractor run --local && node ../scripts/dtsgen.js 'Microsoft.ApplicationInsights'",
"sri": "node ../tools/subResourceIntegrity/generateIntegrityFile.js"
},
"devDependencies": {
"@microsoft/applicationinsights-rollup-plugin-uglify3-js": "1.0.0",
Expand Down
5 changes: 3 additions & 2 deletions channels/applicationinsights-channel-js/package.json
Expand Up @@ -11,13 +11,14 @@
"repository": "github:Microsoft/applicationinsights-js",
"scripts": {
"clean": "rm -rf browser dist dist-esm types",
"build": "npm run build:esm && npm run build:browser && npm run dtsgen",
"build": "npm run build:esm && npm run build:browser && npm run sri && npm run dtsgen",
"build:esm": "grunt aichannel",
"build:browser": "rollup -c",
"rebuild": "npm run build",
"test": "grunt aichanneltest",
"lint": "tslint -p tsconfig.json",
"dtsgen": "api-extractor run --local && node ../../scripts/dtsgen.js \"Microsoft Application Insights JavaScript SDK Channel\""
"dtsgen": "api-extractor run --local && node ../../scripts/dtsgen.js \"Microsoft Application Insights JavaScript SDK Channel\"",
"sri": "node ../../tools/subResourceIntegrity/generateIntegrityFile.js"
},
"devDependencies": {
"@microsoft/ai-test-framework": "0.0.1",
Expand Down

0 comments on commit 5d8757b

Please sign in to comment.