gitlab.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. const (
  28. errGitlabCredSecretName = "credentials are empty"
  29. errInvalidClusterStoreMissingSAKNamespace = "invalid clusterStore missing SAK namespace"
  30. errFetchSAKSecret = "couldn't find secret on cluster: %w"
  31. errMissingSAK = "missing credentials while setting auth"
  32. errList = "could not verify if the client is valid: %w"
  33. errAuth = "client is not allowed to get secrets"
  34. errUninitializedGitlabProvider = "provider gitlab is not initialized"
  35. errJSONSecretUnmarshal = "unable to unmarshal secret: %w"
  36. )
  37. // https://github.com/external-secrets/external-secrets/issues/644
  38. var _ esv1beta1.SecretsClient = &Gitlab{}
  39. var _ esv1beta1.Provider = &Gitlab{}
  40. type Client interface {
  41. GetVariable(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  42. ListVariables(pid interface{}, opt *gitlab.ListProjectVariablesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error)
  43. }
  44. // Gitlab Provider struct with reference to a GitLab client and a projectID.
  45. type Gitlab struct {
  46. client Client
  47. url string
  48. projectID interface{}
  49. environment string
  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. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  100. func (g *Gitlab) Capabilities() esv1beta1.SecretStoreCapabilities {
  101. return esv1beta1.SecretStoreReadOnly
  102. }
  103. // Method on Gitlab Provider to set up client with credentials, populate projectID and environment.
  104. func (g *Gitlab) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  105. storeSpec := store.GetSpec()
  106. if storeSpec == nil || storeSpec.Provider == nil || storeSpec.Provider.Gitlab == nil {
  107. return nil, fmt.Errorf("no store type or wrong store type")
  108. }
  109. storeSpecGitlab := storeSpec.Provider.Gitlab
  110. cliStore := gClient{
  111. kube: kube,
  112. store: storeSpecGitlab,
  113. namespace: namespace,
  114. storeKind: store.GetObjectKind().GroupVersionKind().Kind,
  115. }
  116. if err := cliStore.setAuth(ctx); err != nil {
  117. return nil, err
  118. }
  119. var err error
  120. // Create client options
  121. var opts []gitlab.ClientOptionFunc
  122. if cliStore.store.URL != "" {
  123. opts = append(opts, gitlab.WithBaseURL(cliStore.store.URL))
  124. }
  125. // ClientOptionFunc from the gitlab package can be mapped with the CRD
  126. // in a similar way to extend functionality of the provider
  127. // Create a new Gitlab client using credentials and options
  128. gitlabClient, err := gitlab.NewClient(string(cliStore.credentials), opts...)
  129. if err != nil {
  130. return nil, err
  131. }
  132. g.client = gitlabClient.ProjectVariables
  133. g.projectID = cliStore.store.ProjectID
  134. g.environment = cliStore.store.Environment
  135. g.url = cliStore.store.URL
  136. return g, nil
  137. }
  138. func (g *Gitlab) DeleteSecret(ctx context.Context, remoteRef esv1beta1.PushRemoteRef) error {
  139. return fmt.Errorf("not implemented")
  140. }
  141. // Not Implemented SetSecret.
  142. func (g *Gitlab) SetSecret(ctx context.Context, value []byte, remoteRef esv1beta1.PushRemoteRef) error {
  143. return fmt.Errorf("not implemented")
  144. }
  145. // Empty GetAllSecrets.
  146. func (g *Gitlab) GetAllSecrets(ctx context.Context, ref esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  147. // TO be implemented
  148. return nil, fmt.Errorf("GetAllSecrets not implemented")
  149. }
  150. func (g *Gitlab) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  151. if utils.IsNil(g.client) {
  152. return nil, fmt.Errorf(errUninitializedGitlabProvider)
  153. }
  154. // Need to replace hyphens with underscores to work with Gitlab API
  155. ref.Key = strings.ReplaceAll(ref.Key, "-", "_")
  156. // Retrieves a gitlab variable in the form
  157. // {
  158. // "key": "TEST_VARIABLE_1",
  159. // "variable_type": "env_var",
  160. // "value": "TEST_1",
  161. // "protected": false,
  162. // "masked": true,
  163. // "environment_scope": "*"
  164. // }
  165. var vopts *gitlab.GetProjectVariableOptions
  166. if g.environment != "" {
  167. vopts = &gitlab.GetProjectVariableOptions{Filter: &gitlab.VariableFilter{EnvironmentScope: g.environment}}
  168. }
  169. data, _, err := g.client.GetVariable(g.projectID, ref.Key, vopts)
  170. if err != nil {
  171. return nil, err
  172. }
  173. if ref.Property == "" {
  174. if data.Value != "" {
  175. return []byte(data.Value), nil
  176. }
  177. return nil, fmt.Errorf("invalid secret received. no secret string for key: %s", ref.Key)
  178. }
  179. var payload string
  180. if data.Value != "" {
  181. payload = data.Value
  182. }
  183. val := gjson.Get(payload, ref.Property)
  184. if !val.Exists() {
  185. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  186. }
  187. return []byte(val.String()), nil
  188. }
  189. func (g *Gitlab) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  190. // Gets a secret as normal, expecting secret value to be a json object
  191. data, err := g.GetSecret(ctx, ref)
  192. if err != nil {
  193. return nil, fmt.Errorf("error getting secret %s: %w", ref.Key, err)
  194. }
  195. // Maps the json data to a string:string map
  196. kv := make(map[string]string)
  197. err = json.Unmarshal(data, &kv)
  198. if err != nil {
  199. return nil, fmt.Errorf(errJSONSecretUnmarshal, err)
  200. }
  201. // Converts values in K:V pairs into bytes, while leaving keys as strings
  202. secretData := make(map[string][]byte)
  203. for k, v := range kv {
  204. secretData[k] = []byte(v)
  205. }
  206. return secretData, nil
  207. }
  208. func (g *Gitlab) Close(ctx context.Context) error {
  209. return nil
  210. }
  211. // Validate will use the gitlab client to validate the gitlab provider using the ListVariable call to ensure get permissions without needing a specific key.
  212. func (g *Gitlab) Validate() (esv1beta1.ValidationResult, error) {
  213. _, resp, err := g.client.ListVariables(g.projectID, nil)
  214. if err != nil {
  215. return esv1beta1.ValidationResultError, fmt.Errorf(errList, err)
  216. } else if resp == nil || resp.StatusCode != http.StatusOK {
  217. return esv1beta1.ValidationResultError, fmt.Errorf(errAuth)
  218. }
  219. return esv1beta1.ValidationResultReady, nil
  220. }
  221. func (g *Gitlab) ValidateStore(store esv1beta1.GenericStore) error {
  222. storeSpec := store.GetSpec()
  223. gitlabSpec := storeSpec.Provider.Gitlab
  224. accessToken := gitlabSpec.Auth.SecretRef.AccessToken
  225. err := utils.ValidateSecretSelector(store, accessToken)
  226. if err != nil {
  227. return err
  228. }
  229. if gitlabSpec.ProjectID == "" {
  230. return fmt.Errorf("projectID cannot be empty")
  231. }
  232. if accessToken.Key == "" {
  233. return fmt.Errorf("accessToken.key cannot be empty")
  234. }
  235. if accessToken.Name == "" {
  236. return fmt.Errorf("accessToken.name cannot be empty")
  237. }
  238. return nil
  239. }