Skip to content

Commit ac46389

Browse files
authored
Merge pull request #1175 from timofurrer/bugfix/ignore-sshkey-whitespace
resource/gitlab_user_sshkey: Ignore leading and trailing whitespaces in key
2 parents 1b2d42b + df8d4c1 commit ac46389

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

internal/provider/resource_gitlab_user_sshkey.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ var _ = registerResource("gitlab_user_sshkey", func() *schema.Resource {
4848
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
4949
// NOTE: the ssh keys consist of three parts: `type`, `data`, `comment`, whereas the `comment` is optional
5050
// and suppressed in the diffing. It's overridden by GitLab with the username and GitLab hostname.
51-
newParts := strings.SplitN(new, " ", 3)
52-
oldParts := strings.SplitN(old, " ", 3)
51+
newParts := strings.Fields(new)
52+
oldParts := strings.Fields(old)
5353
if len(newParts) < 2 || len(oldParts) < 2 {
5454
// NOTE: at least one of the keys doesn't have the required two parts, thus we just compare them
5555
return new == old

internal/provider/resource_gitlab_user_sshkey_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var testRSAPubKey string = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCi+ErxScCKIVqg
1717
var testRSAPubKeyUpdatedComment string = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCi+ErxScCKIVqg2ZRJ6Mx2Yd/RTsh2DGyhUR8z8Iey4rpi1YOBlpTgjxxnSLy26J++Un/iWYDP8wMvEjXElkWz3z4I+Z3mfF3dv039FTCu+O17Mw20Ek4DJxdrKvOgul040sUG/ABVHo6DjqjokjoVJwzUrUmoOtbeMMD8hFN9bWdEVyTj18XQO8nvEe/VkbhCRhAlZC1l60fM07/7Tw83SV5UNAnBtOB+nfa3b24baO+Ijc4+PqYcBuUAF6DvhXW2gZPqf5wjDBJqlDlRTYDdHarMXZAKBpWfWj0gntbtEOM+Fnp6hS1HajaeveNSs6yQwgQEDN2boQnDuvXJ8Y7zW3YQKZp8z0uqWYJSIrYRKVEVYL7gDWL9NvdRV52d/RKPnE/BlL2chiAWBRCT8buQdjVtEPPoYbA1667PXZg6PI9yhCGEIjCj71XzPssA6VL/R7yUafsmNLsirWz9Uyh3HJWCcgNuO9mglP5nfFHIXSHQVhEUEYMfzv1iX5FrenU= [email protected]"
1818
var updatedRSAPubKey string = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDStVqW58VZ5afXFphIvu2JahndXslJZMkgWsNiYCNdk/NvrEbc4i7yZVoDPFQsbS9I6Ty1RMW7qy3KxJalMsVHcw8arCQFDxs/ka1NHGCUPl68t5ZxUOl900KRQ0lOzGnDQMqG/UUZdPw4CCmigTr6Z9ZBcD1fXAiUwbXR4tWrr5z9KWXC2HgF4WkIJUTIct7ilY1m9W0y79dI/+K8bZrurn3q2QK83pxqqWkLwvUsCxtlhMpwuyflyzyuz8xPZl2GlZgxeIpr68gsPHIzzWizibwFfbRYKCZO4wD0r7JCDOYs9KjcIPpCG6d3HUqijClgdQSBnLwHTdE04ZtdzO8akvy0hMzRCooI5TSc8IAHos53Gp9aaW92sPA8za+WRP6OSH6UsOW4N+iQc4jyl7/fckMSgIZlJouNqqV+P8iqIFJGs70Tj5L8G/m+P2lc3kcE4Vjmj+Fc0xG5+I/PsSOpcc6DfDfZdVDRe8yklYd/qC1jI89OCeqjxu3XcUGHj9s= [email protected]"
1919
var updatedRSAPubKeyWithoutComment string = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDStVqW58VZ5afXFphIvu2JahndXslJZMkgWsNiYCNdk/NvrEbc4i7yZVoDPFQsbS9I6Ty1RMW7qy3KxJalMsVHcw8arCQFDxs/ka1NHGCUPl68t5ZxUOl900KRQ0lOzGnDQMqG/UUZdPw4CCmigTr6Z9ZBcD1fXAiUwbXR4tWrr5z9KWXC2HgF4WkIJUTIct7ilY1m9W0y79dI/+K8bZrurn3q2QK83pxqqWkLwvUsCxtlhMpwuyflyzyuz8xPZl2GlZgxeIpr68gsPHIzzWizibwFfbRYKCZO4wD0r7JCDOYs9KjcIPpCG6d3HUqijClgdQSBnLwHTdE04ZtdzO8akvy0hMzRCooI5TSc8IAHos53Gp9aaW92sPA8za+WRP6OSH6UsOW4N+iQc4jyl7/fckMSgIZlJouNqqV+P8iqIFJGs70Tj5L8G/m+P2lc3kcE4Vjmj+Fc0xG5+I/PsSOpcc6DfDfZdVDRe8yklYd/qC1jI89OCeqjxu3XcUGHj9s="
20+
var testKeyWithTrailingNewline string = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMG5+BWfNRCNE9chUUooEwS/QeNMN5Z1RBdY1GQ0VqMa\n"
2021

2122
func TestAccGitlabUserSSHKey_basic(t *testing.T) {
2223
var key gitlab.SSHKey
@@ -79,6 +80,48 @@ func TestAccGitlabUserSSHKey_basic(t *testing.T) {
7980
})
8081
}
8182

83+
func TestAccGitlabUserSSHKey_ignoreTrailingWhitespaces(t *testing.T) {
84+
testUser := testAccCreateUsers(t, 1)[0]
85+
86+
resource.ParallelTest(t, resource.TestCase{
87+
ProviderFactories: providerFactories,
88+
CheckDestroy: testAccCheckGitlabUserSSHKeyDestroy,
89+
Steps: []resource.TestStep{
90+
// Create a user + sshkey
91+
{
92+
Config: fmt.Sprintf(`
93+
resource "gitlab_user_sshkey" "this" {
94+
user_id = %d
95+
title = "test"
96+
key = <<EOF
97+
%s
98+
EOF
99+
}
100+
`, testUser.ID, testKeyWithTrailingNewline),
101+
},
102+
// Check for no-op plan
103+
{
104+
Config: fmt.Sprintf(`
105+
resource "gitlab_user_sshkey" "this" {
106+
user_id = %d
107+
title = "test"
108+
key = <<EOF
109+
%s
110+
EOF
111+
}
112+
`, testUser.ID, testKeyWithTrailingNewline),
113+
PlanOnly: true,
114+
},
115+
// Verify Import
116+
{
117+
ResourceName: "gitlab_user_sshkey.this",
118+
ImportState: true,
119+
ImportStateVerify: true,
120+
},
121+
},
122+
})
123+
}
124+
82125
func testAccCheckGitlabUserSSHKeyDestroy(s *terraform.State) error {
83126
for _, rs := range s.RootModule().Resources {
84127
if rs.Type != "gitlab_user_sshkey" {

0 commit comments

Comments
 (0)