sshkey_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package sshkey
  13. import (
  14. "context"
  15. "strings"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  20. )
  21. func TestGenerate(t *testing.T) {
  22. g := &Generator{}
  23. tests := []struct {
  24. name string
  25. jsonSpec *apiextensions.JSON
  26. wantErr bool
  27. expectedErr string
  28. validate func(t *testing.T, result map[string][]byte)
  29. }{
  30. {
  31. name: "nil spec should return error",
  32. jsonSpec: nil,
  33. wantErr: true,
  34. expectedErr: errNoSpec,
  35. },
  36. {
  37. name: "empty spec should use defaults",
  38. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{}}`)},
  39. wantErr: false,
  40. validate: func(t *testing.T, result map[string][]byte) {
  41. assert.Contains(t, result, "privateKey")
  42. assert.Contains(t, result, "publicKey")
  43. assert.True(t, len(result["privateKey"]) > 0)
  44. assert.True(t, len(result["publicKey"]) > 0)
  45. // Should contain RSA private key header
  46. assert.Contains(t, string(result["privateKey"]), "BEGIN OPENSSH PRIVATE KEY")
  47. // Should contain ssh-rsa public key
  48. assert.True(t, strings.HasPrefix(string(result["publicKey"]), "ssh-rsa "))
  49. },
  50. },
  51. {
  52. name: "rsa key with custom size",
  53. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{"keyType":"rsa","keySize":4096}}`)},
  54. wantErr: false,
  55. validate: func(t *testing.T, result map[string][]byte) {
  56. assert.Contains(t, result, "privateKey")
  57. assert.Contains(t, result, "publicKey")
  58. assert.True(t, len(result["privateKey"]) > 0)
  59. assert.True(t, len(result["publicKey"]) > 0)
  60. },
  61. },
  62. {
  63. name: "ed25519 key",
  64. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{"keyType":"ed25519"}}`)},
  65. wantErr: false,
  66. validate: func(t *testing.T, result map[string][]byte) {
  67. assert.Contains(t, result, "privateKey")
  68. assert.Contains(t, result, "publicKey")
  69. assert.True(t, len(result["privateKey"]) > 0)
  70. assert.True(t, len(result["publicKey"]) > 0)
  71. // Should contain ed25519 public key
  72. assert.True(t, strings.HasPrefix(string(result["publicKey"]), "ssh-ed25519 "))
  73. },
  74. },
  75. {
  76. name: "ed25519 key with explicit keySize (should be ignored)",
  77. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{"keyType":"ed25519","keySize":4096}}`)},
  78. wantErr: false,
  79. validate: func(t *testing.T, result map[string][]byte) {
  80. assert.Contains(t, result, "privateKey")
  81. assert.Contains(t, result, "publicKey")
  82. assert.True(t, len(result["privateKey"]) > 0)
  83. assert.True(t, len(result["publicKey"]) > 0)
  84. // Should contain ed25519 public key (keySize should be ignored)
  85. assert.True(t, strings.HasPrefix(string(result["publicKey"]), "ssh-ed25519 "))
  86. },
  87. },
  88. {
  89. name: "key with comment",
  90. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{"keyType":"rsa","comment":"test@example.com"}}`)},
  91. wantErr: false,
  92. validate: func(t *testing.T, result map[string][]byte) {
  93. assert.Contains(t, result, "privateKey")
  94. assert.Contains(t, result, "publicKey")
  95. // Should contain the comment in public key
  96. assert.Contains(t, string(result["publicKey"]), "test@example.com")
  97. },
  98. },
  99. {
  100. name: "unsupported key type",
  101. jsonSpec: &apiextensions.JSON{Raw: []byte(`{"spec":{"keyType":"unsupported"}}`)},
  102. wantErr: true,
  103. expectedErr: "unsupported key type",
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. result, _, err := g.Generate(context.Background(), tt.jsonSpec, nil, "")
  109. if tt.wantErr {
  110. assert.Error(t, err)
  111. if tt.expectedErr != "" {
  112. assert.Contains(t, err.Error(), tt.expectedErr)
  113. }
  114. return
  115. }
  116. assert.NoError(t, err)
  117. if tt.validate != nil {
  118. tt.validate(t, result)
  119. }
  120. })
  121. }
  122. }
  123. func TestCleanup(t *testing.T) {
  124. g := &Generator{}
  125. err := g.Cleanup(context.Background(), nil, nil, nil, "")
  126. assert.NoError(t, err)
  127. }
  128. func TestParseSpec(t *testing.T) {
  129. tests := []struct {
  130. name string
  131. data []byte
  132. expected *genv1alpha1.SSHKey
  133. wantErr bool
  134. }{
  135. {
  136. name: "valid spec",
  137. data: []byte(`{"spec":{"keyType":"rsa","keySize":2048,"comment":"test"}}`),
  138. expected: &genv1alpha1.SSHKey{
  139. Spec: genv1alpha1.SSHKeySpec{
  140. KeyType: "rsa",
  141. KeySize: func() *int { i := 2048; return &i }(),
  142. Comment: "test",
  143. },
  144. },
  145. wantErr: false,
  146. },
  147. {
  148. name: "empty spec",
  149. data: []byte(`{"spec":{}}`),
  150. wantErr: false,
  151. },
  152. }
  153. for _, tt := range tests {
  154. t.Run(tt.name, func(t *testing.T) {
  155. result, err := parseSpec(tt.data)
  156. if tt.wantErr {
  157. assert.Error(t, err)
  158. return
  159. }
  160. assert.NoError(t, err)
  161. if tt.expected != nil {
  162. assert.Equal(t, tt.expected.Spec.KeyType, result.Spec.KeyType)
  163. assert.Equal(t, tt.expected.Spec.KeySize, result.Spec.KeySize)
  164. assert.Equal(t, tt.expected.Spec.Comment, result.Spec.Comment)
  165. }
  166. })
  167. }
  168. }