utils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. errGetKubeSecret = "cannot get Kubernetes secret %q: %w"
  33. errSecretKeyFmt = "cannot find secret data for key: %q"
  34. errGetKubeSA = "cannot get Kubernetes service account %q: %w"
  35. errGetKubeSASecrets = "cannot find secrets bound to service account: %q"
  36. errGetKubeSANoToken = "cannot find token in secrets bound to service account: %q"
  37. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  38. errInvalidKubeSA = "invalid Auth.Kubernetes.ServiceAccountRef: %w"
  39. )
  40. // GetAKeylessProvider does the necessary nil checks and returns the akeyless provider or an error.
  41. func GetAKeylessProvider(store esv1beta1.GenericStore) (*esv1beta1.AkeylessProvider, error) {
  42. if store == nil {
  43. return nil, fmt.Errorf(errNilStore)
  44. }
  45. spc := store.GetSpec()
  46. if spc == nil {
  47. return nil, fmt.Errorf(errMissingStoreSpec)
  48. }
  49. if spc.Provider == nil {
  50. return nil, fmt.Errorf(errMissingProvider)
  51. }
  52. prov := spc.Provider.Akeyless
  53. if prov == nil {
  54. return nil, fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  55. }
  56. return prov, nil
  57. }
  58. func getV2Url(path string) string {
  59. // add check if not v2
  60. rebody := sendReq(path)
  61. if strings.Contains(rebody, "unknown command") {
  62. return path
  63. }
  64. if strings.HasSuffix(path, "/v2") {
  65. return path
  66. }
  67. url, err := url.Parse(path)
  68. if err != nil {
  69. return path
  70. }
  71. if strings.HasSuffix(url.Host, "/v2") {
  72. return path
  73. }
  74. url.Host += "/v2"
  75. p := url.Scheme + "://" + url.Host
  76. if url.Port() != "" {
  77. p = p + ":" + url.Port()
  78. }
  79. return p
  80. }
  81. func sendReq(url string) string {
  82. req, err := http.NewRequest("POST", url, http.NoBody)
  83. if err != nil {
  84. return ""
  85. }
  86. req.Header.Set("Content-Type", "application/json")
  87. client := &http.Client{
  88. Timeout: 10 * time.Second,
  89. }
  90. resp, err := client.Do(req)
  91. if err != nil {
  92. return ""
  93. }
  94. defer resp.Body.Close()
  95. body, _ := io.ReadAll(resp.Body)
  96. return string(body)
  97. }