execute-all-sdl-tools.ps1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Param(
  2. [string] $GuardianPackageName, # Required: the name of guardian CLI package (not needed if GuardianCliLocation is specified)
  3. [string] $NugetPackageDirectory, # Required: directory where NuGet packages are installed (not needed if GuardianCliLocation is specified)
  4. [string] $GuardianCliLocation, # Optional: Direct location of Guardian CLI executable if GuardianPackageName & NugetPackageDirectory are not specified
  5. [string] $Repository=$env:BUILD_REPOSITORY_NAME, # Required: the name of the repository (e.g. dotnet/arcade)
  6. [string] $BranchName=$env:BUILD_SOURCEBRANCH, # Optional: name of branch or version of gdn settings; defaults to master
  7. [string] $SourceDirectory=$env:BUILD_SOURCESDIRECTORY, # Required: the directory where source files are located
  8. [string] $ArtifactsDirectory = (Join-Path $env:BUILD_ARTIFACTSTAGINGDIRECTORY ('artifacts')), # Required: the directory where build artifacts are located
  9. [string] $AzureDevOpsAccessToken, # Required: access token for dnceng; should be provided via KeyVault
  10. # Optional: list of SDL tools to run on source code. See 'configure-sdl-tool.ps1' for tools list
  11. # format.
  12. [object[]] $SourceToolsList,
  13. # Optional: list of SDL tools to run on built artifacts. See 'configure-sdl-tool.ps1' for tools
  14. # list format.
  15. [object[]] $ArtifactToolsList,
  16. # Optional: list of SDL tools to run without automatically specifying a target directory. See
  17. # 'configure-sdl-tool.ps1' for tools list format.
  18. [object[]] $CustomToolsList,
  19. [bool] $TsaPublish=$False, # Optional: true will publish results to TSA; only set to true after onboarding to TSA; TSA is the automated framework used to upload test results as bugs.
  20. [string] $TsaBranchName=$env:BUILD_SOURCEBRANCH, # Optional: required for TSA publish; defaults to $(Build.SourceBranchName); TSA is the automated framework used to upload test results as bugs.
  21. [string] $TsaRepositoryName=$env:BUILD_REPOSITORY_NAME, # Optional: TSA repository name; will be generated automatically if not submitted; TSA is the automated framework used to upload test results as bugs.
  22. [string] $BuildNumber=$env:BUILD_BUILDNUMBER, # Optional: required for TSA publish; defaults to $(Build.BuildNumber)
  23. [bool] $UpdateBaseline=$False, # Optional: if true, will update the baseline in the repository; should only be run after fixing any issues which need to be fixed
  24. [bool] $TsaOnboard=$False, # Optional: if true, will onboard the repository to TSA; should only be run once; TSA is the automated framework used to upload test results as bugs.
  25. [string] $TsaInstanceUrl, # Optional: only needed if TsaOnboard or TsaPublish is true; the instance-url registered with TSA; TSA is the automated framework used to upload test results as bugs.
  26. [string] $TsaCodebaseName, # Optional: only needed if TsaOnboard or TsaPublish is true; the name of the codebase registered with TSA; TSA is the automated framework used to upload test results as bugs.
  27. [string] $TsaProjectName, # Optional: only needed if TsaOnboard or TsaPublish is true; the name of the project registered with TSA; TSA is the automated framework used to upload test results as bugs.
  28. [string] $TsaNotificationEmail, # Optional: only needed if TsaOnboard is true; the email(s) which will receive notifications of TSA bug filings (e.g. [email protected]); TSA is the automated framework used to upload test results as bugs.
  29. [string] $TsaCodebaseAdmin, # Optional: only needed if TsaOnboard is true; the aliases which are admins of the TSA codebase (e.g. DOMAIN\alias); TSA is the automated framework used to upload test results as bugs.
  30. [string] $TsaBugAreaPath, # Optional: only needed if TsaOnboard is true; the area path where TSA will file bugs in AzDO; TSA is the automated framework used to upload test results as bugs.
  31. [string] $TsaIterationPath, # Optional: only needed if TsaOnboard is true; the iteration path where TSA will file bugs in AzDO; TSA is the automated framework used to upload test results as bugs.
  32. [string] $GuardianLoggerLevel='Standard', # Optional: the logger level for the Guardian CLI; options are Trace, Verbose, Standard, Warning, and Error
  33. [string[]] $CrScanAdditionalRunConfigParams, # Optional: Additional Params to custom build a CredScan run config in the format @("xyz:abc","sdf:1")
  34. [string[]] $PoliCheckAdditionalRunConfigParams, # Optional: Additional Params to custom build a Policheck run config in the format @("xyz:abc","sdf:1")
  35. [string[]] $CodeQLAdditionalRunConfigParams, # Optional: Additional Params to custom build a Semmle/CodeQL run config in the format @("xyz < abc","sdf < 1")
  36. [bool] $BreakOnFailure=$False # Optional: Fail the build if there were errors during the run
  37. )
  38. try {
  39. $ErrorActionPreference = 'Stop'
  40. Set-StrictMode -Version 2.0
  41. $disableConfigureToolsetImport = $true
  42. $global:LASTEXITCODE = 0
  43. # `tools.ps1` checks $ci to perform some actions. Since the SDL
  44. # scripts don't necessarily execute in the same agent that run the
  45. # build.ps1/sh script this variable isn't automatically set.
  46. $ci = $true
  47. . $PSScriptRoot\..\tools.ps1
  48. #Replace repo names to the format of org/repo
  49. if (!($Repository.contains('/'))) {
  50. $RepoName = $Repository -replace '(.*?)-(.*)', '$1/$2';
  51. }
  52. else{
  53. $RepoName = $Repository;
  54. }
  55. if ($GuardianPackageName) {
  56. $guardianCliLocation = Join-Path $NugetPackageDirectory (Join-Path $GuardianPackageName (Join-Path 'tools' 'guardian.cmd'))
  57. } else {
  58. $guardianCliLocation = $GuardianCliLocation
  59. }
  60. $workingDirectory = (Split-Path $SourceDirectory -Parent)
  61. $ValidPath = Test-Path $guardianCliLocation
  62. if ($ValidPath -eq $False)
  63. {
  64. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message 'Invalid Guardian CLI Location.'
  65. ExitWithExitCode 1
  66. }
  67. Exec-BlockVerbosely {
  68. & $(Join-Path $PSScriptRoot 'init-sdl.ps1') -GuardianCliLocation $guardianCliLocation -Repository $RepoName -BranchName $BranchName -WorkingDirectory $workingDirectory -AzureDevOpsAccessToken $AzureDevOpsAccessToken -GuardianLoggerLevel $GuardianLoggerLevel
  69. }
  70. $gdnFolder = Join-Path $workingDirectory '.gdn'
  71. if ($TsaOnboard) {
  72. if ($TsaCodebaseName -and $TsaNotificationEmail -and $TsaCodebaseAdmin -and $TsaBugAreaPath) {
  73. Exec-BlockVerbosely {
  74. & $guardianCliLocation tsa-onboard --codebase-name "$TsaCodebaseName" --notification-alias "$TsaNotificationEmail" --codebase-admin "$TsaCodebaseAdmin" --instance-url "$TsaInstanceUrl" --project-name "$TsaProjectName" --area-path "$TsaBugAreaPath" --iteration-path "$TsaIterationPath" --working-directory $workingDirectory --logger-level $GuardianLoggerLevel
  75. }
  76. if ($LASTEXITCODE -ne 0) {
  77. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message "Guardian tsa-onboard failed with exit code $LASTEXITCODE."
  78. ExitWithExitCode $LASTEXITCODE
  79. }
  80. } else {
  81. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message 'Could not onboard to TSA -- not all required values ($TsaCodebaseName, $TsaNotificationEmail, $TsaCodebaseAdmin, $TsaBugAreaPath) were specified.'
  82. ExitWithExitCode 1
  83. }
  84. }
  85. # Configure a list of tools with a default target directory. Populates the ".gdn/r" directory.
  86. function Configure-ToolsList([object[]] $tools, [string] $targetDirectory) {
  87. if ($tools -and $tools.Count -gt 0) {
  88. Exec-BlockVerbosely {
  89. & $(Join-Path $PSScriptRoot 'configure-sdl-tool.ps1') `
  90. -GuardianCliLocation $guardianCliLocation `
  91. -WorkingDirectory $workingDirectory `
  92. -TargetDirectory $targetDirectory `
  93. -GdnFolder $gdnFolder `
  94. -ToolsList $tools `
  95. -AzureDevOpsAccessToken $AzureDevOpsAccessToken `
  96. -GuardianLoggerLevel $GuardianLoggerLevel `
  97. -CrScanAdditionalRunConfigParams $CrScanAdditionalRunConfigParams `
  98. -PoliCheckAdditionalRunConfigParams $PoliCheckAdditionalRunConfigParams `
  99. -CodeQLAdditionalRunConfigParams $CodeQLAdditionalRunConfigParams
  100. if ($BreakOnFailure) {
  101. Exit-IfNZEC "Sdl"
  102. }
  103. }
  104. }
  105. }
  106. # Configure Artifact and Source tools with default Target directories.
  107. Configure-ToolsList $ArtifactToolsList $ArtifactsDirectory
  108. Configure-ToolsList $SourceToolsList $SourceDirectory
  109. # Configure custom tools with no default Target directory.
  110. Configure-ToolsList $CustomToolsList $null
  111. # At this point, all tools are configured in the ".gdn" directory. Run them all in a single call.
  112. # (If we used "run" multiple times, each run would overwrite data from earlier runs.)
  113. Exec-BlockVerbosely {
  114. & $(Join-Path $PSScriptRoot 'run-sdl.ps1') `
  115. -GuardianCliLocation $guardianCliLocation `
  116. -WorkingDirectory $SourceDirectory `
  117. -UpdateBaseline $UpdateBaseline `
  118. -GdnFolder $gdnFolder
  119. }
  120. if ($TsaPublish) {
  121. if ($TsaBranchName -and $BuildNumber) {
  122. if (-not $TsaRepositoryName) {
  123. $TsaRepositoryName = "$($Repository)-$($BranchName)"
  124. }
  125. Exec-BlockVerbosely {
  126. & $guardianCliLocation tsa-publish --all-tools --repository-name "$TsaRepositoryName" --branch-name "$TsaBranchName" --build-number "$BuildNumber" --onboard $True --codebase-name "$TsaCodebaseName" --notification-alias "$TsaNotificationEmail" --codebase-admin "$TsaCodebaseAdmin" --instance-url "$TsaInstanceUrl" --project-name "$TsaProjectName" --area-path "$TsaBugAreaPath" --iteration-path "$TsaIterationPath" --working-directory $workingDirectory --logger-level $GuardianLoggerLevel
  127. }
  128. if ($LASTEXITCODE -ne 0) {
  129. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message "Guardian tsa-publish failed with exit code $LASTEXITCODE."
  130. ExitWithExitCode $LASTEXITCODE
  131. }
  132. } else {
  133. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message 'Could not publish to TSA -- not all required values ($TsaBranchName, $BuildNumber) were specified.'
  134. ExitWithExitCode 1
  135. }
  136. }
  137. if ($BreakOnFailure) {
  138. Write-Host "Failing the build in case of breaking results..."
  139. Exec-BlockVerbosely {
  140. & $guardianCliLocation break --working-directory $workingDirectory --logger-level $GuardianLoggerLevel
  141. }
  142. } else {
  143. Write-Host "Letting the build pass even if there were breaking results..."
  144. }
  145. }
  146. catch {
  147. Write-Host $_.ScriptStackTrace
  148. Write-PipelineTelemetryError -Force -Category 'Sdl' -Message $_
  149. exit 1
  150. }