sdk

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: MPL-2.0 Imports: 3 Imported by: 1

README

Sentinel Plugin SDK

Tests GoDoc Heimdall

This repository contains the Sentinel plugin SDK. This SDK allows developers to extend Sentinel to source external information for use in their policies.

Sentinel plugins can be written in any language, but the recommended language is Go. We provide a high-level framework to make writing plugins in Go extremely easy. For other languages, plugins can be written by implementing the protocol over gRPC.

To get started writing a Sentinel plugin, we recommend reading the extending Sentinel guide.

You can also view the plugin API via GoDoc.

SDK Compatibility Matrix

Sentinel's plugin protocol is, at this time, not backwards compatible. This means that a specific version of the Sentinel runtime is always coupled to a specific version of the plugin protocol, and SDK. The following table can help you determine which version of the SDK is necessary to work with which versions of Sentinel.

Sentinel Version Plugin Protocol Version SDK Version
Up to v0.10.4 1 Up to v0.1.1
Up to v0.18.13 2 Up to v0.3.13
From v0.19.0 3 Since v0.4.0

Development Info

The following tools are required to work with the Sentinel SDK:

After both of these are installed, you can use the following make commands:

  • make test will run tests on the SDK. You can use the TEST and TESTARGS variables to control the packages and test arguments, respectively.
  • make tools will install any necessary Go tools.
  • make generate will generate any auto-generated code. Currently this includes the protocol, mockery files, and the code for the plugin testing toolkit.

The modules, test-circle, and /usr/bin/sentinel targets are only used in Circle and are not necessary for interactive development.

Help and Discussion

For issues specific to the SDK, please use the GitHub issue tracker (the Issues tab).

For general Sentinel support and discussion, please use the Sentinel Community Forum.

Documentation

Overview

Do not edit mock_Plugin_Closer.go directly as your changes will be overwritten. Instead, edit mock_Plugin_Closer.go.src and re-run "go generate ./" in the root SDK package.

Package sdk contains the low-level interfaces and API for creating Sentinel plugins. A Sentinel plugin can provide data dynamically to Sentinel policies.

For plugin authors, the subfolder "framework" contains a high-level framework for easily implementing plugins in Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Undefined is a constant value that represents the undefined object in
	// Sentinel. By making this the return value, it'll be converted to
	// Undefined.
	Undefined = &undefined{}

	// Null is a constant value that represents the null object in Sentinel.
	// By making this a return value, it will convert to null.
	Null = &null{}
)

Functions

This section is empty.

Types

type GetKey

type GetKey struct {
	// The key for this part of the request.
	Key string

	// The list of arguments for a call expression. This is "nil" if
	// this key is not a call. This may be length zero (but non-nil) if
	// this is a call with no arguments.
	Args []interface{}
}

GetKey is an individual key in the larger possible selector of the specific plugin call, along with any supplied arguments for the specific key.

func (*GetKey) Call

func (g *GetKey) Call() bool

Call returns true if this request is a call expression.

type GetReq

type GetReq struct {
	// ExecId is a unique ID representing the particular execution for this
	// request. This can be used to maintain state locally.
	//
	// ExecDeadline is a hint of when the execution will be over and the
	// state can be thrown away. The time given here will always be in UTC
	// time. Note that this is susceptible to clock shifts, but Go is planning
	// to make the time APIs monotonic by default (see proposal 12914). After
	// that this will be resolved.
	ExecId       uint64
	ExecDeadline time.Time

	// Keys is the list of keys being requested. For example for "a.b.c"
	// where "a" is the plugin, Keys would be ["b", "c"].
	Keys []GetKey

	// KeyId is a unique ID for this key. This should match exactly the
	// GetResult KeyId so that the result for this can be found quickly.
	KeyId uint64

	// Context, if supplied, is an arbitrary object intended to
	// represent the data from an existing namespace. If the plugin
	// supports the framework.New interface, the contents are passed to
	// it, with any resulting namespace being what Get operates on.
	//
	// The Get call operates on the root of the plugin if this is set
	// to nil. If this is set and the plugin does not implement
	// framework.New, an error is returned.
	Context map[string]interface{}
}

GetReq are the arguments given to Get for an Plugin.

func (*GetReq) GetKeys

func (g *GetReq) GetKeys() []string

GetKeys returns a list of the string keys in the GetReq, without the arguments.

type GetResult

type GetResult struct {
	KeyId    uint64                 // KeyId matching GetReq.KeyId, or zero.
	Keys     []string               // Keys structure from GetReq.Keys, or new key set.
	Value    interface{}            // Value compatible with lang/object.ToObject
	Context  map[string]interface{} // Updated Context if it was sent
	Callable bool                   // true if returned Value is callable
}

GetResult is the result structure for a Get request.

type GetResultList

type GetResultList []*GetResult

GetResultList is a wrapper around a slice of GetResult structures to provide helpers.

func (GetResultList) KeyId

func (r GetResultList) KeyId(id uint64) *GetResult

KeyId gets the result with the given key ID, or nil if its not found.

type MockPlugin added in v0.4.0

type MockPlugin struct {
	mock.Mock
}

MockPlugin is an autogenerated mock type for the Plugin type

func NewMockPlugin added in v0.4.0

func NewMockPlugin(t mockConstructorTestingTNewMockPlugin) *MockPlugin

NewMockPlugin creates a new instance of MockPlugin. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.

func (*MockPlugin) Configure added in v0.4.0

func (_m *MockPlugin) Configure(_a0 map[string]interface{}) error

Configure provides a mock function with given fields: _a0

func (*MockPlugin) Get added in v0.4.0

func (_m *MockPlugin) Get(reqs []*GetReq) ([]*GetResult, error)

Get provides a mock function with given fields: reqs

type MockPluginCloser added in v0.4.0

type MockPluginCloser struct {
	MockPlugin
}

MockPluginCloser augments MockPlugin to also implement io.Closer

func (*MockPluginCloser) Close added in v0.4.0

func (_m *MockPluginCloser) Close() error

Close mocks Close for MockPluginCloser

type Plugin added in v0.4.0

type Plugin interface {
	// Configure is called to configure the plugin before it is accessed.
	// This must be called before any call to Get().
	Configure(map[string]interface{}) error

	// Get is called when an plugin field is accessed or called as a function.
	//
	// Get may request more than one value at a time, represented by multiple
	// GetReq values. The result GetResult should contain the matching
	// KeyId for the requests.
	//
	// The result value is not a map keyed on KeyId to allow flexibility
	// in the future of potentially allowing pre-fetched data. This has no
	// effect currently.
	Get(reqs []*GetReq) ([]*GetResult, error)
}

Plugin is an importable package.

Plugins are a namespace that may contain objects and functions. The root level has no value nor can it be called. For example `import "a'` allows access to fields within "a" such as `a.b` but doesn't allow referencing or calling it directly with `a` alone. This is an important difference between plugins and external objects (which themselves express a value).

Directories

Path Synopsis
Package framework contains a high-level framework for implementing Sentinel plugins with Go.
Package framework contains a high-level framework for implementing Sentinel plugins with Go.
proto
go
Package proto contains the Go generated files for the protocol buffer files.
Package proto contains the Go generated files for the protocol buffer files.
Package rpc contains the API that can be used to serve Sentinel plugins over an RPC interface.
Package rpc contains the API that can be used to serve Sentinel plugins over an RPC interface.
Package testing provides support for automated testing for plugins.
Package testing provides support for automated testing for plugins.
testplugin
Package testplugin contains a test plugin that the testing package uses for unit tests.
Package testplugin contains a test plugin that the testing package uses for unit tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL