kms.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 alibaba
  13. import (
  14. "context"
  15. "encoding/json"
  16. "fmt"
  17. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  18. kmssdk "github.com/alibabacloud-go/kms-20160120/v3/client"
  19. util "github.com/alibabacloud-go/tea-utils/v2/service"
  20. credential "github.com/aliyun/credentials-go/credentials"
  21. "github.com/avast/retry-go/v4"
  22. "github.com/tidwall/gjson"
  23. corev1 "k8s.io/api/core/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  26. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  27. "github.com/external-secrets/external-secrets/pkg/utils"
  28. )
  29. const (
  30. errAlibabaClient = "cannot setup new Alibaba client: %w"
  31. errAlibabaCredSecretName = "invalid Alibaba SecretStore resource: missing Alibaba APIKey"
  32. errUninitalizedAlibabaProvider = "provider Alibaba is not initialized"
  33. errInvalidClusterStoreMissingAKIDNamespace = "invalid ClusterStore, missing AccessKeyID namespace"
  34. errInvalidClusterStoreMissingSKNamespace = "invalid ClusterStore, missing namespace"
  35. errFetchAKIDSecret = "could not fetch AccessKeyID secret: %w"
  36. errMissingSAK = "missing AccessSecretKey"
  37. errMissingAKID = "missing AccessKeyID"
  38. )
  39. // https://github.com/external-secrets/external-secrets/issues/644
  40. var _ esv1beta1.SecretsClient = &KeyManagementService{}
  41. var _ esv1beta1.Provider = &KeyManagementService{}
  42. type KeyManagementService struct {
  43. Client SMInterface
  44. Config *openapi.Config
  45. }
  46. type SMInterface interface {
  47. GetSecretValue(ctx context.Context, request *kmssdk.GetSecretValueRequest) (*kmssdk.GetSecretValueResponseBody, error)
  48. Endpoint() string
  49. }
  50. func (kms *KeyManagementService) PushSecret(_ context.Context, _ *corev1.Secret, _ esv1beta1.PushSecretData) error {
  51. return fmt.Errorf("not implemented")
  52. }
  53. func (kms *KeyManagementService) DeleteSecret(_ context.Context, _ esv1beta1.PushSecretRemoteRef) error {
  54. return fmt.Errorf("not implemented")
  55. }
  56. // Empty GetAllSecrets.
  57. func (kms *KeyManagementService) GetAllSecrets(_ context.Context, _ esv1beta1.ExternalSecretFind) (map[string][]byte, error) {
  58. // TO be implemented
  59. return nil, fmt.Errorf("GetAllSecrets not implemented")
  60. }
  61. // GetSecret returns a single secret from the provider.
  62. func (kms *KeyManagementService) GetSecret(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) ([]byte, error) {
  63. if utils.IsNil(kms.Client) {
  64. return nil, fmt.Errorf(errUninitalizedAlibabaProvider)
  65. }
  66. request := &kmssdk.GetSecretValueRequest{
  67. SecretName: &ref.Key,
  68. }
  69. if ref.Version != "" {
  70. request.VersionId = &ref.Version
  71. }
  72. secretOut, err := kms.Client.GetSecretValue(ctx, request)
  73. if err != nil {
  74. return nil, SanitizeErr(err)
  75. }
  76. if ref.Property == "" {
  77. if utils.Deref(secretOut.SecretData) != "" {
  78. return []byte(utils.Deref(secretOut.SecretData)), nil
  79. }
  80. return nil, fmt.Errorf("invalid secret received. no secret string nor binary for key: %s", ref.Key)
  81. }
  82. var payload string
  83. if utils.Deref(secretOut.SecretData) != "" {
  84. payload = utils.Deref(secretOut.SecretData)
  85. }
  86. val := gjson.Get(payload, ref.Property)
  87. if !val.Exists() {
  88. return nil, fmt.Errorf("key %s does not exist in secret %s", ref.Property, ref.Key)
  89. }
  90. return []byte(val.String()), nil
  91. }
  92. // GetSecretMap returns multiple k/v pairs from the provider.
  93. func (kms *KeyManagementService) GetSecretMap(ctx context.Context, ref esv1beta1.ExternalSecretDataRemoteRef) (map[string][]byte, error) {
  94. data, err := kms.GetSecret(ctx, ref)
  95. if err != nil {
  96. return nil, err
  97. }
  98. kv := make(map[string]string)
  99. err = json.Unmarshal(data, &kv)
  100. if err != nil {
  101. return nil, fmt.Errorf("unable to unmarshal secret %s: %w", ref.Key, err)
  102. }
  103. secretData := make(map[string][]byte)
  104. for k, v := range kv {
  105. secretData[k] = []byte(v)
  106. }
  107. return secretData, nil
  108. }
  109. // Capabilities return the provider supported capabilities (ReadOnly, WriteOnly, ReadWrite).
  110. func (kms *KeyManagementService) Capabilities() esv1beta1.SecretStoreCapabilities {
  111. return esv1beta1.SecretStoreReadOnly
  112. }
  113. // NewClient constructs a new secrets client based on the provided store.
  114. func (kms *KeyManagementService) NewClient(ctx context.Context, store esv1beta1.GenericStore, kube kclient.Client, namespace string) (esv1beta1.SecretsClient, error) {
  115. storeSpec := store.GetSpec()
  116. alibabaSpec := storeSpec.Provider.Alibaba
  117. credentials, err := newAuth(ctx, kube, store, namespace)
  118. if err != nil {
  119. return nil, fmt.Errorf("failed to create Alibaba credentials: %w", err)
  120. }
  121. config := &openapi.Config{
  122. RegionId: utils.Ptr(alibabaSpec.RegionID),
  123. Credential: credentials,
  124. }
  125. options := newOptions(store)
  126. client, err := newClient(config, options)
  127. if err != nil {
  128. return nil, fmt.Errorf(errAlibabaClient, err)
  129. }
  130. kms.Client = client
  131. kms.Config = config
  132. return kms, nil
  133. }
  134. func newOptions(store esv1beta1.GenericStore) *util.RuntimeOptions {
  135. storeSpec := store.GetSpec()
  136. options := &util.RuntimeOptions{}
  137. // Setup retry options, if present in storeSpec
  138. if storeSpec.RetrySettings != nil {
  139. var retryAmount int
  140. if storeSpec.RetrySettings.MaxRetries != nil {
  141. retryAmount = int(*storeSpec.RetrySettings.MaxRetries)
  142. } else {
  143. retryAmount = 3
  144. }
  145. options.Autoretry = utils.Ptr(true)
  146. options.MaxAttempts = utils.Ptr(retryAmount)
  147. }
  148. return options
  149. }
  150. func newAuth(ctx context.Context, kube kclient.Client, store esv1beta1.GenericStore, namespace string) (credential.Credential, error) {
  151. storeSpec := store.GetSpec()
  152. alibabaSpec := storeSpec.Provider.Alibaba
  153. switch {
  154. case alibabaSpec.Auth.RRSAAuth != nil:
  155. credentials, err := newRRSAAuth(store)
  156. if err != nil {
  157. return nil, fmt.Errorf("failed to create Alibaba OIDC credentials: %w", err)
  158. }
  159. return credentials, nil
  160. case alibabaSpec.Auth.SecretRef != nil:
  161. credentials, err := newAccessKeyAuth(ctx, kube, store, namespace)
  162. if err != nil {
  163. return nil, fmt.Errorf("failed to create Alibaba AccessKey credentials: %w", err)
  164. }
  165. return credentials, nil
  166. default:
  167. return nil, fmt.Errorf("alibaba authentication methods wasn't provided")
  168. }
  169. }
  170. func newRRSAAuth(store esv1beta1.GenericStore) (credential.Credential, error) {
  171. storeSpec := store.GetSpec()
  172. alibabaSpec := storeSpec.Provider.Alibaba
  173. credentialConfig := &credential.Config{
  174. OIDCProviderArn: &alibabaSpec.Auth.RRSAAuth.OIDCProviderARN,
  175. OIDCTokenFilePath: &alibabaSpec.Auth.RRSAAuth.OIDCTokenFilePath,
  176. RoleArn: &alibabaSpec.Auth.RRSAAuth.RoleARN,
  177. RoleSessionName: &alibabaSpec.Auth.RRSAAuth.SessionName,
  178. Type: utils.Ptr("oidc_role_arn"),
  179. ConnectTimeout: utils.Ptr(30),
  180. Timeout: utils.Ptr(60),
  181. }
  182. return credential.NewCredential(credentialConfig)
  183. }
  184. func newAccessKeyAuth(ctx context.Context, kube kclient.Client, store esv1beta1.GenericStore, namespace string) (credential.Credential, error) {
  185. storeSpec := store.GetSpec()
  186. alibabaSpec := storeSpec.Provider.Alibaba
  187. storeKind := store.GetObjectKind().GroupVersionKind().Kind
  188. credentialsSecret := &corev1.Secret{}
  189. credentialsSecretName := alibabaSpec.Auth.SecretRef.AccessKeyID.Name
  190. if credentialsSecretName == "" {
  191. return nil, fmt.Errorf(errAlibabaCredSecretName)
  192. }
  193. objectKey := types.NamespacedName{
  194. Name: credentialsSecretName,
  195. Namespace: namespace,
  196. }
  197. // only ClusterStore is allowed to set namespace (and then it's required)
  198. if storeKind == esv1beta1.ClusterSecretStoreKind {
  199. if alibabaSpec.Auth.SecretRef.AccessKeyID.Namespace == nil {
  200. return nil, fmt.Errorf(errInvalidClusterStoreMissingAKIDNamespace)
  201. }
  202. objectKey.Namespace = *alibabaSpec.Auth.SecretRef.AccessKeyID.Namespace
  203. }
  204. err := kube.Get(ctx, objectKey, credentialsSecret)
  205. if err != nil {
  206. return nil, fmt.Errorf(errFetchAKIDSecret, err)
  207. }
  208. objectKey = types.NamespacedName{
  209. Name: alibabaSpec.Auth.SecretRef.AccessKeySecret.Name,
  210. Namespace: namespace,
  211. }
  212. if storeKind == esv1beta1.ClusterSecretStoreKind {
  213. if alibabaSpec.Auth.SecretRef.AccessKeySecret.Namespace == nil {
  214. return nil, fmt.Errorf(errInvalidClusterStoreMissingSKNamespace)
  215. }
  216. objectKey.Namespace = *alibabaSpec.Auth.SecretRef.AccessKeySecret.Namespace
  217. }
  218. accessKeyID := credentialsSecret.Data[alibabaSpec.Auth.SecretRef.AccessKeyID.Key]
  219. if (accessKeyID == nil) || (len(accessKeyID) == 0) {
  220. return nil, fmt.Errorf(errMissingAKID)
  221. }
  222. accessKeySecret := credentialsSecret.Data[alibabaSpec.Auth.SecretRef.AccessKeySecret.Key]
  223. if (accessKeySecret == nil) || (len(accessKeySecret) == 0) {
  224. return nil, fmt.Errorf(errMissingSAK)
  225. }
  226. credentialConfig := &credential.Config{
  227. AccessKeyId: utils.Ptr(string(accessKeyID)),
  228. AccessKeySecret: utils.Ptr(string(accessKeySecret)),
  229. Type: utils.Ptr("access_key"),
  230. ConnectTimeout: utils.Ptr(30),
  231. Timeout: utils.Ptr(60),
  232. }
  233. return credential.NewCredential(credentialConfig)
  234. }
  235. func (kms *KeyManagementService) Close(_ context.Context) error {
  236. return nil
  237. }
  238. func (kms *KeyManagementService) Validate() (esv1beta1.ValidationResult, error) {
  239. err := retry.Do(
  240. func() error {
  241. _, err := kms.Config.Credential.GetSecurityToken()
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. },
  247. retry.Attempts(5),
  248. )
  249. if err != nil {
  250. return esv1beta1.ValidationResultError, SanitizeErr(err)
  251. }
  252. return esv1beta1.ValidationResultReady, nil
  253. }
  254. func (kms *KeyManagementService) ValidateStore(store esv1beta1.GenericStore) error {
  255. storeSpec := store.GetSpec()
  256. alibabaSpec := storeSpec.Provider.Alibaba
  257. regionID := alibabaSpec.RegionID
  258. if regionID == "" {
  259. return fmt.Errorf("missing alibaba region")
  260. }
  261. return kms.validateStoreAuth(store)
  262. }
  263. func (kms *KeyManagementService) validateStoreAuth(store esv1beta1.GenericStore) error {
  264. storeSpec := store.GetSpec()
  265. alibabaSpec := storeSpec.Provider.Alibaba
  266. switch {
  267. case alibabaSpec.Auth.RRSAAuth != nil:
  268. return kms.validateStoreRRSAAuth(store)
  269. case alibabaSpec.Auth.SecretRef != nil:
  270. return kms.validateStoreAccessKeyAuth(store)
  271. default:
  272. return fmt.Errorf("missing alibaba auth provider")
  273. }
  274. }
  275. func (kms *KeyManagementService) validateStoreRRSAAuth(store esv1beta1.GenericStore) error {
  276. storeSpec := store.GetSpec()
  277. alibabaSpec := storeSpec.Provider.Alibaba
  278. if alibabaSpec.Auth.RRSAAuth.OIDCProviderARN == "" {
  279. return fmt.Errorf("missing alibaba OIDC proivder ARN")
  280. }
  281. if alibabaSpec.Auth.RRSAAuth.OIDCTokenFilePath == "" {
  282. return fmt.Errorf("missing alibaba OIDC token file path")
  283. }
  284. if alibabaSpec.Auth.RRSAAuth.RoleARN == "" {
  285. return fmt.Errorf("missing alibaba Assume Role ARN")
  286. }
  287. if alibabaSpec.Auth.RRSAAuth.SessionName == "" {
  288. return fmt.Errorf("missing alibaba session name")
  289. }
  290. return nil
  291. }
  292. func (kms *KeyManagementService) validateStoreAccessKeyAuth(store esv1beta1.GenericStore) error {
  293. storeSpec := store.GetSpec()
  294. alibabaSpec := storeSpec.Provider.Alibaba
  295. accessKeyID := alibabaSpec.Auth.SecretRef.AccessKeyID
  296. err := utils.ValidateSecretSelector(store, accessKeyID)
  297. if err != nil {
  298. return err
  299. }
  300. if accessKeyID.Name == "" {
  301. return fmt.Errorf("missing alibaba access ID name")
  302. }
  303. if accessKeyID.Key == "" {
  304. return fmt.Errorf("missing alibaba access ID key")
  305. }
  306. accessKeySecret := alibabaSpec.Auth.SecretRef.AccessKeySecret
  307. err = utils.ValidateSecretSelector(store, accessKeySecret)
  308. if err != nil {
  309. return err
  310. }
  311. if accessKeySecret.Name == "" {
  312. return fmt.Errorf("missing alibaba access key secret name")
  313. }
  314. if accessKeySecret.Key == "" {
  315. return fmt.Errorf("missing alibaba access key secret key")
  316. }
  317. return nil
  318. }
  319. func init() {
  320. esv1beta1.Register(&KeyManagementService{}, &esv1beta1.SecretStoreProvider{
  321. Alibaba: &esv1beta1.AlibabaProvider{},
  322. })
  323. }