device42_api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 device42
  14. import (
  15. "bytes"
  16. "context"
  17. "crypto/tls"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "net/http"
  22. "strconv"
  23. "time"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. )
  26. const (
  27. DoRequestError = "error: do request: %w"
  28. errJSONSecretUnmarshal = "unable to unmarshal secret from JSON: %w"
  29. )
  30. type HTTPClient interface {
  31. Do(*http.Request) (*http.Response, error)
  32. }
  33. type API struct {
  34. client HTTPClient
  35. baseURL string
  36. hostPort string
  37. password string
  38. username string
  39. }
  40. type D42PasswordResponse struct {
  41. Passwords []D42Password
  42. }
  43. type D42Password struct {
  44. Password string `json:"password"`
  45. ID int `json:"id"`
  46. }
  47. func NewAPI(baseURL, username, password, hostPort string) *API {
  48. api := &API{
  49. baseURL: baseURL,
  50. hostPort: hostPort,
  51. username: username,
  52. password: password,
  53. }
  54. tr := &http.Transport{
  55. TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
  56. }
  57. api.client = &http.Client{Transport: tr}
  58. return api
  59. }
  60. func (api *API) doAuthenticatedRequest(r *http.Request) (*http.Response, error) {
  61. r.SetBasicAuth(api.username, api.password)
  62. return api.client.Do(r)
  63. }
  64. func ReadAndUnmarshal(resp *http.Response, target any) error {
  65. var buf bytes.Buffer
  66. defer func() {
  67. err := resp.Body.Close()
  68. if err != nil {
  69. return
  70. }
  71. }()
  72. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  73. return fmt.Errorf("failed to authenticate with the given credentials: %d %s", resp.StatusCode, buf.String())
  74. }
  75. _, err := buf.ReadFrom(resp.Body)
  76. if err != nil {
  77. return err
  78. }
  79. return json.Unmarshal(buf.Bytes(), target)
  80. }
  81. func (api *API) GetSecret(secretID string) (D42Password, error) {
  82. // https://api.device42.com/#!/Passwords/getPassword
  83. endpointURL := fmt.Sprintf("https://%s:%s/api/1.0/passwords/?id=%s&plain_text=yes", api.baseURL, api.hostPort, secretID)
  84. ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  85. defer cancel()
  86. readSecretRequest, err := http.NewRequestWithContext(ctx, "GET", endpointURL, http.NoBody)
  87. if err != nil {
  88. return D42Password{}, fmt.Errorf("error: creating secrets request: %w", err)
  89. }
  90. respSecretRead, err := api.doAuthenticatedRequest(readSecretRequest) //nolint:bodyclose // linters bug
  91. if err != nil {
  92. return D42Password{}, fmt.Errorf(DoRequestError, err)
  93. }
  94. d42PasswordResponse := D42PasswordResponse{}
  95. err = ReadAndUnmarshal(respSecretRead, &d42PasswordResponse)
  96. if err != nil {
  97. return D42Password{}, fmt.Errorf(errJSONSecretUnmarshal, err)
  98. }
  99. if len(d42PasswordResponse.Passwords) == 0 {
  100. return D42Password{}, err
  101. }
  102. // There should only be one response
  103. return d42PasswordResponse.Passwords[0], err
  104. }
  105. func (api *API) GetSecretMap(_ context.Context, _ esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  106. return nil, errors.New(errNotImplemented)
  107. }
  108. func (s D42Password) ToMap() map[string][]byte {
  109. m := make(map[string][]byte)
  110. m["password"] = []byte(s.Password)
  111. m["id"] = []byte(strconv.Itoa(s.ID))
  112. return m
  113. }