-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Manage Resource Groups in the lacework cli (#538)
Signed-off-by: Darren Murray <[email protected]>
- Loading branch information
1 parent
754e8f4
commit 5e27cc8
Showing
9 changed files
with
873 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Author:: Darren Murray(<[email protected]>) | ||
// Copyright:: Copyright 2021, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func createAwsResourceGroup() error { | ||
questions := []*survey.Question{ | ||
{ | ||
Name: "name", | ||
Prompt: &survey.Input{Message: "Name: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "description", | ||
Prompt: &survey.Input{Message: "Description: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "account_ids", | ||
Prompt: &survey.Multiline{Message: "List of Account IDs: "}, | ||
Validate: survey.Required, | ||
}, | ||
} | ||
|
||
answers := struct { | ||
Name string | ||
Description string `survey:"description"` | ||
AccountIDs string `survey:"account_ids"` | ||
}{} | ||
|
||
err := survey.Ask(questions, &answers, | ||
survey.WithIcons(promptIconsFunc), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
aws := api.NewResourceGroup( | ||
answers.Name, | ||
api.AwsResourceGroup, | ||
api.AwsResourceGroupProps{ | ||
Description: answers.Description, | ||
AccountIDs: strings.Split(answers.AccountIDs, "\n"), | ||
}) | ||
|
||
cli.StartProgress(" Creating resource group...") | ||
_, err = cli.LwApi.V2.ResourceGroups.Create(aws) | ||
cli.StopProgress() | ||
return err | ||
} | ||
|
||
func setAwsProps(group string) []string { | ||
var awsProps api.AwsResourceGroupProps | ||
err := json.Unmarshal([]byte(group), &awsProps) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
return []string{"ACCOUNT IDS", strings.Join(awsProps.AccountIDs, ",")} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// Author:: Darren Murray(<[email protected]>) | ||
// Copyright:: Copyright 2021, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func createAzureResourceGroup() error { | ||
questions := []*survey.Question{ | ||
{ | ||
Name: "name", | ||
Prompt: &survey.Input{Message: "Name: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "description", | ||
Prompt: &survey.Input{Message: "Description: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "tenant", | ||
Prompt: &survey.Input{Message: "Tenant: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "subscriptions", | ||
Prompt: &survey.Multiline{Message: "List of Subscriptions: "}, | ||
Validate: survey.Required, | ||
}, | ||
} | ||
|
||
answers := struct { | ||
Name string | ||
Description string `survey:"description"` | ||
Tenant string `survey:"tenant"` | ||
Subscriptions string `survey:"subscriptions"` | ||
}{} | ||
|
||
err := survey.Ask(questions, &answers, | ||
survey.WithIcons(promptIconsFunc), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
azure := api.NewResourceGroup( | ||
answers.Name, | ||
api.AzureResourceGroup, | ||
api.AzureResourceGroupProps{ | ||
Description: answers.Description, | ||
Tenant: answers.Tenant, | ||
Subscriptions: strings.Split(answers.Subscriptions, "\n"), | ||
}) | ||
|
||
cli.StartProgress(" Creating resource group...") | ||
_, err = cli.LwApi.V2.ResourceGroups.Create(azure) | ||
cli.StopProgress() | ||
return err | ||
} | ||
|
||
func setAzureProps(group string) [][]string { | ||
var ( | ||
azProps api.AzureResourceGroupProps | ||
details [][]string | ||
) | ||
err := json.Unmarshal([]byte(group), &azProps) | ||
if err != nil { | ||
return [][]string{} | ||
} | ||
|
||
details = append(details, []string{"TENANT", azProps.Tenant}) | ||
details = append(details, []string{"SUBSCRIPTIONS", strings.Join(azProps.Subscriptions, ",")}) | ||
return details | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// Author:: Darren Murray(<[email protected]>) | ||
// Copyright:: Copyright 2021, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func createContainerResourceGroup() error { | ||
questions := []*survey.Question{ | ||
{ | ||
Name: "name", | ||
Prompt: &survey.Input{Message: "Name: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "description", | ||
Prompt: &survey.Input{Message: "Description: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "tags", | ||
Prompt: &survey.Multiline{Message: "List of Tags: "}, | ||
Validate: survey.Required, | ||
}, { | ||
Name: "labels", | ||
Prompt: &survey.Multiline{Message: "List of 'key:value' Labels:"}, | ||
Validate: survey.Required, | ||
}, | ||
} | ||
|
||
answers := struct { | ||
Name string | ||
Description string `survey:"description"` | ||
Tags string `survey:"tags"` | ||
Labels string `survey:"labels"` | ||
}{} | ||
|
||
err := survey.Ask(questions, &answers, | ||
survey.WithIcons(promptIconsFunc), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
container := api.NewResourceGroup( | ||
answers.Name, | ||
api.ContainerResourceGroup, | ||
api.ContainerResourceGroupProps{ | ||
Description: answers.Description, | ||
ContainerTags: strings.Split(answers.Tags, "\n"), | ||
ContainerLabels: castStringToLimitByLabel(answers.Labels), | ||
}) | ||
|
||
cli.StartProgress(" Creating resource group...") | ||
_, err = cli.LwApi.V2.ResourceGroups.Create(container) | ||
cli.StopProgress() | ||
return err | ||
} | ||
|
||
func setContainerProps(group string) [][]string { | ||
var ( | ||
ctrProps api.ContainerResourceGroupProps | ||
labels []string | ||
details [][]string | ||
) | ||
err := json.Unmarshal([]byte(group), &ctrProps) | ||
if err != nil { | ||
return [][]string{} | ||
} | ||
|
||
for _, labelMap := range ctrProps.ContainerLabels { | ||
for key, val := range labelMap { | ||
labels = append(labels, fmt.Sprintf("%s: %v", key, val)) | ||
} | ||
} | ||
details = append(details, []string{"CONTAINER LABELS", strings.Join(labels, ",")}) | ||
details = append(details, []string{"CONTAINER TAGS", strings.Join(ctrProps.ContainerTags, ",")}) | ||
return details | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Author:: Darren Murray(<[email protected]>) | ||
// Copyright:: Copyright 2021, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func createGcpResourceGroup() error { | ||
questions := []*survey.Question{ | ||
{ | ||
Name: "name", | ||
Prompt: &survey.Input{Message: "Name: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "description", | ||
Prompt: &survey.Input{Message: "Description: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "organization", | ||
Prompt: &survey.Input{Message: "Organization: "}, | ||
Validate: survey.Required, | ||
}, | ||
{ | ||
Name: "projects", | ||
Prompt: &survey.Multiline{Message: "List of Projects: "}, | ||
Validate: survey.Required, | ||
}, | ||
} | ||
|
||
answers := struct { | ||
Name string | ||
Description string `survey:"description"` | ||
Organization string `survey:"organization"` | ||
Projects string `survey:"projects"` | ||
}{} | ||
|
||
err := survey.Ask(questions, &answers, | ||
survey.WithIcons(promptIconsFunc), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gcp := api.NewResourceGroup( | ||
answers.Name, | ||
api.GcpResourceGroup, | ||
api.GcpResourceGroupProps{ | ||
Description: answers.Description, | ||
Organization: answers.Organization, | ||
Projects: strings.Split(answers.Projects, "\n"), | ||
}) | ||
|
||
cli.StartProgress(" Creating resource group...") | ||
_, err = cli.LwApi.V2.ResourceGroups.Create(gcp) | ||
cli.StopProgress() | ||
return err | ||
} | ||
|
||
func setGcpProps(group string) [][]string { | ||
var ( | ||
gcpProps api.GcpResourceGroupProps | ||
details [][]string | ||
) | ||
err := json.Unmarshal([]byte(group), &gcpProps) | ||
if err != nil { | ||
return [][]string{} | ||
} | ||
|
||
details = append(details, []string{"ORGANIZATION", gcpProps.Organization}) | ||
details = append(details, []string{"PROJECTS", strings.Join(gcpProps.Projects, ",")}) | ||
return details | ||
} |
Oops, something went wrong.