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

azurerm_mssql_server : fix for administrator_login_password not being able to be updated #25182

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sinbai
Copy link
Contributor

@sinbai sinbai commented Mar 8, 2024

  • The behavior of the API when updating the password of resource azurerm_mssql_server:

If azuread_authentication_only is set to true when creating resource azurerm_mssql_server. In this case, the password is not allowed to be updated. Password is allowed to be updated when azuread_authentication_only is set to false.

  1. Create a resource azurerm_mssql_serverwith azuread_administrator.0.azuread_authentication_only = true.
  2. Update the password of azurerm_mssql_server and update azuread_administrator.0.azuread_authentication_only = false.
    Actual: TF returns error: administrator_login_passwordcannot be changed onceazuread_administrator.0.azuread_authentication_only = true
  • TF’s current implementation:
  1. The validation in code prevents the password from being updated because of the old value of azuread_authentication_only is true (this is the value set when creating the new resource).
  2. Even if the validation in 1. is deleted, it is still not possible to update the password, because the password is updated first in code (this is not allowed by API since the azuread_authentication_only is true), and then updating the azuread_authentication_only in code.

Therefore, I removed the validation in code, and adjusted the update order in 2. to fix #25116.

Test results:
image

  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Copy link

@AtakanColak AtakanColak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for your PR. I'm not really familiar with this codebase, but I wanted to review to the best of my ability.

version = "12.0"

administrator_login = "missadministrator"
administrator_login_password = "thisIsKat11"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if having azuread_authentication_only = true and administrator_login_password present is a feasible state. My bug report's initial state is aad auth is true and there is no additional administrator_login setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that it is possible because in the Terraform documentation it is stated that When true, the administrator_login and administrator_login_password properties can be omitted. . I assuming this means omitted means azure authentication only, not omitted means both. Also, the test case TestAccMsSqlServer_passwordUpdate passed.

@@ -762,6 +784,72 @@ resource "azurerm_mssql_server" "test" {
`, data.RandomInteger, data.Locations.Primary)
}

func (MsSqlServerResource) password(data acceptance.TestData) string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not knowing much about the code styling of this repositiry, I think this function naming could be improved to something like mssqlServerResourceWithAadAuthOnly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed to aadAdminWithAADAuthOnlyWithPassword.

`, data.RandomInteger, data.Locations.Primary)
}

func (MsSqlServerResource) passwordUpdate(data acceptance.TestData) string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly would rename to something like mssqServerResourceWithAdminLogin

Copy link
Contributor Author

@sinbai sinbai Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed to aadAdminWithAADAuthOnlyWithPasswordUpdate.

@@ -177,6 +177,28 @@ func TestAccMsSqlServer_azureadAdmin(t *testing.T) {
})
}

func TestAccMsSqlServer_passwordUpdate(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might prefer rename to be more precise (see comments below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the TestAccMsSqlServer_azureadAdminWithAADAuthOnlyWithPasswordUpdate would be more appropriate.

Comment on lines 320 to 322
aadOnlyAuthentictionsEnabled := expandMsSqlServerAADOnlyAuthentictions(d.Get("azuread_administrator").([]interface{}))
if _, ok := d.GetOk("administrator_login_password"); ok && aadOnlyAuthentictionsEnabled && d.HasChange("administrator_login_password") {
return fmt.Errorf("`administrator_login_password` cannot be changed when `azuread_administrator.0.azuread_authentication_only = true`")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authentiction typos?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the correction.

Properties: &serverazureadonlyauthentications.AzureADOnlyAuthProperties{
AzureADOnlyAuthentication: aadOnlyAuthentictionsEnabled,
},
if payload := existing.Model; payload != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this may be a silly question but was there a reason for moving this part of the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this part of the code is key to solving issue #25116, where the Azure Rest API does not allow administrator_login_password updates when azuread_authentication_only = true. Because the API has such limitations, the correct behavior of TF should be to first update azuread_authentication_only = false (specified by the user), and then update administrator_login_password, so that the update can be successful.

@sinbai
Copy link
Contributor Author

sinbai commented Mar 11, 2024

Thanks very much for your PR. I'm not really familiar with this codebase, but I wanted to review to the best of my ability.

Hi @AtakanColak thanks for your time and feedback.

Copy link
Contributor

@manicminer manicminer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this @sinbai. Isn't the problem here due to azuread_authentication_only being optional + computed? If we make this not computed, the validation should pass as-is?

@@ -319,6 +317,11 @@ func resourceMsSqlServerUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

aadOnlyAuthenticationsEnabled := expandMsSqlServerAADOnlyAuthentictions(d.Get("azuread_administrator").([]interface{}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(typo)

Suggested change
aadOnlyAuthenticationsEnabled := expandMsSqlServerAADOnlyAuthentictions(d.Get("azuread_administrator").([]interface{}))
aadOnlyAuthenticationsEnabled := expandMsSqlServerAADOnlyAuthentications(d.Get("azuread_administrator").([]interface{}))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -443,18 +388,80 @@ func resourceMsSqlServerUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
return fmt.Errorf("deleting Azure Active Directory Administrator %s: %+v", id, err)
}
}

if aadOnlyAuthenticationsEnabled {
aadOnlyAuthentictionsProps := serverazureadonlyauthentications.ServerAzureADOnlyAuthentication{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(typo)

Suggested change
aadOnlyAuthentictionsProps := serverazureadonlyauthentications.ServerAzureADOnlyAuthentication{
aadOnlyAuthenticationsProps := serverazureadonlyauthentications.ServerAzureADOnlyAuthentication{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@sinbai
Copy link
Contributor Author

sinbai commented Mar 28, 2024

Thanks for looking at this @sinbai. Isn't the problem here due to azuread_authentication_only being optional + computed? If we make this not computed, the validation should pass as-is?

Hi @manicminer thanks for your time and feedback. I assume that even if we make azuread_authentication_only not computed, the validation still fails. Detail see the explanation below for details.

  • The behavior of the API when updating the password of resource azurerm_mssql_server:

If azuread_authentication_only is set to true when creating resource azurerm_mssql_server. In this case, the password is not allowed to be updated. Password is allowed to be updated when azuread_authentication_only is set to false.

  1. Create a resource azurerm_mssql_serverwith azuread_administrator.0.azuread_authentication_only = true.
  2. Update the password of azurerm_mssql_server and update azuread_administrator.0.azuread_authentication_only = false.
    Actual: TF returns error: administrator_login_passwordcannot be changed onceazuread_administrator.0.azuread_authentication_only = true
  • TF’s current implementation:
  1. The validation in code prevents the password from being updated because of the old value of azuread_authentication_only is true (this is the value set when creating the new resource).
  2. Even if the validation in 1. is deleted, it is still not possible to update the password, because the password is updated first in code (this is not allowed by API since the azuread_authentication_only is true), and then updating the azuread_authentication_only in code.

Therefore, I removed the validation and adjusted the update order in 2. to fix #25116. Please let me know if you have any feedback. Thanks in advance.

@rcskosir rcskosir added the bug label Oct 14, 2024
@katbyte katbyte requested review from katbyte and a team as code owners November 14, 2024 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug service/mssql Microsoft SQL Server size/L
Projects
None yet
5 participants