utils.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 akeyless
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "net/url"
  20. "strings"
  21. "time"
  22. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  23. )
  24. const (
  25. errNilStore = "found nil store"
  26. errMissingStoreSpec = "store is missing spec"
  27. errMissingProvider = "storeSpec is missing provider"
  28. errInvalidProvider = "invalid provider spec. Missing Akeyless field in store %s"
  29. errJSONSecretUnmarshal = "unable to unmarshal secret from JSON: %w"
  30. errUninitalizedAkeylessProvider = "provider akeyless is not initialized"
  31. errInvalidAkeylessURL = "invalid akeyless GW API URL"
  32. errInvalidAkeylessAccessIDName = "missing akeyless accessID name"
  33. errInvalidAkeylessAccessIDKey = "missing akeyless accessID key"
  34. errGetKubeSecret = "cannot get Kubernetes secret %q: %w"
  35. errSecretKeyFmt = "cannot find secret data for key: %q"
  36. errGetKubeSA = "cannot get Kubernetes service account %q: %w"
  37. errGetKubeSASecrets = "cannot find secrets bound to service account: %q"
  38. errGetKubeSANoToken = "cannot find token in secrets bound to service account: %q"
  39. errGetKubeSATokenRequest = "cannot request Kubernetes service account token for service account %q: %w"
  40. errInvalidKubeSA = "invalid Auth.Kubernetes.ServiceAccountRef: %w"
  41. )
  42. // GetAKeylessProvider does the necessary nil checks and returns the akeyless provider or an error.
  43. func GetAKeylessProvider(store esv1.GenericStore) (*esv1.AkeylessProvider, error) {
  44. if store == nil {
  45. return nil, errors.New(errNilStore)
  46. }
  47. spc := store.GetSpec()
  48. if spc == nil {
  49. return nil, errors.New(errMissingStoreSpec)
  50. }
  51. if spc.Provider == nil {
  52. return nil, errors.New(errMissingStoreSpec)
  53. }
  54. prov := spc.Provider.Akeyless
  55. if prov == nil {
  56. return nil, fmt.Errorf(errInvalidProvider, store.GetObjectMeta().String())
  57. }
  58. return prov, nil
  59. }
  60. func getV2Url(path string) string {
  61. // add check if not v2
  62. rebody := sendReq(path)
  63. if strings.Contains(rebody, "unknown command") {
  64. return path
  65. }
  66. if strings.HasSuffix(path, "/v2") {
  67. return path
  68. }
  69. url, err := url.Parse(path)
  70. if err != nil {
  71. return path
  72. }
  73. if strings.HasSuffix(url.Host, "/v2") {
  74. return path
  75. }
  76. url.Host += "/v2"
  77. p := url.Scheme + "://" + url.Host
  78. if url.Port() != "" {
  79. p = p + ":" + url.Port()
  80. }
  81. return p
  82. }
  83. func sendReq(url string) string {
  84. req, err := http.NewRequest("POST", url, http.NoBody)
  85. if err != nil {
  86. return ""
  87. }
  88. req.Header.Set("Content-Type", "application/json")
  89. client := &http.Client{
  90. Timeout: 10 * time.Second,
  91. }
  92. resp, err := client.Do(req)
  93. if err != nil {
  94. return ""
  95. }
  96. defer func() {
  97. _ = resp.Body.Close()
  98. }()
  99. body, _ := io.ReadAll(resp.Body)
  100. return string(body)
  101. }