device42_api.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 is the error format string for HTTP request failures.
  28. DoRequestError = "error: do request: %w"
  29. errJSONSecretUnmarshal = "unable to unmarshal secret from JSON: %w"
  30. )
  31. // HTTPClient is the interface for making HTTP requests.
  32. type HTTPClient interface {
  33. Do(*http.Request) (*http.Response, error)
  34. }
  35. // API implements the Device42 REST API client.
  36. type API struct {
  37. client HTTPClient
  38. baseURL string
  39. hostPort string
  40. password string
  41. username string
  42. }
  43. // D42PasswordResponse represents the response from Device42 passwords API.
  44. type D42PasswordResponse struct {
  45. Passwords []D42Password
  46. }
  47. // D42Password represents a password entry in Device42.
  48. type D42Password struct {
  49. Password string `json:"password"`
  50. ID int `json:"id"`
  51. }
  52. // NewAPI creates a new Device42 API client.
  53. func NewAPI(baseURL, username, password, hostPort string) *API {
  54. tr := &http.Transport{
  55. TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
  56. }
  57. api := &API{
  58. baseURL: baseURL,
  59. hostPort: hostPort,
  60. username: username,
  61. password: password,
  62. client: &http.Client{Transport: tr},
  63. }
  64. return api
  65. }
  66. func (api *API) doAuthenticatedRequest(r *http.Request) (*http.Response, error) {
  67. r.SetBasicAuth(api.username, api.password)
  68. return api.client.Do(r)
  69. }
  70. // ReadAndUnmarshal reads an HTTP response body and unmarshals it into the target structure.
  71. func ReadAndUnmarshal(resp *http.Response, target any) error {
  72. var buf bytes.Buffer
  73. defer func() {
  74. err := resp.Body.Close()
  75. if err != nil {
  76. return
  77. }
  78. }()
  79. if resp.StatusCode < 200 || resp.StatusCode > 299 {
  80. return fmt.Errorf("failed to authenticate with the given credentials: %d %s", resp.StatusCode, buf.String())
  81. }
  82. _, err := buf.ReadFrom(resp.Body)
  83. if err != nil {
  84. return err
  85. }
  86. return json.Unmarshal(buf.Bytes(), target)
  87. }
  88. // GetSecret retrieves a password from Device42.
  89. func (api *API) GetSecret(secretID string) (D42Password, error) {
  90. // https://api.device42.com/#!/Passwords/getPassword
  91. endpointURL := fmt.Sprintf("https://%s:%s/api/1.0/passwords/?id=%s&plain_text=yes", api.baseURL, api.hostPort, secretID)
  92. ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
  93. defer cancel()
  94. readSecretRequest, err := http.NewRequestWithContext(ctx, "GET", endpointURL, http.NoBody)
  95. if err != nil {
  96. return D42Password{}, fmt.Errorf("error: creating secrets request: %w", err)
  97. }
  98. respSecretRead, err := api.doAuthenticatedRequest(readSecretRequest) //nolint:bodyclose // linters bug
  99. if err != nil {
  100. return D42Password{}, fmt.Errorf(DoRequestError, err)
  101. }
  102. d42PasswordResponse := D42PasswordResponse{}
  103. err = ReadAndUnmarshal(respSecretRead, &d42PasswordResponse)
  104. if err != nil {
  105. return D42Password{}, fmt.Errorf(errJSONSecretUnmarshal, err)
  106. }
  107. if len(d42PasswordResponse.Passwords) == 0 {
  108. return D42Password{}, err
  109. }
  110. // There should only be one response
  111. return d42PasswordResponse.Passwords[0], err
  112. }
  113. // GetSecretMap returns a map of secret values from Device42.
  114. func (api *API) GetSecretMap(_ context.Context, _ esv1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  115. return nil, errors.New(errNotImplemented)
  116. }
  117. // ToMap converts a D42Password to a map of secret values.
  118. func (s D42Password) ToMap() map[string][]byte {
  119. m := make(map[string][]byte)
  120. m["password"] = []byte(s.Password)
  121. m["id"] = []byte(strconv.Itoa(s.ID))
  122. return m
  123. }