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 AllowedProjectsRequest
- type AllowedProjectsRequestInput
- type AllowedProjectsResponse
- type Client
- type ClientKind
- type Config
- type HTTPClient
- func (c *HTTPClient) QueryAllowedProjects(ctx context.Context, permissionOptions *PermissionOptions) ([]string, error)
- func (c *HTTPClient) QueryPermissions(ctx context.Context, resource string, action Action, ...) (bool, error)
- func (c *HTTPClient) QueryPermissionsMultiResources(ctx context.Context, resources []string, action Action, ...) ([]bool, error)
- type MockClient
- func (mc *MockClient) QueryAllowedProjects(ctx context.Context, permissionOptions *PermissionOptions) ([]string, error)
- func (mc *MockClient) QueryPermissions(ctx context.Context, resource string, action Action, ...) (bool, error)
- func (mc *MockClient) QueryPermissionsMultiResources(ctx context.Context, resources []string, action Action, ...) ([]bool, error)
- type NopClient
- func (c *NopClient) QueryAllowedProjects(ctx context.Context, permissionOptions *PermissionOptions) ([]string, error)
- func (c *NopClient) QueryPermissions(ctx context.Context, resource string, action Action, ...) (bool, error)
- func (c *NopClient) QueryPermissionsMultiResources(ctx context.Context, resources []string, action Action, ...) ([]bool, error)
- type PermissionFilterRequest
- type PermissionFilterRequestInput
- type PermissionFilterResponse
- type PermissionOptions
- type PermissionQueryRequest
- type PermissionQueryRequestInput
- type PermissionQueryResponse
Constants ¶
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 AllowedProjectsRequest ¶ added in v0.0.4
type AllowedProjectsRequest struct {
Input AllowedProjectsRequestInput `json:"input,omitempty"`
}
type AllowedProjectsRequestInput ¶ added in v0.0.4
type AllowedProjectsRequestInput struct {
Ids []string `json:"ids,omitempty"`
}
type AllowedProjectsResponse ¶ added in v0.0.4
type AllowedProjectsResponse struct {
Result []string `json:"result,omitempty"`
}
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)
// QueryAllowedProjects returns the set of project names the caller (identified by
// PermissionOptions.MemberIds) is allowed to read or list. A returned slice
// containing "*" signals that all projects are accessible — callers are responsible
// for treating "*" as a wildcard; no concrete names are returned alongside it.
QueryAllowedProjects(context.Context, *PermissionOptions) ([]string, 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"`
// the path used when querying allowed projects against opa server (e.g.: /v1/data/platform/authz/allowed_projects)
AllowedProjectsQueryPath string `json:"allowedProjectsQueryPath,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) QueryAllowedProjects ¶ added in v0.0.4
func (c *HTTPClient) QueryAllowedProjects(ctx context.Context, permissionOptions *PermissionOptions) ([]string, error)
QueryAllowedProjects returns the set of project names the caller (identified by permissionOptions.MemberIds) is allowed to read or list. A returned slice containing "*" signals that all projects are accessible.
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) QueryAllowedProjects ¶ added in v0.0.4
func (mc *MockClient) QueryAllowedProjects(ctx context.Context, permissionOptions *PermissionOptions) ([]string, error)
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) QueryAllowedProjects ¶ added in v0.0.4
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"`
}