-
Notifications
You must be signed in to change notification settings - Fork 123
[ISSUE-127] Only change password and password_hash when value changes #128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| package security_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/acctest" | ||
|
|
@@ -29,13 +33,134 @@ func TestAccResourceSecurityUser(t *testing.T) { | |
| ), | ||
| }, | ||
| { | ||
| Config: testAccResourceSecurityUpdate(username), | ||
| Config: testAccResourceSecurityUpdate(username, "kibana_user"), | ||
| Check: resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_user.test", "email", "[email protected]"), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccImportedUserDoesNotResetPassword(t *testing.T) { | ||
| username := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
| initialPassword := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
| userUpdatedPassword := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) | ||
|
|
||
| resource.ParallelTest(t, resource.TestCase{ | ||
| IsUnitTest: true, | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| CheckDestroy: checkResourceSecurityUserDestroy, | ||
| ProviderFactories: acctest.Providers, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testAccResourceSecurityUserUpdateNoPassword(username), | ||
| SkipFunc: func() (bool, error) { | ||
| client := acctest.ApiClient() | ||
| body := fmt.Sprintf("{\"roles\": [\"kibana_admin\"], \"password\": \"%s\"}", initialPassword) | ||
|
|
||
| resp, err := client.GetESClient().Security.PutUser(username, strings.NewReader(body)) | ||
| if err != nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| if resp.IsError() { | ||
| body, err := io.ReadAll(resp.Body) | ||
| return false, fmt.Errorf("failed to manually create import test user [%s] %s %s", username, body, err) | ||
| } | ||
| return false, err | ||
| }, | ||
| ResourceName: "elasticstack_elasticsearch_security_user.test", | ||
| ImportStateIdFunc: func(s *terraform.State) (string, error) { | ||
| client := acctest.ApiClient() | ||
| clusterId, diag := client.ClusterID(context.Background()) | ||
| if diag.HasError() { | ||
| return "", fmt.Errorf("failed to get cluster uuid: %s", diag[0].Summary) | ||
| } | ||
|
|
||
| return fmt.Sprintf("%s/%s", *clusterId, username), nil | ||
| }, | ||
| ImportState: true, | ||
| ImportStatePersist: true, | ||
| ImportStateCheck: func(is []*terraform.InstanceState) error { | ||
| importedUsername := is[0].Attributes["username"] | ||
| if importedUsername != username { | ||
| return fmt.Errorf("expected imported username [%s] to equal [%s]", importedUsername, username) | ||
| } | ||
|
|
||
| if _, ok := is[0].Attributes["password"]; ok { | ||
| return fmt.Errorf("expected imported user to not have a password set - got [%s]", is[0].Attributes["password"]) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| Check: checkUserCanAuthenticate(username, initialPassword), | ||
| }, | ||
| { | ||
| Config: testAccResourceSecurityUserUpdateNoPassword(username), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_user.test", "username", username), | ||
| resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_security_user.test", "roles.*", "kibana_admin"), | ||
| resource.TestCheckNoResourceAttr("elasticstack_elasticsearch_security_user.test", "password"), | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_user.test", "full_name", "Test User"), | ||
| checkUserCanAuthenticate(username, initialPassword), | ||
| ), | ||
| }, | ||
| { | ||
| Config: testAccResourceSecurityUpdate(username, "kibana_admin"), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_user.test", "email", "[email protected]"), | ||
| resource.TestCheckResourceAttrSet("elasticstack_elasticsearch_security_user.test", "password"), | ||
| ), | ||
| }, | ||
| { | ||
| SkipFunc: func() (bool, error) { | ||
| client := acctest.ApiClient().GetESClient() | ||
| body := fmt.Sprintf("{\"password\": \"%s\"}", userUpdatedPassword) | ||
|
|
||
| req := client.API.Security.ChangePassword.WithUsername(username) | ||
| resp, err := client.Security.ChangePassword(strings.NewReader(body), req) | ||
| if err != nil { | ||
| return false, nil | ||
| } | ||
|
|
||
| if resp.IsError() { | ||
| body, err := io.ReadAll(resp.Body) | ||
| return false, fmt.Errorf("failed to manually change import test user password [%s] %s %s", username, body, err) | ||
| } | ||
| return false, err | ||
| }, | ||
| Config: testAccResourceSecurityUpdate(username, "kibana_user"), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_user.test", "email", "[email protected]"), | ||
| resource.TestCheckResourceAttrSet("elasticstack_elasticsearch_security_user.test", "password"), | ||
| resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_security_user.test", "roles.*", "kibana_user"), | ||
| checkUserCanAuthenticate(username, userUpdatedPassword), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func checkUserCanAuthenticate(username string, password string) func(*terraform.State) error { | ||
| return func(s *terraform.State) error { | ||
| client := acctest.ApiClient().GetESClient() | ||
| credentials := fmt.Sprintf("%s:%s", username, password) | ||
| authHeader := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(credentials))) | ||
|
|
||
| req := client.API.Security.Authenticate.WithHeader(map[string]string{"Authorization": authHeader}) | ||
| resp, err := client.Security.Authenticate(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if resp.IsError() { | ||
| body, err := io.ReadAll(resp.Body) | ||
|
|
||
| return fmt.Errorf("failed to authenticate as test user [%s] %s %s", username, body, err) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func testAccResourceSecurityUserCreate(username string) string { | ||
| return fmt.Sprintf(` | ||
| provider "elasticstack" { | ||
|
|
@@ -51,24 +176,38 @@ resource "elasticstack_elasticsearch_security_user" "test" { | |
| `, username) | ||
| } | ||
|
|
||
| func testAccResourceSecurityUpdate(username string) string { | ||
| func testAccResourceSecurityUserUpdateNoPassword(username string) string { | ||
| return fmt.Sprintf(` | ||
| provider "elasticstack" { | ||
| elasticsearch {} | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_security_user" "test" { | ||
| username = "%s" | ||
| roles = ["kibana_user"] | ||
| roles = ["kibana_admin"] | ||
| full_name = "Test User" | ||
| } | ||
| `, username) | ||
| } | ||
|
|
||
| func testAccResourceSecurityUpdate(username string, role string) string { | ||
| return fmt.Sprintf(` | ||
| provider "elasticstack" { | ||
| elasticsearch {} | ||
| } | ||
|
|
||
| resource "elasticstack_elasticsearch_security_user" "test" { | ||
| username = "%s" | ||
| roles = ["%s"] | ||
| full_name = "Test User" | ||
| email = "[email protected]" | ||
| password = "qwerty123" | ||
| } | ||
| `, username) | ||
| `, username, role) | ||
| } | ||
|
|
||
| func checkResourceSecurityUserDestroy(s *terraform.State) error { | ||
| client := acctest.Provider.Meta().(*clients.ApiClient) | ||
| client := acctest.ApiClient() | ||
|
|
||
| for _, rs := range s.RootModule().Resources { | ||
| if rs.Type != "elasticstack_elasticsearch_security_user" { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.