github.com/pingcap/tiup@v1.15.1/components/cluster/command/audit.go (about)

     1  // Copyright 2020 PingCAP, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package command
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/tiup/pkg/cluster/audit"
    19  	"github.com/pingcap/tiup/pkg/cluster/spec"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  // retainDay number of days to keep audit logs for deletion
    24  var retainDays int
    25  
    26  func newAuditCmd() *cobra.Command {
    27  	cmd := &cobra.Command{
    28  		Use:   "audit [audit-id]",
    29  		Short: "Show audit log of cluster operation",
    30  		RunE: func(cmd *cobra.Command, args []string) error {
    31  			switch len(args) {
    32  			case 0:
    33  				return audit.ShowAuditList(spec.AuditDir())
    34  			case 1:
    35  				return audit.ShowAuditLog(spec.AuditDir(), args[0])
    36  			default:
    37  				return cmd.Help()
    38  			}
    39  		},
    40  	}
    41  	cmd.AddCommand(newAuditCleanupCmd())
    42  	return cmd
    43  }
    44  
    45  func newAuditCleanupCmd() *cobra.Command {
    46  	cmd := &cobra.Command{
    47  		Use:   "cleanup",
    48  		Short: "cleanup cluster audit logs",
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			if retainDays < 0 {
    51  				return errors.Errorf("retain-days cannot be less than 0")
    52  			}
    53  
    54  			err := audit.DeleteAuditLog(spec.AuditDir(), retainDays, skipConfirm, gOpt.DisplayMode)
    55  			if err != nil {
    56  				return err
    57  			}
    58  			return nil
    59  		},
    60  	}
    61  
    62  	cmd.Flags().IntVar(&retainDays, "retain-days", 60, "Number of days to keep audit logs for deletion")
    63  	return cmd
    64  }