provider_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 aws
  13. import (
  14. "context"
  15. "testing"
  16. "github.com/aws/aws-sdk-go/aws"
  17. "github.com/stretchr/testify/assert"
  18. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  19. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  21. "github.com/external-secrets/external-secrets/pkg/provider/aws/parameterstore"
  22. "github.com/external-secrets/external-secrets/pkg/provider/aws/secretsmanager"
  23. )
  24. func TestProvider(t *testing.T) {
  25. cl := clientfake.NewClientBuilder().Build()
  26. p := Provider{}
  27. tbl := []struct {
  28. test string
  29. store esv1alpha1.GenericStore
  30. expType interface{}
  31. expErr bool
  32. }{
  33. {
  34. test: "should not create provider due to nil store",
  35. store: nil,
  36. expErr: true,
  37. },
  38. {
  39. test: "should not create provider due to missing provider",
  40. expErr: true,
  41. store: &esv1alpha1.SecretStore{
  42. Spec: esv1alpha1.SecretStoreSpec{},
  43. },
  44. },
  45. {
  46. test: "should not create provider due to missing provider field",
  47. expErr: true,
  48. store: &esv1alpha1.SecretStore{
  49. Spec: esv1alpha1.SecretStoreSpec{
  50. Provider: &esv1alpha1.SecretStoreProvider{},
  51. },
  52. },
  53. },
  54. {
  55. test: "should create parameter store client",
  56. expErr: false,
  57. expType: &parameterstore.ParameterStore{},
  58. store: &esv1alpha1.SecretStore{
  59. Spec: esv1alpha1.SecretStoreSpec{
  60. Provider: &esv1alpha1.SecretStoreProvider{
  61. AWS: &esv1alpha1.AWSProvider{
  62. Service: esv1alpha1.AWSServiceParameterStore,
  63. },
  64. },
  65. },
  66. },
  67. },
  68. {
  69. test: "should create secretsmanager client",
  70. expErr: false,
  71. expType: &secretsmanager.SecretsManager{},
  72. store: &esv1alpha1.SecretStore{
  73. Spec: esv1alpha1.SecretStoreSpec{
  74. Provider: &esv1alpha1.SecretStoreProvider{
  75. AWS: &esv1alpha1.AWSProvider{
  76. Service: esv1alpha1.AWSServiceSecretsManager,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. {
  83. test: "invalid service should return an error",
  84. expErr: true,
  85. store: &esv1alpha1.SecretStore{
  86. Spec: esv1alpha1.SecretStoreSpec{
  87. Provider: &esv1alpha1.SecretStoreProvider{
  88. AWS: &esv1alpha1.AWSProvider{
  89. Service: "HIHIHIHHEHEHEHEHEHE",
  90. },
  91. },
  92. },
  93. },
  94. },
  95. {
  96. test: "newSession error should be returned",
  97. expErr: true,
  98. store: &esv1alpha1.SecretStore{
  99. Spec: esv1alpha1.SecretStoreSpec{
  100. Provider: &esv1alpha1.SecretStoreProvider{
  101. AWS: &esv1alpha1.AWSProvider{
  102. Service: esv1alpha1.AWSServiceParameterStore,
  103. Auth: esv1alpha1.AWSAuth{
  104. SecretRef: &esv1alpha1.AWSAuthSecretRef{
  105. AccessKeyID: esmeta.SecretKeySelector{
  106. Name: "foo",
  107. Namespace: aws.String("NOOP"),
  108. },
  109. },
  110. },
  111. },
  112. },
  113. },
  114. },
  115. },
  116. }
  117. for i := range tbl {
  118. row := tbl[i]
  119. t.Run(row.test, func(t *testing.T) {
  120. sc, err := p.NewClient(context.TODO(), row.store, cl, "foo")
  121. if row.expErr {
  122. assert.Error(t, err)
  123. assert.Nil(t, sc)
  124. } else {
  125. assert.Nil(t, err)
  126. assert.NotNil(t, sc)
  127. assert.IsType(t, row.expType, sc)
  128. }
  129. })
  130. }
  131. }