sts_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 sts
  14. import (
  15. "context"
  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/sts"
  22. ststypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
  23. v1 "k8s.io/api/core/v1"
  24. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "sigs.k8s.io/controller-runtime/pkg/client"
  27. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  28. "github.com/external-secrets/external-secrets/pkg/utils"
  29. )
  30. func TestGenerate(t *testing.T) {
  31. type args struct {
  32. ctx context.Context
  33. jsonSpec *apiextensions.JSON
  34. kube client.Client
  35. namespace string
  36. tokenFunc func(context.Context, *sts.GetSessionTokenInput, ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error)
  37. }
  38. tests := []struct {
  39. name string
  40. g *Generator
  41. args args
  42. want map[string][]byte
  43. wantErr bool
  44. }{
  45. {
  46. name: "nil spec",
  47. args: args{
  48. ctx: context.Background(),
  49. jsonSpec: nil,
  50. },
  51. wantErr: true,
  52. },
  53. {
  54. name: "invalid json",
  55. args: args{
  56. ctx: context.Background(),
  57. tokenFunc: func(ctx context.Context, input *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
  58. return nil, errors.New("boom")
  59. },
  60. jsonSpec: &apiextensions.JSON{
  61. Raw: []byte(``),
  62. },
  63. },
  64. wantErr: true,
  65. },
  66. {
  67. name: "full spec",
  68. args: args{
  69. ctx: context.Background(),
  70. namespace: "foobar",
  71. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  72. ObjectMeta: metav1.ObjectMeta{
  73. Name: "my-aws-creds",
  74. Namespace: "foobar",
  75. },
  76. Data: map[string][]byte{
  77. "key-id": []byte("foo"),
  78. "access-secret": []byte("bar"),
  79. },
  80. }).Build(),
  81. tokenFunc: func(ctx context.Context, input *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
  82. t := time.Unix(1234, 0)
  83. return &sts.GetSessionTokenOutput{
  84. Credentials: &ststypes.Credentials{
  85. AccessKeyId: utils.Ptr("access-key-id"),
  86. Expiration: utils.Ptr(t),
  87. SecretAccessKey: utils.Ptr("secret-access-key"),
  88. SessionToken: utils.Ptr("session-token"),
  89. },
  90. }, nil
  91. },
  92. jsonSpec: &apiextensions.JSON{
  93. Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
  94. kind: STSSessionToken
  95. spec:
  96. region: eu-west-1
  97. role: "my-role"
  98. auth:
  99. secretRef:
  100. accessKeyIDSecretRef:
  101. name: "my-aws-creds"
  102. key: "key-id"
  103. secretAccessKeySecretRef:
  104. name: "my-aws-creds"
  105. key: "access-secret"`),
  106. },
  107. },
  108. want: map[string][]byte{
  109. "access_key_id": []byte("access-key-id"),
  110. "expiration": []byte("1234"),
  111. "secret_access_key": []byte("secret-access-key"),
  112. "session_token": []byte("session-token"),
  113. },
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. g := &Generator{}
  119. got, _, err := g.generate(
  120. tt.args.ctx,
  121. tt.args.jsonSpec,
  122. tt.args.kube,
  123. tt.args.namespace,
  124. func(cfg *aws.Config) stsAPI {
  125. return &FakeSTS{
  126. getSessionToken: tt.args.tokenFunc,
  127. }
  128. },
  129. )
  130. if (err != nil) != tt.wantErr {
  131. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  132. return
  133. }
  134. if !reflect.DeepEqual(got, tt.want) {
  135. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  136. }
  137. })
  138. }
  139. }
  140. type FakeSTS struct {
  141. getSessionToken func(context.Context, *sts.GetSessionTokenInput, ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error)
  142. }
  143. func (e *FakeSTS) GetSessionToken(ctx context.Context, params *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
  144. return e.getSessionToken(ctx, params, optFns...)
  145. }