utils.go 3.4 KB

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