openbao.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 addon
  14. import (
  15. "crypto/rand"
  16. "fmt"
  17. "path/filepath"
  18. . "github.com/onsi/ginkgo/v2"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  22. "github.com/external-secrets/external-secrets-e2e/framework/util"
  23. )
  24. type OpenBao struct {
  25. chart *HelmChart
  26. Namespace string
  27. URLs struct {
  28. InClusterPlainText string
  29. InClusterTLS string
  30. Local string
  31. }
  32. RootToken string
  33. ServerCA []byte
  34. portForwarder *PortForward
  35. }
  36. func NewOpenBao() *OpenBao {
  37. rootToken := rand.Text()
  38. repo := "openbao"
  39. return &OpenBao{
  40. chart: &HelmChart{
  41. Namespace: "openbao",
  42. ReleaseName: "openbao",
  43. Chart: fmt.Sprintf("%s/openbao", repo),
  44. ChartVersion: "0.28.3",
  45. Repo: ChartRepo{
  46. Name: repo,
  47. URL: "https://openbao.github.io/openbao-helm",
  48. },
  49. Args: []string{
  50. "--create-namespace",
  51. },
  52. Values: []string{filepath.Join(AssetDir(), "openbao.values.yaml")},
  53. Vars: []StringTuple{{
  54. Key: "server.dev.devRootToken",
  55. Value: rootToken,
  56. }},
  57. },
  58. Namespace: "openbao",
  59. RootToken: rootToken,
  60. }
  61. }
  62. func (l *OpenBao) Install() error {
  63. serverRootPem, serverPem, serverKeyPem, _, _, _, err := genVaultCertificates(l.Namespace, l.chart.ReleaseName)
  64. if err != nil {
  65. return err
  66. }
  67. l.ServerCA = serverRootPem
  68. if err := l.chart.Install(); err != nil {
  69. return err
  70. }
  71. sec := &v1.Secret{
  72. ObjectMeta: metav1.ObjectMeta{
  73. Name: "openbao-config",
  74. Namespace: l.Namespace,
  75. },
  76. Data: map[string][]byte{},
  77. }
  78. _, err = controllerutil.CreateOrUpdate(GinkgoT().Context(), l.chart.config.CRClient, sec, func() error {
  79. sec.Data = map[string][]byte{
  80. "server-cert.pem": serverPem,
  81. "server-cert-key.pem": serverKeyPem,
  82. "config.hcl": []byte(`
  83. ui = true
  84. listener "tcp" {
  85. address = "[::]:8300"
  86. tls_cert_file = "/etc/bao/server-cert.pem"
  87. tls_key_file = "/etc/bao/server-cert-key.pem"
  88. }
  89. `),
  90. }
  91. return nil
  92. })
  93. if err != nil {
  94. return err
  95. }
  96. if err := l.initBao(); err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. func (l *OpenBao) initBao() error {
  102. err := util.WaitForPodsReady(l.chart.config.KubeClientSet, 1, l.Namespace, metav1.ListOptions{
  103. LabelSelector: "app.kubernetes.io/name=openbao",
  104. })
  105. if err != nil {
  106. return fmt.Errorf("error waiting for OpenBao to be ready: %w", err)
  107. }
  108. // This e2e test provider uses a local port-forwarded to talk to the OpenBao API instead
  109. // of using the kubernetes service. This allows us to run the e2e test suite locally.
  110. l.portForwarder, err = NewPortForward(l.chart.config.KubeClientSet, l.chart.config.KubeConfig, "openbao", l.chart.Namespace, 8200)
  111. if err != nil {
  112. return err
  113. }
  114. if err := l.portForwarder.Start(); err != nil {
  115. return err
  116. }
  117. l.URLs.InClusterTLS = fmt.Sprintf("https://%s.%s.svc.cluster.local:8300", l.chart.ReleaseName, l.Namespace)
  118. l.URLs.InClusterPlainText = fmt.Sprintf("http://%s.%s.svc.cluster.local:8200", l.chart.ReleaseName, l.Namespace)
  119. l.URLs.Local = fmt.Sprintf("http://localhost:%d", l.portForwarder.localPort)
  120. return nil
  121. }
  122. func (l *OpenBao) Logs() error {
  123. return l.chart.Logs()
  124. }
  125. func (l *OpenBao) Uninstall() error {
  126. if l.portForwarder != nil {
  127. l.portForwarder.Close()
  128. l.portForwarder = nil
  129. }
  130. if err := l.chart.Uninstall(); err != nil {
  131. return err
  132. }
  133. return l.chart.config.KubeClientSet.CoreV1().Namespaces().Delete(GinkgoT().Context(), l.chart.Namespace, metav1.DeleteOptions{})
  134. }
  135. func (l *OpenBao) Setup(cfg *Config) error {
  136. return l.chart.Setup(cfg)
  137. }