A Go client library for Open Policy Agent (OPA) with support for HTTP-based policy queries.
- 🚀 Multiple Client Types: HTTP, Mock, and No-op clients
- 🔄 Retry Logic: Built-in retry mechanism for HTTP requests
- 📊 Batch Queries: Query permissions for multiple resources at once
- 🛡️ Override Support: Bypass policy checks with override headers
- 🔧 Configurable: Flexible configuration options
- 🧪 Well Tested: Comprehensive test coverage
- 📝 Structured Logging: Integration with nuclio logger
go get github.com/nuclio/opa-client
package main
import (
"context"
"time"
"github.com/nuclio/logger"
"github.com/nuclio/opa-client"
)
func main() {
// Create configuration
config := &opa.Config{
ClientKind: opa.ClientKindHTTP,
Address: "http://localhost:8181",
PermissionQueryPath: "/v1/data/authz/allow",
PermissionFilterPath: "/v1/data/authz/filter_allowed",
RequestTimeout: 10,
Verbose: false,
}
// Create client
logger := // your logger instance
client := opa.CreateOpaClient(logger, config)
// Query single permission
allowed, err := client.QueryPermissions(
"resource1",
opa.ActionRead,
&opa.PermissionOptions{
MemberIds: []string{"user123"},
},
)
// Query multiple permissions
permissions, err := client.QueryPermissionsMultiResources(
context.Background(),
[]string{"resource1", "resource2"},
opa.ActionRead,
&opa.PermissionOptions{
MemberIds: []string{"user123"},
},
)
}
Field | Type | Description | Default |
---|---|---|---|
ClientKind |
ClientKind |
Type of client (http , nop , mock ) |
nop |
Address |
string |
OPA server URL | - |
PermissionQueryPath |
string |
Single permission query endpoint | - |
PermissionFilterPath |
string |
Multi-resource query endpoint | - |
RequestTimeout |
int |
HTTP timeout in seconds | 10 |
Verbose |
bool |
Enable verbose logging | false |
OverrideHeaderValue |
string |
Value for bypass functionality | - |
Production client that communicates with OPA over HTTP.
Always returns true
for all permission checks. Useful for development/testing.
Test client using testify/mock
for unit testing.
Supported actions: read
, create
, update
, delete
- Go 1.23+
- Make
make fmt
make test
make test-coverage
make lint
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.