From cbbdaabb0cdbbe52082004a7d9d5eccb592e7b5c Mon Sep 17 00:00:00 2001 From: Gregory Stottsgit config --get-all Date: Tue, 5 Apr 2022 11:36:35 -0500 Subject: [PATCH 01/10] Initial --- cloud_organizations.go | 156 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 cloud_organizations.go diff --git a/cloud_organizations.go b/cloud_organizations.go new file mode 100644 index 0000000..ebffb21 --- /dev/null +++ b/cloud_organizations.go @@ -0,0 +1,156 @@ +package insightcloudsec + +import ( + "bytes" + "encoding/json" + "net/http" + "strings" +) + +// STRUCTS +/////////////////////////////////////////// + +type Cloud_Organization_Create struct { + // For use in creating Cloud Organizations + Cloud_Type string `json:"cloud_type"` // AWS, AZURE_ARM, GCE + Credentials string `json:"credentials"` + Nickname string `json:"nickname"` + Auto_Add bool `json:"auto_add,omitempty"` //GCP Only + Auto_Badge bool `json:"auto_badge,omitempty"` //GCP Only + Auto_Remove bool `json:"auto_remove,omitempty"` // GCP Only + Domain_Name string `json:"domain_name"` + Parent_Folder_ID []string `json:"parent_folder_id"` + Remove_Suspended bool `json:"remove_suspended,omitempty"` // AWS Only + Skip_Prefixes []string `json:"skip_prefixes,omitempty"` +} + +type Cloud_Organization struct { + Organization_ID int `json:"organization_id"` + Status int `json:"status"` + Auto_Badge bool `jso:"auto_badge"` + Auto_Add bool `json:"auto_add"` + Added_Timestamp string `json:"added_timestamp"` + Failures int `json:"failures"` + Cloud_Type_ID string `json:"cloud_type_id"` + Domain_ID string `json:"domain_id"` + Projects int `json:"projects"` + Domain_Name string `json:"domain_name"` +} + +type Cloud_Organizations_List struct { + Domains []Cloud_Organization `json:"domains"` +} + +// Functions +/////////////////////////////////////////// + +func (c Client) Create_Cloud_Organization(cloud_type string, creds string, nickname string, auto_add bool, auto_remove bool, domain_name string, parent_folder_id []string, remove_suspended bool, skip_prefixes []string) (Cloud_Organization, error) { + // Creates a cloud organization + + if creds == "" { + return Cloud_Organization{}, ValidationError{ + ItemToValidate: "creds", + ExpectedValues: []string{"Credentials are required for cloud organization creation"}, + } + } + + if nickname == "" { + return Cloud_Organization{}, ValidationError{ + ItemToValidate: "nickname", + ExpectedValues: []string{"Nicknames are required for cloud organization creation"}, + } + } + + var cloud_org Cloud_Organization_Create + cloud_type = strings.ToUpper(cloud_type) + if cloud_type == "AWS" { + cloud_org = create_AWS_Cloud_Org(creds, nickname, domain_name, parent_folder_id, remove_suspended, skip_prefixes) + } else if cloud_type == "AZURE_ARM" { + cloud_org = create_Azure_Cloud_Org(creds, nickname, domain_name, parent_folder_id, skip_prefixes) + } else if cloud_type == "GCE" { + cloud_org = create_GCE_Cloud_Org(creds, nickname, auto_add, auto_remove, domain_name, parent_folder_id, skip_prefixes) + } else { + return Cloud_Organization{}, ValidationError{ + ItemToValidate: "cloud_type", + ExpectedValues: []string{"AWS", "AZURE_ARM", "GCE"}, + } + } + + payload, err := json.Marshal(cloud_org) + if err != nil { + return Cloud_Organization{}, err + } + + resp, err := c.makeRequest(http.MethodPost, "/v2/public/cloud/domain/add", bytes.NewBuffer(payload)) + if err != nil { + return Cloud_Organization{}, err + } + + var ret Cloud_Organization + if err = json.NewDecoder(resp.Body).Decode(&ret); err != nil { + return Cloud_Organization{}, err + } + + return ret, nil +} + +func create_AWS_Cloud_Org(creds string, nickname string, domain_name string, parent_folder_id []string, remove_suspended bool, skip_prefixes []string) Cloud_Organization_Create { + // Creates AWS Cloud Organization + return Cloud_Organization_Create{ + Cloud_Type: "AWS", + Credentials: creds, + Nickname: nickname, + Domain_Name: domain_name, + Parent_Folder_ID: parent_folder_id, + Remove_Suspended: remove_suspended, + Skip_Prefixes: skip_prefixes, + } +} + +func create_Azure_Cloud_Org(creds string, nickname string, domain_name string, parent_folder_id []string, skip_prefixes []string) Cloud_Organization_Create { + return Cloud_Organization_Create{ + Cloud_Type: "AZURE_ARM", + Credentials: creds, + Nickname: nickname, + Domain_Name: domain_name, + Parent_Folder_ID: parent_folder_id, + Skip_Prefixes: skip_prefixes, + } +} + +func create_GCE_Cloud_Org(creds string, nickname string, auto_add bool, auto_remove bool, domain_name string, parent_folder_id []string, skip_prefixes []string) Cloud_Organization_Create { + return Cloud_Organization_Create{ + Cloud_Type: "GCE", + Credentials: creds, + Nickname: nickname, + Auto_Add: auto_add, + Auto_Badge: false, + Auto_Remove: auto_remove, + Domain_Name: domain_name, + Parent_Folder_ID: parent_folder_id, + Skip_Prefixes: skip_prefixes, + } +} + +func (c Client) List_Cloud_Organizations() ([]Cloud_Organization, error) { + // Returns a list of cloud organizations + resp, err := c.makeRequest(http.MethodGet, "/v2/public/cloud/domains", nil) + if err != nil { + return []Cloud_Organization{}, err + } + + var ret Cloud_Organizations_List + if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil { + return []Cloud_Organization{}, err + } + + return ret.Domains, nil +} + +func (c Client) Delete_Cloud_Organization() error { + return nil +} + +func (c Client) Edit_Cloud_Organization() error { + return nil +} From 2bd7c06ec53e4712441b5ba790afa4b1b00e124a Mon Sep 17 00:00:00 2001 From: Gregory Stottsgit config --get-all Date: Tue, 5 Apr 2022 11:39:46 -0500 Subject: [PATCH 02/10] Remove unnecessary comments --- clouds.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clouds.go b/clouds.go index ac372d6..eaf395c 100644 --- a/clouds.go +++ b/clouds.go @@ -342,10 +342,7 @@ func (c Client) DeleteCloud(cloud_resource_id string) error { return nil } -// CLOUD ORG FUNCTIONS -/////////////////////////////////////////// - -// MANAGING CLOUD FUNCTIONS +// CLOUD FUNCTIONS /////////////////////////////////////////// func (c Client) ListClouds() (CloudList, error) { From 4b1db38e55c6844ebcbeb84f0759c5d3e89eda34 Mon Sep 17 00:00:00 2001 From: gstotts <32727686+gstotts@users.noreply.github.com> Date: Wed, 6 Apr 2022 16:58:47 -0500 Subject: [PATCH 03/10] Update clouds.go --- clouds.go | 1 - 1 file changed, 1 deletion(-) diff --git a/clouds.go b/clouds.go index 86e0f8c..50af12c 100644 --- a/clouds.go +++ b/clouds.go @@ -360,7 +360,6 @@ func (s *clouds) Delete(cloud_resource_id string) error { return nil } - // MANAGING CLOUD FUNCTIONS /////////////////////////////////////////// From 08d7448d32f0b2e941663587f3207d2bca89ebd3 Mon Sep 17 00:00:00 2001 From: Gregory Stottsgit config --get-all Date: Thu, 7 Apr 2022 08:42:01 -0500 Subject: [PATCH 04/10] Proper structure for calling --- cloud_organizations.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cloud_organizations.go b/cloud_organizations.go index ebffb21..520d846 100644 --- a/cloud_organizations.go +++ b/cloud_organizations.go @@ -7,8 +7,14 @@ import ( "strings" ) -// STRUCTS -/////////////////////////////////////////// +type CloudOrganizations interface { + Create(cloud_type string, creds string, nickname string, auto_add bool, auto_remove bool, domain_name string, parent_folder_id []string, remove_suspended bool, skip_prefixes []string) (Cloud_Organization, error) + List() ([]Cloud_Organization, error) +} + +type corgs struct { + client *Client +} type Cloud_Organization_Create struct { // For use in creating Cloud Organizations @@ -44,7 +50,7 @@ type Cloud_Organizations_List struct { // Functions /////////////////////////////////////////// -func (c Client) Create_Cloud_Organization(cloud_type string, creds string, nickname string, auto_add bool, auto_remove bool, domain_name string, parent_folder_id []string, remove_suspended bool, skip_prefixes []string) (Cloud_Organization, error) { +func (c *corgs) Create(cloud_type string, creds string, nickname string, auto_add bool, auto_remove bool, domain_name string, parent_folder_id []string, remove_suspended bool, skip_prefixes []string) (Cloud_Organization, error) { // Creates a cloud organization if creds == "" { @@ -81,7 +87,7 @@ func (c Client) Create_Cloud_Organization(cloud_type string, creds string, nickn return Cloud_Organization{}, err } - resp, err := c.makeRequest(http.MethodPost, "/v2/public/cloud/domain/add", bytes.NewBuffer(payload)) + resp, err := c.client.makeRequest(http.MethodPost, "/v2/public/cloud/domain/add", bytes.NewBuffer(payload)) if err != nil { return Cloud_Organization{}, err } @@ -132,9 +138,9 @@ func create_GCE_Cloud_Org(creds string, nickname string, auto_add bool, auto_rem } } -func (c Client) List_Cloud_Organizations() ([]Cloud_Organization, error) { +func (c *corgs) List() ([]Cloud_Organization, error) { // Returns a list of cloud organizations - resp, err := c.makeRequest(http.MethodGet, "/v2/public/cloud/domains", nil) + resp, err := c.client.makeRequest(http.MethodGet, "/v2/public/cloud/domains", nil) if err != nil { return []Cloud_Organization{}, err } @@ -147,10 +153,10 @@ func (c Client) List_Cloud_Organizations() ([]Cloud_Organization, error) { return ret.Domains, nil } -func (c Client) Delete_Cloud_Organization() error { +func (c Client) Delete() error { return nil } -func (c Client) Edit_Cloud_Organization() error { +func (c Client) Edit() error { return nil } From e9d5d5888a82cef57aaea5910143efcf85bafa32 Mon Sep 17 00:00:00 2001 From: Gregory Stottsgit config --get-all Date: Thu, 7 Apr 2022 08:55:31 -0500 Subject: [PATCH 05/10] Proper structure for calling --- client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client.go b/client.go index 7ee64c0..9e7e2de 100644 --- a/client.go +++ b/client.go @@ -44,6 +44,7 @@ type Client struct { Badges Badges Bots Bots Clouds Clouds + CloudOrgs CloudOrganizations Filters Filters Insights Insights Organizations Organizations @@ -96,6 +97,7 @@ func NewClient(cfg *Config) (*Client, error) { client.Organizations = &orgs{client: client} client.Resources = &resources{client: client} client.ResourceGroups = &rsgroup{client: client} + client.CloudOrgs = &corgs{client: client} return client, nil } From 6474016f92430eda083e94dade42b056179c7d44 Mon Sep 17 00:00:00 2001 From: Gregory Stottsgit config --get-all Date: Tue, 19 Apr 2022 23:49:51 -0500 Subject: [PATCH 06/10] Cloud_orgs test framework --- cloud_organizations_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 cloud_organizations_test.go diff --git a/cloud_organizations_test.go b/cloud_organizations_test.go new file mode 100644 index 0000000..895a55d --- /dev/null +++ b/cloud_organizations_test.go @@ -0,0 +1,13 @@ +package insightcloudsec + +import ( + "testing" +) + +func TestCloudOrgs_Create(t *testing.T) {} + +func TestCloudOrgs_List(t *testing.T) {} + +func TestCloudOrgs_Delete(t *testing.T) {} + +func TestCloudOrgs_Edit(t *testing.T) {} From c703ba9e8db0e7c2951d20cbde6d7597c79e1368 Mon Sep 17 00:00:00 2001 From: gstotts <32727686+gstotts@users.noreply.github.com> Date: Fri, 23 Sep 2022 19:24:32 -0500 Subject: [PATCH 07/10] Update cloud_organizations.go --- cloud_organizations.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cloud_organizations.go b/cloud_organizations.go index 520d846..aac92b8 100644 --- a/cloud_organizations.go +++ b/cloud_organizations.go @@ -82,12 +82,7 @@ func (c *corgs) Create(cloud_type string, creds string, nickname string, auto_ad } } - payload, err := json.Marshal(cloud_org) - if err != nil { - return Cloud_Organization{}, err - } - - resp, err := c.client.makeRequest(http.MethodPost, "/v2/public/cloud/domain/add", bytes.NewBuffer(payload)) + resp, err := c.client.makeRequest(http.MethodPost, "/v2/public/cloud/domain/add", cloud_org) if err != nil { return Cloud_Organization{}, err } From 26ff07e56f2812ab9e876080bfc931f80dba8eb0 Mon Sep 17 00:00:00 2001 From: gstotts <32727686+gstotts@users.noreply.github.com> Date: Fri, 23 Sep 2022 19:25:57 -0500 Subject: [PATCH 08/10] Remove bytes --- client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client.go b/client.go index 72a8f9b..009b7de 100644 --- a/client.go +++ b/client.go @@ -1,7 +1,6 @@ package insightcloudsec import ( - "bytes" "encoding/json" "fmt" "net/http" From c7579c56539f0dac4803ba7ad04c2ba8ce1670b5 Mon Sep 17 00:00:00 2001 From: gstotts <32727686+gstotts@users.noreply.github.com> Date: Fri, 23 Sep 2022 19:26:20 -0500 Subject: [PATCH 09/10] Revert --- client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/client.go b/client.go index 009b7de..72a8f9b 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package insightcloudsec import ( + "bytes" "encoding/json" "fmt" "net/http" From 3c59e057aca18d66d9a2c86d39699e87374c08d9 Mon Sep 17 00:00:00 2001 From: gstotts <32727686+gstotts@users.noreply.github.com> Date: Fri, 23 Sep 2022 19:26:29 -0500 Subject: [PATCH 10/10] Update cloud_organizations.go --- cloud_organizations.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cloud_organizations.go b/cloud_organizations.go index aac92b8..640a937 100644 --- a/cloud_organizations.go +++ b/cloud_organizations.go @@ -1,7 +1,6 @@ package insightcloudsec import ( - "bytes" "encoding/json" "net/http" "strings"