gitlab.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "time"
  19. "github.com/tidwall/gjson"
  20. gitlab "github.com/xanzy/go-gitlab"
  21. corev1 "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  24. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  25. "github.com/external-secrets/external-secrets/e2e/framework/log"
  26. "github.com/external-secrets/external-secrets/pkg/utils"
  27. )
  28. // Requires GITLAB_TOKEN and GITLAB_PROJECT_ID to be set in environment variables
  29. const (
  30. errGitlabCredSecretName = "credentials are empty"
  31. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  32. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  33. errMissingSAK = "missing credentials while setting auth"
  34. errUninitalizedGitlabProvider = "provider gitlab is not initialized"
  35. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  36. )
  37. type Client interface {
  38. GetVariable(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  39. }
  40. // Gitlab Provider struct with reference to a GitLab client and a projectID.
  41. type Gitlab struct {
  42. client Client
  43. url string
  44. projectID interface{}
  45. }
  46. // Client for interacting with kubernetes cluster...?
  47. type gClient struct {
  48. kube kclient.Client
  49. store *esv1beta1.GitlabProvider
  50. namespace string
  51. storeKind string
  52. credentials []byte
  53. }
  54. func init() {
  55. esv1beta1.Register(&Gitlab{}, &esv1beta1.SecretStoreProvider{
  56. Gitlab: &esv1beta1.GitlabProvider{},
  57. })
  58. }
  59. // Set gClient credentials to Access Token.
  60. func (c *gClient) setAuth(ctx context.Context) error {
  61. credentialsSecret := &corev1.Secret{}
  62. credentialsSecretName := c.store.Auth.SecretRef.AccessToken.Name
  63. if credentialsSecretName == "" {
  64. return fmt.Errorf(errGitlabCredSecretName)
  65. }
  66. objectKey := types.NamespacedName{
  67. Name: credentialsSecretName,
  68. Namespace: c.namespace,
  69. }
  70. // only ClusterStore is allowed to set namespace (and then it's required)
  71. if c.storeKind == esv1beta1.ClusterSecretStoreKind {
  72. if c.store.Auth.SecretRef.AccessToken.Namespace == nil {
  73. return fmt.Errorf(errInvalidClusterStoreMissingSAKNamespace)
  74. }
  75. objectKey.Namespace = *c.store.Auth.SecretRef.AccessToken.Namespace
  76. }
  77. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  78. if err != nil {
  79. return fmt.Errorf(errFetchSAKSecret, err)
  80. }
  81. c.credentials = credentialsSecret.Data[c.store.Auth.SecretRef.AccessToken.Key]
  82. if (c.credentials == nil) || (len(c.credentials) == 0) {
  83. return fmt.Errorf(errMissingSAK)
  84. }
  85. // I don't know where ProjectID is being set
  86. // This line SHOULD set it, but instead just breaks everything :)
  87. // c.store.ProjectID = string(credentialsSecret.Data[c.store.ProjectID])
  88. return nil
  89. }
  90. // Function newGitlabProvider returns a reference to a new instance of a 'Gitlab' struct.
  91. func NewGitlabProvider() *Gitlab {
  92. return &Gitlab{}
  93. }
  94. // Method on Gitlab Provider to set up client with credentials and populate projectID.
  95. func (g *Gitlab) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  96. storeSpec := store.GetSpec()
  97. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Gitlab == nil {
  98. return nil, fmt.Errorf("no store type or wrong store type")
  99. }
  100. storeSpecGitlab := storeSpec.Provider.Gitlab
  101. cliStore := gClient{
  102. kube: kube,
  103. store: storeSpecGitlab,
  104. namespace: namespace,
  105. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  106. }
  107. if err := cliStore.setAuth(ctx); err != nil {
  108. return nil, err
  109. }
  110. var err error
  111. // Create client options
  112. var opts []gitlab.ClientOptionFunc
  113. if cliStore.store.URL != "" {
  114. opts = append(opts, gitlab.WithBaseURL(cliStore.store.URL))
  115. }
  116. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  117. // in a similar way to extend functionality of the provider
  118. // Create a new Gitlab client using credentials and options
  119. gitlabClient, err := gitlab.NewClient(string(cliStore.credentials), opts...)
  120. if err != nil {
  121. log.Logf("Failed to create client: %v", err)
  122. }
  123. g.client = gitlabClient.ProjectVariables
  124. g.projectID = cliStore.store.ProjectID
  125. g.url = cliStore.store.URL
  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. timeout := 4 * time.Second
  190. url := g.url
  191. return utils.NetworkValidate(url, timeout)
  192. }
  193. func (g *Gitlab) ValidateStore(store esv1beta1.GenericStore) error {
  194. return nil
  195. }