client_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // /*
  14. // Licensed under the Apache License, Version 2.0 (the "License");
  15. // you may not use this file except in compliance with the License.
  16. // You may obtain a copy of the License at
  17. //
  18. // https://www.apache.org/licenses/LICENSE-2.0
  19. //
  20. // Unless required by applicable law or agreed to in writing, software
  21. // distributed under the License is distributed on an "AS IS" BASIS,
  22. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. // See the License for the specific language governing permissions and
  24. // limitations under the License.
  25. // */
  26. package github
  27. import (
  28. "context"
  29. "errors"
  30. "testing"
  31. github "github.com/google/go-github/v56/github"
  32. "github.com/stretchr/testify/assert"
  33. corev1 "k8s.io/api/core/v1"
  34. "k8s.io/utils/ptr"
  35. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  36. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  37. )
  38. type getSecretFn func(ctx context.Context, ref esv1.PushSecretRemoteRef) (*github.Secret, *github.Response, error)
  39. func withGetSecretFn(secret *github.Secret, response *github.Response, err error) getSecretFn {
  40. return func(_ context.Context, _ esv1.PushSecretRemoteRef) (*github.Secret, *github.Response, error) {
  41. return secret, response, err
  42. }
  43. }
  44. type getPublicKeyFn func(ctx context.Context) (*github.PublicKey, *github.Response, error)
  45. func withGetPublicKeyFn(key *github.PublicKey, response *github.Response, err error) getPublicKeyFn {
  46. return func(_ context.Context) (*github.PublicKey, *github.Response, error) {
  47. return key, response, err
  48. }
  49. }
  50. type createOrUpdateSecretFn func(ctx context.Context, encryptedSecret *github.EncryptedSecret) (*github.Response, error)
  51. func withCreateOrUpdateSecretFn(response *github.Response, err error) createOrUpdateSecretFn {
  52. return func(_ context.Context, _ *github.EncryptedSecret) (*github.Response, error) {
  53. return response, err
  54. }
  55. }
  56. func TestSecretExists(t *testing.T) {
  57. type testCase struct {
  58. name string
  59. prov *esv1.GithubProvider
  60. remoteRef esv1.PushSecretData
  61. getSecretFn getSecretFn
  62. wantErr error
  63. exists bool
  64. }
  65. tests := []testCase{
  66. {
  67. name: "getSecret fail",
  68. getSecretFn: withGetSecretFn(nil, nil, errors.New("boom")),
  69. exists: false,
  70. wantErr: errors.New("error fetching secret"),
  71. },
  72. {
  73. name: "no secret",
  74. getSecretFn: withGetSecretFn(nil, nil, nil),
  75. exists: false,
  76. },
  77. {
  78. name: "with secret",
  79. getSecretFn: withGetSecretFn(&github.Secret{}, nil, nil),
  80. exists: true,
  81. },
  82. }
  83. for _, test := range tests {
  84. t.Run(test.name, func(t *testing.T) {
  85. g := Client{
  86. provider: test.prov,
  87. }
  88. g.getSecretFn = test.getSecretFn
  89. ok, err := g.SecretExists(context.TODO(), test.remoteRef)
  90. assert.Equal(t, test.exists, ok)
  91. if test.wantErr == nil {
  92. assert.NoError(t, err)
  93. } else {
  94. assert.ErrorContains(t, err, test.wantErr.Error())
  95. }
  96. })
  97. }
  98. }
  99. func TestPushSecret(t *testing.T) {
  100. type testCase struct {
  101. name string
  102. prov *esv1.GithubProvider
  103. secret *corev1.Secret
  104. remoteRef esv1.PushSecretData
  105. getSecretFn getSecretFn
  106. getPublicKeyFn getPublicKeyFn
  107. createOrUpdateFn createOrUpdateSecretFn
  108. wantErr error
  109. }
  110. tests := []testCase{
  111. {
  112. name: "failGetSecretFn",
  113. getSecretFn: withGetSecretFn(nil, nil, errors.New("boom")),
  114. wantErr: errors.New("error fetching secret"),
  115. },
  116. {
  117. name: "failGetPublicKey",
  118. getSecretFn: withGetSecretFn(&github.Secret{
  119. Name: "foo",
  120. }, nil, nil),
  121. getPublicKeyFn: withGetPublicKeyFn(nil, nil, errors.New("boom")),
  122. wantErr: errors.New("error fetching public key"),
  123. },
  124. {
  125. name: "failDecodeKey",
  126. getSecretFn: withGetSecretFn(&github.Secret{
  127. Name: "foo",
  128. }, nil, nil),
  129. getPublicKeyFn: withGetPublicKeyFn(&github.PublicKey{
  130. Key: ptr.To("broken"),
  131. KeyID: ptr.To("123"),
  132. }, nil, nil),
  133. wantErr: errors.New("unable to decode public key"),
  134. },
  135. {
  136. name: "failSecretData",
  137. getSecretFn: withGetSecretFn(&github.Secret{
  138. Name: "foo",
  139. }, nil, nil),
  140. getPublicKeyFn: withGetPublicKeyFn(&github.PublicKey{
  141. Key: ptr.To("Cg=="),
  142. KeyID: ptr.To("123"),
  143. }, nil, nil),
  144. secret: &corev1.Secret{
  145. Data: map[string][]byte{
  146. "foo": []byte("bar"),
  147. },
  148. },
  149. remoteRef: esv1alpha1.PushSecretData{
  150. Match: esv1alpha1.PushSecretMatch{
  151. SecretKey: "bar",
  152. },
  153. },
  154. wantErr: errors.New("not found in secret"),
  155. },
  156. {
  157. name: "failSecretData",
  158. getSecretFn: withGetSecretFn(&github.Secret{
  159. Name: "foo",
  160. }, nil, nil),
  161. getPublicKeyFn: withGetPublicKeyFn(&github.PublicKey{
  162. Key: ptr.To("Zm9vYmFyCg=="),
  163. KeyID: ptr.To("123"),
  164. }, nil, nil),
  165. secret: &corev1.Secret{
  166. Data: map[string][]byte{
  167. "foo": []byte("bingg"),
  168. },
  169. },
  170. remoteRef: esv1alpha1.PushSecretData{
  171. Match: esv1alpha1.PushSecretMatch{
  172. SecretKey: "foo",
  173. },
  174. },
  175. createOrUpdateFn: withCreateOrUpdateSecretFn(nil, errors.New("boom")),
  176. wantErr: errors.New("failed to create secret"),
  177. },
  178. {
  179. name: "Success",
  180. getSecretFn: withGetSecretFn(&github.Secret{
  181. Name: "foo",
  182. }, nil, nil),
  183. getPublicKeyFn: withGetPublicKeyFn(&github.PublicKey{
  184. Key: ptr.To("Zm9vYmFyCg=="),
  185. KeyID: ptr.To("123"),
  186. }, nil, nil),
  187. secret: &corev1.Secret{
  188. Data: map[string][]byte{
  189. "foo": []byte("bingg"),
  190. },
  191. },
  192. remoteRef: esv1alpha1.PushSecretData{
  193. Match: esv1alpha1.PushSecretMatch{
  194. SecretKey: "foo",
  195. },
  196. },
  197. createOrUpdateFn: withCreateOrUpdateSecretFn(nil, nil),
  198. },
  199. }
  200. for _, test := range tests {
  201. t.Run(test.name, func(t *testing.T) {
  202. g := Client{
  203. provider: test.prov,
  204. }
  205. g.getSecretFn = test.getSecretFn
  206. g.getPublicKeyFn = test.getPublicKeyFn
  207. g.createOrUpdateFn = test.createOrUpdateFn
  208. err := g.PushSecret(context.TODO(), test.secret, test.remoteRef)
  209. if test.wantErr == nil {
  210. assert.NoError(t, err)
  211. } else {
  212. assert.ErrorContains(t, err, test.wantErr.Error())
  213. }
  214. })
  215. }
  216. }