Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Azure China & Government Cloud #464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions postgresql/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/lib/pq"
)
Expand Down Expand Up @@ -619,3 +620,19 @@ func quoteTableName(tableName string) string {
}
return strings.Join(parts, ".")
}

func cloudConfigFromName(name string) (cloud.Configuration, error) {
switch strings.ToLower(name) {
case "china":
return cloud.AzureChina, nil

case "usgovernment":
return cloud.AzureGovernment, nil

case "public":
return cloud.AzurePublic, nil

default:
return cloud.Configuration{}, fmt.Errorf("unsupported Azure Cloud environment: %s", name)
}
}
47 changes: 40 additions & 7 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"

"github.com/blang/semver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -19,10 +20,23 @@ import (
)

const (
defaultProviderMaxOpenConnections = 20
defaultExpectedPostgreSQLVersion = "9.0.0"
defaultProviderMaxOpenConnections = 20
defaultExpectedPostgreSQLVersion = "9.0.0"
serviceName cloud.ServiceName = "ossrdbms-aad"
)

func init() {
cloud.AzureChina.Services[serviceName] = cloud.ServiceConfiguration{
Audience: "https://ossrdbms-aad.database.chinacloudapi.cn",
}
cloud.AzureGovernment.Services[serviceName] = cloud.ServiceConfiguration{
Audience: "https://ossrdbms-aad.database.usgovcloudapi.net",
}
cloud.AzurePublic.Services[serviceName] = cloud.ServiceConfiguration{
Audience: "https://ossrdbms-aad.database.windows.net",
}
}

// Provider returns a terraform.ResourceProvider.
func Provider() *schema.Provider {
return &schema.Provider{
Expand Down Expand Up @@ -97,6 +111,13 @@ func Provider() *schema.Provider {
"(see: https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-configure-sign-in-azure-ad-authentication)",
},

"azure_environment": {
Type: schema.TypeString,
Optional: true,
Default: "public",
Description: "MS Azure Cloud environment (see: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#environment)",
},

"azure_tenant_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -274,14 +295,25 @@ func createGoogleCredsFileIfNeeded() error {
return os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", tmpFile.Name())
}

func acquireAzureOauthToken(tenantId string) (string, error) {
func acquireAzureOauthToken(environment, tenantId string) (string, error) {
cloudConfig, err := cloudConfigFromName(environment)
if err != nil {
return "", err
}
credential, err := azidentity.NewDefaultAzureCredential(
&azidentity.DefaultAzureCredentialOptions{TenantID: tenantId})
&azidentity.DefaultAzureCredentialOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloudConfig,
},
TenantID: tenantId,
})
if err != nil {
return "", err
}
token, err := credential.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: []string{"https://ossrdbms-aad.database.windows.net/.default"},
Scopes: []string{
cloudConfig.Services[serviceName].Audience + "/.default",
},
TenantID: tenantId,
})
if err != nil {
Expand Down Expand Up @@ -317,12 +349,13 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}
} else if d.Get("azure_identity_auth").(bool) {
environment := d.Get("azure_environment").(string)
tenantId := d.Get("azure_tenant_id").(string)
if tenantId == "" {
return nil, fmt.Errorf("postgresql: azure_identity_auth is enabled, azure_tenant_id must be provided also")
}
var err error
password, err = acquireAzureOauthToken(tenantId)
password, err = acquireAzureOauthToken(environment, tenantId)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ The following arguments are supported:
* `aws_rds_iam_region` - (Optional) The AWS region to use while using AWS RDS IAM Auth.
* `azure_identity_auth` - (Optional) If set to `true`, call the Azure OAuth token endpoint for temporary token
* `azure_tenant_id` - (Optional) (Required if `azure_identity_auth` is `true`) Azure tenant ID [read more](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config.html)
* `azure_environment` - (Optional) The Azure Cloud environment. Possible values are `public`, `usgovernment` and `china`. Defaults to `public`.

## GoCloud

Expand Down