gitlab.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 gitlab
  13. import (
  14. "context"
  15. "encoding/json"
  16. "fmt"
  17. "strings"
  18. "github.com/tidwall/gjson"
  19. gitlab "github.com/xanzy/go-gitlab"
  20. corev1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  23. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  24. "github.com/external-secrets/external-secrets/e2e/framework/log"
  25. "github.com/external-secrets/external-secrets/pkg/provider"
  26. "github.com/external-secrets/external-secrets/pkg/provider/schema"
  27. "github.com/external-secrets/external-secrets/pkg/utils"
  28. )
  29. // Requires GITLAB_TOKEN and GITLAB_PROJECT_ID to be set in environment variables
  30. const (
  31. // TODO: Make these more descriptive.
  32. errGitlabCredSecretName = "error with credentials"
  33. errInvalidClusterStoreMissingSAKNamespace = "error"
  34. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  35. errMissingSAK = "error"
  36. errUninitalizedGitlabProvider = "provider gitlab is not initialized"
  37. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  38. )
  39. type Client interface {
  40. GetVariable(pid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  41. }
  42. // Gitlab Provider struct with reference to a github client and a projectID.
  43. type Gitlab struct {
  44. client Client
  45. projectID interface{}
  46. }
  47. // Client for interacting with kubernetes cluster...?
  48. type gClient struct {
  49. kube kclient.Client
  50. store *esv1alpha1.GitlabProvider
  51. namespace string
  52. storeKind string
  53. credentials []byte
  54. }
  55. func init() {
  56. schema.Register(&Gitlab{}, &esv1alpha1.SecretStoreProvider{
  57. Gitlab: &esv1alpha1.GitlabProvider{},
  58. })
  59. }
  60. // Set gClient credentials to Access Token.
  61. func (c *gClient) setAuth(ctx context.Context) error {
  62. credentialsSecret := &corev1.Secret{}
  63. credentialsSecretName := c.store.Auth.SecretRef.AccessToken.Name
  64. if credentialsSecretName == "" {
  65. return fmt.Errorf(errGitlabCredSecretName)
  66. }
  67. objectKey := types.NamespacedName{
  68. Name: credentialsSecretName,
  69. Namespace: c.namespace,
  70. }
  71. // only ClusterStore is allowed to set namespace (and then it's required)
  72. if c.storeKind == esv1alpha1.ClusterSecretStoreKind {
  73. if c.store.Auth.SecretRef.AccessToken.Namespace == nil {
  74. return fmt.Errorf(errInvalidClusterStoreMissingSAKNamespace)
  75. }
  76. objectKey.Namespace = *c.store.Auth.SecretRef.AccessToken.Namespace
  77. }
  78. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  79. if err != nil {
  80. return fmt.Errorf(errFetchSAKSecret, err)
  81. }
  82. c.credentials = credentialsSecret.Data[c.store.Auth.SecretRef.AccessToken.Key]
  83. if (c.credentials == nil) || (len(c.credentials) == 0) {
  84. return fmt.Errorf(errMissingSAK)
  85. }
  86. // I don't know where ProjectID is being set
  87. // This line SHOULD set it, but instead just breaks everything :)
  88. // c.store.ProjectID = string(credentialsSecret.Data[c.store.ProjectID])
  89. return nil
  90. }
  91. // Function newGitlabProvider returns a reference to a new instance of a 'Gitlab' struct.
  92. func NewGitlabProvider() *Gitlab {
  93. return &Gitlab{}
  94. }
  95. // Method on Gitlab Provider to set up client with credentials and populate projectID.
  96. func (g *Gitlab) NewClient(ctx context.Context, store esv1alpha1.GenericStore, kube kclient.Client, namespace string) (provider.SecretsClient, error) {
  97. storeSpec := store.GetSpec()
  98. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Gitlab == nil {
  99. return nil, fmt.Errorf("no store type or wrong store type")
  100. }
  101. storeSpecGitlab := storeSpec.Provider.Gitlab
  102. cliStore := gClient{
  103. kube: kube,
  104. store: storeSpecGitlab,
  105. namespace: namespace,
  106. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  107. }
  108. if err := cliStore.setAuth(ctx); err != nil {
  109. return nil, err
  110. }
  111. var err error
  112. // Create a new Gitlab client using credentials
  113. gitlabClient, err := gitlab.NewClient(string(cliStore.credentials), nil)
  114. if err != nil {
  115. log.Logf("Failed to create client: %v", err)
  116. }
  117. g.client = gitlabClient.ProjectVariables
  118. g.projectID = cliStore.store.ProjectID
  119. return g, nil
  120. }
  121. func (g *Gitlab) GetSecret(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) ([]byte, error) {
  122. if utils.IsNil(g.client) {
  123. return nil, fmt.Errorf(errUninitalizedGitlabProvider)
  124. }
  125. // Need to replace hyphens with underscores to work with Gitlab API
  126. ref.Key = strings.ReplaceAll(ref.Key, "-", "_")
  127. // Retrieves a gitlab variable in the form
  128. // {
  129. // "key": "TEST_VARIABLE_1",
  130. // "variable_type": "env_var",
  131. // "value": "TEST_1",
  132. // "protected": false,
  133. // "masked": true
  134. data, _, err := g.client.GetVariable(g.projectID, ref.Key, nil) // Optional 'filter' parameter could be added later
  135. if err != nil {
  136. return nil, err
  137. }
  138. if ref.Property == "" {
  139. if data.Value != "" {
  140. return []byte(data.Value), nil
  141. }
  142. return nil, fmt.Errorf("invalid secret received. no secret string for key: %s", ref.Key)
  143. }
  144. var payload string
  145. if data.Value != "" {
  146. payload = data.Value
  147. }
  148. val := gjson.Get(payload, ref.Property)
  149. if !val.Exists() {
  150. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  151. }
  152. return []byte(val.String()), nil
  153. }
  154. func (g *Gitlab) GetSecretMap(ctx context.Context, ref esv1alpha1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  155. // Gets a secret as normal, expecting secret value to be a json object
  156. data, err := g.GetSecret(ctx, ref)
  157. if err != nil {
  158. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  159. }
  160. // Maps the json data to a string:string map
  161. kv := make(map[string]string)
  162. err = json.Unmarshal(data, &kv)
  163. if err != nil {
  164. return nil, fmt.Errorf(errJSONSecretUnmarshal, err)
  165. }
  166. // Converts values in K:V pairs into bytes, while leaving keys as strings
  167. secretData := make(map[string][]byte)
  168. for k, v := range kv {
  169. secretData[k] = []byte(v)
  170. }
  171. return secretData, nil
  172. }
  173. func (g *Gitlab) Close() error {
  174. return nil
  175. }