Skip to content

Commit

Permalink
refactor(provider): refactor ListLogEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd committed Jul 16, 2023
1 parent 7966278 commit e206ed2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
15 changes: 6 additions & 9 deletions plugins/providers/bigquery/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ func (r bqResource) fullURN() string {
return s
}

type ImportActivitiesFilter struct {
type importActivitiesFilter struct {
domain.ImportActivitiesFilter
Types []string
Authorizations []string
Limit int
}

type bqFilter ImportActivitiesFilter

func (f bqFilter) String() string {
func (f importActivitiesFilter) String() string {
criteria := []string{
`protoPayload.serviceName="bigquery.googleapis.com"`,
`resource.type="bigquery_dataset"`, // exclude logs for bigquery jobs ("bigquery_project")
Expand Down Expand Up @@ -204,16 +202,16 @@ func (f bqFilter) String() string {
return strings.Join(criteria, " AND ")
}

func (c *cloudLoggingClient) ListLogEntries(ctx context.Context, filter ImportActivitiesFilter) ([]*Activity, error) {
func (c *cloudLoggingClient) ListLogEntries(ctx context.Context, filter string, limit int) ([]*Activity, error) {
var entries []*Activity

options := []logadmin.EntriesOption{logadmin.Filter(bqFilter(filter).String())}
if filter.Limit > 0 {
options := []logadmin.EntriesOption{logadmin.Filter(filter)}
if limit > 0 {
options = append(options, logadmin.NewestFirst())
}
it := c.client.Entries(ctx, options...)
for {
if filter.Limit > 0 && len(entries) >= filter.Limit {
if limit > 0 && len(entries) >= limit {
break
}

Expand All @@ -228,7 +226,6 @@ func (c *cloudLoggingClient) ListLogEntries(ctx context.Context, filter ImportAc
entries = append(entries, &Activity{e})
}
}

return entries, nil
}

Expand Down
59 changes: 44 additions & 15 deletions plugins/providers/bigquery/mocks/cloudLoggingClientI.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions plugins/providers/bigquery/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BigQueryClient interface {
//go:generate mockery --name=cloudLoggingClientI --exported --with-expecter
type cloudLoggingClientI interface {
Close() error
ListLogEntries(context.Context, ImportActivitiesFilter) ([]*Activity, error)
ListLogEntries(context.Context, string, int) ([]*Activity, error)
}

//go:generate mockery --name=encryptor --exported --with-expecter
Expand Down Expand Up @@ -302,10 +302,11 @@ func (p *Provider) GetActivities(ctx context.Context, pd domain.Provider, filter
return nil, fmt.Errorf("initializing cloud logging client: %w", err)
}

entries, err := logClient.ListLogEntries(ctx, ImportActivitiesFilter{
activityFilter := importActivitiesFilter{
ImportActivitiesFilter: filter,
Types: BigQueryAuditMetadataMethods,
})
}
entries, err := logClient.ListLogEntries(ctx, activityFilter.String(), 0)
if err != nil {
return nil, fmt.Errorf("listing log entries: %w", err)
}
Expand Down
12 changes: 8 additions & 4 deletions plugins/providers/bigquery/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1084,10 +1085,13 @@ func (s *BigQueryProviderTestSuite) TestGetActivities_Success() {
},
},
}
expectedListLogEntriesFilter := strings.Join([]string{
`protoPayload.serviceName="bigquery.googleapis.com"`,
`resource.type="bigquery_dataset"`,
fmt.Sprintf(`protoPayload.methodName=("%s")`, strings.Join(bigquery.BigQueryAuditMetadataMethods, `" OR "`)),
}, ` AND `)
s.mockCloudLoggingClient.EXPECT().
ListLogEntries(mock.AnythingOfType("*context.emptyCtx"), bigquery.ImportActivitiesFilter{
Types: bigquery.BigQueryAuditMetadataMethods,
}).Return(expectedBigQueryActivities, nil).Once()
ListLogEntries(mock.AnythingOfType("*context.emptyCtx"), expectedListLogEntriesFilter, 0).Return(expectedBigQueryActivities, nil).Once()
s.mockBigQueryClient.EXPECT().
GetRolePermissions(mock.AnythingOfType("*context.emptyCtx"), "roles/bigquery.dataViewer").Return([]string{"bigquery.datasets.get"}, nil).Once()
s.mockBigQueryClient.EXPECT().
Expand Down Expand Up @@ -1176,7 +1180,7 @@ func (s *BigQueryProviderTestSuite) TestGetActivities_Success() {
s.Run("should return error if there is an error on listing log entries", func() {
expectedError := errors.New("error")
s.mockCloudLoggingClient.EXPECT().
ListLogEntries(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("bigquery.ImportActivitiesFilter")).Return(nil, expectedError).Once()
ListLogEntries(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string"), 0).Return(nil, expectedError).Once()

_, err := s.provider.GetActivities(context.Background(), *s.validProvider, domain.ImportActivitiesFilter{})

Expand Down

0 comments on commit e206ed2

Please sign in to comment.