Enable/Disable a reporting source (#3183)

* Enable/Disable a reporting source

* misc options update

Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
This commit is contained in:
Jaideep Khandelwal
2023-01-15 22:21:16 +05:30
committed by GitHub
parent 84b0581306
commit a1642be911
6 changed files with 112 additions and 1 deletions

View File

@@ -306,17 +306,20 @@ on extensive configurability, massive extensibility and ease of use.`)
flagSet.StringVarP(&options.AddTarget, "add-target", "atr", "", "add target(s) to cloud"),
flagSet.StringVarP(&options.AddTemplate, "add-template", "atm", "", "add template(s) to cloud"),
flagSet.BoolVarP(&options.ScanList, "list-scan", "lsn", false, "list previous cloud scans"),
flagSet.StringVarP(&options.ScanOutput, "list-output", "lso", "", "list scan output by scan id"),
flagSet.BoolVarP(&options.ListTargets, "list-target", "ltr", false, "list cloud target by id"),
flagSet.BoolVarP(&options.ListTemplates, "list-template", "ltm", false, "list cloud template by id"),
flagSet.BoolVarP(&options.ListDatasources, "list-datasource", "lds", false, "list cloud datasource by id"),
flagSet.BoolVarP(&options.ListReportingSources, "list-reportsource", "lrs", false, "list reporting sources"),
flagSet.StringVarP(&options.DeleteScan, "delete-scan", "dsn", "", "delete cloud scan by id"),
flagSet.StringVarP(&options.RemoveTarget, "delete-target", "dtr", "", "delete target(s) from cloud"),
flagSet.StringVarP(&options.RemoveTemplate, "delete-template", "dtm", "", "delete template(s) from cloud"),
flagSet.StringVarP(&options.RemoveDatasource, "delete-datasource", "dds", "", "delete specified data source"),
flagSet.StringVarP(&options.DisableReportingSource, "disable-reportsource", "drs", "", "disable specified reporting source"),
flagSet.StringVarP(&options.EnableReportingSource, "enable-reportsource", "ers", "", "enable specified reporting source"),
flagSet.StringVarP(&options.GetTarget, "get-target", "gtr", "", "get target content by id"),
flagSet.StringVarP(&options.GetTemplate, "get-template", "gtm", "", "get template content by id"),
flagSet.BoolVarP(&options.NoStore, "no-store", "nos", false, "disable scan/output storage on cloud"),
flagSet.StringVarP(&options.ScanOutput, "scan-output", "sno", "", "display scan output by scan id"),
flagSet.BoolVar(&options.NoTables, "no-tables", false, "do not display pretty-printed tables"),
flagSet.IntVar(&options.OutputLimit, "limit", 100, "limit the number of output to display"),
)

View File

@@ -83,6 +83,33 @@ func (r *Runner) listDatasources() error {
return nil
}
func (r *Runner) listReportingSources() error {
items, err := r.cloudClient.ListReportingSources()
if err != nil {
return err
}
if len(items) == 0 {
return errors.New("no reporting source found")
}
header := []string{"ID", "Type", "ProjectName", "Enabled"}
var values [][]string
for _, source := range items {
if r.options.JSON {
_ = jsoniter.NewEncoder(os.Stdout).Encode(source)
} else if !r.options.NoTables {
values = append(values, []string{strconv.FormatInt(source.ID, 10), source.Type, source.ProjectName, strconv.FormatBool(source.Enabled)})
} else {
gologger.Silent().Msgf("%d. [%s] [%s] [%t]", source.ID, source.Type, source.ProjectName, source.Enabled)
}
}
if !r.options.NoTables {
r.prettyPrintTable(header, values)
}
return nil
}
func (r *Runner) listTargets() error {
items, err := r.cloudClient.ListTargets("")
if err != nil {
@@ -217,6 +244,23 @@ func (r *Runner) removeDatasource(datasource string) error {
return err
}
func (r *Runner) toggleReportingSource(source string, status bool) error {
ID, parseErr := strconv.ParseInt(source, 10, 64)
if parseErr != nil {
return errors.Wrap(parseErr, "could not parse reporting source id")
}
err := r.cloudClient.ToggleReportingSource(ID, status)
if err == nil {
t := "enabled"
if !status {
t = "disabled"
}
gologger.Info().Msgf("Reporting source %s %s", t, source)
}
return err
}
func (r *Runner) addTemplate(location string) error {
walkErr := filepath.WalkDir(location, func(path string, d fs.DirEntry, err error) error {
if err != nil {

View File

@@ -292,6 +292,46 @@ func (c *Client) ListDatasources() ([]GetDataSourceResponse, error) {
return items, nil
}
func (c *Client) ListReportingSources() ([]GetReportingSourceResponse, error) {
var items []GetReportingSourceResponse
httpReq, err := retryablehttp.NewRequest(http.MethodGet, fmt.Sprintf("%s/reporting", c.baseURL), nil)
if err != nil {
return items, errors.Wrap(err, "could not make request")
}
resp, err := c.sendRequest(httpReq)
if err != nil {
return nil, errors.Wrap(err, "could not do request")
}
defer resp.Body.Close()
if err := jsoniter.NewDecoder(resp.Body).Decode(&items); err != nil {
return items, errors.Wrap(err, "could not decode results")
}
return items, nil
}
func (c *Client) ToggleReportingSource(ID int64, status bool) error {
r := ReportingSourceStatus{Enabled: status}
var buf bytes.Buffer
if err := jsoniter.NewEncoder(&buf).Encode(r); err != nil {
return errors.Wrap(err, "could not encode request")
}
httpReq, err := retryablehttp.NewRequest(http.MethodPut, fmt.Sprintf("%s/reporting/%d", c.baseURL, ID), bytes.NewReader(buf.Bytes()))
if err != nil {
return errors.Wrap(err, "could not make request")
}
resp, err := c.sendRequest(httpReq)
if err != nil {
return errors.Wrap(err, "could not do request")
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
return nil
}
func (c *Client) ListTargets(query string) ([]GetTargetResponse, error) {
var builder strings.Builder
_, _ = builder.WriteString(c.baseURL)

View File

@@ -135,6 +135,18 @@ type GetTemplatesResponse struct {
Type string `json:"type,omitempty"`
}
type GetReportingSourceResponse struct {
ID int64 `json:"id"`
Type string `json:"type"`
ProjectName string `json:"project_name"`
Enabled bool `json:"enabled"`
Updatedat time.Time `json:"updated_at"`
}
type ReportingSourceStatus struct {
Enabled bool `json:"enabled"`
}
// AddItemResponse is the response to add item request
type AddItemResponse struct {
Ok string `json:"ok"`

View File

@@ -505,10 +505,16 @@ func (r *Runner) RunEnumeration() error {
err = r.listTargets()
} else if r.options.ListTemplates {
err = r.listTemplates()
} else if r.options.ListReportingSources {
err = r.listReportingSources()
} else if r.options.AddDatasource != "" {
err = r.addCloudDataSource(r.options.AddDatasource)
} else if r.options.RemoveDatasource != "" {
err = r.removeDatasource(r.options.RemoveDatasource)
} else if r.options.DisableReportingSource != "" {
err = r.toggleReportingSource(r.options.DisableReportingSource, false)
} else if r.options.EnableReportingSource != "" {
err = r.toggleReportingSource(r.options.EnableReportingSource, true)
} else if r.options.AddTarget != "" {
err = r.addTarget(r.options.AddTarget)
} else if r.options.AddTemplate != "" {

View File

@@ -103,6 +103,12 @@ type Options struct {
ListTargets bool
// ListTemplates enables listing of templates for user
ListTemplates bool
// ListReportingSources enables listing of reporting source
ListReportingSources bool
// DisableReportingSource disables a reporting source
DisableReportingSource string
// EnableReportingSource enables a reporting source
EnableReportingSource string
// Limit the number of items at a time
OutputLimit int
// Nostore