azure

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 16 Imported by: 15 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Azure DNS for libdns

This package implements the libdns interfaces for the Azure DNS API.

Authenticating

This package supports authentication using a service principal with a secret, a workload identity, and a managed identity through azure-sdk-for-go.

Service Principal with a Secret

To attempt to authenticate using a service principal with a secret, pass TenantId, ClientId, and ClientSecret to the Provider. If any of these three values are not empty, this package will attempt to authenticate using a service principal with a secret.

You will need to create a service principal using Azure CLI or Azure Portal, and assign the DNS Zone Contributor role to the service principal for the DNS zones that you want to manage.

Then keep the following information to pass to the Provider struct fields for authentication:

  • SubscriptionId (json:"subscription_id")
    • [DNS zones] > Your Zone > [Subscription ID]
  • ResourceGroupName (json:"resource_group_name")
    • [DNS zones] > Your Zone > [Resource group]
  • TenantId (json:"tenant_id")
    • [Microsoft Entra ID] > [Overview] > [Tenant ID]
  • ClientId (json:"client_id")
    • [Microsoft Entra ID] > [Manage] > [App registrations] > Your Application > [Application (client) ID]
  • ClientSecret (json:"client_secret")
    • [Microsoft Entra ID] > [Manage] > [App registrations] > Your Application > [Manage] > [Certificates & secrets] > [Client secrets] > [Value]
Workload Identity and Managed Identity

To attempt to authenticate using a workload identity or a managed identity, leave all of TenantId, ClientId, and ClientSecret unset or empty to the Provider. If all three values are unset or empty, this package will attempt to authenticate using a workload identity if it is configured, and then using a managed identity.

A workload identity (for example, Microsoft Entra Workload ID on AKS) is used when the environment variables AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_FEDERATED_TOKEN_FILE are set, which Workload ID sets on your pods. AZURE_AUTHORITY_HOST is also used if it is set. If they are not set, only a managed identity is attempted.

You will need to assign the DNS Zone Contributor role to the workload identity or the managed identity for the DNS zones that you want to manage.

Then keep the following information to pass to the Provider struct fields for authentication:

  • SubscriptionId (json:"subscription_id")
    • [DNS zones] > Your Zone > [Subscription ID]
  • ResourceGroupName (json:"resource_group_name")
    • [DNS zones] > Your Zone > [Resource group]

[!NOTE] If this package is running outside of an Azure VM like Azure Arc, ensure required environment variables to use a managed identity (IDENTITY_ENDPOINT, IMDS_ENDPOINT, etc.) are available on your resources. azure-sdk-for-go uses some environment variables to determine the endpoint for IMDS or HIMDS, and this package is also in the same manner. Refer to the Azure documentation for each services to use a managed identity.

Record Sets

Azure DNS keeps all records with the same name and type in one record set, which has a single TTL. This package maps the libdns interfaces to record sets as follows:

  • AppendRecords adds the values to the existing record set, or creates it. Values that the record set already has are skipped and not returned. The TTL of an existing record set is kept.
  • DeleteRecords removes only the given values, and deletes the record set when no value is left. Values that the record set doesn't have are skipped and not returned. The TTL is not compared. A record with empty data deletes the whole record set.
  • SetRecords replaces the values of each name and type in the given records with the given values, so that multiple values for the same name and type are all kept. The TTL of the record set is the lowest TTL of them, ignoring records without a TTL. Setting records of different names or types is not atomic.
  • AppendRecords and DeleteRecords are safe against concurrent changes, also from other processes.

Example

Here's a minimal example of how to get all your DNS records using this libdns provider (see _example/main.go)

package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/libdns/azure"
	"github.com/libdns/libdns"
)

// main shows how libdns works with Azure DNS.
//
// In this example, the information required for authentication is passed as environment variables.
func main() {

	// Create new provider instance by authenticating using a service principal with a secret.
	// To authenticate using a workload identity or a managed identity, remove TenantId, ClientId, and ClientSecret.
	provider := azure.Provider{
		SubscriptionId:    os.Getenv("AZURE_SUBSCRIPTION_ID"),
		ResourceGroupName: os.Getenv("AZURE_RESOURCE_GROUP_NAME"),
		TenantId:          os.Getenv("AZURE_TENANT_ID"),
		ClientId:          os.Getenv("AZURE_CLIENT_ID"),
		ClientSecret:      os.Getenv("AZURE_CLIENT_SECRET"),
	}
	zone := os.Getenv("AZURE_DNS_ZONE_FQDN")

	// List existing records
	fmt.Printf("List existing records\n")
	currentRecords, err := provider.GetRecords(context.TODO(), zone)
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}
	for _, record := range currentRecords {
		fmt.Printf("Exists: %v\n", record)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an abstraction of RecordSetsClient for Azure DNS

type Provider

type Provider struct {

	// Subscription ID is the ID of the subscription in which the DNS zone is located. Required.
	SubscriptionId string `json:"subscription_id,omitempty"`

	// Resource Group Name is the name of the resource group in which the DNS zone is located. Required.
	ResourceGroupName string `json:"resource_group_name,omitempty"`

	// (Optional)
	// Tenant ID is the ID of the tenant of the Microsoft Entra ID in which the application is located.
	// Required only when authenticating using a service principal with a secret.
	// Do not set any value to authenticate using a managed identity.
	TenantId string `json:"tenant_id,omitempty"`

	// (Optional)
	// Client ID is the ID of the application.
	// Required only when authenticating using a service principal with a secret.
	// Do not set any value to authenticate using a managed identity.
	ClientId string `json:"client_id,omitempty"`

	// (Optional)
	// Client Secret is the client secret of the application.
	// Required only when authenticating using a service principal with a secret.
	// Do not set any value to authenticate using a managed identity.
	ClientSecret string `json:"client_secret,omitempty"`
	// contains filtered or unexported fields
}

Provider implements the libdns interfaces for Azure DNS

func (*Provider) AppendRecords

func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

AppendRecords adds records to the zone. It returns the records that were added. A record that the zone already has is not added again, and not returned.

func (*Provider) DeleteRecords

func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

DeleteRecords deletes the records from the zone. It returns the records that were deleted. A record that the zone doesn't have is ignored, and not returned. The TTL is not compared, because Azure has a single TTL per record set. A record without data deletes the whole record set.

func (*Provider) GetRecords

func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)

GetRecords lists all the records in the zone.

func (*Provider) SetRecords

func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error)

SetRecords sets the records in the zone, either by updating existing records or creating new ones. It returns the updated records.

For each name and type in records, the values in the zone are replaced by the given ones, and the TTL is the lowest TTL of them. Setting records of different names or types is not atomic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址