role.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. )
  24. type SCloudpolicy struct {
  25. Id string
  26. Description string
  27. DisplayName string
  28. IsBuildIn bool
  29. IsEnabled bool
  30. ResourceScopes []string
  31. TemplateId string
  32. Version string
  33. RolePermissions []struct {
  34. allowedResourceActions []string
  35. Condition string
  36. }
  37. InheritsPermissionsFrom []struct {
  38. Id string
  39. }
  40. }
  41. func (role *SCloudpolicy) GetName() string {
  42. return role.DisplayName
  43. }
  44. func (role *SCloudpolicy) GetGlobalId() string {
  45. return role.Id
  46. }
  47. func (role *SCloudpolicy) GetDescription() string {
  48. return role.Description
  49. }
  50. func (role *SCloudpolicy) GetPolicyType() api.TPolicyType {
  51. if role.IsBuildIn {
  52. return api.PolicyTypeSystem
  53. }
  54. return api.PolicyTypeCustom
  55. }
  56. func (role *SCloudpolicy) UpdateDocument(document *jsonutils.JSONDict) error {
  57. return cloudprovider.ErrNotImplemented
  58. }
  59. func (role *SCloudpolicy) GetDocument() (*jsonutils.JSONDict, error) {
  60. return jsonutils.Marshal(role).(*jsonutils.JSONDict), nil
  61. }
  62. func (role *SCloudpolicy) Delete() error {
  63. return cloudprovider.ErrNotImplemented
  64. }
  65. func (cli *SAzureClient) GetRoles(name string) ([]SCloudpolicy, error) {
  66. ret := []SCloudpolicy{}
  67. filter := []string{}
  68. if len(name) > 0 {
  69. filter = append(filter, fmt.Sprintf("displayName eq '%s'", name))
  70. }
  71. params := url.Values{}
  72. if len(filter) > 0 {
  73. params.Set("$filter", strings.Join(filter, " and "))
  74. }
  75. resp, err := cli._list_v2(SERVICE_GRAPH, "rolemanagement/directory/roleDefinitions", "", nil)
  76. if err != nil {
  77. return nil, errors.Wrap(err, "list")
  78. }
  79. err = resp.Unmarshal(&ret, "value")
  80. if err != nil {
  81. return nil, err
  82. }
  83. return ret, nil
  84. }
  85. func (cli *SAzureClient) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  86. roles, err := cli.GetRoles("")
  87. if err != nil {
  88. return nil, errors.Wrap(err, "GetRoles")
  89. }
  90. ret := []cloudprovider.ICloudpolicy{}
  91. for i := range roles {
  92. ret = append(ret, &roles[i])
  93. }
  94. return ret, nil
  95. }
  96. func (cli *SAzureClient) AssignPolicy(objectId, roleId string) error {
  97. body := map[string]interface{}{
  98. "roleDefinitionId": roleId,
  99. "principalId": objectId,
  100. "directoryScopeId": "/",
  101. }
  102. _, err := cli._post_v2(SERVICE_GRAPH, "roleManagement/directory/roleAssignments", "", body)
  103. return err
  104. }
  105. type SPrincipalPolicy struct {
  106. RoleDefinitionId string
  107. PrincipalId string
  108. Id string
  109. }
  110. func (cli *SAzureClient) GetPrincipalPolicy(principalId string) ([]SPrincipalPolicy, error) {
  111. params := url.Values{}
  112. filter := []string{}
  113. if len(principalId) > 0 {
  114. filter = append(filter, fmt.Sprintf("principalId eq '%s'", principalId))
  115. }
  116. if len(filter) > 0 {
  117. params.Set("$filter", strings.Join(filter, " and "))
  118. }
  119. resp, err := cli._list_v2(SERVICE_GRAPH, "rolemanagement/directory/roleAssignments", "", params)
  120. if err != nil {
  121. return nil, err
  122. }
  123. ret := []SPrincipalPolicy{}
  124. err = resp.Unmarshal(&ret, "value")
  125. if err != nil {
  126. return nil, err
  127. }
  128. return ret, nil
  129. }
  130. func (cli *SAzureClient) DeletePrincipalPolicy(assignmentId string) error {
  131. res := fmt.Sprintf("roleManagement/directory/roleAssignments/%s", assignmentId)
  132. _, err := cli._delete_v2(SERVICE_GRAPH, res, "")
  133. return err
  134. }