|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + |
| 10 | +) |
| 11 | + |
| 12 | +type RoleMappingSaas struct { |
| 13 | + CspRole string `json:"csp_role"` |
| 14 | + SamlGroups []string `json:"saml_groups"` |
| 15 | + Id int `json:"id"` |
| 16 | + Created string `json:"created"` |
| 17 | + AccountId int `json:"account_id"` |
| 18 | +} |
| 19 | + |
| 20 | +type RoleMappingSaasList struct { |
| 21 | + Items []RoleMappingSaas `json:"data"` |
| 22 | +} |
| 23 | + |
| 24 | +type RoleMappingSaasResponse struct { |
| 25 | + RoleMappingSaas RoleMappingSaas `json:"data"` |
| 26 | +} |
| 27 | + |
| 28 | +const roleMappingBasePath = "/api/cspm/v2/samlmappings" |
| 29 | + |
| 30 | +func (cli *Client) GetRoleMappingSaas(id string) (*RoleMappingSaas, error) { |
| 31 | + if cli.clientType != Saas && cli.clientType != SaasDev { |
| 32 | + return nil, fmt.Errorf("GetRoleMappingSaas is supported only in Aqua SaaS environment") |
| 33 | + } |
| 34 | + |
| 35 | + if err := cli.limiter.Wait(context.Background()); err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + url := fmt.Sprintf("%s%s/%s", cli.saasUrl, roleMappingBasePath, id) |
| 40 | + resp, body, errs := cli.gorequest.Clone().Get(url). |
| 41 | + Set("Authorization", fmt.Sprintf(authHeaderFormat, cli.token)).End() |
| 42 | + |
| 43 | + if len(errs) > 0 { |
| 44 | + return nil, fmt.Errorf("failed GET %s: %v", url, errs) |
| 45 | + } |
| 46 | + if resp.StatusCode != statusOK { |
| 47 | + return nil, fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body) |
| 48 | + } |
| 49 | + |
| 50 | + var result RoleMappingSaasResponse |
| 51 | + if err := json.Unmarshal([]byte(body), &result); err != nil { |
| 52 | + return nil, errors.Wrap(err, "failed to unmarshal RoleMappingSaas response") |
| 53 | + } |
| 54 | + return &result.RoleMappingSaas, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (cli *Client) GetRolesMappingSaas() (*RoleMappingSaasList, error) { |
| 58 | + if cli.clientType != Saas && cli.clientType != SaasDev { |
| 59 | + return nil, fmt.Errorf("GetRolesMappingSaas is supported only in Aqua SaaS environment") |
| 60 | + } |
| 61 | + |
| 62 | + if err := cli.limiter.Wait(context.Background()); err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + url := fmt.Sprintf("%s%s", cli.saasUrl, roleMappingBasePath) |
| 67 | + resp, body, errs := cli.gorequest.Clone().Get(url). |
| 68 | + Set("Authorization", fmt.Sprintf(authHeaderFormat, cli.token)).End() |
| 69 | + |
| 70 | + if len(errs) > 0 { |
| 71 | + return nil, fmt.Errorf("failed GET %s: %v", url, errs) |
| 72 | + } |
| 73 | + if resp.StatusCode != statusOK { |
| 74 | + return nil, fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body) |
| 75 | + } |
| 76 | + |
| 77 | + var result RoleMappingSaasList |
| 78 | + if err := json.Unmarshal([]byte(body), &result); err != nil { |
| 79 | + return nil, errors.Wrap(err, "failed to unmarshal RoleMappingSaas list") |
| 80 | + } |
| 81 | + return &result, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (cli *Client) CreateRoleMappingSaas(saas *RoleMappingSaas) error { |
| 85 | + if cli.clientType != Saas && cli.clientType != SaasDev { |
| 86 | + return fmt.Errorf("CreateRoleMappingSaas is supported only in Aqua SaaS environment") |
| 87 | + } |
| 88 | + |
| 89 | + if err := cli.limiter.Wait(context.Background()); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + url := fmt.Sprintf("%s%s", cli.saasUrl, roleMappingBasePath) |
| 94 | + saasPayload := map[string]interface{}{ |
| 95 | + "csp_role": saas.CspRole, |
| 96 | + "saml_groups": saas.SamlGroups, |
| 97 | + } |
| 98 | + payload, _ := json.Marshal(saasPayload) |
| 99 | + |
| 100 | + resp, body, errs := cli.gorequest.Clone().Post(url). |
| 101 | + Set("Authorization", fmt.Sprintf(authHeaderFormat, cli.token)). |
| 102 | + Send(string(payload)).End() |
| 103 | + |
| 104 | + if len(errs) > 0 { |
| 105 | + return fmt.Errorf("failed POST %s: %v", url, errs) |
| 106 | + } |
| 107 | + if resp.StatusCode != statusCreated { |
| 108 | + return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body) |
| 109 | + } |
| 110 | + |
| 111 | + var response RoleMappingSaasResponse |
| 112 | + if err := json.Unmarshal([]byte(body), &response); err != nil { |
| 113 | + return errors.Wrap(err, "failed to unmarshal response after creation") |
| 114 | + } |
| 115 | + saas.Id = response.RoleMappingSaas.Id |
| 116 | + saas.Created = response.RoleMappingSaas.Created |
| 117 | + saas.AccountId = response.RoleMappingSaas.AccountId |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (cli *Client) UpdateRoleMappingSaas(saas *RoleMappingSaas, id string) error { |
| 122 | + if cli.clientType != Saas && cli.clientType != SaasDev { |
| 123 | + return fmt.Errorf("UpdateRoleMappingSaas is supported only in Aqua SaaS environment") |
| 124 | + } |
| 125 | + |
| 126 | + if err := cli.limiter.Wait(context.Background()); err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + url := fmt.Sprintf("%s%s/%s", cli.saasUrl, roleMappingBasePath, id) |
| 131 | + payloadMap := map[string]interface{}{ |
| 132 | + "saml_groups": saas.SamlGroups, |
| 133 | + } |
| 134 | + payload, _ := json.Marshal(payloadMap) |
| 135 | + |
| 136 | + resp, body, errs := cli.gorequest.Clone().Put(url). |
| 137 | + Set("Authorization", fmt.Sprintf(authHeaderFormat, cli.token)). |
| 138 | + Send(string(payload)).End() |
| 139 | + |
| 140 | + if len(errs) > 0 { |
| 141 | + return fmt.Errorf("failed PUT %s: %v", url, errs) |
| 142 | + } |
| 143 | + if resp.StatusCode != statusNoContent { |
| 144 | + return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body) |
| 145 | + } |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (cli *Client) DeleteRoleMappingSaas(id string) error { |
| 150 | + if cli.clientType != Saas && cli.clientType != SaasDev { |
| 151 | + return fmt.Errorf("DeleteRoleMappingSaas is supported only in Aqua SaaS environment") |
| 152 | + } |
| 153 | + |
| 154 | + if err := cli.limiter.Wait(context.Background()); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + url := fmt.Sprintf("%s%s/%s", cli.saasUrl, roleMappingBasePath, id) |
| 159 | + resp, body, errs := cli.gorequest.Clone().Delete(url). |
| 160 | + Set("Authorization", fmt.Sprintf(authHeaderFormat, cli.token)).End() |
| 161 | + |
| 162 | + if len(errs) > 0 { |
| 163 | + return fmt.Errorf("failed DELETE %s: %v", url, errs) |
| 164 | + } |
| 165 | + if resp.StatusCode != statusOK { |
| 166 | + return fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body) |
| 167 | + } |
| 168 | + return nil |
| 169 | +} |
0 commit comments