credential_aes_key.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package identity
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/printutils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  21. )
  22. func init() {
  23. type CredentialAesKeyOptions struct {
  24. User string `help:"User"`
  25. UserDomain string `help:"domain of user"`
  26. }
  27. type CredentialCreateAesKeyOptions struct {
  28. CredentialAesKeyOptions
  29. NAME string `help:"name of key"`
  30. ALG string `help:"name of alg" choices:"aes-256|sm4"`
  31. }
  32. R(&CredentialCreateAesKeyOptions{}, "credential-create-enckey", "Create AES 256 Secret Key", func(s *mcclient.ClientSession, args *CredentialCreateAesKeyOptions) error {
  33. var uid string
  34. var err error
  35. if len(args.User) > 0 {
  36. uid, err = modules.UsersV3.FetchId(s, args.User, args.UserDomain)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. secret, err := modules.Credentials.CreateEncryptKey(s, uid, args.NAME, args.ALG)
  42. if err != nil {
  43. return err
  44. }
  45. printObject(secret.Marshal())
  46. return nil
  47. })
  48. R(&CredentialAesKeyOptions{}, "credential-get-enckey", "Get encryption secret keys for user", func(s *mcclient.ClientSession, args *CredentialAesKeyOptions) error {
  49. var uid string
  50. var err error
  51. if len(args.User) > 0 {
  52. uid, err = modules.UsersV3.FetchId(s, args.User, args.UserDomain)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. secrets, err := modules.Credentials.GetEncryptKeys(s, uid)
  58. if err != nil {
  59. return err
  60. }
  61. result := printutils.ListResult{}
  62. result.Data = make([]jsonutils.JSONObject, len(secrets))
  63. for i := range secrets {
  64. result.Data[i] = secrets[i].Marshal()
  65. }
  66. printList(&result, nil)
  67. return nil
  68. })
  69. type CredentialAesKeyEncryptOptions struct {
  70. ID string `help:"id or name of credential"`
  71. SECRET string `help:"secret to encrypt"`
  72. }
  73. R(&CredentialAesKeyEncryptOptions{}, "credential-enckey-encrypt", "Encrypt with a encryption key", func(s *mcclient.ClientSession, args *CredentialAesKeyEncryptOptions) error {
  74. sec, err := modules.Credentials.EncryptKeyEncryptBase64(s, args.ID, []byte(args.SECRET))
  75. if err != nil {
  76. return err
  77. }
  78. fmt.Println(sec)
  79. return nil
  80. })
  81. type CredentialAesKeyDecryptOptions struct {
  82. ID string `help:"id or name of credential"`
  83. SECRET string `help:"secret to decrypt"`
  84. }
  85. R(&CredentialAesKeyDecryptOptions{}, "credential-enckey-decrypt", "Decrypt with a encryption key", func(s *mcclient.ClientSession, args *CredentialAesKeyDecryptOptions) error {
  86. sec, err := modules.Credentials.EncryptKeyDecryptBase64(s, args.ID, args.SECRET)
  87. if err != nil {
  88. return err
  89. }
  90. fmt.Println(string(sec))
  91. return nil
  92. })
  93. }