Skip to content

Commit

Permalink
refactor(integration): move CRUD gcp config code
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `NewGCPIntegrationData()` has been changed and
renamed to the new standard, users must switch to use
`NewGCPIntegration()`

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Mar 19, 2020
1 parent 0f83504 commit 962191b
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 129 deletions.
153 changes: 24 additions & 129 deletions api/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,94 +26,44 @@ import (
type integrationType int

const (
// awsCFG - AWS Config integration type
awsCFG integrationType = iota
// AWS Config integration type
AwsCfgIntegration integrationType = iota

// awsCT - AWS CloudTrail integration type
awsCT
// AWS CloudTrail integration type
AwsCloudTrailIntegration

// gcpCFG - GCP Config integration type
gcpCFG
// GCP Config integration type
GcpCfgIntegration

// gcpAT - GCP Audit Log integration type
gcpAT
// GCP Audit Log integration type
GcpAuditLogIntegration

// azureCFG - Azure Config integration type
azureCFG
// Azure Config integration type
AzureCfgIntegration

// azureAL - Azure Activity Log integration type
azureAL
// Azure Activity Log integration type
AzureActivityLogIntegration
)

var integrationTypes = map[integrationType]string{
awsCFG: "AWS_CFG",
awsCT: "AWS_CT_SQS",
gcpCFG: "GCP_CFG",
gcpAT: "GCP_AT_SES",
azureCFG: "AZURE_CFG",
azureAL: "AZURE_AL_SEQ",
AwsCfgIntegration: "AWS_CFG",
AwsCloudTrailIntegration: "AWS_CT_SQS",
GcpCfgIntegration: "GCP_CFG",
GcpAuditLogIntegration: "GCP_AT_SES",
AzureCfgIntegration: "AZURE_CFG",
AzureActivityLogIntegration: "AZURE_AL_SEQ",
}

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

// gcpResourceLevel determines 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 = map[gcpResourceLevel]string{
GcpProject: "PROJECT",
GcpOrganization: "ORGANIZATION",
}

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

// GetIntegrations lists the external integrations available on the server
func (c *Client) GetIntegrations() (response integrationsResponse, err error) {
err = c.RequestDecoder("GET", apiIntegrations, nil, &response)
return
}

func (c *Client) GetGCPIntegrations() (response gcpIntegrationsResponse, err error) {
return
}
func (c *Client) GetAzureIntegrations() (response azureIntegrationsResponse, err error) {
return
}
func (c *Client) GetAWSIntegrations() (response awsIntegrationsResponse, err error) {
return
}

// NewGCPIntegrationData returns an instance of gcpIntegrationData
func NewGCPIntegrationData(name string, idType gcpResourceLevel) gcpIntegrationData {
return gcpIntegrationData{
commonIntegrationData: commonIntegrationData{
Name: name,
Type: gcpCFG.String(),
Enabled: 1,
},
Data: gcpCfg{
IdType: idType.String(),
},
}
}

// CreateGCPConfigIntegration creates a single integration on the server
func (c *Client) CreateGCPConfigIntegration(data gcpIntegrationData) (response gcpIntegrationsResponse, err error) {
err = c.createIntegration(data, &response)
return
}

func (c *Client) createIntegration(data interface{}, response interface{}) error {
body, err := jsonReader(data)
if err != nil {
Expand All @@ -124,23 +74,11 @@ func (c *Client) createIntegration(data interface{}, response interface{}) error
return err
}

// GetGCPConfigIntegration gets a single integration matching the integration guid available on the server
func (c *Client) GetGCPConfigIntegration(intgGuid string) (response gcpIntegrationsResponse, err error) {
err = c.getIntegration(intgGuid, &response)
return
}

func (c *Client) getIntegration(intgGuid string, response interface{}) error {
apiPath := fmt.Sprintf(apiIntegrationByGUID, intgGuid)
return c.RequestDecoder("GET", apiPath, nil, response)
}

// UpdateGCPConfigIntegration updates a single integration on the server
func (c *Client) UpdateGCPConfigIntegration(data gcpIntegrationData) (response gcpIntegrationsResponse, err error) {
err = c.updateIntegration(data.IntgGuid, data, &response)
return
}

func (c *Client) updateIntegration(intgGuid string, data interface{}, response interface{}) error {
body, err := jsonReader(data)
if err != nil {
Expand All @@ -152,12 +90,6 @@ func (c *Client) updateIntegration(intgGuid string, data interface{}, response i
return err
}

// DeleteGCPConfigIntegration gets a single integration matching the integration guid available on the server
func (c *Client) DeleteGCPConfigIntegration(intgGuid string) (response gcpIntegrationsResponse, err error) {
err = c.deleteIntegration(intgGuid, &response)
return
}

func (c *Client) deleteIntegration(intgGuid string, response interface{}) error {
apiPath := fmt.Sprintf(apiIntegrationByGUID, intgGuid)
return c.RequestDecoder("DELETE", apiPath, nil, response)
Expand All @@ -175,6 +107,12 @@ type commonIntegrationData struct {
TypeName string `json:"TYPE_NAME,omitempty"`
}

type state struct {
Ok bool `json:"ok"`
LastUpdatedTime string `json:"lastUpdatedTime"`
LastSuccessfulTime string `json:"lastSuccessfulTime"`
}

type integrationsResponse struct {
Data []commonIntegrationData `json:"data"`
Ok bool `json:"ok"`
Expand All @@ -188,46 +126,3 @@ func (integrations *integrationsResponse) List() string {
}
return strings.Join(out, "\n")
}

type state struct {
Ok bool `json:"ok"`
LastUpdatedTime string `json:"lastUpdatedTime"`
LastSuccessfulTime string `json:"lastSuccessfulTime"`
}

type awsIntegrationsResponse struct {
//Data []gcpIntegrationData `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}
type azureIntegrationsResponse struct {
//Data []gcpIntegrationData `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}

type gcpIntegrationsResponse struct {
Data []gcpIntegrationData `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}

type gcpIntegrationData struct {
commonIntegrationData
Data gcpCfg `json:"DATA"`
}

type gcpCfg struct {
ID string `json:"ID"`
IdType string `json:"ID_TYPE"`
IssueGrouping string `json:"ISSUE_GROUPING,omitempty"`
Credentials gcpCredentials `json:"CREDENTIALS"`
SubscriptionName string `json:"SUBSCRIPTION_NAME,omitempty"`
}

type gcpCredentials struct {
ClientId string `json:"CLIENT_ID"`
ClientEmail string `json:"CLIENT_EMAIL"`
PrivateKeyId string `json:"PRIVATE_KEY_ID"`
PrivateKey string `json:"PRIVATE_KEY"`
}
129 changes: 129 additions & 0 deletions api/integrations_gcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Author:: Salim Afiune Maya (<[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 api

// gcpResourceLevel determines Project or Organization level integration
type gcpResourceLevel int

const (
// Project level integration with GCP
GcpProjectIntegration gcpResourceLevel = iota

// Organization level integration with GCP
GcpOrganizationIntegration
)

var gcpResourceLevels = map[gcpResourceLevel]string{
GcpProjectIntegration: "PROJECT",
GcpOrganizationIntegration: "ORGANIZATION",
}

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

// NewGcpIntegration returns an instance of gcpIntegration
//
// Basic usage: Initialize a new gcpIntegration struct, then
// use the new instance to do CRUD operations
//
// gcp, err := api.NewGcpIntegration("abc",
// api.GcpIntegrationData{
// ID: "1234",
// Credentials: api.GcpCredentials{
// ClientId: "id",
// ClientEmail: "email",
// PrivateKeyId: "key_id",
// PrivateKey: "key",
// },
// },
// )
// if err != nil {
// return err
// }
//
// integrationResponse, err := api.CreateGcpConfigIntegration(gcp)
// if err != nil {
// return err
// }
//
func NewGcpIntegration(name string, data GcpIntegrationData) gcpIntegration {
return gcpIntegration{
commonIntegrationData: commonIntegrationData{
Name: name,
Type: GcpCfgIntegration.String(),
Enabled: 1,
},
Data: data,
}
}

// CreateGcpConfigIntegration creates a single GCP_CFG integration on the Lacework Server
func (c *Client) CreateGcpConfigIntegration(data gcpIntegration) (response gcpIntegrationsResponse, err error) {
err = c.createIntegration(data, &response)
return
}

// GetGcpConfigIntegration gets a single integration matching the integration guid available on the server
func (c *Client) GetGcpConfigIntegration(intgGuid string) (response gcpIntegrationsResponse, err error) {
err = c.getIntegration(intgGuid, &response)
return
}

// UpdateGcpConfigIntegration updates a single integration on the server
func (c *Client) UpdateGcpConfigIntegration(data gcpIntegration) (response gcpIntegrationsResponse, err error) {
err = c.updateIntegration(data.IntgGuid, data, &response)
return
}

// DeleteGcpConfigIntegration gets a single integration matching the integration guid available on the server
func (c *Client) DeleteGcpConfigIntegration(intgGuid string) (response gcpIntegrationsResponse, err error) {
err = c.deleteIntegration(intgGuid, &response)
return
}

func (c *Client) GetGcpIntegrations() (response gcpIntegrationsResponse, err error) {
return
}

type gcpIntegrationsResponse struct {
Data []gcpIntegration `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}

type gcpIntegration struct {
commonIntegrationData
Data GcpIntegrationData `json:"DATA"`
}

type GcpIntegrationData struct {
ID string `json:"ID"`
IdType string `json:"ID_TYPE"`
IssueGrouping string `json:"ISSUE_GROUPING,omitempty"`
Credentials GcpCredentials `json:"CREDENTIALS"`
SubscriptionName string `json:"SUBSCRIPTION_NAME,omitempty"`
}

type GcpCredentials struct {
ClientId string `json:"CLIENT_ID"`
ClientEmail string `json:"CLIENT_EMAIL"`
PrivateKeyId string `json:"PRIVATE_KEY_ID"`
PrivateKey string `json:"PRIVATE_KEY"`
}

0 comments on commit 962191b

Please sign in to comment.