api_fake.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 api
  14. import (
  15. "context"
  16. "crypto/x509"
  17. "encoding/json"
  18. "encoding/pem"
  19. "net/http"
  20. "net/http/httptest"
  21. "net/url"
  22. infisical "github.com/infisical/go-sdk"
  23. infisicalSdk "github.com/infisical/go-sdk"
  24. )
  25. func newMockServer(status int, data any) *httptest.Server {
  26. body, err := json.Marshal(data)
  27. if err != nil {
  28. panic(err)
  29. }
  30. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  31. w.Header().Set("Content-Type", "application/json")
  32. w.WriteHeader(status)
  33. _, err := w.Write(body)
  34. if err != nil {
  35. panic(err)
  36. }
  37. }))
  38. }
  39. // NewMockClient creates an InfisicalClient with a mocked HTTP client that has a
  40. // fixed response.
  41. func NewMockClient(status int, data any) (infisicalSdk.InfisicalClientInterface, func()) {
  42. server := newMockServer(status, data)
  43. caCert := server.Certificate()
  44. infisicalConfig := infisicalSdk.Config{
  45. SiteUrl: server.URL,
  46. }
  47. if caCert != nil {
  48. infisicalConfig.CaCertificate = string(pem.EncodeToMemory(&pem.Block{
  49. Type: "CERTIFICATE",
  50. Bytes: caCert.Raw,
  51. }))
  52. }
  53. ctx, cancel := context.WithCancel(context.Background())
  54. infisicalSdk := infisicalSdk.NewInfisicalClient(ctx, infisicalConfig)
  55. closeFunc := func() {
  56. cancel()
  57. server.Close()
  58. }
  59. return infisicalSdk, closeFunc
  60. }
  61. // NewAPIClient creates a new Infisical API client with the specified base URL and optional certificate.
  62. func NewAPIClient(baseURL string, certificate *x509.Certificate) (infisicalSdk.InfisicalClientInterface, context.CancelFunc, error) {
  63. baseParsedURL, err := url.Parse(baseURL)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. infisicalConfig := infisical.Config{
  68. SiteUrl: baseParsedURL.String(),
  69. }
  70. if certificate != nil {
  71. infisicalConfig.CaCertificate = string(pem.EncodeToMemory(&pem.Block{
  72. Type: "CERTIFICATE",
  73. Bytes: certificate.Raw,
  74. }))
  75. }
  76. ctx, cancel := context.WithCancel(context.Background())
  77. infisicalSdk := infisicalSdk.NewInfisicalClient(ctx, infisicalConfig)
  78. return infisicalSdk, cancel, nil
  79. }