fix: incorrect azure cloud environment

This commit is contained in:
Fu Diwei 2025-02-24 20:37:38 +08:00
parent 78600079a4
commit 3c91f29a91
2 changed files with 53 additions and 12 deletions

View File

@ -1,13 +1,12 @@
package azuredns package azuredns
import ( import (
"fmt"
"strings"
"time" "time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/providers/dns/azuredns" "github.com/go-acme/lego/v4/providers/dns/azuredns"
azcommon "github.com/usual2970/certimate/internal/pkg/vendors/azure-sdk/common"
) )
type ChallengeProviderConfig struct { type ChallengeProviderConfig struct {
@ -29,16 +28,11 @@ func NewChallengeProvider(config *ChallengeProviderConfig) (challenge.Provider,
providerConfig.ClientID = config.ClientId providerConfig.ClientID = config.ClientId
providerConfig.ClientSecret = config.ClientSecret providerConfig.ClientSecret = config.ClientSecret
if config.CloudName != "" { if config.CloudName != "" {
switch strings.ToLower(config.CloudName) { env, err := azcommon.GetEnvironmentConfiguration(config.CloudName)
case "default", "public", "cloud", "azurecloud": if err != nil {
providerConfig.Environment = cloud.AzurePublic return nil, err
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)
} }
providerConfig.Environment = env
} }
if config.DnsPropagationTimeout != 0 { if config.DnsPropagationTimeout != 0 {
providerConfig.PropagationTimeout = time.Duration(config.DnsPropagationTimeout) * time.Second providerConfig.PropagationTimeout = time.Duration(config.DnsPropagationTimeout) * time.Second

View File

@ -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)
}