Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add List Azure Tenants command #341

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion cli/cmd/compliance_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
// complianceAzureListSubsCmd represents the list-subscriptions sub-command inside the azure command
complianceAzureListSubsCmd = &cobra.Command{
Use: "list-subscriptions <tenant_id>",
Aliases: []string{"list-subs", "list"},
Aliases: []string{"list-subs"},
Short: "list subscriptions from tenant",
Long: `List all Azure subscriptions from the provided tenant ID.

Expand Down Expand Up @@ -68,6 +68,33 @@ Then, select one GUID from an integration and visualize its details using the co
},
}

// complianceAzureListTenantsCmd represents the list-tenants sub-command inside the azure command
complianceAzureListTenantsCmd = &cobra.Command{
Use: "list-tenants",
Aliases: []string{"list"},
Short: "list subscriptions from tenant",
Long: `List all Azure Tenants.`,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
response, err := cli.LwApi.Integrations.ListAzureCfg()
if err != nil {
return errors.Wrap(err, "unable to list azure tenants")
}

if cli.JSONOutput() {
return cli.OutputJSON(response.Data[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an account doesn't have any Azure CFG integrations, we will panic here!

lacework comp az list --json -p mini
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/lacework/go-sdk/cli/cmd.glob..func8(0x1d49f80, 0xc0004fcf60, 0x0, 0x3, 0x0, 0x0)
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/cli/cmd/compliance_azure.go:85 +0x48f
github.com/spf13/cobra.(*Command).execute(0x1d49f80, 0xc0004fcf30, 0x3, 0x3, 0x1d49f80, 0xc0004fcf30)
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/vendor/github.com/spf13/cobra/command.go:842 +0x47c
github.com/spf13/cobra.(*Command).ExecuteC(0x1d4cc20, 0x16529cc, 0xc000607700, 0x1d58b22)
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/vendor/github.com/spf13/cobra/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/vendor/github.com/spf13/cobra/command.go:887
github.com/lacework/go-sdk/cli/cmd.Execute(0x0, 0x0)
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/cli/cmd/root.go:100 +0x85
main.main()
	/Users/salimmaya/go/src/github.com/lacework/go-sdk/cli/main.go:29 +0x26

}

var rows [][]string
for _, azure := range response.Data {
rows = append(rows, []string{azure.Data.TenantID})
}

cli.OutputHuman(renderSimpleTable([]string{"Tenants"}, rows))
return nil
},
}

// complianceAzureGetReportCmd represents the get-report sub-command inside the azure command
complianceAzureGetReportCmd = &cobra.Command{
Use: "get-report <tenant_id> <subscriptions_id>",
Expand Down Expand Up @@ -201,6 +228,7 @@ To run an ad-hoc compliance assessment use the command:
func init() {
// add sub-commands to the azure command
complianceAzureCmd.AddCommand(complianceAzureListSubsCmd)
complianceAzureCmd.AddCommand(complianceAzureListTenantsCmd)
complianceAzureCmd.AddCommand(complianceAzureGetReportCmd)
complianceAzureCmd.AddCommand(complianceAzureRunAssessmentCmd)

Expand Down
33 changes: 33 additions & 0 deletions integration/compliance_azure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Author:: Darren Murray (<[email protected]>)
// Copyright:: Copyright 2020, 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 integration

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestComplianceAzureListTenants(t *testing.T) {
out, err, exitcode := LaceworkCLIWithTOMLConfig("compliance", "az", "list-tenants")

assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
assert.Contains(t, out.String(), "TENANTS",
"STDOUT table headers changed, please check")
}