Lesson 3 of 4 Sensitive Information in Automation

Managing Sensitive Files During Deployment

This lesson explains secure files, and the alternatives on each platform.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why sensitive files need special handling.
  • Use secure files in a pipeline.
  • Handle a sensitive file in GitHub Actions.
  • Identify designs that avoid storing a file.

Why files need separate handling

Certificates, code-signing keys, Apple provisioning profiles, SSH keys and licence files should never go into a repository, because anyone who has clone access would then have them for ever. These files do not fit a secret variable, either, as they are binary or long. Pipelines therefore need a way to deliver a file to an agent during a job, and remove it afterwards.

Azure Pipelines secure files

A secure file is uploaded to the Library in Azure Pipelines, where it is stored encrypted. Each file is limited to 10 MB, and it cannot be downloaded through the interface once uploaded. Access follows the same model as other protected resources, with roles for people, and permission for each pipeline, and it can have approvals and checks.

A pipeline retrieves one with the DownloadSecureFile@1 task, which saves it in a temporary folder on the agent, and exposes the location through an output variable. The agent removes the file when the job finishes.

steps:
  - task: DownloadSecureFile@1
    name: signingCert
    inputs:
      secureFile: contoso-signing.pfx

  - script: |
      chmod 400 "$(signingCert.secureFilePath)"
      ./sign.sh "$(signingCert.secureFilePath)" dist/app.zip
    displayName: Sign the package
    env:
      CERT_PASSWORD: $(CertPassword)

Note the pattern $(name.secureFilePath), where the task's name sets the prefix. The certificate's password should be stored separately, preferably in Key Vault, so that neither item is sufficient alone. Other tasks, such as the Apple certificate and provisioning profile installers, use secure files directly.

The following diagram follows signing files stored as secure files to the build agent.

Signing files kept out of the repository. The provisioning profile and the code-signing certificate are uploaded as secure files, encrypted at rest and authorized for one pipeline; DownloadSecureFile@1 or the Apple install tasks place them on the macOS agent, which removes them after the job.
The scenario is illustrative.
True or false

Secure files in Azure Pipelines

Exam skills 4.2.3

Check how secure files are stored and delivered.

Log in to try this activity Interactive activities are free with an account.

Options in GitHub Actions

GitHub has no secure file feature. Repository secrets hold text up to 48 KB, so a small binary file can be base64-encoded and stored as a secret, then decoded during a job.

- name: Restore the certificate
  run: |
    echo "$SIGNING_CERT" | base64 --decode > "$RUNNER_TEMP/signing.pfx"
  env:
    SIGNING_CERT: ${{ secrets.SIGNING_CERT_BASE64 }}

- name: Sign the package
  run: ./sign.sh "$RUNNER_TEMP/signing.pfx" dist/app.zip

- name: Remove the certificate
  if: always()
  run: rm -f "$RUNNER_TEMP/signing.pfx"

RUNNER_TEMP is emptied at the end of each job on hosted runners, and the final step gives protection for self-hosted runners, which persist between jobs. For larger or more sensitive files, a better design stores them in Key Vault or in Azure Blob Storage, and the workflow retrieves them through a federated identity.

Spot the bug

A certificate left behind

Exam skills 4.2.3

Find the lines that make this certificate handling unsafe or broken.

Log in to try this activity Interactive activities are free with an account.

Better options for common cases

Before storing a file at all, ask whether it is needed. Several common cases have better solutions.

  • Code signing: Azure Key Vault can hold the private key, and a signing tool sends a digest to the vault, so the key never reaches the agent. Azure Artifact Signing is a managed alternative.
  • TLS certificates: App Service and Application Gateway can reference a Key Vault certificate directly, so no pipeline handles the file.
  • SSH keys: the Install SSH Key task uses a secure file, but managed identities or Azure Bastion may remove the need altogether.
  • Kubernetes secrets: the Secrets Store CSI driver can mount Key Vault secrets as files in a pod, at run time.

The following comparison recaps where to keep a sensitive file.

OptionSize limitBest for
Secure file10 MBFiles that a pipeline needs in Azure Pipelines
GitHub secret, base64-encoded48 KBSmall files in GitHub Actions
Key Vault certificate or secret25 KB for a secretCertificates and values that need audit and rotation
Azure Blob Storage with an identityVery largeLarge files retrieved through federation
Match the pairs

Avoiding the file altogether

Exam skills 4.2.3

Match each common sensitive-file case with the option that avoids handling the file.

Log in to try this activity Interactive activities are free with an account.
Worked example

Signing an application

An Azure Pipelines pipeline must use a 6 KB Apple provisioning profile and a code-signing certificate on a Microsoft-hosted macOS agent. The files must not be in the repository, and only one pipeline may use them. What should you recommend?

Upload both as secure files, authorize only that pipeline, and use DownloadSecureFile@1 or the Apple install tasks. The files are encrypted at rest, and they are removed from the agent after the job. Base64 values in a variable group would work less cleanly, and committing an encrypted file to the repository brings key-management problems.

Check yourself

How does a later step find a file downloaded by DownloadSecureFile@1?

Show answer

Through the output variable $(name.secureFilePath), where name is the task's name.

Exam Tips

Remember for the exam

  • Secure files are limited to 10 MB and stored encrypted.
  • DownloadSecureFile@1 exposes the path through secureFilePath.
  • Authorize each pipeline; do not open a file to all pipelines.
  • GitHub has no secure files; base64-encode a small file as a secret.
  • Key Vault can sign without exposing the private key.