Skip to content

Commit

Permalink
Merge pull request #230 from yossig-aquasec/client_auth_crashing_fix
Browse files Browse the repository at this point in the history
bug: fixing gorequest crashing
  • Loading branch information
yossig-aquasec authored Jan 22, 2023
2 parents 784ae36 + 328d809 commit a23ff32
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 137 deletions.
4 changes: 2 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ git clone https://github.com/aquasecurity/terraform-provider-aquasec.git
cd terraform-provider-aquasec
git checkout v0.8.17
git checkout v0.8.20
```

**Build and install the provider**
Expand All @@ -55,7 +55,7 @@ In order to test the provider installed locally, the provider block will have to
terraform {
required_providers {
aquasec = {
version = "0.8.17"
version = "0.8.20"
source = "terraform-provider-aquasec/aquasec/aquasec"
}
}
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ HOSTNAME := github.com
NAMESPACE := aquasec
NAME := aquasec
BINARY := terraform-provider-${NAME}
VERSION := 0.8.19
VERSION := 0.8.20
OS_ARCH := $(shell go env GOOS)_$(shell go env GOARCH)

default: build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ To quickly get started using the Aquasec provider for Terraform, configure the p
terraform {
required_providers {
aquasec = {
version = "0.8.17"
version = "0.8.20"
source = "aquasecurity/aquasec"
}
}
Expand Down
2 changes: 2 additions & 0 deletions aquasec/resource_permission_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestAquasecPermissionSetManagement(t *testing.T) {
actions := "risks.vulnerabilities.read,images.read"

if isSaasEnv() {
// todo: remove this after solving the following issue: https://scalock.atlassian.net/browse/SLK-62403
t.Skip("Skipping user test because its saas env")
author = os.Getenv("AQUA_USER")
}

Expand Down
10 changes: 4 additions & 6 deletions client/acknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ func (cli *Client) AcknowledgeCreate(acknowledgePost AcknowledgePost) error {
}

request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/risks/acknowledge")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, data, errs := request.Clone().Post(cli.url + apiPath).Send(string(payload)).End()
resp, data, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(err, "failed creating security acknowledges")
}
Expand All @@ -77,13 +76,13 @@ func (cli *Client) AcknowledgeRead() (*AcknowledgeList, error) {
var response AcknowledgeList

request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)

apiPath := fmt.Sprintf("/api/v2/risks/acknowledge?order_by=date")
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Get(cli.url + apiPath).End()
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
err = fmt.Errorf("error calling %s", apiPath)
return nil, err
Expand All @@ -105,13 +104,12 @@ func (cli *Client) AcknowledgeDelete(acknowledgePost AcknowledgePost) error {
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/risks/acknowledge/multiple")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, data, errs := request.Clone().Delete(cli.url + apiPath).Send(string(payload)).End()
resp, data, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(err, "failed deleting security acknowledges")
}
Expand Down
9 changes: 3 additions & 6 deletions client/application_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ func (cli *Client) CreateApplicationScope(applicationscope *ApplicationScope) er
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/access_management/scopes")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Post(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed creating Application Scope.")
}
Expand Down Expand Up @@ -134,13 +133,12 @@ func (cli *Client) UpdateApplicationScope(applicationscope *ApplicationScope, na
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/access_management/scopes/%s", name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Put(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Put(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed modifying Application Scope")
}
Expand All @@ -164,13 +162,12 @@ func (cli *Client) UpdateApplicationScope(applicationscope *ApplicationScope, na
// DeleteApplicationScope removes a Application Scope
func (cli *Client) DeleteApplicationScope(name string) error {
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/access_management/scopes/%s", name)
err := cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Delete(cli.url + apiPath).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed deleting Application Scope")
}
Expand Down
12 changes: 4 additions & 8 deletions client/assurance_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ type ScanTimeAuto struct {
func (cli *Client) GetAssurancePolicy(name string, at string) (*AssurancePolicy, error) {
var err error
var response AssurancePolicy
cli.gorequest.Set("Authorization", "Bearer "+cli.token)
var atype string
if strings.EqualFold(at, "host") {
atype = "host"
Expand All @@ -165,7 +164,7 @@ func (cli *Client) GetAssurancePolicy(name string, at string) (*AssurancePolicy,
if err != nil {
return nil, err
}
resp, body, errs := cli.gorequest.Clone().Get(cli.url + apiPath).End()
resp, body, errs := cli.gorequest.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
return nil, errors.Wrap(getMergedError(errs), "failed getting Assurance Policy")
}
Expand Down Expand Up @@ -214,12 +213,11 @@ func (cli *Client) CreateAssurancePolicy(assurancepolicy *AssurancePolicy, at st
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Post(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed creating Assurance Policy.")
}
Expand Down Expand Up @@ -258,12 +256,11 @@ func (cli *Client) UpdateAssurancePolicy(assurancepolicy *AssurancePolicy, at st
}
apiPath := "/api/v2/assurance_policy/" + atype + "/" + assurancepolicy.Name
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Put(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Put(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed modifying Assurance Policy")
}
Expand All @@ -287,7 +284,6 @@ func (cli *Client) UpdateAssurancePolicy(assurancepolicy *AssurancePolicy, at st
// DeleteAssurancePolicy removes a Assurance Policy
func (cli *Client) DeleteAssurancePolicy(name string, at string) error {
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
var atype string
if strings.EqualFold(at, "host") {
atype = "host"
Expand All @@ -303,7 +299,7 @@ func (cli *Client) DeleteAssurancePolicy(name string, at string) error {
if err != nil {
return err
}
resp, _, errs := request.Clone().Delete(cli.url + apiPath).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed deleting Assurance Policy")
}
Expand Down
15 changes: 5 additions & 10 deletions client/enforcers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ func (cli *Client) GetEnforcerGroup(name string) (*EnforcerGroup, error) {
var err error
var response EnforcerGroup
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/hostsbatch/%s", name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Get(cli.url + apiPath).End()
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()

if errs != nil {
err = fmt.Errorf("error calling %s", apiPath)
Expand All @@ -128,13 +127,12 @@ func (cli *Client) GetEnforcerGroups() ([]EnforcerGroup, error) {
var err error
var response []EnforcerGroup
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/hostsbatch")
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Get(cli.url + apiPath).End()
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
err = fmt.Errorf("error calling %s", apiPath)
return nil, err
Expand All @@ -156,13 +154,12 @@ func (cli *Client) CreateEnforcerGroup(group EnforcerGroup) error {
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/hostsbatch")
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, data, errs := request.Clone().Post(cli.url + apiPath).Send(string(payload)).End()
resp, data, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(err, "failed creating enforcer group")
}
Expand All @@ -183,13 +180,12 @@ func (cli *Client) UpdateEnforcerGroup(group EnforcerGroup) error {
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := "/api/v1/hostsbatch"
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Put(cli.url+apiPath).Send(string(payload)).Param("update_enforcers", "true").End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Put(cli.url+apiPath).Send(string(payload)).Param("update_enforcers", "true").End()
//resp, _, errs := request.Clone().Put(cli.url + apiPath).Send(string(payload)).End()

if errs != nil {
Expand All @@ -204,13 +200,12 @@ func (cli *Client) UpdateEnforcerGroup(group EnforcerGroup) error {
// DeleteEnforcerGroup removes an enforcer group
func (cli *Client) DeleteEnforcerGroup(name string) error {
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/hostsbatch/%s?delete_related=true", name)
err := cli.limiter.Wait(context.Background())
if err != nil {
return err
}
events, _, errs := request.Clone().Delete(cli.url + apiPath).End()
events, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).End()
if errs != nil {
return fmt.Errorf("error while calling DELETE on /api/v1/hostsbatch/%s: %v", name, events.StatusCode)
}
Expand Down
14 changes: 5 additions & 9 deletions client/firewall_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ func (cli *Client) GetFirewallPolicies() (*FirewallPolicyList, error) {
func (cli *Client) GetFirewallPolicy(name string) (*FirewallPolicy, error) {
var err error
var response FirewallPolicy
cli.gorequest.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/firewall_policies/%s", name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
resp, body, errs := cli.gorequest.Clone().Get(cli.url + apiPath).End()
resp, body, errs := cli.gorequest.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
return nil, errors.Wrap(getMergedError(errs), "failed getting firewall policy")
}
Expand Down Expand Up @@ -112,13 +111,12 @@ func (cli *Client) CreateFirewallPolicy(firewallPolicy FirewallPolicy) error {
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/firewall_policies")
apiPath := "/api/v2/firewall_policies"
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Post(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Post(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed creating firewall policy.")
}
Expand Down Expand Up @@ -146,13 +144,12 @@ func (cli *Client) UpdateFirewallPolicy(firewallPolicy FirewallPolicy) error {
return err
}
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/firewall_policies/%s", firewallPolicy.Name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Put(cli.url + apiPath).Send(string(payload)).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Put(cli.url + apiPath).Send(string(payload)).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed modifying firewall policy")
}
Expand All @@ -176,13 +173,12 @@ func (cli *Client) UpdateFirewallPolicy(firewallPolicy FirewallPolicy) error {
// DeleteFirewallPolicy removes a Firewall Policy
func (cli *Client) DeleteFirewallPolicy(name string) error {
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v2/firewall_policies/%s", name)
err := cli.limiter.Wait(context.Background())
if err != nil {
return err
}
resp, _, errs := request.Clone().Delete(cli.url + apiPath).End()
resp, _, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Delete(cli.url + apiPath).End()
if errs != nil {
return errors.Wrap(getMergedError(errs), "failed deleting firewall policy")
}
Expand Down
8 changes: 3 additions & 5 deletions client/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ type Gateway struct {
func (cli *Client) GetGateway(name string) (*Gateway, error) {
var err error
var response Gateway
cli.gorequest.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/servers/%s", name)
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := cli.gorequest.Clone().Get(cli.url + apiPath).End()
events, body, errs := cli.gorequest.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
log.Println(events.StatusCode)
err = fmt.Errorf("error calling %s", apiPath)
Expand All @@ -56,13 +55,12 @@ func (cli *Client) GetGateways() ([]Gateway, error) {
var err error
var response []Gateway
request := cli.gorequest
request.Set("Authorization", "Bearer "+cli.token)
apiPath := fmt.Sprintf("/api/v1/servers")
apiPath := "/api/v1/servers"
err = cli.limiter.Wait(context.Background())
if err != nil {
return nil, err
}
events, body, errs := request.Clone().Get(cli.url + apiPath).End()
events, body, errs := request.Clone().Set("Authorization", "Bearer "+cli.token).Get(cli.url + apiPath).End()
if errs != nil {
err = fmt.Errorf("error calling %s", apiPath)
return nil, err
Expand Down
Loading

0 comments on commit a23ff32

Please sign in to comment.