diff --git a/internal/pkg/core/applicant/acme-dns-01/lego-providers/azure-dns/azure-dns.go b/internal/pkg/core/applicant/acme-dns-01/lego-providers/azure-dns/azure-dns.go index bf36f3fb..21958e00 100644 --- a/internal/pkg/core/applicant/acme-dns-01/lego-providers/azure-dns/azure-dns.go +++ b/internal/pkg/core/applicant/acme-dns-01/lego-providers/azure-dns/azure-dns.go @@ -1,13 +1,12 @@ package azuredns import ( - "fmt" - "strings" "time" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" "github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/providers/dns/azuredns" + + azcommon "github.com/usual2970/certimate/internal/pkg/vendors/azure-sdk/common" ) type ChallengeProviderConfig struct { @@ -29,16 +28,11 @@ func NewChallengeProvider(config *ChallengeProviderConfig) (challenge.Provider, providerConfig.ClientID = config.ClientId providerConfig.ClientSecret = config.ClientSecret if config.CloudName != "" { - switch strings.ToLower(config.CloudName) { - case "default", "public", "cloud", "azurecloud": - providerConfig.Environment = cloud.AzurePublic - case "usgovernment", "azureusgovernment": - providerConfig.Environment = cloud.AzureGovernment - case "china", "chinacloud", "azurechina", "azurechinacloud": - providerConfig.Environment = cloud.AzureChina - default: - return nil, fmt.Errorf("azuredns: unknown environment %s", config.CloudName) + env, err := azcommon.GetEnvironmentConfiguration(config.CloudName) + if err != nil { + return nil, err } + providerConfig.Environment = env } if config.DnsPropagationTimeout != 0 { providerConfig.PropagationTimeout = time.Duration(config.DnsPropagationTimeout) * time.Second diff --git a/internal/pkg/vendors/azure-sdk/common/config.go b/internal/pkg/vendors/azure-sdk/common/config.go new file mode 100644 index 00000000..844c49bb --- /dev/null +++ b/internal/pkg/vendors/azure-sdk/common/config.go @@ -0,0 +1,47 @@ +package common + +import ( + "fmt" + "strings" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" +) + +func IsEnvironmentPublic(env string) bool { + switch strings.ToLower(env) { + case "", "default", "public", "azurecloud": + return true + default: + return false + } +} + +func IsEnvironmentGovernment(env string) bool { + switch strings.ToLower(env) { + case "usgovernment", "government", "azureusgovernment", "azuregovernment": + return true + default: + return false + } +} + +func IsEnvironmentChina(env string) bool { + switch strings.ToLower(env) { + case "china", "chinacloud", "azurechina", "azurechinacloud": + return true + default: + return false + } +} + +func GetEnvironmentConfiguration(env string) (cloud.Configuration, error) { + if IsEnvironmentPublic(env) { + return cloud.AzurePublic, nil + } else if IsEnvironmentGovernment(env) { + return cloud.AzureGovernment, nil + } else if IsEnvironmentChina(env) { + return cloud.AzureChina, nil + } + + return cloud.Configuration{}, fmt.Errorf("unknown azure cloud environment %s", env) +}