handler.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 sshkeys
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "net/http"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/rbacscope"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/mcclient/auth"
  30. )
  31. func AddSshKeysHandler(prefix string, app *appsrv.Application) {
  32. app.AddHandler2("GET", fmt.Sprintf("%s/sshkeypairs", prefix), auth.Authenticate(sshKeysHandler), nil, "get_sshkeys", nil)
  33. app.AddHandler2("GET", fmt.Sprintf("%s/sshkeypairs/<tenant_id>", prefix), auth.Authenticate(adminSshKeysHandler), nil, "get_sshkeys", nil)
  34. }
  35. func adminSshKeysHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  36. publicOnly := false
  37. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  38. if !policy.PolicyManager.Allow(rbacscope.ScopeDomain, userCred, consts.GetServiceType(), "sshkeypairs", policy.PolicyActionGet).Result.IsAllow() {
  39. publicOnly = true
  40. }
  41. params, query, _ := appsrv.FetchEnv(ctx, w, r)
  42. projectId := params["<tenant_id>"]
  43. if len(projectId) == 0 {
  44. httperrors.InputParameterError(ctx, w, "empty project_id/tenant_id")
  45. return
  46. }
  47. domainId, _ := jsonutils.GetAnyString2(query, db.DomainFetchKeys)
  48. tenant, err := db.TenantCacheManager.FetchTenantByIdOrNameInDomain(ctx, projectId, domainId)
  49. if err != nil {
  50. if errors.Cause(err) == sql.ErrNoRows {
  51. httperrors.ResourceNotFoundError(ctx, w, "tenant/project %s not found", projectId)
  52. return
  53. } else {
  54. httperrors.GeneralServerError(ctx, w, err)
  55. return
  56. }
  57. }
  58. // get project key of specific project
  59. sendSshKey(ctx, w, userCred, tenant.Id, false, publicOnly)
  60. }
  61. func sshKeysHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  62. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  63. query, err := jsonutils.ParseQueryString(r.URL.RawQuery)
  64. if err != nil {
  65. httperrors.GeneralServerError(ctx, w, err)
  66. return
  67. }
  68. isAdmin := jsonutils.QueryBoolean(query, "admin", false)
  69. // get owner project key or get admin key if admin presents
  70. sendSshKey(ctx, w, userCred, userCred.GetProjectId(), isAdmin, false)
  71. }
  72. func sendSshKey(ctx context.Context, w http.ResponseWriter, userCred mcclient.TokenCredential, projectId string, isAdmin bool, publicOnly bool) {
  73. var privKey, pubKey string
  74. if isAdmin {
  75. if policy.PolicyManager.Allow(rbacscope.ScopeSystem, userCred, consts.GetServiceType(), "sshkeypairs", policy.PolicyActionGet).Result.IsAllow() {
  76. privKey, pubKey, _ = GetSshAdminKeypair(ctx)
  77. } else {
  78. httperrors.ForbiddenError(ctx, w, "not allow to access admin key")
  79. return
  80. }
  81. } else {
  82. privKey, pubKey, _ = GetSshProjectKeypair(ctx, projectId)
  83. }
  84. ret := jsonutils.NewDict()
  85. if !publicOnly {
  86. ret.Add(jsonutils.NewString(privKey), "private_key")
  87. }
  88. ret.Add(jsonutils.NewString(pubKey), "public_key")
  89. body := jsonutils.NewDict()
  90. body.Add(ret, "sshkeypair")
  91. appsrv.SendJSON(w, body)
  92. }