utils.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. errInvalidAkeylessURL = "invalid akeyless GW API URL"
  30. errInvalidAkeylessAccessIDName = "missing akeyless accessID name"
  31. errInvalidAkeylessAccessIDKey = "missing akeyless accessID key"
  32. )
  33. // GetAKeylessProvider does the necessary nil checks and returns the akeyless provider or an error.
  34. func GetAKeylessProvider(store esv1beta1.GenericStore) (*esv1beta1.AkeylessProvider, error) {
  35. if store == nil {
  36. return nil, fmt.Errorf(errNilStore)
  37. }
  38. spc := store.GetSpec()
  39. if spc == nil {
  40. return nil, fmt.Errorf(errMissingStoreSpec)
  41. }
  42. if spc.Provider == nil {
  43. return nil, fmt.Errorf(errMissingProvider)
  44. }
  45. prov := spc.Provider.Akeyless
  46. if prov == nil {
  47. return nil, fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  48. }
  49. return prov, nil
  50. }
  51. func getV2Url(path string) string {
  52. // add check if not v2
  53. rebody := sendReq(path)
  54. if strings.Contains(rebody, "unknown command") {
  55. return path
  56. }
  57. if strings.HasSuffix(path, "/v2") {
  58. return path
  59. }
  60. url, err := url.Parse(path)
  61. if err != nil {
  62. return path
  63. }
  64. if strings.HasSuffix(url.Host, "/v2") {
  65. return path
  66. }
  67. url.Host += "/v2"
  68. p := url.Scheme + "://" + url.Host
  69. if url.Port() != "" {
  70. p = p + ":" + url.Port()
  71. }
  72. return p
  73. }
  74. func sendReq(url string) string {
  75. req, err := http.NewRequest("POST", url, http.NoBody)
  76. if err != nil {
  77. return ""
  78. }
  79. req.Header.Set("Content-Type", "application/json")
  80. client := &http.Client{
  81. Timeout: 10 * time.Second,
  82. }
  83. resp, err := client.Do(req)
  84. if err != nil {
  85. return ""
  86. }
  87. defer resp.Body.Close()
  88. body, _ := io.ReadAll(resp.Body)
  89. return string(body)
  90. }