github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/secreport/secreport.go (about) 1 /* 2 Copyright 2023 Gravitational, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package secreport 18 19 import ( 20 "context" 21 22 "github.com/gravitational/trace" 23 "github.com/gravitational/trace/trail" 24 25 pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/secreports/v1" 26 "github.com/gravitational/teleport/api/types/secreports" 27 v1 "github.com/gravitational/teleport/api/types/secreports/convert/v1" 28 ) 29 30 // Client is a gRPC implementation of SecReportsService. 31 type Client struct { 32 grpcClient pb.SecReportsServiceClient 33 } 34 35 func (c *Client) GetSecurityAuditQueries(ctx context.Context) ([]*secreports.AuditQuery, error) { 36 var items []*pb.AuditQuery 37 nextKey := "" 38 for { 39 resp, err := c.grpcClient.ListAuditQueries(ctx, &pb.ListAuditQueriesRequest{ 40 PageSize: 0, 41 PageToken: nextKey, 42 }) 43 if err != nil { 44 return nil, trace.Wrap(err) 45 } 46 items = append(items, resp.GetQueries()...) 47 if resp.GetNextPageToken() == "" { 48 break 49 } 50 nextKey = resp.GetNextPageToken() 51 } 52 out, err := v1.FromProtoAuditQueries(items) 53 if err != nil { 54 return nil, trace.Wrap(err) 55 } 56 return out, nil 57 } 58 59 func (c *Client) ListSecurityAuditQueries(ctx context.Context, size int, token string) ([]*secreports.AuditQuery, string, error) { 60 resp, err := c.grpcClient.ListAuditQueries(ctx, &pb.ListAuditQueriesRequest{ 61 PageSize: int32(size), 62 PageToken: token, 63 }) 64 if err != nil { 65 return nil, "", trace.Wrap(err) 66 } 67 68 out, err := v1.FromProtoAuditQueries(resp.Queries) 69 if err != nil { 70 return nil, "", trace.Wrap(err) 71 } 72 return out, resp.GetNextPageToken(), nil 73 74 } 75 76 func (c *Client) GetSecurityReports(ctx context.Context) ([]*secreports.Report, error) { 77 var resources []*pb.Report 78 nextKey := "" 79 for { 80 resp, err := c.grpcClient.ListReports(ctx, &pb.ListReportsRequest{ 81 PageSize: 0, 82 PageToken: nextKey, 83 }) 84 if err != nil { 85 return nil, trace.Wrap(err) 86 } 87 resources = append(resources, resp.GetReports()...) 88 if resp.GetNextPageToken() == "" { 89 break 90 } 91 nextKey = resp.GetNextPageToken() 92 } 93 out := make([]*secreports.Report, 0, len(resources)) 94 for _, v := range resources { 95 item, err := v1.FromProtoReport(v) 96 if err != nil { 97 return nil, trace.Wrap(err) 98 } 99 out = append(out, item) 100 } 101 return out, nil 102 } 103 104 func (c *Client) ListSecurityReports(ctx context.Context, pageSize int, token string) ([]*secreports.Report, string, error) { 105 resp, err := c.grpcClient.ListReports(ctx, &pb.ListReportsRequest{ 106 PageSize: int32(pageSize), 107 PageToken: token, 108 }) 109 if err != nil { 110 return nil, "", trace.Wrap(err) 111 } 112 reports, err := v1.FromProtoReports(resp.Reports) 113 if err != nil { 114 return nil, "", trace.Wrap(err) 115 } 116 return reports, resp.GetNextPageToken(), nil 117 } 118 119 // GetSecurityReportExecutionState returns the execution state of the report. 120 func (c *Client) GetSecurityReportExecutionState(ctx context.Context, name string, days int32) (*secreports.ReportState, error) { 121 resp, err := c.grpcClient.GetReportState(ctx, &pb.GetReportStateRequest{ 122 Name: name, 123 Days: uint32(days), 124 }) 125 if err != nil { 126 return nil, trail.FromGRPC(err) 127 } 128 out, err := v1.FromProtoReportState(resp) 129 if err != nil { 130 return nil, trace.Wrap(err) 131 } 132 return out, nil 133 } 134 135 // NewClient creates a new SecReports client. 136 func NewClient(grpcClient pb.SecReportsServiceClient) *Client { 137 return &Client{ 138 grpcClient: grpcClient, 139 } 140 } 141 142 // GetSchema returns the schema for the audit query language. 143 func (c *Client) GetSchema(ctx context.Context) (*pb.GetSchemaResponse, error) { 144 resp, err := c.grpcClient.GetSchema(ctx, &pb.GetSchemaRequest{}) 145 if err != nil { 146 return nil, trail.FromGRPC(err) 147 } 148 return resp, nil 149 } 150 151 // RunAuditQueryAndGetResult runs an audit query and returns the result. 152 func (c *Client) RunAuditQueryAndGetResult(ctx context.Context, queryText string, days int) ([]*pb.QueryRowResult, error) { 153 resp, err := c.RunAuditQuery(ctx, queryText, days) 154 if err != nil { 155 return nil, trace.Wrap(err) 156 } 157 rows, err := c.GetAuditQueryResultAll(ctx, resp.GetResultId()) 158 if err != nil { 159 return nil, trace.Wrap(err) 160 } 161 return rows, nil 162 } 163 164 // RunAuditQuery runs an audit query. 165 func (c *Client) RunAuditQuery(ctx context.Context, queryText string, days int) (*pb.RunAuditQueryResponse, error) { 166 resp, err := c.grpcClient.RunAuditQuery(ctx, &pb.RunAuditQueryRequest{ 167 Query: queryText, 168 Days: int32(days), 169 }) 170 if err != nil { 171 return nil, trail.FromGRPC(err) 172 } 173 return resp, nil 174 } 175 176 // GetAuditQueryResultAll returns all results for an audit query. 177 func (c *Client) GetAuditQueryResultAll(ctx context.Context, queryID string) ([]*pb.QueryRowResult, error) { 178 var out []*pb.QueryRowResult 179 var nextToken string 180 for { 181 resp, err := c.grpcClient.GetAuditQueryResult(ctx, &pb.GetAuditQueryResultRequest{ 182 ResultId: queryID, 183 NextToken: nextToken, 184 }) 185 if err != nil { 186 return nil, trail.FromGRPC(err) 187 } 188 out = append(out, resp.Result.GetRows()...) 189 if resp.GetNextToken() == "" { 190 break 191 } 192 nextToken = resp.GetNextToken() 193 } 194 return out, nil 195 } 196 197 // DeleteSecurityReport deletes a security report. 198 func (c *Client) DeleteSecurityReport(ctx context.Context, name string) error { 199 _, err := c.grpcClient.DeleteReport(ctx, &pb.DeleteReportRequest{ 200 Name: name, 201 }) 202 if err != nil { 203 return trail.FromGRPC(err) 204 } 205 return nil 206 }