-
Notifications
You must be signed in to change notification settings - Fork 0
Authentication
AzureDevOpsDscNative supports multiple authentication methods for connecting to Azure DevOps. This guide covers all available authentication options and how to configure them.
- Personal Access Token (PAT) - Most common and flexible
- Managed Identity - Best for Azure resources
- Service Principal - For service-to-service authentication
- Certificate-Based Authentication - For secure service principals
- Azure CLI Token - Use existing Azure CLI login session
- Workload Identity Federation - Keyless authentication for CI/CD
Personal Access Tokens are the most straightforward authentication method for most scenarios.
- In Azure DevOps, click on your profile icon (top right)
- Select Personal access tokens
- Click New Token
- Configure:
- Name: Give your token a meaningful name
- Organization: Select your organization
- Expiration: Set expiration (30, 60, 90 days or custom)
- Scopes: Select required scopes (typically "Full access" for DSC operations)
- Click Create
- Copy the token immediately (you won't see it again)
Configuration ExampleWithPAT {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# Method 1: Using hashtable with token
$authToken = @{
PersonalAccessToken = 'your-pat-token-here'
OrganizationName = 'your-org-name'
}
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
ProjectDescription = 'Sample project'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
}
}
AzureDevOpsConfig
Start-DscConfiguration -Path ./AzureDevOpsConfig -Wait -Verbose- ✅ Store PAT in secure location (Azure Key Vault, Windows Credential Manager, etc.)
- ✅ Use minimal required scopes
- ✅ Set reasonable expiration (30-90 days)
- ✅ Rotate tokens periodically
- ✅ Never commit PAT to version control
- ❌ Don't use "Full access" scope if more limited scope suffices
- ❌ Don't hardcode PAT in configuration files
Managed Identity is recommended for Azure resources, as it doesn't require managing tokens or credentials.
- Resource running in Azure (VM, App Service, Container Instance, etc.)
- Managed Identity enabled on the resource
- Appropriate permissions assigned to the identity
Configuration ExampleWithManagedIdentity {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# Managed Identity authentication is automatically discovered
# No explicit credential configuration needed
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
ProjectDescription = 'Sample project'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
}
}-
For Azure VM:
# Enable system-assigned identity Update-AzVm -ResourceGroupName $rg -VmName $vm -IdentityType SystemAssigned
-
Grant permissions to the identity in Azure DevOps:
- Add the identity as a member of appropriate groups
- Assign necessary permissions
-
On the resource:
- Managed Identity is automatically available
- Use without explicit credential configuration
- ✅ Use for Azure-hosted resources
- ✅ Use system-assigned identity when possible
- ✅ Principle of least privilege - grant minimal required permissions
- ✅ No token rotation needed
- ✅ Audit identity access regularly
Service Principals are ideal for CI/CD pipelines and cross-tenant scenarios.
# Create a service principal
$sp = New-AzADServicePrincipal -DisplayName "AzureDevOpsDsc-ServicePrincipal"
# Note the Application ID and Tenant ID for later use
$appId = $sp.AppId
$tenantId = (Get-AzContext).Tenant.IdConfiguration ExampleWithServicePrincipal {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
$authToken = @{
ServicePrincipalId = 'your-app-id'
ServicePrincipalSecret = 'your-client-secret'
TenantId = 'your-tenant-id'
OrganizationName = 'your-org-name'
}
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
ProjectDescription = 'Sample project'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
}
}- ✅ Use in automated/CI-CD scenarios
- ✅ Store secrets in Key Vault
- ✅ Use certificate-based auth instead of client secret when possible
- ✅ Rotate secrets regularly
- ✅ Use minimal required permissions
- ✅ Audit service principal activity
- ❌ Don't hardcode credentials
- ❌ Don't commit secrets to version control
Certificate-based authentication provides additional security for service principals.
# Create self-signed certificate
$cert = New-SelfSignedCertificate `
-Subject "CN=AzureDevOpsDsc" `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyExportPolicy Exportable `
-KeySpec Signature
# Export certificate
$thumbprint = $cert.Thumbprint
Export-PfxCertificate -Cert $cert -FilePath "C:\cert.pfx" -Password (ConvertTo-SecureString -String "password" -AsPlainText)Configuration ExampleWithCertificate {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
$authToken = @{
ServicePrincipalId = 'your-app-id'
CertificateThumbprint = 'your-cert-thumbprint'
TenantId = 'your-tenant-id'
OrganizationName = 'your-org-name'
}
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
ProjectDescription = 'Sample project'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
}
}- ✅ More secure than client secrets
- ✅ Longer expiration than secrets
- ✅ Store certificate securely
- ✅ Monitor certificate expiration
- ✅ Rotate before expiration
- ❌ Don't share certificates
Use your existing Azure CLI session for authentication.
- Azure CLI installed and authenticated
- Run
az loginto authenticate first
# Azure CLI authentication is automatic if you've run 'az login'
Configuration ExampleWithAzureCli {
Import-DscResource -ModuleName 'AzureDevOpsDscNative'
Node localhost {
# No explicit credential configuration needed
# Uses current Azure CLI context
AzDoProject 'MyProject' {
Ensure = 'Present'
ProjectName = 'MyProject'
ProjectDescription = 'Sample project'
SourceControlType = 'Git'
ProcessTemplate = 'Agile'
Visibility = 'Private'
}
}
}- ✅ Convenient for local development
- ✅ Automatically uses your Azure login
- ✅ Token automatically refreshed
- ✅ No manual credential management
- ❌ Not ideal for automated scenarios
- ❌ Only works on machines with Azure CLI installed
Keyless authentication for GitHub Actions, GitLab CI, and other CI/CD systems.
- Register your OIDC provider with Azure AD
- Create a service principal and configure trust
- Configure your CI/CD pipeline to use federated credentials
# GitHub Actions example
name: Deploy with Azure DevOps DSC
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- name: Run DSC Configuration
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Apply Configuration
run: |
pwsh -Command {
# DSC configuration runs here
./ApplyDscConfig.ps1
}- ✅ No secrets stored in CI/CD
- ✅ Short-lived tokens
- ✅ Secure by default
- ✅ Audit trail in OIDC provider
- ✅ Recommended for modern CI/CD systems
Set environment variables for authentication:
# PAT
$env:AZDO_PAT = 'your-token'
$env:AZDO_ORG = 'your-organization'
# Service Principal
$env:AZDO_SERVICE_PRINCIPAL_ID = 'your-app-id'
$env:AZDO_SERVICE_PRINCIPAL_SECRET = 'your-secret'
$env:AZDO_TENANT_ID = 'your-tenant-id'
# Managed Identity
$env:AZDO_MANAGED_IDENTITY = 'true'# Store PAT in Credential Manager
$cred = New-Object System.Management.Automation.PSCredential(
'AzureDevOpsDsc',
(ConvertTo-SecureString 'your-pat-token' -AsPlainText -Force)
)
$cred | Export-Clixml -Path "$env:APPDATA\AzureDevOpsDsc\cred.xml"
# Retrieve in configuration
$cred = Import-Clixml -Path "$env:APPDATA\AzureDevOpsDsc\cred.xml"# Store in Key Vault
Set-AzKeyVaultSecret -VaultName 'MyKeyVault' `
-Name 'AzureDevOpsPAT' `
-SecretValue (ConvertTo-SecureString 'your-pat-token' -AsPlainText -Force)
# Retrieve in configuration
$token = Get-AzKeyVaultSecret -VaultName 'MyKeyVault' -Name 'AzureDevOpsPAT'# Install SecretStore module
Install-Module Microsoft.PowerShell.SecretStore
# Store credential
Set-Secret -Name AzureDevOpsPAT -Secret 'your-pat-token' -Vault SecretStore
# Retrieve in configuration
$token = Get-Secret -Name AzureDevOpsPATSolution: Verify your token/credentials:
# Test connectivity
Get-DscResource -Module AzureDevOpsDscNativeSolution: Ensure your authentication account has necessary permissions in Azure DevOps:
- Check group memberships
- Verify permission assignments
- Review scope settings (for PAT)
Solution: Refresh or regenerate:
- Create new PAT
- Rotate service principal secret
- Re-authenticate with Azure CLI
Solution: Verify module is installed:
# List installed modules
Get-Module -ListAvailable | Where-Object Name -eq AzureDevOpsDscNative
# Import module explicitly
Import-Module -Name AzureDevOpsDscNative| Method | Best For | Pros | Cons |
|---|---|---|---|
| PAT | General use, local dev | Simple, flexible | Requires token management |
| Managed Identity | Azure resources | No credential mgmt, secure | Azure-only |
| Service Principal | CI/CD, automation | Cross-tenant, automated | Secret management needed |
| Certificate | Secure scenarios | More secure than secret | More complex setup |
| Azure CLI | Local development | Automatic, convenient | Not for automation |
| Workload Identity | Modern CI/CD | Keyless, secure | Setup complexity |
- Use minimal required permissions
- Store credentials securely (not in code)
- Rotate credentials regularly
- Monitor authentication logs
- Use expiration dates on tokens
- Revoke unused credentials
- Audit who has access
- Use MFA for personal accounts
- Enable activity logging
- Review access regularly