cloudpods_adapter.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 adapters
  15. import (
  16. "context"
  17. "yunion.io/x/log"
  18. api "yunion.io/x/onecloud/pkg/apis/identity"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/auth"
  22. "yunion.io/x/onecloud/pkg/mcp-server/options"
  23. )
  24. // Context key 类型,用于从 HTTP Header 传入的 AK/SK 存入 context(供 Cursor/Claude 等客户端使用)
  25. type headerCredKey string
  26. const (
  27. ContextKeyAK headerCredKey = "mcp_header_ak"
  28. ContextKeySK headerCredKey = "mcp_header_sk"
  29. )
  30. // GetAKSKFromContext 从 context 中读取连接时通过 Header 传入的 AK/SK(未设置时返回空字符串)
  31. func GetAKSKFromContext(ctx context.Context) (ak, sk string) {
  32. if v := ctx.Value(ContextKeyAK); v != nil {
  33. if s, ok := v.(string); ok {
  34. ak = s
  35. }
  36. }
  37. if v := ctx.Value(ContextKeySK); v != nil {
  38. if s, ok := v.(string); ok {
  39. sk = s
  40. }
  41. }
  42. return ak, sk
  43. }
  44. // CloudpodsAdapter 是与 Cloudpods API 交互的适配器,负责认证和资源管理
  45. type CloudpodsAdapter struct {
  46. client *mcclient.Client
  47. session *mcclient.ClientSession
  48. }
  49. type CloudRegion struct {
  50. RegionId string `json:"region_id"`
  51. }
  52. // NewCloudpodsAdapter 创建一个新的 Cloudpods 适配器实例
  53. func NewCloudpodsAdapter() *CloudpodsAdapter {
  54. client := mcclient.NewClient(
  55. options.Options.AuthURL,
  56. options.Options.Timeout,
  57. false,
  58. true,
  59. "",
  60. "",
  61. )
  62. return &CloudpodsAdapter{
  63. client: client,
  64. }
  65. }
  66. // authenticate 实现 Cloudpods 的认证逻辑,例如获取访问令牌
  67. func (a *CloudpodsAdapter) authenticate(ak string, sk string) (mcclient.TokenCredential, error) {
  68. if a.session != nil {
  69. return a.session.GetToken(), nil
  70. }
  71. token, err := a.client.AuthenticateByAccessKey(ak, sk, "")
  72. if err != nil {
  73. return nil, err
  74. }
  75. return token, nil
  76. }
  77. func (a *CloudpodsAdapter) getSession(ctx context.Context, ak string, sk string) (*mcclient.ClientSession, error) {
  78. // 若工具未传入 ak/sk,则使用连接时 Header 中的 AK/SK(与 Cursor/Claude 配置一致)
  79. if ak == "" && sk == "" {
  80. ak, sk = GetAKSKFromContext(ctx)
  81. }
  82. var userCred mcclient.TokenCredential
  83. if auth.IsAuthed() {
  84. userCred = policy.FetchUserCredential(ctx)
  85. if userCred != nil {
  86. log.Infof("getSessionWithUserCred: %v", userCred)
  87. } else {
  88. log.Infof("No userCred in context, will use ak/sk for authentication")
  89. token, err := a.authenticate(ak, sk)
  90. if err != nil {
  91. return nil, err
  92. }
  93. userCred = token
  94. }
  95. a.session = auth.GetSession(ctx, userCred, "")
  96. } else {
  97. token, err := a.authenticate(ak, sk)
  98. if err != nil {
  99. return nil, err
  100. }
  101. a.session = a.client.NewSession(
  102. context.Background(),
  103. "",
  104. "",
  105. api.EndpointInterfaceApigateway,
  106. token,
  107. )
  108. }
  109. return a.session, nil
  110. }