Skip to content

Commit

Permalink
feat(cli): Manage Resource Groups in the lacework cli (#538)
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Murray <[email protected]>
  • Loading branch information
dmurray-lacework authored Sep 6, 2021
1 parent 754e8f4 commit 5e27cc8
Show file tree
Hide file tree
Showing 9 changed files with 873 additions and 12 deletions.
7 changes: 7 additions & 0 deletions api/resource_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ func (group ResourceGroupData) ID() string {
return group.ResourceGuid
}

func (group ResourceGroupData) Status() string {
if group.Enabled == 1 {
return "Enabled"
}
return "Disabled"
}

type ResourceGroupResponse struct {
Data ResourceGroupData `json:"data"`
}
Expand Down
84 changes: 84 additions & 0 deletions cli/cmd/resource_group_aws.go
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, ",")}
}
96 changes: 96 additions & 0 deletions cli/cmd/resource_group_azure.go
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
}
102 changes: 102 additions & 0 deletions cli/cmd/resource_group_container.go
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
}
95 changes: 95 additions & 0 deletions cli/cmd/resource_group_gcp.go
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
}
Loading

0 comments on commit 5e27cc8

Please sign in to comment.