validate_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 conjur
  13. import (
  14. "errors"
  15. "testing"
  16. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  17. )
  18. type ValidateStoreTestCase struct {
  19. store *esv1.SecretStore
  20. err error
  21. }
  22. func TestValidateStore(t *testing.T) {
  23. testCases := []ValidateStoreTestCase{
  24. {
  25. store: makeAPIKeySecretStore(svcURL, svcUser, svcApikey, svcAccount),
  26. err: nil,
  27. },
  28. {
  29. store: makeAPIKeySecretStore("", svcUser, svcApikey, svcAccount),
  30. err: errors.New("conjur URL cannot be empty"),
  31. },
  32. {
  33. store: makeAPIKeySecretStore(svcURL, "", svcApikey, svcAccount),
  34. err: errors.New("missing Auth.Apikey.UserRef"),
  35. },
  36. {
  37. store: makeAPIKeySecretStore(svcURL, svcUser, "", svcAccount),
  38. err: errors.New("missing Auth.Apikey.ApiKeyRef"),
  39. },
  40. {
  41. store: makeAPIKeySecretStore(svcURL, svcUser, svcApikey, ""),
  42. err: errors.New("missing Auth.ApiKey.Account"),
  43. },
  44. {
  45. store: makeJWTSecretStore(svcURL, "conjur", "", jwtAuthnService, "", "myconjuraccount"),
  46. err: nil,
  47. },
  48. {
  49. store: makeJWTSecretStore(svcURL, "", jwtSecretName, jwtAuthnService, "", "myconjuraccount"),
  50. err: nil,
  51. },
  52. {
  53. store: makeJWTSecretStore(svcURL, "conjur", "", jwtAuthnService, "", ""),
  54. err: errors.New("missing Auth.Jwt.Account"),
  55. },
  56. {
  57. store: makeJWTSecretStore(svcURL, "conjur", "", "", "", "myconjuraccount"),
  58. err: errors.New("missing Auth.Jwt.ServiceID"),
  59. },
  60. {
  61. store: makeJWTSecretStore("", "conjur", "", jwtAuthnService, "", "myconjuraccount"),
  62. err: errors.New("conjur URL cannot be empty"),
  63. },
  64. {
  65. store: makeJWTSecretStore(svcURL, "", "", jwtAuthnService, "", "myconjuraccount"),
  66. err: errors.New("must specify Auth.Jwt.SecretRef or Auth.Jwt.ServiceAccountRef"),
  67. },
  68. {
  69. store: makeNoAuthSecretStore(svcURL),
  70. err: errors.New("missing Auth.* configuration"),
  71. },
  72. }
  73. p := Provider{}
  74. for _, tc := range testCases {
  75. _, err := p.ValidateStore(tc.store)
  76. if tc.err != nil && err != nil && err.Error() != tc.err.Error() {
  77. t.Errorf("test failed! want %v, got %v", tc.err, err)
  78. } else if tc.err == nil && err != nil {
  79. t.Errorf("want nil got err %v", err)
  80. } else if tc.err != nil && err == nil {
  81. t.Errorf("want err %v got nil", tc.err)
  82. }
  83. }
  84. }