ecr_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. package ecr
  14. import (
  15. "context"
  16. "encoding/base64"
  17. "errors"
  18. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/aws/aws-sdk-go-v2/aws"
  22. "github.com/aws/aws-sdk-go-v2/service/ecr"
  23. ecrtypes "github.com/aws/aws-sdk-go-v2/service/ecr/types"
  24. "github.com/aws/aws-sdk-go-v2/service/ecrpublic"
  25. ecrpublictypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types"
  26. v1 "k8s.io/api/core/v1"
  27. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  28. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  29. utilpointer "k8s.io/utils/ptr"
  30. "sigs.k8s.io/controller-runtime/pkg/client"
  31. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  32. )
  33. func TestGenerate(t *testing.T) {
  34. type args struct {
  35. ctx context.Context
  36. jsonSpec *apiextensions.JSON
  37. kube client.Client
  38. namespace string
  39. authTokenPrivateFunc func(*ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
  40. authTokenPublicFunc func(*ecrpublic.GetAuthorizationTokenInput) (*ecrpublic.GetAuthorizationTokenOutput, error)
  41. }
  42. tests := []struct {
  43. name string
  44. g *Generator
  45. args args
  46. want map[string][]byte
  47. wantErr bool
  48. }{
  49. {
  50. name: "nil spec",
  51. args: args{
  52. ctx: context.Background(),
  53. jsonSpec: nil,
  54. },
  55. wantErr: true,
  56. },
  57. {
  58. name: "invalid json",
  59. args: args{
  60. ctx: context.Background(),
  61. authTokenPrivateFunc: func(gati *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
  62. return nil, errors.New("boom")
  63. },
  64. jsonSpec: &apiextensions.JSON{
  65. Raw: []byte(``),
  66. },
  67. },
  68. wantErr: true,
  69. },
  70. {
  71. name: "private ECR full spec",
  72. args: args{
  73. ctx: context.Background(),
  74. namespace: "foobar",
  75. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: "my-aws-creds",
  78. Namespace: "foobar",
  79. },
  80. Data: map[string][]byte{
  81. "key-id": []byte("foo"),
  82. "access-secret": []byte("bar"),
  83. },
  84. }).Build(),
  85. authTokenPrivateFunc: func(in *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error) {
  86. t := time.Unix(1234, 0)
  87. return &ecr.GetAuthorizationTokenOutput{
  88. AuthorizationData: []ecrtypes.AuthorizationData{
  89. {
  90. AuthorizationToken: utilpointer.To(base64.StdEncoding.EncodeToString([]byte("uuser:pass"))),
  91. ProxyEndpoint: utilpointer.To("foo"),
  92. ExpiresAt: &t,
  93. },
  94. },
  95. }, nil
  96. },
  97. jsonSpec: &apiextensions.JSON{
  98. Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
  99. kind: ECRAuthorizationToken
  100. spec:
  101. region: eu-west-1
  102. role: "my-role"
  103. scope: private
  104. auth:
  105. secretRef:
  106. accessKeyIDSecretRef:
  107. name: "my-aws-creds"
  108. key: "key-id"
  109. secretAccessKeySecretRef:
  110. name: "my-aws-creds"
  111. key: "access-secret"`),
  112. },
  113. },
  114. want: map[string][]byte{
  115. "username": []byte("uuser"),
  116. "password": []byte("pass"),
  117. "proxy_endpoint": []byte("foo"),
  118. "expires_at": []byte("1234"),
  119. },
  120. },
  121. {
  122. name: "public ECR full spec",
  123. args: args{
  124. ctx: context.Background(),
  125. namespace: "foobar",
  126. authTokenPublicFunc: func(in *ecrpublic.GetAuthorizationTokenInput) (*ecrpublic.GetAuthorizationTokenOutput, error) {
  127. t := time.Unix(5678, 0)
  128. return &ecrpublic.GetAuthorizationTokenOutput{
  129. AuthorizationData: &ecrpublictypes.AuthorizationData{
  130. AuthorizationToken: utilpointer.To(base64.StdEncoding.EncodeToString([]byte("pubuser:pubpass"))),
  131. ExpiresAt: &t,
  132. },
  133. }, nil
  134. },
  135. jsonSpec: &apiextensions.JSON{
  136. Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
  137. kind: ECRAuthorizationToken
  138. spec:
  139. region: us-east-1
  140. role: "my-role"
  141. scope: public`),
  142. },
  143. },
  144. want: map[string][]byte{
  145. "username": []byte("pubuser"),
  146. "password": []byte("pubpass"),
  147. "expires_at": []byte("5678"),
  148. },
  149. },
  150. }
  151. for _, tt := range tests {
  152. t.Run(tt.name, func(t *testing.T) {
  153. g := &Generator{}
  154. got, _, err := g.generate(
  155. tt.args.ctx,
  156. tt.args.jsonSpec,
  157. tt.args.kube,
  158. tt.args.namespace,
  159. func(cfg *aws.Config) ecrAPI {
  160. return &FakeECRPrivate{
  161. authTokenFunc: tt.args.authTokenPrivateFunc,
  162. }
  163. },
  164. func(cfg *aws.Config) ecrPublicAPI {
  165. return &FakeECRPublic{
  166. authTokenFunc: tt.args.authTokenPublicFunc,
  167. }
  168. },
  169. )
  170. if (err != nil) != tt.wantErr {
  171. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  172. return
  173. }
  174. if !reflect.DeepEqual(got, tt.want) {
  175. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  176. }
  177. })
  178. }
  179. }
  180. type FakeECRPrivate struct {
  181. authTokenFunc func(*ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
  182. }
  183. func (e *FakeECRPrivate) GetAuthorizationToken(ctx context.Context, params *ecr.GetAuthorizationTokenInput, optFns ...func(*ecr.Options)) (*ecr.GetAuthorizationTokenOutput, error) {
  184. return e.authTokenFunc(params)
  185. }
  186. type FakeECRPublic struct {
  187. authTokenFunc func(*ecrpublic.GetAuthorizationTokenInput) (*ecrpublic.GetAuthorizationTokenOutput, error)
  188. }
  189. func (e *FakeECRPublic) GetAuthorizationToken(ctx context.Context, params *ecrpublic.GetAuthorizationTokenInput, optFns ...func(*ecrpublic.Options)) (*ecrpublic.GetAuthorizationTokenOutput, error) {
  190. return e.authTokenFunc(params)
  191. }