Documentation
¶
Overview ¶
package opaclient provides a Go client library for Open Policy Agent (OPA) with support for HTTP-based policy queries.
The package supports multiple client types:
- HTTPClient: Production client for communicating with OPA over HTTP
- NopClient: Always returns true, useful for development/testing
- MockClient: Test client using testify/mock for unit testing
Example usage:
config := &opa.Config{
ClientKind: opa.ClientKindHTTP,
Address: "http://localhost:8181",
PermissionQueryPath: "/v1/data/authz/allow",
RequestTimeout: 10,
}
client := opa.CreateOpaClient(logger, config)
allowed, err := client.QueryPermissions("resource1", opa.ActionRead, &opa.PermissionOptions{
MemberIds: []string{"user123"},
})
Index ¶
- Constants
- type Action
- type Client
- type ClientKind
- type Config
- type HTTPClient
- type MockClient
- type NopClient
- type PermissionFilterRequest
- type PermissionFilterRequestInput
- type PermissionFilterResponse
- type PermissionOptions
- type PermissionQueryRequest
- type PermissionQueryRequestInput
- type PermissionQueryResponse
Constants ¶
View Source
const ( // Version is the current version of the OPA client library Version = "0.0.1" // UserAgent is used in HTTP requests to identify the client UserAgent = "nuclio-opa-client/" + Version )
Version information
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// QueryPermissions queries permission for a single resource.
QueryPermissions(context.Context, string, Action, *PermissionOptions) (bool, error)
// QueryPermissionsMultiResources queries permissions for multiple resources at once.
// Returns a slice of booleans where each index corresponds to the resource at the same index.
QueryPermissionsMultiResources(context.Context, []string, Action, *PermissionOptions) ([]bool, error)
}
Client represents an OPA client that can query permissions.
type ClientKind ¶
type ClientKind string
const ( ClientKindHTTP ClientKind = "http" ClientKindNop ClientKind = "nop" ClientKindMock ClientKind = "mock" DefaultClientKind = ClientKindNop DefaultRequestTimeOut = 10 * time.Second )
type Config ¶
type Config struct {
// OPA server address
Address string `json:"address,omitempty"`
// client kind to use (nop | http | mock)
ClientKind ClientKind `json:"clientKind,omitempty"`
// timeout period when querying opa server
RequestTimeout int `json:"requestTimeout,omitempty"`
// the path used when querying single resource against opa server (e.g.: /v1/data/somewhere/authz/allow)
PermissionQueryPath string `json:"permissionQueryPath,omitempty"`
// the path used when querying multiple resources against opa server (e.g.: /v1/data/somewhere/authz/filter_allowed)
PermissionFilterPath string `json:"permissionFilterPath,omitempty"`
// for extra verbosity
Verbose bool `json:"verbose,omitempty"`
// the header value for bypassing OPA if needed
OverrideHeaderValue string `json:"overrideHeaderValue,omitempty"`
// SkipTLSVerify indicates whether to skip TLS verification for the OPA server
SkipTLSVerify bool `json:"skipTLSVerify,omitempty"`
}
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
func NewHTTPClient ¶
func (*HTTPClient) QueryPermissions ¶
func (c *HTTPClient) QueryPermissions(ctx context.Context, resource string, action Action, permissionOptions *PermissionOptions) (bool, error)
func (*HTTPClient) QueryPermissionsMultiResources ¶
func (c *HTTPClient) QueryPermissionsMultiResources(ctx context.Context, resources []string, action Action, permissionOptions *PermissionOptions) ([]bool, error)
QueryPermissionsMultiResources query permissions for multiple resources at once. The response is a list of booleans indicating for each resource if the action against such resource is allowed or not. Therefore, it is guaranteed that len(resources) and len(results) are equal and resources[i] query permission is at results[i]
type MockClient ¶
func (*MockClient) QueryPermissions ¶
func (mc *MockClient) QueryPermissions(ctx context.Context, resource string, action Action, permissionOptions *PermissionOptions) (bool, error)
func (*MockClient) QueryPermissionsMultiResources ¶
func (mc *MockClient) QueryPermissionsMultiResources(ctx context.Context, resources []string, action Action, permissionOptions *PermissionOptions) ([]bool, error)
type NopClient ¶
type NopClient struct {
// contains filtered or unexported fields
}
func (*NopClient) QueryPermissions ¶
func (*NopClient) QueryPermissionsMultiResources ¶
type PermissionFilterRequest ¶
type PermissionFilterRequest struct {
Input PermissionFilterRequestInput `json:"input,omitempty"`
}
type PermissionFilterResponse ¶
type PermissionFilterResponse struct {
Result []string `json:"result,omitempty"`
}
type PermissionOptions ¶
type PermissionQueryRequest ¶
type PermissionQueryRequest struct {
Input PermissionQueryRequestInput `json:"input,omitempty"`
}
type PermissionQueryResponse ¶
type PermissionQueryResponse struct {
Result bool `json:"result,omitempty"`
}
Click to show internal directories.
Click to hide internal directories.