gitlab.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  24. "github.com/external-secrets/external-secrets/e2e/framework/log"
  25. "github.com/external-secrets/external-secrets/pkg/utils"
  26. )
  27. // Requires GITLAB_TOKEN and GITLAB_PROJECT_ID to be set in environment variables
  28. const (
  29. errGitlabCredSecretName = "credentials are empty"
  30. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  31. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  32. errMissingSAK = "missing credentials while setting auth"
  33. errUninitalizedGitlabProvider = "provider gitlab is not initialized"
  34. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  35. )
  36. // https://github.com/external-secrets/external-secrets/issues/644
  37. var _ esv1beta1.SecretsClient = &Gitlab{}
  38. var _ esv1beta1.Provider = &Gitlab{}
  39. type Client interface {
  40. GetVariable(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  41. }
  42. // Gitlab Provider struct with reference to a GitLab 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 *esv1beta1.GitlabProvider
  51. namespace string
  52. storeKind string
  53. credentials []byte
  54. }
  55. func init() {
  56. esv1beta1.Register(&Gitlab{}, &esv1beta1.SecretStoreProvider{
  57. Gitlab: &esv1beta1.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 == esv1beta1.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 esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.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 client options
  113. var opts []gitlab.ClientOptionFunc
  114. if cliStore.store.URL != "" {
  115. opts = append(opts, gitlab.WithBaseURL(cliStore.store.URL))
  116. }
  117. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  118. // in a similar way to extend functionality of the provider
  119. // Create a new Gitlab client using credentials and options
  120. gitlabClient, err := gitlab.NewClient(string(cliStore.credentials), opts...)
  121. if err != nil {
  122. log.Logf("Failed to create client: %v", err)
  123. }
  124. g.client = gitlabClient.ProjectVariables
  125. g.projectID = cliStore.store.ProjectID
  126. return g, nil
  127. }
  128. // Empty GetAllSecrets.
  129. func (g *Gitlab) GetAllSecrets(ctx context.Context, ref esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  130. // TO be implemented
  131. return nil, fmt.Errorf("GetAllSecrets not implemented")
  132. }
  133. func (g *Gitlab) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  134. if utils.IsNil(g.client) {
  135. return nil, fmt.Errorf(errUninitalizedGitlabProvider)
  136. }
  137. // Need to replace hyphens with underscores to work with Gitlab API
  138. ref.Key = strings.ReplaceAll(ref.Key, "-", "_")
  139. // Retrieves a gitlab variable in the form
  140. // {
  141. // "key": "TEST_VARIABLE_1",
  142. // "variable_type": "env_var",
  143. // "value": "TEST_1",
  144. // "protected": false,
  145. // "masked": true
  146. data, _, err := g.client.GetVariable(g.projectID, ref.Key, nil) // Optional 'filter' parameter could be added later
  147. if err != nil {
  148. return nil, err
  149. }
  150. if ref.Property == "" {
  151. if data.Value != "" {
  152. return []byte(data.Value), nil
  153. }
  154. return nil, fmt.Errorf("invalid secret received. no secret string for key: %s", ref.Key)
  155. }
  156. var payload string
  157. if data.Value != "" {
  158. payload = data.Value
  159. }
  160. val := gjson.Get(payload, ref.Property)
  161. if !val.Exists() {
  162. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  163. }
  164. return []byte(val.String()), nil
  165. }
  166. func (g *Gitlab) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  167. // Gets a secret as normal, expecting secret value to be a json object
  168. data, err := g.GetSecret(ctx, ref)
  169. if err != nil {
  170. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  171. }
  172. // Maps the json data to a string:string map
  173. kv := make(map[string]string)
  174. err = json.Unmarshal(data, &kv)
  175. if err != nil {
  176. return nil, fmt.Errorf(errJSONSecretUnmarshal, err)
  177. }
  178. // Converts values in K:V pairs into bytes, while leaving keys as strings
  179. secretData := make(map[string][]byte)
  180. for k, v := range kv {
  181. secretData[k] = []byte(v)
  182. }
  183. return secretData, nil
  184. }
  185. func (g *Gitlab) Close(ctx context.Context) error {
  186. return nil
  187. }
  188. func (g *Gitlab) Validate() error {
  189. return nil
  190. }
  191. func (g *Gitlab) ValidateStore(store esv1beta1.GenericStore) error {
  192. return nil
  193. }