Skip to content

Commit 439a0b6

Browse files
quantumsheepOursin
andauthored
fix(iam): update tests to match SSH key format update (#2477)
* fix(iam): update tests to match SSH key format update * feat: compare ssh keys by parsing them Signed-off-by: Nathanael DEMACON <[email protected]> * tests: update cassettes Signed-off-by: Nathanael DEMACON <[email protected]> * fix linter issues Signed-off-by: Nathanael DEMACON <[email protected]> --------- Signed-off-by: Nathanael DEMACON <[email protected]> Co-authored-by: Maxime Corbin <[email protected]> Co-authored-by: Nathanael DEMACON <[email protected]>
1 parent a1cf4d7 commit 439a0b6

15 files changed

+1067
-1093
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/robfig/cron/v3 v3.0.1
1919
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.25.0.20240325161646-a49fad6abe89
2020
github.com/stretchr/testify v1.9.0
21+
golang.org/x/crypto v0.19.0
2122
)
2223

2324
require (
@@ -82,7 +83,6 @@ require (
8283
go.opentelemetry.io/otel/metric v1.22.0 // indirect
8384
go.opentelemetry.io/otel/sdk v1.22.0 // indirect
8485
go.opentelemetry.io/otel/trace v1.22.0 // indirect
85-
golang.org/x/crypto v0.19.0 // indirect
8686
golang.org/x/mod v0.15.0 // indirect
8787
golang.org/x/net v0.19.0 // indirect
8888
golang.org/x/sys v0.17.0 // indirect

scaleway/data_source_account_ssh_key_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
func TestAccScalewayDataSourceAccountSSHKey_Basic(t *testing.T) {
1313
dataSourceAccountSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILHy/M5FVm5ydLGcal3e5LNcfTalbeN7QL/ZGCvDEdqJ [email protected]"
14+
dataSourceAccountSSHKeyWithoutComment := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILHy/M5FVm5ydLGcal3e5LNcfTalbeN7QL/ZGCvDEdqJ"
1415
sshKeyName := "TestAccScalewayDataSourceAccountSSHKey_Basic"
1516
tt := acctest.NewTestTools(t)
1617
defer tt.Cleanup()
@@ -44,10 +45,10 @@ func TestAccScalewayDataSourceAccountSSHKey_Basic(t *testing.T) {
4445
Check: resource.ComposeTestCheckFunc(
4546
iam.CheckSSHKeyExists(tt, "data.scaleway_account_ssh_key.prod"),
4647
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "name", sshKeyName),
47-
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "public_key", dataSourceAccountSSHKey),
48+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.prod", "public_key", dataSourceAccountSSHKeyWithoutComment),
4849
iam.CheckSSHKeyExists(tt, "data.scaleway_account_ssh_key.stg"),
4950
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "name", sshKeyName),
50-
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "public_key", dataSourceAccountSSHKey),
51+
resource.TestCheckResourceAttr("data.scaleway_account_ssh_key.stg", "public_key", dataSourceAccountSSHKeyWithoutComment),
5152
),
5253
},
5354
},

scaleway/data_source_iam_ssh_key_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
func TestAccScalewayDataSourceIamSSHKey_Basic(t *testing.T) {
1313
dataSourceIamSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILHy/M5FVm5ydLGcal3e5LNcfTalbeN7QL/ZGCvDEdqJ [email protected]"
14+
dataSourceIamSSHKeyWithoutComment := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILHy/M5FVm5ydLGcal3e5LNcfTalbeN7QL/ZGCvDEdqJ"
1415
sshKeyName := "tf-test-ds-iam-ssh-key-basic"
1516
tt := acctest.NewTestTools(t)
1617
defer tt.Cleanup()
@@ -44,10 +45,10 @@ func TestAccScalewayDataSourceIamSSHKey_Basic(t *testing.T) {
4445
Check: resource.ComposeTestCheckFunc(
4546
iam.CheckSSHKeyExists(tt, "data.scaleway_iam_ssh_key.prod"),
4647
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.prod", "name", sshKeyName),
47-
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.prod", "public_key", dataSourceIamSSHKey),
48+
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.prod", "public_key", dataSourceIamSSHKeyWithoutComment),
4849
iam.CheckSSHKeyExists(tt, "data.scaleway_iam_ssh_key.stg"),
4950
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.stg", "name", sshKeyName),
50-
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.stg", "public_key", dataSourceIamSSHKey),
51+
resource.TestCheckResourceAttr("data.scaleway_iam_ssh_key.stg", "public_key", dataSourceIamSSHKeyWithoutComment),
5152
),
5253
},
5354
},

scaleway/resource_account_ssh_key_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
func TestAccScalewayAccountSSHKey_basic(t *testing.T) {
1313
name := "tf-test-account-ssh-key-basic"
1414
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX [email protected]"
15+
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEEYrzDOZmhItdKaDAEqJQ4ORS2GyBMtBozYsK5kiXXX"
1516
tt := acctest.NewTestTools(t)
1617
defer tt.Cleanup()
1718

@@ -30,7 +31,7 @@ func TestAccScalewayAccountSSHKey_basic(t *testing.T) {
3031
Check: resource.ComposeTestCheckFunc(
3132
iam.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
3233
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
33-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
34+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
3435
),
3536
},
3637
{
@@ -43,7 +44,7 @@ func TestAccScalewayAccountSSHKey_basic(t *testing.T) {
4344
Check: resource.ComposeTestCheckFunc(
4445
iam.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
4546
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name+"-updated"),
46-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
47+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
4748
),
4849
},
4950
},
@@ -53,6 +54,7 @@ func TestAccScalewayAccountSSHKey_basic(t *testing.T) {
5354
func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
5455
name := "tf-test-account-ssh-key-newline"
5556
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup [email protected]"
57+
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDjfkdWCwkYlVQMDUfiZlVrmjaGOfBYnmkucssae8Iup"
5658
tt := acctest.NewTestTools(t)
5759
defer tt.Cleanup()
5860

@@ -71,7 +73,7 @@ func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
7173
Check: resource.ComposeTestCheckFunc(
7274
iam.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.main"),
7375
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "name", name),
74-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", SSHKey),
76+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.main", "public_key", FormattedSSHKey),
7577
),
7678
},
7779
},
@@ -81,6 +83,7 @@ func TestAccScalewayAccountSSHKey_WithNewLine(t *testing.T) {
8183
func TestAccScalewayAccountSSHKey_ChangeResourceName(t *testing.T) {
8284
name := "TestAccScalewayAccountSSHKey_ChangeResourceName"
8385
SSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
86+
FormattedSSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
8487
tt := acctest.NewTestTools(t)
8588
defer tt.Cleanup()
8689

@@ -99,7 +102,7 @@ func TestAccScalewayAccountSSHKey_ChangeResourceName(t *testing.T) {
99102
Check: resource.ComposeTestCheckFunc(
100103
iam.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.first"),
101104
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "name", name),
102-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", SSHKey),
105+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.first", "public_key", FormattedSSHKey),
103106
),
104107
},
105108
{
@@ -112,7 +115,7 @@ func TestAccScalewayAccountSSHKey_ChangeResourceName(t *testing.T) {
112115
Check: resource.ComposeTestCheckFunc(
113116
iam.CheckSSHKeyExists(tt, "scaleway_account_ssh_key.second"),
114117
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "name", name),
115-
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", SSHKey),
118+
resource.TestCheckResourceAttr("scaleway_account_ssh_key.second", "public_key", FormattedSSHKey),
116119
),
117120
},
118121
},

scaleway/resource_iam_ssh_key.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scaleway
22

33
import (
4+
"bytes"
45
"context"
56
"strings"
67

@@ -10,6 +11,7 @@ import (
1011
"github.com/scaleway/scaleway-sdk-go/scw"
1112
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
14+
"golang.org/x/crypto/ssh"
1315
)
1416

1517
func ResourceScalewayIamSSKKey() *schema.Resource {
@@ -36,7 +38,21 @@ func ResourceScalewayIamSSKKey() *schema.Resource {
3638
Description: "The public SSH key",
3739
// We don't consider trailing \n as diff
3840
DiffSuppressFunc: func(_, oldValue, newValue string, _ *schema.ResourceData) bool {
39-
return strings.Trim(oldValue, "\n") == strings.Trim(newValue, "\n")
41+
parsedOldValue, _, _, _, err := ssh.ParseAuthorizedKey([]byte(oldValue))
42+
if err != nil {
43+
return false
44+
}
45+
46+
parsedNewValue, _, _, _, err := ssh.ParseAuthorizedKey([]byte(newValue))
47+
if err != nil {
48+
return false
49+
}
50+
51+
marshalledOldValue := ssh.MarshalAuthorizedKey(parsedOldValue)
52+
marshalledNewValue := ssh.MarshalAuthorizedKey(parsedNewValue)
53+
54+
areEqual := bytes.Equal(marshalledOldValue, marshalledNewValue)
55+
return areEqual
4056
},
4157
},
4258
"fingerprint": {
@@ -175,3 +191,11 @@ func resourceScalewayIamSSKKeyDelete(ctx context.Context, d *schema.ResourceData
175191

176192
return nil
177193
}
194+
195+
func removePublicSSHKeyComment(publicKey string) string {
196+
parts := strings.Split(publicKey, " ")
197+
if len(parts) == 3 {
198+
return strings.Join(parts[:2], " ")
199+
}
200+
return publicKey
201+
}

scaleway/resource_iam_ssh_key_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/iam"
1313
)
1414

15-
const SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
15+
const (
16+
SSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO [email protected]"
17+
SSHKeyWithoutComment = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICJEoOOgQBLJPs4g/XcPTKT82NywNPpxeuA20FlOPlpO"
18+
)
1619

1720
func init() {
1821
resource.AddTestSweepers("scaleway_iam_ssh_key", &resource.Sweeper{
@@ -68,7 +71,7 @@ func TestAccScalewayIamSSHKey_basic(t *testing.T) {
6871
Check: resource.ComposeTestCheckFunc(
6972
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
7073
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
71-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
74+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
7275
),
7376
},
7477
{
@@ -81,7 +84,7 @@ func TestAccScalewayIamSSHKey_basic(t *testing.T) {
8184
Check: resource.ComposeTestCheckFunc(
8285
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
8386
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name+"-updated"),
84-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
87+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
8588
),
8689
},
8790
},
@@ -108,7 +111,7 @@ func TestAccScalewayIamSSHKey_WithNewLine(t *testing.T) {
108111
Check: resource.ComposeTestCheckFunc(
109112
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
110113
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
111-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
114+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
112115
),
113116
},
114117
},
@@ -135,7 +138,7 @@ func TestAccScalewayIamSSHKey_ChangeResourceName(t *testing.T) {
135138
Check: resource.ComposeTestCheckFunc(
136139
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.first"),
137140
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "name", name),
138-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKey),
141+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.first", "public_key", SSHKeyWithoutComment),
139142
),
140143
},
141144
{
@@ -148,7 +151,7 @@ func TestAccScalewayIamSSHKey_ChangeResourceName(t *testing.T) {
148151
Check: resource.ComposeTestCheckFunc(
149152
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.second"),
150153
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "name", name),
151-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKey),
154+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.second", "public_key", SSHKeyWithoutComment),
152155
),
153156
},
154157
},
@@ -175,7 +178,7 @@ func TestAccScalewayIamSSHKey_Disabled(t *testing.T) {
175178
Check: resource.ComposeTestCheckFunc(
176179
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
177180
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
178-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
181+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
179182
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"),
180183
),
181184
},
@@ -190,7 +193,7 @@ func TestAccScalewayIamSSHKey_Disabled(t *testing.T) {
190193
Check: resource.ComposeTestCheckFunc(
191194
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
192195
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
193-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
196+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
194197
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "true"),
195198
),
196199
},
@@ -205,7 +208,7 @@ func TestAccScalewayIamSSHKey_Disabled(t *testing.T) {
205208
Check: resource.ComposeTestCheckFunc(
206209
iam.CheckSSHKeyExists(tt, "scaleway_iam_ssh_key.main"),
207210
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "name", name),
208-
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKey),
211+
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "public_key", SSHKeyWithoutComment),
209212
resource.TestCheckResourceAttr("scaleway_iam_ssh_key.main", "disabled", "false"),
210213
),
211214
},

0 commit comments

Comments
 (0)