- It's essential to have a secure environment where pipelines can access resources, such as packages, secrets, and services, without exposing sensitive information.
- Configure pipeline access to packages stored in Azure Artifacts. It involves creating and managing feed permissions, which allow you to control who can access and manage your packages. By controlling access to these packages, you can ensure that only authorized users can access and use the packages in your projects.
-
Navigate to your Azure DevOps organization and select the project that contains the Azure Artifacts repository you want to configure.
-
In the left-side menu, select Artifacts.
-
If you don't have any feed, Create a feed.
-
In the Artifacts menu, select Feed Settings.
-
Click on the Permissions tab.
-
Add users or groups to the repository.
-
Select the permissions you want to assign to each user or group (for example, Owner, Reader, Contributor, or Collaborator).
-
Select Add users/groups, and then add your build identity as a Contributor. The project-level build identity is named as follows: [Project name] Build Service ([Organization name]). Example: Implement security through a pipeline using DevOps Build Service (contoso).
-
Save Your changes
- In Azure Pipelines, you can use the classic editor or the YAML tasks to publish your NuGet or other packages within your pipeline to your Azure Artifacts feed or public registries such as nuget.org.
steps:
- task: NuGetCommand@2
inputs:
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'select'
vstsFeed: 'SecurePipelineFeed'
- Replace your Azure Artifacts feed name with the name of your Azure Artifacts feed and your solution file.sln with the name of your solution file.
-
Open your Azure DevOps project and navigate to the Pipelines section.
-
Click on Pipelines in the left-hand menu.
-
Open your pipeline, or create a new one.
-
Click the Edit button in the right-top corner to edit your pipeline.
-
Click the Variables button.
-
Click the New variable button to create a new variable.
-
Enter the name and value for your secret.
-
Check the "Keep this value secret" checkbox to encrypt your secret.
-
(Optional) Check the "Let users override this value when running this pipeline" checkbox to allow users to override the value of your variable at queue time.
-
Click the OK button to save your variable.
-
Click the Save button to save your pipeline.
-
Open your Azure DevOps project and navigate to the Pipelines section.
-
Click on Library in the left-hand menu.
-
Open your variable group, or create a new one.
-
Click the Add button to add a new variable.
-
Give your variable a name (for example " Secret Key").
-
Enter the value for your secret in the Value field.
-
Click the Save button to save your variable group.
-
Open your Variable Group.
-
Click on the Pipeline permissions button.
-
Add the pipelines that will use this Variable Group.
-
Click the Save button to save your Variable Group.
-
Open your pipeline YAML file.
-
Add the following code to the top of your YAML file:
variables:
- group: <group_name>
- Use the following syntax to access your secrets within your pipeline
$(<group_name> Secret Key)
- If you want to use your pipeline variables, you can use the following:
$(New Credential Secret)
- Save your YAML file
-
Secret variables are encrypted at rest with a 2048-bit RSA key. Secrets are available on the agent for tasks and scripts to use. Be careful about who has access to alter your pipeline.
-
You must decide whether to use the Variable Groups or the pipeline UI variables. The advantage of using the Variable Groups is that you can use the same variables in multiple pipelines. The advantage of using the pipeline UI variables is that you can override the variable's value at queue time.
- Securing access to services is essential when working with pipelines in Azure DevOps. Service connections allow you to store your pipelines' credentials to access external resources, such as databases, web APIs, and other systems.
-
Go to your Azure DevOps project.
-
Navigate to the Project settings.
-
Click on Service connections under Pipelines.
-
Click on New service connection.
-
Select the type of service connection you want to create (for example, Azure Service Bus, Kubernetes, Apple App Store or other).
-
Enter the required information for the service connection.
-
Click on Save.
-
Go to your pipeline definition.
-
Click on Edit.
-
Click on Variables.
-
Create a new variable with the name that represents the service connection (for example, service_bus_connection).
-
Enter the value of the service connection.
-
Check the checkbox "Keep this value secret" to encrypt the variable.
-
Click on Save.
- Unlike normal variables, they are not automatically decrypted into script environment variables. You need to map secret variables explicitly.
steps:
- powershell: |
Write-Host "Using the mapped env var for this task works and is recommended: $env:MY_MAPPED_ENV_VAR"
env:
MY_MAPPED_ENV_VAR: $(service_bus_connection) # the recommended way to map to an env variable
- task: PublishToAzureServiceBus@1
inputs:
azureSubscription: $(service_bus_connection)
messageBody: '"hello world!"'
signPayload: false
waitForCompletion: true
- Securing access to sensitive information, such as passwords and API keys, is essential to DevOps.
-
The first step in securing access to credential secrets is to store them in Azure Key Vault. This service allows you to store and manage secrets, keys, and certificates securely and provides you with the ability to control access to these secrets.
-
To create an Azure Key Vault, go to the Azure portal and click on the "Create a resource" button.
-
Search and select the "Key Vault" option, click create and then fill out the required information to create a new vault.
-
Create a new Service Principal in Microsoft Entra ID to grant access to the Key Vault.
-
Assign the service Principal to the Key Vault
-
The service principal that you created will need to have Secret permissions access ("Get, List") to the Key Vault. If the service principal does not have access to the Key Vault, you will see an error message when you try to link the Variable Group to the Key Vault.
-
Once you've created your Key Vault, you need to store the secrets that you want to use in your pipeline. You can create secrets directly in your Key Vault, or from the Azure DevOps.
-
-
In the Azure portal, go to the Azure Key Vault that you created in step 1.
-
Open the "Secrets" option and click on the "Generate/Import" button.
-
From the Azure Key Vault you can create manual secrets, or upload a certificate.
-
Once you've created your secret, you can use it in your pipeline.
-
In Azure DevOps, go to the Azure DevOps organization and project that you want to use.
-
Click on Library and then open your Variable group.
-
Toggle the "Link to Key Vault" option and select the Key Vault and secret that you want to use.
-
Click "Authorize" to enable Azure Pipelines to set these permissions or manage secret permissions in the Azure portal.
-
When authorization is complete, click Add under Variables to add the secret from your linked Key Vault to your Variable group.
-
Select the secret that you want to use in your pipeline and click OK to add it to your Variable group.
-
Save the Variable group.
-
Now that you've stored your secrets in Azure Key Vault, you need to grant Azure DevOps access to the Key Vault so that your pipeline can retrieve the secrets.
- In the Azure portal, go to the Azure DevOps organization and project.
- Go to the "Project Settings" and then "Service connections".
- Click the "New service connection" button, and then select "Azure Resource Manager".
- Fill out the required information to create the connection, including the name of the Key Vault and the secrets that you want to use in your pipeline.
- After you've created the service connection, you'll need to grant Azure DevOps access to the Key Vault. To do this, go to the Azure Key Vault and click on the "Access policies" option.
- Add a new policy, and then select the Azure DevOps service connection that you created in step 4.
- Assign the "Get" and "List" permissions to the service connection.
steps:
- task: AzureKeyVault@2
inputs:
azureSubscription: '<your_azure_subscription_name>'
KeyVaultName: '<your_key_vault_name>'
SecretsFilter: |
<secret_name>
-
Replace <your_azure_subscription_name> with the name of your Azure subscription, <your_key_vault_name> with the name of your Key Vault, and <secret_name> with the name of the secret that you want to use in your pipeline.
-
Save the pipeline definition, and then run the pipeline. The secret should now be available in your pipeline, and you can use it as needed.
-
The job details page provides detailed information about the pipeline run, including the tasks executed, their status, and any output generated.
-
You can access the logs for a specific pipeline run by following these steps:
- In your Azure DevOps project, navigate to the pipelines section, under pipelines menu.
- Select the pipeline for which you want to view the logs.
- Click on a specific run of the pipeline.
- In the run details page, find the Jobs tab and click on the job for which you want to view the logs.
-
You can also access the logs for a specific task by clicking on the task name, or download logs for the entire job by clicking on the "Download logs" link.
-
Securing log files in Azure Pipelines is crucial to ensure that sensitive information, such as secrets and credentials, isn't displayed in plain text. Azure Pipelines attempts to scrub secrets from logs wherever possible. This filtering is on a best-effort basis and can't catch every way that secrets can be leaked. Avoid echoing secrets to the console, using them in command line parameters, or logging them to files.
-
There are many ways to secure log files in Azure Pipelines, including:
- By using the issecret=true command in a script or task, you can ensure that specific values aren't displayed in the logs. When issecret is set to true, the variable's value is saved as secret and masked out from the log. Secret variables aren't passed into tasks as environment variables and must instead be passed as inputs.
steps:
- pwsh: |
Write-Host "##vso[task.setvariable variable=nonSecretVar;]Now you can see me!"
Write-Host "##vso[task.setvariable variable=secretVar;issecret=true]Now you don't!"
name: SetVariables
- Read the variables
- pwsh: |
Write-Host "The magician says: $env:NONSECRETVAR = Not a secret."
Write-Host "The magician says: $env:SECRETVAR = Yes, it's hidden, can't you see it? =)"
Write-Host "The magician says: $(secretVar) = It's encrypted."
- By using the isoutput=false command in a script or task, the variable's value is hidden out from the log.
steps:
- pwsh: |
Write-Host "##vso[task.setvariable variable=outputVarTrue;isoutput=true]No, it's not a secret!"
Write-Host "##vso[task.setvariable variable=outputVarFalse;isoutput=false]Yes, it's a secret!"
name: SetVariables
- Read the Variables
- pwsh: |
Write-Host "Hidden out from the log: $env:SETVARIABLES_OUTPUTVARTRUE"
Write-Host "Hidden out from the log: $(SetVariables.outputVarTrue)"
Write-Host "Hidden out from the log: $env:SETVARIABLES_OUTPUTVARFALSE = Yes, it's hidden."
-
A few other ways to secure log files in Azure Pipelines include:
-
Use Secure Files type to upload a file to Azure Pipelines and then download it to the pipeline using the "Download secure file" task. This is useful for uploading certificates and other files that are required by tasks in the pipeline, but shouldn't be displayed in plain text.
-
Azure Pipelines will automatically delete log files after a certain amount of time, or by the retention settings. This is useful for ensuring that secrets and other sensitive information aren't stored indefinitely.
-
Azure Key Vault integration is another way to secure your secrets from log files.
-
Secure files, Secret variables, and Variable groups are another way to secure log files in Azure Pipelines.
-