Skip to content

Commit

Permalink
test(cli): for compliance az list command
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Feb 2, 2022
1 parent c3d4aee commit a98599c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 29 deletions.
6 changes: 6 additions & 0 deletions cli/cmd/cli_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ func (c *cliState) NonInteractive() {
c.nonInteractive = true
}

// Interactive turns on interactive mode, that is, progress bars and spinners
func (c *cliState) Interactive() {
c.Log.Info("turning on interactive mode")
c.nonInteractive = false
}

// NoCache turns off the Lacework CLI caching mechanism, so nothing will be cached
func (c *cliState) NoCache() {
c.Log.Info("turning off caching mechanism")
Expand Down
14 changes: 11 additions & 3 deletions cli/cmd/compliance_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ Use the following command to list all Azure Tenants configured in your account:
Long: `Get the latest Azure compliance assessment report, these reports run on a regular schedule,
typically once a day. The available report formats are human-readable (default), json and pdf.
To list all Azure tenants and subscriptions configured in your account:
lacework compliance azure list
To run an ad-hoc compliance assessment use the command:
lacework compliance azure run-assessment <tenant_id>
Expand Down Expand Up @@ -244,8 +248,12 @@ To run an ad-hoc compliance assessment use the command:
Use: "run-assessment <tenant_id>",
Aliases: []string{"run"},
Short: "Run a new Azure compliance assessment",
Long: `Run a compliance assessment of the provided Azure tenant.`,
Args: cobra.ExactArgs(1),
Long: `Run a compliance assessment of the provided Azure tenant.
To list all Azure tenants and subscriptions configured in your account:
lacework compliance azure list`,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
response, err := cli.LwApi.Compliance.RunAzureReport(args[0])
if err != nil {
Expand Down Expand Up @@ -425,7 +433,7 @@ func getAzureSubscriptions(tenantID, status string) []azureSubscription {
subsResponse, err := cli.LwApi.Compliance.ListAzureSubscriptions(tenantID)
cli.StopProgress()
if err != nil {
cli.Log.Warn("unable to list azure subscriptions", "tenant_id", tenantID, "error", err.Error())
cli.Log.Warnw("unable to list azure subscriptions", "tenant_id", tenantID, "error", err.Error())
return subs
}
for _, subsRes := range subsResponse.Data {
Expand Down
94 changes: 70 additions & 24 deletions cli/cmd/compliance_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
package cmd

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/lacework/go-sdk/api"
"github.com/lacework/go-sdk/internal/lacework"
)

func TestSplitAzureSubscriptionsApiResponse(t *testing.T) {
Expand Down Expand Up @@ -105,42 +111,68 @@ func TestCliListAzureTenantsAndSubscriptionsWithoutData(t *testing.T) {
})
}

/*
func TestCliListAzureTenantsAndSubscriptionsWithDataEnabled(t *testing.T) {
cliOutput := captureOutput(func() {
assert.Nil(t, cliListTenantsAndSubscriptions(mockAzureIntegrationsResponse(1)))
})
// NOTE (@afiune): We purposly leave trailing spaces in this table, we need them!
expectedTable := `
AZURE TENANT AZURE SUBSCRIPTION STATUS
func TestCliListAzureTenantsAndSubscriptionsWithData(t *testing.T) {
var (
fakeServer = lacework.MockServer()
tenantID = "abc123xy-1234-abcd-a1b2-09876zxy1234"
)
fakeServer.MockToken("TOKEN")
fakeServer.MockAPI(
"external/compliance/azure/ListSubscriptionsForTenant",
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, mockAzureSubsResponse(tenantID))
})
defer fakeServer.Close()

c, err := api.NewClient("test",
api.WithToken("TOKEN"),
api.WithURL(fakeServer.URL()),
)
assert.Nil(t, err)

cli.LwApi = c
cli.NonInteractive()
defer func() {
cli.LwApi = nil
cli.Interactive()
}()

t.Run("enabled", func(t *testing.T) {
cliOutput := captureOutput(func() {
assert.Nil(t, cliListTenantsAndSubscriptions(mockAzureIntegrationsResponse(tenantID, 1)))
})
// NOTE (@afiune): We purposly leave trailing spaces in this table, we need them!
expectedTable := `
AZURE TENANT AZURE SUBSCRIPTION STATUS
---------------------------------------+--------------------------------------+----------
abc123xy-1234-abcd-a1b2-09876zxy1234 ABC123XX-1234-ABCD-1234-ABCD1234XYZZ Enabled
abc123xy-1234-abcd-a1b2-09876zxy1234 ABC123XX-1234-ABCD-1234-ABCD1234XYZZ Enabled
`
assert.Equal(t, strings.TrimPrefix(expectedTable, "\n"), cliOutput)
}
func TestCliListAzureTenantsAndSubscriptionsWithDataDisabled(t *testing.T) {
cliOutput := captureOutput(func() {
assert.Nil(t, cliListTenantsAndSubscriptions(mockAzureIntegrationsResponse(0)))
assert.Equal(t, strings.TrimPrefix(expectedTable, "\n"), cliOutput)
})
// NOTE (@afiune): We purposly leave trailing spaces in this table, we need them!
expectedTable := `
AZURE TENANT AZURE SUBSCRIPTION STATUS
---------------------------------------+--------------------------------------+----------
abc123xy-1234-abcd-a1b2-09876zxy1234 ABC123XX-1234-ABCD-1234-ABCD1234XYZZ Disabled

t.Run("disabled", func(t *testing.T) {
cliOutput := captureOutput(func() {
assert.Nil(t, cliListTenantsAndSubscriptions(mockAzureIntegrationsResponse(tenantID, 0)))
})
// NOTE (@afiune): We purposly leave trailing spaces in this table, we need them!
expectedTable := `
AZURE TENANT AZURE SUBSCRIPTION STATUS
---------------------------------------+--------------------------------------+-----------
abc123xy-1234-abcd-a1b2-09876zxy1234 ABC123XX-1234-ABCD-1234-ABCD1234XYZZ Disabled
`
assert.Equal(t, strings.TrimPrefix(expectedTable, "\n"), cliOutput)
assert.Equal(t, strings.TrimPrefix(expectedTable, "\n"), cliOutput)
})
}

func mockAzureIntegrationsResponse(enabled int) *api.AzureIntegrationsResponse {
func mockAzureIntegrationsResponse(tenantID string, enabled int) *api.AzureIntegrationsResponse {
response := &api.AzureIntegrationsResponse{}
err := json.Unmarshal([]byte(`{
"data": [
{
"CREATED_OR_UPDATED_BY": "[email protected]",
"CREATED_OR_UPDATED_TIME": "2021-08-02T17:53:24.116Z",
"DATA": {
"TENANT_ID": "abc123xy-1234-abcd-a1b2-09876zxy1234"
"TENANT_ID": "`+tenantID+`"
},
"ENABLED": `+strconv.Itoa(enabled)+`,
"INTG_GUID": "MOCK_1234",
Expand All @@ -164,4 +196,18 @@ func mockAzureIntegrationsResponse(enabled int) *api.AzureIntegrationsResponse {
}
return response
}
*/

func mockAzureSubsResponse(tenantID string) string {
return `{
"data": [
{
"subscriptions": [
"ABC123XX-1234-ABCD-1234-ABCD1234XYZZ (Default-account)"
],
"tenant": "` + tenantID + ` (Default Directory)"
}
],
"message": "SUCCESS",
"ok": true
}`
}
12 changes: 10 additions & 2 deletions cli/cmd/compliance_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Then, select one GUID from an integration and visualize its details using the co
Long: `Get the latest compliance assessment report, these reports run on a regular schedule,
typically once a day. The available report formats are human-readable (default), json and pdf.
To list all GCP projects and organizations configured in your account:
lacework compliance gcp list
To run an ad-hoc compliance assessment use the command:
lacework compliance gcp run-assessment <project_id>
Expand Down Expand Up @@ -253,8 +257,12 @@ To run an ad-hoc compliance assessment use the command:
Use: "run-assessment <org_or_project_id>",
Aliases: []string{"run"},
Short: "Run a new GCP compliance assessment",
Long: `Run a compliance assessment for the provided GCP organization or project.`,
Args: cobra.ExactArgs(1),
Long: `Run a compliance assessment for the provided GCP organization or project.
To list all GCP projects and organizations configured in your account:
lacework compliance gcp list`,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
response, err := cli.LwApi.Compliance.RunGcpReport(args[0])
if err != nil {
Expand Down

0 comments on commit a98599c

Please sign in to comment.