ecr_test.go 5.8 KB

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