Skip to content

Commit f9e48f8

Browse files
Merge pull request #1455 from johannes-engler-mw/feat/azure-private-dns-check
feat(Azure): add new check for private DNS zones
2 parents 3d9c959 + 78bcef4 commit f9e48f8

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

modules/azure/client_factory.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/Azure/azure-sdk-for-go/profiles/latest/frontdoor/mgmt/frontdoor"
2020
"github.com/Azure/azure-sdk-for-go/profiles/latest/mysql/mgmt/mysql"
21+
"github.com/Azure/azure-sdk-for-go/profiles/latest/privatedns/mgmt/privatedns"
2122
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
2223
"github.com/Azure/azure-sdk-for-go/profiles/latest/sql/mgmt/sql"
2324
"github.com/Azure/azure-sdk-for-go/profiles/preview/cosmos-db/mgmt/documentdb"
@@ -176,7 +177,7 @@ func CreateKeyVaultManagementClientE(subscriptionID string) (*kvmng.VaultsClient
176177
return nil, err
177178
}
178179

179-
//create keyvault management clinet
180+
// create keyvault management clinet
180181
vaultClient := kvmng.NewVaultsClientWithBaseURI(baseURI, subscriptionID)
181182

182183
return &vaultClient, nil
@@ -220,7 +221,6 @@ func CreateStorageBlobContainerClientE(subscriptionID string) (*storage.BlobCont
220221

221222
blobContainerClient := storage.NewBlobContainersClientWithBaseURI(baseURI, subscriptionID)
222223
authorizer, err := NewAuthorizer()
223-
224224
if err != nil {
225225
return nil, err
226226
}
@@ -243,7 +243,6 @@ func CreateStorageFileSharesClientE(subscriptionID string) (*storage.FileSharesC
243243

244244
fileShareClient := storage.NewFileSharesClientWithBaseURI(baseURI, subscriptionID)
245245
authorizer, err := NewAuthorizer()
246-
247246
if err != nil {
248247
return nil, err
249248
}
@@ -693,7 +692,7 @@ func CreateLoadBalancerClientE(subscriptionID string) (*network.LoadBalancersCli
693692
return nil, err
694693
}
695694

696-
//create LB client
695+
// create LB client
697696
client := network.NewLoadBalancersClientWithBaseURI(baseURI, subscriptionID)
698697
return &client, nil
699698
}
@@ -741,7 +740,6 @@ func CreateNewVirtualNetworkClientE(subscriptionID string) (*network.VirtualNetw
741740
// CreateAppServiceClientE returns an App service client instance configured with the
742741
// correct BaseURI depending on the Azure environment that is currently setup (or "Public", if none is setup).
743742
func CreateAppServiceClientE(subscriptionID string) (*web.AppsClient, error) {
744-
745743
// Validate Azure subscription ID
746744
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
747745
if err != nil {
@@ -762,7 +760,6 @@ func CreateAppServiceClientE(subscriptionID string) (*web.AppsClient, error) {
762760
// CreateContainerRegistryClientE returns an ACR client instance configured with the
763761
// correct BaseURI depending on the Azure environment that is currently setup (or "Public", if none is setup).
764762
func CreateContainerRegistryClientE(subscriptionID string) (*containerregistry.RegistriesClient, error) {
765-
766763
// Validate Azure subscription ID
767764
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
768765
if err != nil {
@@ -927,6 +924,35 @@ func CreateDataFactoriesClientE(subscriptionID string) (*datafactory.FactoriesCl
927924
return &dataFactoryClient, nil
928925
}
929926

927+
// CreatePrivateDnsZonesClientE is a helper function that will setup a private DNS zone client.
928+
func CreatePrivateDnsZonesClientE(subscriptionID string) (*privatedns.PrivateZonesClient, error) {
929+
// Validate Azure subscription ID
930+
subID, err := getTargetAzureSubscription(subscriptionID)
931+
if err != nil {
932+
return nil, err
933+
}
934+
935+
// Lookup environment URI
936+
baseURI, err := getBaseURI()
937+
if err != nil {
938+
return nil, err
939+
}
940+
941+
// Create a private DNS zone client
942+
privateZonesClient := privatedns.NewPrivateZonesClientWithBaseURI(baseURI, subID)
943+
944+
// Create an authorizer
945+
authorizer, err := NewAuthorizer()
946+
if err != nil {
947+
return nil, err
948+
}
949+
950+
// Attach authorizer to the client
951+
privateZonesClient.Authorizer = *authorizer
952+
953+
return &privateZonesClient, nil
954+
}
955+
930956
func CreateManagedEnvironmentsClientE(subscriptionID string) (*armappcontainers.ManagedEnvironmentsClient, error) {
931957
clientFactory, err := getArmAppContainersClientFactory(subscriptionID)
932958
if err != nil {

modules/azure/privatednszone.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package azure
2+
3+
import (
4+
"context"
5+
6+
"github.com/Azure/azure-sdk-for-go/profiles/latest/privatedns/mgmt/privatedns"
7+
)
8+
9+
// PrivateDNSZoneExistsE indicates whether the specified private DNS zone exists.
10+
func PrivateDNSZoneExistsE(zoneName string, resourceGroupName string, subscriptionID string) (bool, error) {
11+
_, err := GetPrivateDNSZoneE(zoneName, resourceGroupName, subscriptionID)
12+
if err != nil {
13+
if ResourceNotFoundErrorExists(err) {
14+
return false, nil
15+
}
16+
return false, err
17+
}
18+
return true, nil
19+
}
20+
21+
// GetPrivateDNSZoneE gets the private DNS zone object
22+
func GetPrivateDNSZoneE(zoneName string, resGroupName string, subscriptionID string) (*privatedns.PrivateZone, error) {
23+
rgName, err := getTargetAzureResourceGroupName(resGroupName)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
client, err := CreatePrivateDnsZonesClientE(subscriptionID)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
zone, err := client.Get(context.Background(), rgName, zoneName)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
return &zone, nil
39+
}

modules/azure/privatednszone_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package azure
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
/*
10+
The below tests are currently stubbed out, with the expectation that they will throw errors.
11+
If/when CRUD methods are introduced for Azure Synapse, these tests can be extended
12+
*/
13+
func TestPrivateDNSZoneExists(t *testing.T) {
14+
t.Parallel()
15+
16+
zoneName := ""
17+
resourceGroupName := ""
18+
subscriptionID := ""
19+
20+
exists, err := PrivateDNSZoneExistsE(zoneName, resourceGroupName, subscriptionID)
21+
22+
require.False(t, exists)
23+
require.Error(t, err)
24+
}
25+
26+
func TestPrivateDNSZoneExistsE(t *testing.T) {
27+
t.Parallel()
28+
29+
resGroupName := ""
30+
subscriptionID := ""
31+
zoneName := ""
32+
33+
_, err := GetPrivateDNSZoneE(subscriptionID, resGroupName, zoneName)
34+
require.Error(t, err)
35+
}

0 commit comments

Comments
 (0)