fake.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright © The ESO Authors
  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 fake
  14. import (
  15. "errors"
  16. "fmt"
  17. "math/rand"
  18. "github.com/cyberark/conjur-api-go/conjurapi"
  19. )
  20. type ConjurMockClient struct {
  21. }
  22. func (mc *ConjurMockClient) RetrieveSecret(secret string) (result []byte, err error) {
  23. if secret == "error" {
  24. err = errors.New("error")
  25. return nil, err
  26. }
  27. if secret == "json_map" {
  28. return []byte(`{"key1":"value1","key2":"value2"}`), nil
  29. }
  30. if secret == "json_nested" {
  31. return []byte(`{"key1":"value1","key2":{"key3":"value3","key4":"value4"}}`), nil
  32. }
  33. return []byte("secret"), nil
  34. }
  35. func (mc *ConjurMockClient) RetrieveBatchSecrets(variableIDs []string) (map[string][]byte, error) {
  36. secrets := make(map[string][]byte)
  37. for _, id := range variableIDs {
  38. if id == "error" {
  39. return nil, errors.New("error")
  40. }
  41. fullID := fmt.Sprintf("conjur:variable:%s", id)
  42. secrets[fullID] = []byte("secret")
  43. }
  44. return secrets, nil
  45. }
  46. func (mc *ConjurMockClient) Resources(filter *conjurapi.ResourceFilter) (resources []map[string]any, err error) {
  47. policyID := "conjur:policy:root"
  48. if filter.Offset == 0 {
  49. // First "page" of secrets: 2 static ones and 98 random ones
  50. secrets := []map[string]any{ //nolint:prealloc // static init + dynamic append
  51. {
  52. "id": "conjur:variable:secret1",
  53. "annotations": []any{
  54. map[string]any{
  55. "name": "conjur/kind",
  56. "value": "dummy",
  57. },
  58. },
  59. },
  60. {
  61. "id": "conjur:variable:secret2",
  62. "owner": "conjur:policy:admin1",
  63. "annotations": []any{
  64. map[string]any{
  65. "name": "Description",
  66. "policy": policyID,
  67. "value": "Lorem ipsum dolor sit amet",
  68. },
  69. map[string]any{
  70. "name": "conjur/kind",
  71. "policy": policyID,
  72. "value": "password",
  73. },
  74. },
  75. "permissions": map[string]string{
  76. "policy": policyID,
  77. "privilege": "update",
  78. "role": "conjur:group:admins",
  79. },
  80. "policy": policyID,
  81. },
  82. }
  83. // Add 98 random secrets so we can simulate a full "page" of 100 secrets
  84. secrets = append(secrets, generateRandomSecrets(98)...)
  85. return secrets, nil
  86. } else if filter.Offset == 100 {
  87. // Second "page" of secrets: 100 random ones
  88. return generateRandomSecrets(100), nil
  89. }
  90. // Add 50 random secrets so we can simulate a partial "page" of 50 secrets
  91. return generateRandomSecrets(50), nil
  92. }
  93. func generateRandomSecrets(count int) []map[string]any {
  94. secrets := make([]map[string]any, 0, count)
  95. for range count {
  96. //nolint:gosec
  97. randomNumber := rand.Intn(10000)
  98. secrets = append(secrets, generateRandomSecret(randomNumber))
  99. }
  100. return secrets
  101. }
  102. func generateRandomSecret(num int) map[string]any {
  103. return map[string]any{
  104. "id": fmt.Sprintf("conjur:variable:random/var_%d", num),
  105. "annotations": []map[string]any{
  106. {
  107. "name": "random_number",
  108. "value": fmt.Sprintf("%d", num),
  109. },
  110. },
  111. "policy": "conjur:policy:random",
  112. }
  113. }