role.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 volcengine
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/utils"
  20. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. type PolicyDocument struct {
  24. Statement []struct {
  25. Effect string
  26. Action []string
  27. Principal struct {
  28. Federated []string
  29. }
  30. }
  31. }
  32. type SRole struct {
  33. client *SVolcEngineClient
  34. RoleName string
  35. DisplayName string
  36. TrustPolicyDocument string
  37. Description string
  38. }
  39. func (self *SRole) GetGlobalId() string {
  40. return self.RoleName
  41. }
  42. func (self *SRole) GetName() string {
  43. return self.RoleName
  44. }
  45. func (self *SRole) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  46. policies, err := self.client.ListAttachedRolePolicies(self.RoleName)
  47. if err != nil {
  48. return nil, err
  49. }
  50. ret := []cloudprovider.ICloudpolicy{}
  51. for i := range policies {
  52. policies[i].client = self.client
  53. ret = append(ret, &policies[i])
  54. }
  55. return ret, nil
  56. }
  57. func (self *SRole) AttachPolicy(policyName string, policyType api.TPolicyType) error {
  58. return self.client.AttachRolePolicy(self.RoleName, policyName, utils.Capitalize(string(policyType)))
  59. }
  60. func (self *SRole) DetachPolicy(policyName string, policyType api.TPolicyType) error {
  61. return self.client.DetachRolePolicy(self.RoleName, policyName, utils.Capitalize(string(policyType)))
  62. }
  63. func (self *SRole) Delete() error {
  64. return self.client.DeleteRole(self.RoleName)
  65. }
  66. func (self *SRole) GetDocument() *jsonutils.JSONDict {
  67. doc, err := jsonutils.ParseString(self.TrustPolicyDocument)
  68. if err != nil {
  69. return nil
  70. }
  71. return doc.(*jsonutils.JSONDict)
  72. }
  73. func (self *SRole) GetSAMLProvider() string {
  74. document := self.GetDocument()
  75. if document == nil {
  76. return ""
  77. }
  78. info := &PolicyDocument{}
  79. document.Unmarshal(info)
  80. for _, statement := range info.Statement {
  81. for _, sp := range statement.Principal.Federated {
  82. info := strings.Split(sp, "/")
  83. if len(info) == 2 {
  84. return info[1]
  85. }
  86. }
  87. }
  88. return ""
  89. }
  90. func (self *SVolcEngineClient) GetICloudroles() ([]cloudprovider.ICloudrole, error) {
  91. roles, err := self.ListRoles()
  92. if err != nil {
  93. return nil, err
  94. }
  95. ret := []cloudprovider.ICloudrole{}
  96. for i := range roles {
  97. roles[i].client = self
  98. ret = append(ret, &roles[i])
  99. }
  100. return ret, nil
  101. }
  102. func (client *SVolcEngineClient) ListRoles() ([]SRole, error) {
  103. params := map[string]string{
  104. "Limit": "50",
  105. }
  106. offset := 0
  107. ret := []SRole{}
  108. for {
  109. params["Offset"] = fmt.Sprintf("%d", offset)
  110. resp, err := client.iamRequest("", "ListRoles", params)
  111. if err != nil {
  112. return nil, err
  113. }
  114. part := struct {
  115. RoleMetadata []SRole
  116. Total int
  117. }{}
  118. err = resp.Unmarshal(&part)
  119. if err != nil {
  120. return nil, err
  121. }
  122. ret = append(ret, part.RoleMetadata...)
  123. if len(part.RoleMetadata) == 0 || len(ret) >= part.Total {
  124. break
  125. }
  126. offset = len(ret)
  127. }
  128. return ret, nil
  129. }
  130. func (client *SVolcEngineClient) GetRole(name string) (*SRole, error) {
  131. params := map[string]string{
  132. "RoleName": name,
  133. }
  134. resp, err := client.iamRequest("", "GetRole", params)
  135. if err != nil {
  136. return nil, err
  137. }
  138. ret := &SRole{client: client}
  139. err = resp.Unmarshal(ret, "Role")
  140. if err != nil {
  141. return nil, err
  142. }
  143. return ret, nil
  144. }
  145. func (client *SVolcEngineClient) CreateRole(name, statement, desc string) (*SRole, error) {
  146. params := map[string]string{
  147. "RoleName": name,
  148. "DisplayName": name,
  149. "TrustPolicyDocument": statement,
  150. "Description": desc,
  151. }
  152. resp, err := client.iamRequest("", "CreateRole", params)
  153. if err != nil {
  154. return nil, err
  155. }
  156. ret := &SRole{client: client}
  157. err = resp.Unmarshal(ret, "Role")
  158. if err != nil {
  159. return nil, err
  160. }
  161. return ret, nil
  162. }
  163. func (client *SVolcEngineClient) DeleteRole(name string) error {
  164. params := map[string]string{
  165. "RoleName": name,
  166. }
  167. _, err := client.iamRequest("", "DeleteRole", params)
  168. return err
  169. }
  170. func (client *SVolcEngineClient) ListAttachedRolePolicies(name string) ([]SPolicy, error) {
  171. params := map[string]string{
  172. "RoleName": name,
  173. }
  174. resp, err := client.iamRequest("", "ListAttachedRolePolicies", params)
  175. if err != nil {
  176. return nil, err
  177. }
  178. ret := []SPolicy{}
  179. err = resp.Unmarshal(&ret, "AttachedPolicyMetadata")
  180. if err != nil {
  181. return nil, err
  182. }
  183. return ret, nil
  184. }
  185. func (client *SVolcEngineClient) AttachRolePolicy(name, policy, policyType string) error {
  186. params := map[string]string{
  187. "RoleName": name,
  188. "PolicyName": policy,
  189. "PolicyType": policyType,
  190. }
  191. _, err := client.iamRequest("", "AttachRolePolicy", params)
  192. return err
  193. }
  194. func (client *SVolcEngineClient) DetachRolePolicy(name, policy, policyType string) error {
  195. params := map[string]string{
  196. "RoleName": name,
  197. "PolicyName": policy,
  198. "PolicyType": policyType,
  199. }
  200. _, err := client.iamRequest("", "DetachRolePolicy", params)
  201. return err
  202. }