github.com/Uptycs/basequery-go@v0.8.0/plugin/config/config.go (about)

     1  // Package config creates an osquery configuration plugin.
     2  //
     3  // See https://osquery.readthedocs.io/en/latest/development/config-plugins/ for more.
     4  package config
     5  
     6  import (
     7  	"context"
     8  	"log"
     9  
    10  	"github.com/Uptycs/basequery-go/gen/osquery"
    11  )
    12  
    13  // GenerateConfigsFunc returns the configurations generated by this plugin.
    14  // The returned map should use the source name as key, and the config
    15  // JSON as values. The context argument can optionally be used for
    16  // cancellation in long-running operations.
    17  type GenerateConfigsFunc func(ctx context.Context) (map[string]string, error)
    18  
    19  // RefreshConfigsFunc is called by Osquery to provide latest configuration.
    20  // Plugin's can optionally implement this method if they are interested in full
    21  // configuration.
    22  type RefreshConfigsFunc func(ctx context.Context, request osquery.ExtensionPluginRequest) osquery.ExtensionResponse
    23  
    24  // Plugin is an osquery configuration plugin. Plugin implements the OsqueryPlugin
    25  // interface.
    26  type Plugin struct {
    27  	name     string
    28  	generate GenerateConfigsFunc
    29  	refresh  RefreshConfigsFunc
    30  }
    31  
    32  // NewPlugin takes a value that implements ConfigPlugin and wraps it with
    33  // the appropriate methods to satisfy the OsqueryPlugin interface. Use this to
    34  // easily create configuration plugins.
    35  func NewPlugin(name string, gen GenerateConfigsFunc, ref RefreshConfigsFunc) *Plugin {
    36  	return &Plugin{name: name, generate: gen, refresh: ref}
    37  }
    38  
    39  // Name return the plugin name.
    40  func (t *Plugin) Name() string {
    41  	return t.name
    42  }
    43  
    44  // Registry name for config plugins.
    45  const configRegistryName = "config"
    46  
    47  // RegistryName returns the registry name which is always "config" for config plugin.
    48  func (t *Plugin) RegistryName() string {
    49  	return configRegistryName
    50  }
    51  
    52  // Routes returns empty plugin response.
    53  func (t *Plugin) Routes() osquery.ExtensionPluginResponse {
    54  	return osquery.ExtensionPluginResponse{}
    55  }
    56  
    57  // Ping returns success status.
    58  func (t *Plugin) Ping() osquery.ExtensionStatus {
    59  	return osquery.ExtensionStatus{Code: 0, Message: "OK"}
    60  }
    61  
    62  // Key that the request method is stored under
    63  const requestActionKey = "action"
    64  
    65  // Action value used when config is requested
    66  const genConfigAction = "genConfig"
    67  
    68  // Action value used when config is refreshed
    69  const refreshConfigAction = "refresh"
    70  
    71  // Call is the entry point method into config plugin. "action" will be part of the request.
    72  // It should either be "genConfig" or "refresh".
    73  func (t *Plugin) Call(ctx context.Context, request osquery.ExtensionPluginRequest) osquery.ExtensionResponse {
    74  	switch request[requestActionKey] {
    75  	case genConfigAction:
    76  		configs, err := t.generate(ctx)
    77  		if err != nil {
    78  			return osquery.ExtensionResponse{
    79  				Status: &osquery.ExtensionStatus{
    80  					Code:    1,
    81  					Message: "error getting config: " + err.Error(),
    82  				},
    83  			}
    84  		}
    85  
    86  		return osquery.ExtensionResponse{
    87  			Status:   &osquery.ExtensionStatus{Code: 0, Message: "OK"},
    88  			Response: osquery.ExtensionPluginResponse{configs},
    89  		}
    90  
    91  	case refreshConfigAction:
    92  		if t.refresh != nil {
    93  			delete(request, requestActionKey)
    94  			return t.refresh(ctx, request)
    95  		}
    96  		return osquery.ExtensionResponse{
    97  			Status: &osquery.ExtensionStatus{Code: 0, Message: "OK"},
    98  		}
    99  
   100  	default:
   101  		log.Println("Unknown action for config plugin:", request[requestActionKey])
   102  		return osquery.ExtensionResponse{
   103  			Status: &osquery.ExtensionStatus{
   104  				Code:    1,
   105  				Message: "unknown action: " + request["action"],
   106  			},
   107  		}
   108  	}
   109  
   110  }
   111  
   112  // Shutdown plugin and cleanup.
   113  func (t *Plugin) Shutdown() {}