sts_test.go 4.3 KB

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