github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/client/externalauditstorage/externalauditstorage.go (about)

     1  // Copyright 2023 Gravitational, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package externalauditstorage
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/gravitational/trace"
    21  
    22  	externalauditstoragev1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/externalauditstorage/v1"
    23  	"github.com/gravitational/teleport/api/types/externalauditstorage"
    24  	conv "github.com/gravitational/teleport/api/types/externalauditstorage/convert/v1"
    25  )
    26  
    27  // Client is an External Audit Storage client.
    28  type Client struct {
    29  	grpcClient externalauditstoragev1.ExternalAuditStorageServiceClient
    30  }
    31  
    32  // NewClient creates a new ExternalAuditStorage client.
    33  func NewClient(grpcClient externalauditstoragev1.ExternalAuditStorageServiceClient) *Client {
    34  	return &Client{
    35  		grpcClient: grpcClient,
    36  	}
    37  }
    38  
    39  // TestDraftExternalAuditStorageBuckets tests the connection to the current draft buckets.
    40  func (c *Client) TestDraftExternalAuditStorageBuckets(ctx context.Context) error {
    41  	_, err := c.grpcClient.TestDraftExternalAuditStorageBuckets(ctx, &externalauditstoragev1.TestDraftExternalAuditStorageBucketsRequest{})
    42  	return trace.Wrap(err)
    43  }
    44  
    45  // TestDraftExternalAuditStorageGlue tests the configuration to the current draft glue table and database.
    46  func (c *Client) TestDraftExternalAuditStorageGlue(ctx context.Context) error {
    47  	_, err := c.grpcClient.TestDraftExternalAuditStorageGlue(ctx, &externalauditstoragev1.TestDraftExternalAuditStorageGlueRequest{})
    48  	return trace.Wrap(err)
    49  }
    50  
    51  // TestDraftExternalAuditStorageAthena tests the configuration to the current draft athena.
    52  func (c *Client) TestDraftExternalAuditStorageAthena(ctx context.Context) error {
    53  	_, err := c.grpcClient.TestDraftExternalAuditStorageAthena(ctx, &externalauditstoragev1.TestDraftExternalAuditStorageAthenaRequest{})
    54  	return trace.Wrap(err)
    55  }
    56  
    57  // GetDraftExternalAuditStorage returns the draft External Audit Storage configuration resource.
    58  func (c *Client) GetDraftExternalAuditStorage(ctx context.Context) (*externalauditstorage.ExternalAuditStorage, error) {
    59  	resp, err := c.grpcClient.GetDraftExternalAuditStorage(ctx, &externalauditstoragev1.GetDraftExternalAuditStorageRequest{})
    60  	if err != nil {
    61  		return nil, trace.Wrap(err)
    62  	}
    63  	externalAudit, err := conv.FromProtoDraft(resp.GetExternalAuditStorage())
    64  	return externalAudit, trace.Wrap(err)
    65  }
    66  
    67  // CreateDraftExternalAuditStorage creates a draft External Audit Storage
    68  // resource if one does not already exist.
    69  func (c *Client) CreateDraftExternalAuditStorage(ctx context.Context, in *externalauditstorage.ExternalAuditStorage) (*externalauditstorage.ExternalAuditStorage, error) {
    70  	resp, err := c.grpcClient.CreateDraftExternalAuditStorage(ctx, &externalauditstoragev1.CreateDraftExternalAuditStorageRequest{
    71  		ExternalAuditStorage: conv.ToProto(in),
    72  	})
    73  	if err != nil {
    74  		return nil, trace.Wrap(err)
    75  	}
    76  	out, err := conv.FromProtoDraft(resp.GetExternalAuditStorage())
    77  	return out, trace.Wrap(err)
    78  }
    79  
    80  // UpsertDraftExternalAuditStorage upserts a draft External Audit Storage resource.
    81  func (c *Client) UpsertDraftExternalAuditStorage(ctx context.Context, in *externalauditstorage.ExternalAuditStorage) (*externalauditstorage.ExternalAuditStorage, error) {
    82  	resp, err := c.grpcClient.UpsertDraftExternalAuditStorage(ctx, &externalauditstoragev1.UpsertDraftExternalAuditStorageRequest{
    83  		ExternalAuditStorage: conv.ToProto(in),
    84  	})
    85  	if err != nil {
    86  		return nil, trace.Wrap(err)
    87  	}
    88  	out, err := conv.FromProtoDraft(resp.GetExternalAuditStorage())
    89  	return out, trace.Wrap(err)
    90  }
    91  
    92  // GenerateDraftExternalAuditStorage create a new draft External Audit Storage
    93  // resource with randomized resource names and upserts it as the current
    94  // draft.
    95  func (c *Client) GenerateDraftExternalAuditStorage(ctx context.Context, integrationName, region string) (*externalauditstorage.ExternalAuditStorage, error) {
    96  	resp, err := c.grpcClient.GenerateDraftExternalAuditStorage(ctx, &externalauditstoragev1.GenerateDraftExternalAuditStorageRequest{
    97  		IntegrationName: integrationName,
    98  		Region:          region,
    99  	})
   100  	if err != nil {
   101  		return nil, trace.Wrap(err)
   102  	}
   103  	out, err := conv.FromProtoDraft(resp.GetExternalAuditStorage())
   104  	return out, trace.Wrap(err)
   105  }
   106  
   107  // DeleteDraftExternalAuditStorage removes draft External Audit Storage resource.
   108  func (c *Client) DeleteDraftExternalAuditStorage(ctx context.Context) error {
   109  	_, err := c.grpcClient.DeleteDraftExternalAuditStorage(ctx, &externalauditstoragev1.DeleteDraftExternalAuditStorageRequest{})
   110  	return trace.Wrap(err)
   111  }
   112  
   113  // PromoteToClusterExternalAuditStorage promotes the current draft External
   114  // Audit Storage configuration to be used in the cluster.
   115  func (c *Client) PromoteToClusterExternalAuditStorage(ctx context.Context) error {
   116  	_, err := c.grpcClient.PromoteToClusterExternalAuditStorage(ctx, &externalauditstoragev1.PromoteToClusterExternalAuditStorageRequest{})
   117  	return trace.Wrap(err)
   118  }
   119  
   120  // GetClusterExternalAuditStorage gets cluster External Audit Storage.
   121  func (c *Client) GetClusterExternalAuditStorage(ctx context.Context) (*externalauditstorage.ExternalAuditStorage, error) {
   122  	resp, err := c.grpcClient.GetClusterExternalAuditStorage(ctx, &externalauditstoragev1.GetClusterExternalAuditStorageRequest{})
   123  	if err != nil {
   124  		return nil, trace.Wrap(err)
   125  	}
   126  	externalAudit, err := conv.FromProtoCluster(resp.GetClusterExternalAuditStorage())
   127  	return externalAudit, trace.Wrap(err)
   128  }
   129  
   130  // DisableClusterExternalAuditStorage disables the External Audit Storage feature,
   131  // which means default cloud audit will be used.
   132  func (c *Client) DisableClusterExternalAuditStorage(ctx context.Context) error {
   133  	_, err := c.grpcClient.DisableClusterExternalAuditStorage(ctx, &externalauditstoragev1.DisableClusterExternalAuditStorageRequest{})
   134  	return trace.Wrap(err)
   135  }