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

Creating and Getting GCP Integration #18

Merged
4 commits merged into from
Mar 13, 2020
Merged

Creating and Getting GCP Integration #18

4 commits merged into from
Mar 13, 2020

Conversation

mjunglw
Copy link
Contributor

@mjunglw mjunglw commented Mar 11, 2020

Reviewed a bit with @afiunelw and @lwmobeent but we were cut a little short.
Thought it would be best to raise a PR so everyone could see.

CreateGCPConfigIntegration

calls /api/v2/external/integrations Lacework API to create a GCP integration
Example (similarly in integration_test.go):

// just some setup
intgGUID := "integration guid"
account := "test"
token := "token"
c, err := api.NewClient(account, api.WithToken(token))

// getting new gcpIntegrationData instance from api
data := api.NewGCPIntegrationData("integration_name", api.GcpProject)
data.Data.ID = "xxxxxxxxxx"
data.Data.Credentials.ClientId = "xxxxxxxxx"
data.Data.Credentials.ClientEmail = "[email protected]"
data.Data.Credentials.PrivateKeyId = "xxxxxxxxxxxxxxxx"

// calling actual client code
response, err := c.CreateGCPConfigIntegration(data)

GetGCPConfigIntegration

calls /api/v2/external/integrations/<INTG_GUID> Lacework API to get a GCP integration with integration guid
Example (similarly in integration_test.go):

// just some setup
intgGUID := "integration guid"
account := "test"
token := "token"
c, err := api.NewClient(account, api.WithToken(token))

// calling actual client code
response, err := c.GetGCPConfigIntegration(intgGUID)

Go's Constants / Enums

Using iota in go:
Discussed with @afiunelw and @lwmobeent. Referenced here on golang github and here on a random website that I googled.

For Integration Type:

type integrationType int

const (
	// awsCFG - AWS Config integration type
	awsCFG integrationType = iota
	// awsCT - AWS CloudTrail integration type
	awsCT
	// gcpCFG - GCP Config integration type
	gcpCFG
	// gcpAT - GCP Audit Log integration type
	gcpAT
	// azureCFG - Azure Config integration type
	azureCFG
	// azureAL - Azure Activity Log integration type
	azureAL
)

var integrationTypes = []string{
	"AWS_CFG",
	"AWS_CT_SQS",
	"GCP_CFG",
	"GCP_AT_SES",
	"AZURE_CFG",
	"AZURE_AL_SEQ",
}

func (i integrationType) String() string {
	return integrationTypes[i]
}

For GCP resource level integration (customers will tell us whether they want a project or organization level integration):

type gcpResourceLevel int

const (
	// GcpProject level integration with GCP
	GcpProject gcpResourceLevel = iota
	// GcpOrganization level integration with GCP
	GcpOrganization
)

var gcpResourceLevels = []string{
	"PROJECT",
	"ORGANIZATION",
}

func (g gcpResourceLevel) String() string {
	return gcpResourceLevels[g]
}

Closes #17

@mjunglw mjunglw requested review from lwmobeent, scottford-lw and a user March 11, 2020 20:14
@mjunglw mjunglw self-assigned this Mar 11, 2020
ghost
ghost previously requested changes Mar 12, 2020
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Nicely done, I like that you opened a PR to start the code review and you also added
an explanation, thank you @mjunglw - I have a few things we have to change.

api/integrations.go Outdated Show resolved Hide resolved
api/integrations.go Outdated Show resolved Hide resolved
api/integrations.go Show resolved Hide resolved
api/integrations.go Outdated Show resolved Hide resolved
api/integrations.go Outdated Show resolved Hide resolved
api/integrations.go Outdated Show resolved Hide resolved
@mjunglw mjunglw requested a review from a user March 13, 2020 00:06
@mjunglw mjunglw dismissed ghost ’s stale review March 13, 2020 00:07

Updated commit with changes

api/integrations.go Outdated Show resolved Hide resolved
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Fantastic!

tenor-14284579

// azureCFG

// azureAL - Azure Activity Log integration type
// azureAL
Copy link

@ghost ghost Mar 13, 2020

Choose a reason for hiding this comment

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

Now that I think about it, you have to be very careful here, the fact that you add a single
constant with the keyword iota it means that you will have that constant set to 0
(gcpCFG = 0)

I bet you that this is a bug! if we call gcpCFG.String() it will return AWS_CFG

That is why I always recommend using this pattern:

var integrationTypes = map[integrationType]string{}

Mainly because you won't have to worry about the order of the array or index etc.
You just define them and it automatically works.

I would suggest implementing this pattern instead 👆🏽

Copy link

@ghost ghost Mar 13, 2020

Choose a reason for hiding this comment

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

Look at this shippable job link

=== RUN   TestCreateGCPConfigIntegration
    TestCreateGCPConfigIntegration: integrations_test.go:78: 
        	Error Trace:	integrations_test.go:78
        	Error:      	Not equal: 
        	            	expected: "GCP_CFG"
        	            	actual  : "AWS_CFG"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-GCP_CFG
        	            	+AWS_CFG
        	Test:       	TestCreateGCPConfigIntegration
        	Messages:   	a new GCP integration should match its type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you were right. Now I see. For refactoring / adding new features, the map seems simpler.

Comment on lines +46 to +52
const (
// GcpProject level integration with GCP
GcpProject gcpResourceLevel = iota

// GcpOrganization level integration with GCP
GcpOrganization
)
Copy link

Choose a reason for hiding this comment

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

Since all these are global constants, we should make sure to call them appropriately,
It is a good practice to be over descriptive with these kinds of things, just like we
sometimes are with function names. Especially since these ones are public 😉

I would propose:

Suggested change
const (
// GcpProject level integration with GCP
GcpProject gcpResourceLevel = iota
// GcpOrganization level integration with GCP
GcpOrganization
)
const (
// a project level integration with GCP
GcpProjectLevelIntegration gcpResourceLevel = iota
// an organization level integration with GCP
GcpOrganizationLevelIntegration
)

Copy link

Choose a reason for hiding this comment

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

Let us do this in a further PR, I want us to keep iteration over this. Great work James!

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

tenor-152905374

@ghost ghost merged commit 5cc74db into master Mar 13, 2020
@mjunglw mjunglw deleted the james/create-get-gcp branch March 16, 2020 00:50
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create and Read GCP Integration
2 participants