fake.go 3.3 KB

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