gitlab.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "net/http"
  18. "strings"
  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/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. errList = "could not verify if the client is valid: %w"
  34. errAuth = "client is not allowed to get secrets"
  35. errUninitalizedGitlabProvider = "provider gitlab is not initialized"
  36. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  37. )
  38. // https://github.com/external-secrets/external-secrets/issues/644
  39. var _ esv1beta1.SecretsClient = &Gitlab{}
  40. var _ esv1beta1.Provider = &Gitlab{}
  41. type Client interface {
  42. GetVariable(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  43. ListVariables(pid interface{}, opt *gitlab.ListProjectVariablesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error)
  44. }
  45. // Gitlab Provider struct with reference to a GitLab client and a projectID.
  46. type Gitlab struct {
  47. client Client
  48. url string
  49. projectID interface{}
  50. }
  51. // Client for interacting with kubernetes cluster...?
  52. type gClient struct {
  53. kube kclient.Client
  54. store *esv1beta1.GitlabProvider
  55. namespace string
  56. storeKind string
  57. credentials []byte
  58. }
  59. func init() {
  60. esv1beta1.Register(&Gitlab{}, &esv1beta1.SecretStoreProvider{
  61. Gitlab: &esv1beta1.GitlabProvider{},
  62. })
  63. }
  64. // Set gClient credentials to Access Token.
  65. func (c *gClient) setAuth(ctx context.Context) error {
  66. credentialsSecret := &corev1.Secret{}
  67. credentialsSecretName := c.store.Auth.SecretRef.AccessToken.Name
  68. if credentialsSecretName == "" {
  69. return fmt.Errorf(errGitlabCredSecretName)
  70. }
  71. objectKey := types.NamespacedName{
  72. Name: credentialsSecretName,
  73. Namespace: c.namespace,
  74. }
  75. // only ClusterStore is allowed to set namespace (and then it's required)
  76. if c.storeKind == esv1beta1.ClusterSecretStoreKind {
  77. if c.store.Auth.SecretRef.AccessToken.Namespace == nil {
  78. return fmt.Errorf(errInvalidClusterStoreMissingSAKNamespace)
  79. }
  80. objectKey.Namespace = *c.store.Auth.SecretRef.AccessToken.Namespace
  81. }
  82. err := c.kube.Get(ctx, objectKey, credentialsSecret)
  83. if err != nil {
  84. return fmt.Errorf(errFetchSAKSecret, err)
  85. }
  86. c.credentials = credentialsSecret.Data[c.store.Auth.SecretRef.AccessToken.Key]
  87. if (c.credentials == nil) || (len(c.credentials) == 0) {
  88. return fmt.Errorf(errMissingSAK)
  89. }
  90. // I don't know where ProjectID is being set
  91. // This line SHOULD set it, but instead just breaks everything :)
  92. // c.store.ProjectID = string(credentialsSecret.Data[c.store.ProjectID])
  93. return nil
  94. }
  95. // Function newGitlabProvider returns a reference to a new instance of a 'Gitlab' struct.
  96. func NewGitlabProvider() *Gitlab {
  97. return &Gitlab{}
  98. }
  99. // Method on Gitlab Provider to set up client with credentials and populate projectID.
  100. func (g *Gitlab) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  101. storeSpec := store.GetSpec()
  102. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Gitlab == nil {
  103. return nil, fmt.Errorf("no store type or wrong store type")
  104. }
  105. storeSpecGitlab := storeSpec.Provider.Gitlab
  106. cliStore := gClient{
  107. kube: kube,
  108. store: storeSpecGitlab,
  109. namespace: namespace,
  110. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  111. }
  112. if err := cliStore.setAuth(ctx); err != nil {
  113. return nil, err
  114. }
  115. var err error
  116. // Create client options
  117. var opts []gitlab.ClientOptionFunc
  118. if cliStore.store.URL != "" {
  119. opts = append(opts, gitlab.WithBaseURL(cliStore.store.URL))
  120. }
  121. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  122. // in a similar way to extend functionality of the provider
  123. // Create a new Gitlab client using credentials and options
  124. gitlabClient, err := gitlab.NewClient(string(cliStore.credentials), opts...)
  125. if err != nil {
  126. return nil, err
  127. }
  128. g.client = gitlabClient.ProjectVariables
  129. g.projectID = cliStore.store.ProjectID
  130. g.url = cliStore.store.URL
  131. return g, nil
  132. }
  133. // Empty GetAllSecrets.
  134. func (g *Gitlab) GetAllSecrets(ctx context.Context, ref esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  135. // TO be implemented
  136. return nil, fmt.Errorf("GetAllSecrets not implemented")
  137. }
  138. func (g *Gitlab) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  139. if utils.IsNil(g.client) {
  140. return nil, fmt.Errorf(errUninitalizedGitlabProvider)
  141. }
  142. // Need to replace hyphens with underscores to work with Gitlab API
  143. ref.Key = strings.ReplaceAll(ref.Key, "-", "_")
  144. // Retrieves a gitlab variable in the form
  145. // {
  146. // "key": "TEST_VARIABLE_1",
  147. // "variable_type": "env_var",
  148. // "value": "TEST_1",
  149. // "protected": false,
  150. // "masked": true
  151. data, _, err := g.client.GetVariable(g.projectID, ref.Key, nil) // Optional 'filter' parameter could be added later
  152. if err != nil {
  153. return nil, err
  154. }
  155. if ref.Property == "" {
  156. if data.Value != "" {
  157. return []byte(data.Value), nil
  158. }
  159. return nil, fmt.Errorf("invalid secret received. no secret string for key: %s", ref.Key)
  160. }
  161. var payload string
  162. if data.Value != "" {
  163. payload = data.Value
  164. }
  165. val := gjson.Get(payload, ref.Property)
  166. if !val.Exists() {
  167. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  168. }
  169. return []byte(val.String()), nil
  170. }
  171. func (g *Gitlab) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  172. // Gets a secret as normal, expecting secret value to be a json object
  173. data, err := g.GetSecret(ctx, ref)
  174. if err != nil {
  175. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  176. }
  177. // Maps the json data to a string:string map
  178. kv := make(map[string]string)
  179. err = json.Unmarshal(data, &kv)
  180. if err != nil {
  181. return nil, fmt.Errorf(errJSONSecretUnmarshal, err)
  182. }
  183. // Converts values in K:V pairs into bytes, while leaving keys as strings
  184. secretData := make(map[string][]byte)
  185. for k, v := range kv {
  186. secretData[k] = []byte(v)
  187. }
  188. return secretData, nil
  189. }
  190. func (g *Gitlab) Close(ctx context.Context) error {
  191. return nil
  192. }
  193. // Validate will use the gitlab client to validate the gitlab provider using the ListVariable call to ensure get permissions without needing a specific key.
  194. func (g *Gitlab) Validate() (esv1beta1.ValidationResult, error) {
  195. _, resp, err := g.client.ListVariables(g.projectID, nil)
  196. if err != nil {
  197. return esv1beta1.ValidationResultError, fmt.Errorf(errList, err)
  198. } else if resp == nil || resp.StatusCode != http.StatusOK {
  199. return esv1beta1.ValidationResultError, fmt.Errorf(errAuth)
  200. }
  201. return esv1beta1.ValidationResultReady, nil
  202. }
  203. func (g *Gitlab) ValidateStore(store esv1beta1.GenericStore) error {
  204. storeSpec := store.GetSpec()
  205. gitlabSpec := storeSpec.Provider.Gitlab
  206. accessToken := gitlabSpec.Auth.SecretRef.AccessToken
  207. err := utils.ValidateSecretSelector(store, accessToken)
  208. if err != nil {
  209. return err
  210. }
  211. if gitlabSpec.ProjectID == "" {
  212. return fmt.Errorf("projectID cannot be empty")
  213. }
  214. if accessToken.Key == "" {
  215. return fmt.Errorf("accessToken.key cannot be empty")
  216. }
  217. if accessToken.Name == "" {
  218. return fmt.Errorf("accessToken.name cannot be empty")
  219. }
  220. return nil
  221. }