utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 akeyless
  13. import (
  14. "fmt"
  15. "io"
  16. "net/http"
  17. "net/url"
  18. "strings"
  19. "time"
  20. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  21. )
  22. const (
  23. errNilStore = "found nil store"
  24. errMissingStoreSpec = "store is missing spec"
  25. errMissingProvider = "storeSpec is missing provider"
  26. errInvalidProvider = "invalid provider spec. Missing Akeyless field in store %s"
  27. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  28. errUninitalizedAkeylessProvider = "provider akeyless is not initialized"
  29. )
  30. // GetAKeylessProvider does the necessary nil checks and returns the akeyless provider or an error.
  31. func GetAKeylessProvider(store esv1beta1.GenericStore) (*esv1beta1.AkeylessProvider, error) {
  32. if store == nil {
  33. return nil, fmt.Errorf(errNilStore)
  34. }
  35. spc := store.GetSpec()
  36. if spc == nil {
  37. return nil, fmt.Errorf(errMissingStoreSpec)
  38. }
  39. if spc.Provider == nil {
  40. return nil, fmt.Errorf(errMissingProvider)
  41. }
  42. prov := spc.Provider.Akeyless
  43. if prov == nil {
  44. return nil, fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  45. }
  46. return prov, nil
  47. }
  48. func getV2Url(path string) string {
  49. // add check if not v2
  50. rebody := sendReq(path)
  51. if strings.Contains(rebody, "unknown command") {
  52. return path
  53. }
  54. if strings.HasSuffix(path, "/v2") {
  55. return path
  56. }
  57. url, err := url.Parse(path)
  58. if err != nil {
  59. return path
  60. }
  61. if strings.HasSuffix(url.Host, "/v2") {
  62. return path
  63. }
  64. url.Host += "/v2"
  65. p := url.Scheme + "://" + url.Host
  66. if url.Port() != "" {
  67. p = p + ":" + url.Port()
  68. }
  69. return p
  70. }
  71. func sendReq(url string) string {
  72. req, err := http.NewRequest("POST", url, http.NoBody)
  73. if err != nil {
  74. return ""
  75. }
  76. req.Header.Set("Content-Type", "application/json")
  77. client := &http.Client{
  78. Timeout: 10 * time.Second,
  79. }
  80. resp, err := client.Do(req)
  81. if err != nil {
  82. return ""
  83. }
  84. defer resp.Body.Close()
  85. body, _ := io.ReadAll(resp.Body)
  86. return string(body)
  87. }