-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aquatiko
committed
Feb 18, 2024
1 parent
7182b7a
commit 7fabd19
Showing
7 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Licensed to Yugabyte, Inc. under one or more contributor license | ||
// agreements. See the NOTICE file distributed with this work for | ||
// additional information regarding copyright ownership. Yugabyte | ||
// licenses this file to you 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 db_audit | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"encoding/json" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client" | ||
"github.com/yugabyte/ybm-cli/internal/formatter" | ||
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal" | ||
) | ||
|
||
var DbAuditCmd = &cobra.Command{ | ||
Use: "db-audit", | ||
Short: "Manage DB Audit", | ||
Long: "Manage DB Audit", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
var associateDbAuditCmd = &cobra.Command{ | ||
Use: "associate", | ||
Short: "Associate DB Audit", | ||
Long: "Associate DB Audit", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
clusterId, _ := cmd.Flags().GetString("cluster-id") | ||
telemetryProviderId, _ := cmd.Flags().GetString("telemetry-provider-id") | ||
ysqlConfig, _ := cmd.Flags().GetString("ysql-config") | ||
|
||
fmt.Println(ysqlConfig) | ||
dbAuditExporterConfigSpec, err := setDbAuditExporterConfigSpec(ysqlConfig, telemetryProviderId) | ||
|
||
if err != nil { | ||
logrus.Fatalf(err.Error()) | ||
} | ||
|
||
authApi, err := ybmAuthClient.NewAuthApiClient() | ||
if err != nil { | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
authApi.GetInfo("", "") | ||
|
||
resp, r, err := authApi.AssociateDbAuditExporterConfig(clusterId).DbAuditExporterConfigSpec(*dbAuditExporterConfigSpec).Execute() | ||
|
||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", r) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
|
||
dbAudittelemetryProviderId := resp.GetData().Info.Id | ||
|
||
msg := fmt.Sprintf("The db audit exporter config %s is being created", formatter.Colorize(dbAudittelemetryProviderId, formatter.GREEN_COLOR)) | ||
|
||
fmt.Println(msg) | ||
|
||
dbAuditExporterCtx := formatter.Context{ | ||
Output: os.Stdout, | ||
Format: formatter.NewDbAuditFormat(viper.GetString("output")), | ||
} | ||
|
||
formatter.SingleDbAuditWrite(dbAuditExporterCtx, resp.GetData()) | ||
}, | ||
} | ||
|
||
var listDbAuditCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List DB Audit Export Config", | ||
Long: "List DB Audit Export Config", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
authApi, err := ybmAuthClient.NewAuthApiClient() | ||
if err != nil { | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
authApi.GetInfo("", "") | ||
|
||
clusterId, _ := cmd.Flags().GetString("cluster-id") | ||
|
||
resp, r, err := authApi.ListDbAuditExportConfigs(clusterId).Execute() | ||
|
||
if err != nil { | ||
logrus.Debugf("Full HTTP response: %v", r) | ||
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err)) | ||
} | ||
|
||
dbAuditExporterCtx := formatter.Context{ | ||
Output: os.Stdout, | ||
Format: formatter.NewDbAuditFormat(viper.GetString("output")), | ||
} | ||
|
||
if len(resp.GetData()) < 1 { | ||
fmt.Println("No DB audit Export config found") | ||
return | ||
} | ||
|
||
formatter.DbAuditWrite(dbAuditExporterCtx, resp.GetData()) | ||
}, | ||
} | ||
|
||
func init() { | ||
DbAuditCmd.AddCommand(associateDbAuditCmd) | ||
associateDbAuditCmd.Flags().SortFlags = false | ||
associateDbAuditCmd.Flags().String("telemetry-provider-id", "", "[REQUIRED] The ID of the telemetry provider") | ||
associateDbAuditCmd.MarkFlagRequired("telemetry-provider-id") | ||
associateDbAuditCmd.Flags().String("ysql-config", "", "[REQUIRED] The ysql config to setup DB auditting") | ||
associateDbAuditCmd.MarkFlagRequired("ysql-config") | ||
associateDbAuditCmd.Flags().String("cluster-id", "", "[REQUIRED] The cluster ID to associate DB auditting") | ||
associateDbAuditCmd.MarkFlagRequired("cluster-id") | ||
|
||
DbAuditCmd.AddCommand(listDbAuditCmd) | ||
listDbAuditCmd.Flags().SortFlags = false | ||
listDbAuditCmd.Flags().String("cluster-id", "", "[REQUIRED] The cluster ID to list DB audit export config") | ||
listDbAuditCmd.MarkFlagRequired("cluster-id") | ||
} | ||
|
||
func setDbAuditExporterConfigSpec(ysqlConfigString string, telemetryProviderId string) (*ybmclient.DbAuditExporterConfigSpec, error) { | ||
var ysqlConfig ybmclient.DbAuditYsqlExportConfig; | ||
tmp := []byte(ysqlConfigString) | ||
fmt.Println(tmp) | ||
fmt.Println(ysqlConfigString) | ||
var err error = json.Unmarshal(tmp, &ysqlConfig) | ||
|
||
if err != nil { | ||
logrus.Fatal("Failed to parse ysql config: invalid JSON format. ", err) | ||
} | ||
|
||
return ybmclient.NewDbAuditExporterConfigSpec(ysqlConfig ,telemetryProviderId), nil; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Licensed to Yugabyte, Inc. under one or more contributor license | ||
// agreements. See the NOTICE file distributed with this work for | ||
// additional information regarding copyright ownership. Yugabyte | ||
// licenses this file to you 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 formatter | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/sirupsen/logrus" | ||
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal" | ||
) | ||
|
||
const ( | ||
defaultDbAuditListing = "table {{.ID}}\t{{.CreatedAt}}\t{{.ClusterId}}\t{{.TelemetryProviderId}}\t{{.State}}\t{{.YsqlConfig}}" | ||
exportConfigCreatedAtHeader = "Date Created" | ||
clusterIdHeader = "Cluster ID" | ||
telemetryProviderIdHeader = "Telemetry Provider ID" | ||
exportConfigState = "State" | ||
ysqlConfigHeader = "Ysql Config" | ||
) | ||
|
||
type DbAuditContext struct { | ||
HeaderContext | ||
Context | ||
a ybmclient.DbAuditExporterConfigurationData | ||
} | ||
|
||
func NewDbAuditFormat(source string) Format { | ||
switch source { | ||
case "table", "": | ||
format := defaultDbAuditListing | ||
return Format(format) | ||
default: // custom format or json or pretty | ||
return Format(source) | ||
} | ||
} | ||
|
||
// DbAuditWrite renders the context for a list of Db Audit Export config | ||
func DbAuditWrite(ctx Context, dbAuditConfigs []ybmclient.DbAuditExporterConfigurationData) error { | ||
render := func(format func(subContext SubContext) error) error { | ||
for _, dbAuditConfig := range dbAuditConfigs { | ||
err := format(&DbAuditContext{a: dbAuditConfig}) | ||
if err != nil { | ||
logrus.Debugf("Error rendering DB Audit Config: %v", err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
return ctx.Write(NewDbAuditContext(), render) | ||
} | ||
|
||
func SingleDbAuditWrite(ctx Context, dbAuditConfig ybmclient.DbAuditExporterConfigurationData) error { | ||
render := func(format func(subContext SubContext) error) error { | ||
err := format(&DbAuditContext{a: dbAuditConfig}) | ||
if err != nil { | ||
logrus.Debugf("Error rendering DB Audit Config: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
return ctx.Write(NewDbAuditContext(), render) | ||
} | ||
|
||
// NewDbAuditContext creates a new context for rendering Db Audit Export Config | ||
func NewDbAuditContext() *DbAuditContext { | ||
dbAuditCtx := DbAuditContext{} | ||
dbAuditCtx.Header = SubHeaderContext{ | ||
"YsqlConfig": ysqlConfigHeader, | ||
"ID": "ID", | ||
"State": exportConfigState, | ||
"TelemetryProviderId": telemetryProviderIdHeader, | ||
"ClusterId": clusterIdHeader, | ||
"CreatedAt": exportConfigCreatedAtHeader, | ||
} | ||
return &dbAuditCtx | ||
} | ||
|
||
func (a *DbAuditContext) ID() string { | ||
return a.a.Info.Id | ||
} | ||
|
||
func (a *DbAuditContext) State() string { | ||
return string(a.a.Info.State) | ||
} | ||
|
||
func (a *DbAuditContext) TelemetryProviderId() string { | ||
return a.a.Spec.ExporterId | ||
} | ||
|
||
func (a *DbAuditContext) ClusterId() string { | ||
return a.a.Info.ClusterId | ||
} | ||
|
||
func (a *DbAuditContext) YsqlConfig() string { | ||
ysqlConfig, _ := json.Marshal(a.a.Spec.YsqlConfig) | ||
return string(ysqlConfig) | ||
} | ||
|
||
func (a *DbAuditContext) CreatedAt() string { | ||
return a.a.Info.Metadata.Get().GetCreatedOn() | ||
} | ||
|
||
func (a *DbAuditContext) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(a.a) | ||
} |