Skip to content

Commit

Permalink
feat(cli): split list of Google project id and alias (#377)
Browse files Browse the repository at this point in the history
We are implementing a workaround into the Lacework CLI to split the ID
and alias from the list of Google projects when a user runs the command:
```
lacework compliance google list-projects <org_id>
```

Ultimately we will do this in the Lacework backend, but for now, we are
going to adopt this workaround until we start using APIv2.

Example of the new JSON and human-readable outputs:
```
➜ lacework comp gcp list 123456789012 -p mini
       PROJECT ID        PROJECT ALIAS
-----------------------+----------------
  abc-demo-project-123   demo-project
➜ lacework comp gcp list 123456789012 -p mini --json
{
  "organization": {
    "alias": "",
    "id": "123456789012"
  },
  "projects": [
    {
      "alias": "demo-project",
      "id": "abc-demo-project-123"
    }
  ]
}
```

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Apr 7, 2021
1 parent ec6ec28 commit 3f8dd94
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 6 deletions.
73 changes: 67 additions & 6 deletions cli/cmd/compliance_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd

import (
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -53,17 +54,23 @@ Then, select one GUID from an integration and visualize its details using the co
return errors.Wrap(err, "unable to list gcp projects")
}

if len(response.Data) == 0 {
return errors.New("no data found for the provided organization")
}

// ALLY-431 Workaround to split the Project ID and Project Alias
// ultimately, we need to fix this in the API response
cliCompGcpProjects := splitGcpProjectsApiResponse(response.Data[0])

if cli.JSONOutput() {
return cli.OutputJSON(response.Data[0])
return cli.OutputJSON(cliCompGcpProjects)
}

rows := [][]string{}
for _, gcp := range response.Data {
for _, proj := range gcp.Projects {
rows = append(rows, []string{proj})
}
for _, project := range cliCompGcpProjects.Projects {
rows = append(rows, []string{project.ID, project.Alias})
}
cli.OutputHuman(renderSimpleTable([]string{"Projects"}, rows))
cli.OutputHuman(renderSimpleTable([]string{"Project ID", "Project Alias"}, rows))
return nil
},
}
Expand Down Expand Up @@ -242,3 +249,57 @@ func complianceGcpReportDetailsTable(report *api.ComplianceGcpReport) [][]string
[]string{"Report Time", report.ReportTime.UTC().Format(time.RFC3339)},
}
}

// ALLY-431 Workaround to split the Project ID and Project Alias
// ultimately, we need to fix this in the API response
func splitGcpProjectsApiResponse(gcpInfo api.CompGcpProjects) cliComplianceGcpInfo {
var (
orgID, orgAlias = splitIDAndAlias(gcpInfo.Organization)
cliGcpInfo = cliComplianceGcpInfo{
Organization: cliComplianceIDAlias{orgID, orgAlias},
Projects: make([]cliComplianceIDAlias, 0),
}
)

for _, project := range gcpInfo.Projects {
id, alias := splitIDAndAlias(project)
cliGcpInfo.Projects = append(cliGcpInfo.Projects, cliComplianceIDAlias{id, alias})
}

return cliGcpInfo
}

// @afiune we use named return in this function to be explicit about what is it
// that the function is returning, id and alias respectively
func splitIDAndAlias(text string) (id string, alias string) {
// Getting alias from text
aliasRegex := regexp.MustCompile(`\((.*?)\)`)
aliasBytes := aliasRegex.Find([]byte(text))
if len(aliasBytes) == 0 {
// if we couldn't get the alias from the provided text
// it means that the entire text is the id
id = text
return
}
alias = string(aliasBytes)
alias = strings.Trim(alias, "(")
alias = strings.Trim(alias, ")")

// Getting id from text
idRegex := regexp.MustCompile(`^(.*?)\(`)
idBytes := idRegex.Find([]byte(text))
id = string(idBytes)
id = strings.Trim(id, "(")
id = strings.TrimSpace(id)
return
}

type cliComplianceGcpInfo struct {
Organization cliComplianceIDAlias `json:"organization"`
Projects []cliComplianceIDAlias `json:"projects"`
}

type cliComplianceIDAlias struct {
ID string `json:"id"`
Alias string `json:"alias"`
}
116 changes: 116 additions & 0 deletions cli/cmd/compliance_gcp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Author:: Salim Afiune Maya (<[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 (
"fmt"
"testing"

"github.com/lacework/go-sdk/api"
"github.com/stretchr/testify/assert"
)

func TestSplitIDAndAlias(t *testing.T) {
cases := []struct {
subjectText string
expectedID string
expectedAlias string
}{
// empty text will return empty id and alias
{"", "", ""},
// alias should not be empty
{"()", "", ""},
// minimum text that can be splitted
{"a (b)", "a", "b"},
// if we couldn't get the alias from the provided text
// it means that the entire text is the id
{"1234567890", "1234567890", ""},
// other common test cases
{"1234567890 (alias-example)", "1234567890", "alias-example"},
{"proj-id-with-numbers (alias with spaces)", "proj-id-with-numbers", "alias with spaces"},
{"only-project-id-123", "only-project-id-123", ""},
// seriously, we should never have only the alias in the response ;-)
{"(this should never happen)", "", "this should never happen"},
}
for i, kase := range cases {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
actualID, actualAlias := splitIDAndAlias(kase.subjectText)
assert.Equalf(t, kase.expectedID, actualID, "wrong id")
assert.Equalf(t, kase.expectedAlias, actualAlias, "wrong alias")
})
}
}

func TestFixGcpProjectsApiResponse(t *testing.T) {
cases := []struct {
subject api.CompGcpProjects
expected cliComplianceGcpInfo
}{
// empty projects will return empty cli info
{
api.CompGcpProjects{},
cliComplianceGcpInfo{Projects: make([]cliComplianceIDAlias, 0)},
},
// real test case with NO alias
{
api.CompGcpProjects{
Organization: "1234567890123",
Projects: []string{"project-id-1", "project-id-2", "project-id-3", "project-id-4"},
},
cliComplianceGcpInfo{
Organization: cliComplianceIDAlias{"1234567890123", ""},
Projects: []cliComplianceIDAlias{
cliComplianceIDAlias{"project-id-1", ""},
cliComplianceIDAlias{"project-id-2", ""},
cliComplianceIDAlias{"project-id-3", ""},
cliComplianceIDAlias{"project-id-4", ""},
},
},
},
// real test case with alias
{
api.CompGcpProjects{
Organization: "1234567890123 (cool.org.alias.example.com)",
Projects: []string{
"id-1 (a test project)",
"xmen-project (serious alias)",
"disney-movies (Maybe Production)",
"foo (bar)",
},
},
cliComplianceGcpInfo{
Organization: cliComplianceIDAlias{"1234567890123", "cool.org.alias.example.com"},
Projects: []cliComplianceIDAlias{
cliComplianceIDAlias{"id-1", "a test project"},
cliComplianceIDAlias{"xmen-project", "serious alias"},
cliComplianceIDAlias{"disney-movies", "Maybe Production"},
cliComplianceIDAlias{"foo", "bar"},
},
},
},
}
for i, kase := range cases {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
assert.Equalf(t,
kase.expected, splitGcpProjectsApiResponse(kase.subject),
"there is a problem with this test case, please check",
)
})
}
}

0 comments on commit 3f8dd94

Please sign in to comment.