Overview
This guide offers examples to easily update the Contrast .NET Core agent and keep it updated. Contrast frequently releases new versions of agents, so this is important to do. We encourage you to take this guide, make it your own, and distribute it to teams who need to update agents.
Main Steps
- Download the Contrast .NET Core agent to the same installation location. You can use one of the following methods:
- NuGet: we recommend this option
- Contrast repository
- Use automation scripts to update the Contrast .NET Core agent every time your application builds
Supported technologies
Before you begin, please be sure Contrast supports your preferred tools and environments for .NET Core:
.NET Core supported technologies
This guide assumes you have:
- Confirmed your .NET Core application runs properly without the Contrast .NET Core agent
- Previously successfully installed the Contrast .Net Core agent.
- Defined a policy for how and when to update the agent, based on your change management policy and the environment where you deploy agents.
- An existing workflow to manage and keep application dependencies updated.
Instructions
1) Download the Contrast .NET Core agent
Put the agent in the same installation location. Choose the method you initially used to install the Contrast .NET Core agent:
Use NuGet in Visual Studio
- Use the following NuGet command to update the latest Contrast agent. Include the script in the application startup script, automated deployment pipeline, or add the script as a cron job to update the agent automatically.
Linuxdotnet add package Contrast.SensorsNetCore |
PowershellInstall-Package Contrast.SensorsNetCore |
- Add the above command in your application startup, or add the script as a cron job, as described in the second step.
Use the Contrast repository
- SaaS: Contrast synchronizes .NET Core agent releases with public NuGet repositories.
- EOP (on-premises): Contrast does not recommend using newer versions of Contrast agents than those available from your Contrast instance. Use the same version of the .NET Core agent version you would otherwise download directly from the Contrast UI.
Get the following API information:
CONTRAST_URL=<TeamServer URL i.e. https://app.contrastsecurity.com > ORG_ID=<YOUR TEAMSERVER ORGANIZATION ID> AUTH_TOKEN=<YOUR TEAMSERVER AUTHENTICATION TOKEN> API_KEY=<YOUR TEAMSERVER API KEY>
Use one of the following scripts to download Contrast .NET Core agent. Include the script in the application startup script, automated deployment pipeline, or add the script as a cron job to automatically update the agent.
Bash script:
CONTRAST_URL=https://app.contrastsecurity.com ORG_ID=xxxx AUTH_TOKEN=xxxx API_KEY=xxxx curl -X GET $CONTRAST_URL/Contrast/api/ng/$ORG_ID/agents/default/DOTNET_CORE /
-o ./Contrast.NET.Core.zip -H 'Authorization: $AUTH_TOKEN' -H 'API-Key: $API_KEY' /
-H 'Accept: application/json' -OJ
Powershell
$CONTRAST_API_URL="https://app.contrastsecurity.com/Contrast"
$CONTRAST_USER_ID=""
$CONTRAST_SVC_KEY=""
$CONTRAST_API_KEY=""
$CONTRAST_ORG_ID=""
$CONTRAST_AGENT_FOLDER="dotnetcore"
$CONTRAST_DATA_LOCATION=".\$CONTRAST_AGENT_FOLDER"
$CONTRAST_INSTALL_LOCATION=".\$CONTRAST_AGENT_FOLDER"
$CONTRAST_LOG_LOCATION="$CONTRAST_DATA_LOCATION\logs"
$CREDS = "$CONTRAST_USER_ID`:$CONTRAST_SVC_KEY"
$AUTH=[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($CREDS))
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['Invoke-RestMethod:SkipHeaderValidation'] = $true
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
New-Item -ItemType Directory -Force -Path "$CONTRAST_LOG_LOCATION"
Set-Location $CONTRAST_DATA_LOCATION
Invoke-RestMethod "$CONTRAST_API_URL/api/ng/$CONTRAST_ORG_ID/agents/default/DOTNET_CORE" / -Method GET -Headers @{"Authorization"="$AUTH";"API-Key"="$CONTRAST_API_KEY"} / -OutFile "Contrast.zip"
Invoke-RestMethod "$CONTRAST_API_URL/api/ng/$CONTRAST_ORG_ID/agents/external/default/DOTNET_CORE" / -Method GET -Headers @{"Accept"="text/yaml";"Authorization"="$AUTH";"API-Key"="$CONTRAST_API_KEY"} / -OutFile "contrast_security.yaml"
Expand-Archive -Force "Contrast.zip" -DestinationPath "$CONTRAST_INSTALL_LOCATION"
Remove-Item -Force .\Contrast.zip
Unzip the downloaded file and save the contents to the current Contrast agent location. If you don’t know the location, you can look up the environment variables. Use the command for your system.
CORECLR_PROFILER_PATH_64 |
Windows (64-bit) |
CORECLR_PROFILER_PATH_32 |
Windows (32-bit) |
CORECLR_PROFILER_PATH_64 |
Linux (64-bit) |
2) Use automation scripts to update the Contrast .NET Core agent
You can also add scripts as a cron job to update the agent every time you build your application. To add the script as a cron job:
- Modify the cron entry using this command:
crontab -e
- Edit the following command to set the time you want to use. For example, this command updates the agent every day at 3am. Save and close the file.
0 3 * * * /agentupdate.sh
You can learn more about scheduling syntax here: https://crontab.guru/ - Check to see if you successfully added the cron job:
crontab -l
FAQ
- How can I see logs to troubleshoot any issues when I update the .NET core agent?
Troubleshooting > .NET Core Agent