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

Update dns suffix/fully qualified name based on the cloud environment or from the created object #946

Merged
merged 13 commits into from
Apr 21, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/secrets"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -167,9 +168,9 @@ func (f *AzureSqlFailoverGroupManager) GetOrPrepareSecret(ctx context.Context, i
}

secret["azureSqlPrimaryServer"] = []byte(azuresqlprimaryserver)
secret["readWriteListenerEndpoint"] = []byte(failovergroupname + ".database.windows.net")
secret["readWriteListenerEndpoint"] = []byte(failovergroupname + "." + config.Environment().SQLDatabaseDNSSuffix)
secret["azureSqlSecondaryServer"] = []byte(azuresqlsecondaryserver)
secret["readOnlyListenerEndpoint"] = []byte(failovergroupname + ".secondary.database.windows.net")
secret["readOnlyListenerEndpoint"] = []byte(failovergroupname + ".secondary." + config.Environment().SQLDatabaseDNSSuffix)

return secret, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/pollclient"
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/Azure/go-autorest/autorest/to"
Expand Down Expand Up @@ -335,7 +336,7 @@ func NewSecret(serverName string) (map[string][]byte, error) {
secret["fullyQualifiedUsername"] = []byte(fmt.Sprintf("%s@%s", randomUsername, serverName))
secret["password"] = []byte(randomPassword)
secret["azureSqlServerName"] = []byte(serverName)
secret["fullyQualifiedServerName"] = []byte(serverName + ".database.windows.net")
secret["fullyQualifiedServerName"] = []byte(serverName + "." + config.Environment().SQLDatabaseDNSSuffix)

return secret, nil
}
5 changes: 3 additions & 2 deletions pkg/resourcemanager/azuresql/azuresqluser/azuresqluser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
azuresql "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql"
"github.com/Azure/azure-service-operator/pkg/helpers"
azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/secrets"

"github.com/Azure/azure-service-operator/api/v1alpha1"
Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *AzureSqlUserManager) GetDB(ctx context.Context, resourceGroupName strin
// ConnectToSqlDb connects to the SQL db using the given credentials
func (s *AzureSqlUserManager) ConnectToSqlDb(ctx context.Context, drivername string, server string, database string, port int, user string, password string) (*sql.DB, error) {

fullServerAddress := fmt.Sprintf("%s.database.windows.net", server)
fullServerAddress := fmt.Sprintf("%s."+config.Environment().SQLDatabaseDNSSuffix, server)
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30", fullServerAddress, user, password, port, database)

db, err := sql.Open(drivername, connString)
Expand Down Expand Up @@ -231,7 +232,7 @@ func (s *AzureSqlUserManager) GetOrPrepareSecret(ctx context.Context, instance *
"password": []byte(pw),
"azureSqlServerNamespace": []byte(instance.Namespace),
"azureSqlServerName": []byte(instance.Spec.Server),
"fullyQualifiedServerName": []byte(instance.Spec.Server + ".database.windows.net"),
"fullyQualifiedServerName": []byte(instance.Spec.Server + "." + config.Environment().SQLDatabaseDNSSuffix),
"azureSqlDatabaseName": []byte(instance.Spec.DbName),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/secrets"

"github.com/Azure/azure-service-operator/api/v1alpha1"
Expand Down Expand Up @@ -194,7 +195,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op

case "jdbc":
formattedSecrets["jdbc"] = []byte(fmt.Sprintf(
"jdbc:sqlserver://%v:1433;database=%v;user=%v@%v;password=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
"jdbc:sqlserver://%v:1433;database=%v;user=%v@%v;password=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*."+config.Environment().SQLDatabaseDNSSuffix+";loginTimeout=30;",
string(DBSecret["fullyQualifiedServerName"]),
instance.Spec.DbName,
user,
Expand All @@ -203,7 +204,7 @@ func (s *AzureSqlUserManager) Ensure(ctx context.Context, obj runtime.Object, op
))
case "jdbc-urlonly":
formattedSecrets["jdbc-urlonly"] = []byte(fmt.Sprintf(
"jdbc:sqlserver://%v:1433;database=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
"jdbc:sqlserver://%v:1433;database=%v;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*."+config.Environment().SQLDatabaseDNSSuffix+";loginTimeout=30;",
string(DBSecret["fullyQualifiedServerName"]),
instance.Spec.DbName,
))
Expand Down
23 changes: 21 additions & 2 deletions pkg/resourcemanager/psql/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ func (p *PSQLServerClient) AddServerCredsToSecrets(ctx context.Context, secretNa
return nil
}

func (p *PSQLServerClient) UpdateSecretWithFullServerName(ctx context.Context, secretName string, data map[string][]byte, instance *azurev1alpha1.PostgreSQLServer, fullservername string) error {
key := types.NamespacedName{
Name: secretName,
Namespace: instance.Namespace,
}

data["fullyQualifiedServerName"] = []byte(fullservername)

err := p.SecretClient.Upsert(ctx,
key,
data,
secrets.WithOwner(instance),
secrets.WithScheme(p.Scheme),
)
if err != nil {
return err
}

return nil
}

func (p *PSQLServerClient) GetOrPrepareSecret(ctx context.Context, instance *azurev1alpha1.PostgreSQLServer) (map[string][]byte, error) {
name := instance.Name

Expand All @@ -175,8 +196,6 @@ func (p *PSQLServerClient) GetOrPrepareSecret(ctx context.Context, instance *azu
secret["fullyQualifiedUsername"] = []byte(fmt.Sprintf("%s@%s", randomUsername, name))
secret["password"] = []byte(randomPassword)
secret["postgreSqlServerName"] = []byte(name)
// TODO: The below may not be right for non Azure public cloud.
secret["fullyQualifiedServerName"] = []byte(name + ".postgres.database.azure.com")

return secret, nil
}
8 changes: 7 additions & 1 deletion pkg/resourcemanager/psql/server/server_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts
if err != nil {
return false, err
}
// Update secret

// Update secret with the fully qualified server name
err = p.AddServerCredsToSecrets(ctx, instance.Name, secret, instance)
if err != nil {
return false, err
Expand All @@ -52,10 +53,15 @@ func (p *PSQLServerClient) Ensure(ctx context.Context, obj runtime.Object, opts

// succeeded! so end reconcilliation successfully
if getServer.UserVisibleState == "Ready" {

// Update the secret with fully qualified server name. Ignore error as we have the admin creds which is critical.
p.UpdateSecretWithFullServerName(ctx, instance.Name, secret, instance, *getServer.FullyQualifiedDomainName)

instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.ResourceId = *getServer.ID
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.State = string(getServer.UserVisibleState)
buhongw7583c marked this conversation as resolved.
Show resolved Hide resolved
return true, nil
}

Expand Down