gopa

package module
v0.0.0-...-a1744f6 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 9 Imported by: 0

README

Gopa

PkgGoDev

Gopa is a client written in Go for OPA.

Usage

This example can be found on the example_test.go

package main

import (
	"context"
	"fmt"

	"github.com/cycloidio/gopa"
)

func main() {
	c, err := gopa.NewClient()
	if err != nil {
		// Handle error
	}

	ctx := context.Background()

	policyID := "my-policy-id"
	policyBody := []byte(`
package opa.examples

import input.example.flag

default allow_request = false
allow_request { flag == true }
`)

	// First we create a Policy to be used
	_, err = c.PolicyCreateOrUpdate(ctx, policyID, policyBody)
	if err != nil {
		// Handle error
	}

	input := map[string]interface{}{
		"example": map[string]interface{}{
			"flag": true,
		},
	}

	res, err := c.DataGetWithInput(ctx, "/opa/examples/allow_request", input)
	if err != nil {
		// Handle error
	}
	fmt.Println(*res.Result)

	res, err = c.DataGet(ctx, "/opa/examples/allow_request")
	if err != nil {
		// Handle error
	}
	fmt.Println(*res.Result)

	_, err = c.PolicyDelete(ctx, policyID)

	// Output:
	// true
	// false
}

Implementation

The current implementation supports:

  • Policy API
  • Data API
  • Query API (WIP)
  • Compile API

Documentation

Index

Constants

View Source
const (
	DefaultURL = "http://localhost:8181"
)

Defaults

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code     string     `json:"code"`
	Message  string     `json:"message"`
	Errors   []APIError `json:"errors,omitempty"`
	Location Location   `json:"location,omitempty"`
	Details  []string   `json:"details,omitempty"`
}

APIError models an error response sent to the client. We cannot use directly the type they define as it uses the `Errors []error` so it cannot be marshaled back to the type it was before

func (*APIError) Error

func (e *APIError) Error() string

Error transforms the error into a string

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the main struct to connect and use OPA

func NewClient

func NewClient(opts ...ClientOptionFunc) (*Client, error)

NewClient initializes a new client that can be configured with the opts

func (*Client) DataCreateOrOverride

func (c *Client) DataCreateOrOverride(ctx context.Context, path string, data map[string]interface{}) error

DataCreateOrOverride creates or replaces the given data on the path p https://www.openpolicyagent.org/docs/latest/rest-api/#create-or-overwrite-a-document

func (*Client) DataDelete

func (c *Client) DataDelete(ctx context.Context, path string) error

DataDelete deletes the data on the given path p https://www.openpolicyagent.org/docs/latest/rest-api/#delete-a-document

func (*Client) DataGet

func (c *Client) DataGet(ctx context.Context, path string) (*types.DataResponseV1, error)

DataGet get's the data on the given path p https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-document

func (*Client) DataGetWithInput

func (c *Client) DataGetWithInput(ctx context.Context, path string, input map[string]interface{}) (*types.DataResponseV1, error)

DataGetWithInput get's the data on the given path p with the input i https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-document-with-input

func (*Client) DataUpdate

func (c *Client) DataUpdate(ctx context.Context, path string, data map[string]interface{}) error

DataUpdate updates the data on the given path p. Can be used to do partial updates by using the path to specify the element https://www.openpolicyagent.org/docs/latest/rest-api/#patch-a-document

func (*Client) PolicyCreateOrUpdate

func (c *Client) PolicyCreateOrUpdate(ctx context.Context, id string, policy []byte) (*types.PolicyPutResponseV1, error)

PolicyCreateOrUpdate creates or updates the policy with the give id and the content policy https://www.openpolicyagent.org/docs/latest/rest-api/#create-or-update-a-policy

func (*Client) PolicyDelete

func (c *Client) PolicyDelete(ctx context.Context, id string) (*types.PolicyDeleteResponseV1, error)

PolicyDelete deletes the policy with the given id https://www.openpolicyagent.org/docs/latest/rest-api/#delete-a-policy

func (*Client) PolicyGet

func (c *Client) PolicyGet(ctx context.Context, id string) (*types.PolicyGetResponseV1, error)

PolicyGet returns the policy with the given id https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-policy

func (*Client) PolicyList

func (c *Client) PolicyList(ctx context.Context) (*types.PolicyListResponseV1, error)

PolicyList returns all the policies https://www.openpolicyagent.org/docs/latest/rest-api/#list-policies

func (*Client) QueryAdHoc

func (c *Client) QueryAdHoc(ctx context.Context, path string, opt QueryAdHocOptions) (*types.QueryResponseV1, error)

QueryAdHoc makes a AdHoc query to the path p with the give opt https://www.openpolicyagent.org/docs/latest/rest-api/#execute-an-ad-hoc-query

func (*Client) QuerySimple

func (c *Client) QuerySimple(ctx context.Context, path string, input map[string]interface{}) ([]byte, error)

QuerySimple makes a simple query to the path p with the give input https://www.openpolicyagent.org/docs/latest/rest-api/#execute-a-simple-query

type ClientOptionFunc

type ClientOptionFunc func(*Client) error

ClientOptionFunc is a type used to configure the Client on initialization time

func SetClient

func SetClient(client *http.Client) ClientOptionFunc

SetClient sets the http client used to make the requests to OPA

func SetToken

func SetToken(token string) ClientOptionFunc

SetToken sets the token to use on the requests

func SetURL

func SetURL(u string) ClientOptionFunc

SetURL sets the u as URL

type DataService

type DataService struct {
	// contains filtered or unexported fields
}

DataService is the services in charge of the Data interactions

func NewDataService

func NewDataService(c *Client) *DataService

NewDataService initializes a new DataService

func (*DataService) CreateOrOverride

func (ds *DataService) CreateOrOverride(ctx context.Context, p string, data map[string]interface{}) error

CreateOrOverride creates or replaces the given data on the path p https://www.openpolicyagent.org/docs/latest/rest-api/#create-or-overwrite-a-document

func (*DataService) Delete

func (ds *DataService) Delete(ctx context.Context, p string) error

Delete deletes the data on the given path p https://www.openpolicyagent.org/docs/latest/rest-api/#delete-a-document

func (*DataService) Get

Get get's the data on the given path p https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-document

func (*DataService) GetWithInput

func (ds *DataService) GetWithInput(ctx context.Context, p string, i map[string]interface{}) (*types.DataResponseV1, error)

GetWithInput get's the data on the given path p with the input i https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-document-with-input

func (*DataService) Update

func (ds *DataService) Update(ctx context.Context, p string, data map[string]interface{}) error

Update updates the data on the given path p. Can be used to do partial updates by using the path to specify the element https://www.openpolicyagent.org/docs/latest/rest-api/#patch-a-document

type Location

type Location struct {
	Text   []byte `json:"-"`    // The original text fragment from the source.
	File   string `json:"file"` // The name of the source file (which may be empty).
	Row    int    `json:"row"`  // The line in the source.
	Col    int    `json:"col"`  // The column in the row.
	Offset int    `json:"-"`    // The byte offset for the location in the source.
}

Location records a position in source code

type PolicyService

type PolicyService struct {
	// contains filtered or unexported fields
}

PolicyService is the service in charge of the Policy interactions

func NewPolicyService

func NewPolicyService(c *Client) *PolicyService

NewPolicyService initializes a new PolicyService

func (*PolicyService) CreateOrUpdate

func (ps *PolicyService) CreateOrUpdate(ctx context.Context, id string, policy []byte) (*types.PolicyPutResponseV1, error)

CreateOrUpdate creates or updates the policy with the give id and the content policy https://www.openpolicyagent.org/docs/latest/rest-api/#create-or-update-a-policy

func (*PolicyService) Delete

Delete deletes the policy with the given id https://www.openpolicyagent.org/docs/latest/rest-api/#delete-a-policy

func (*PolicyService) Get

Get returns the policy with the given id https://www.openpolicyagent.org/docs/latest/rest-api/#get-a-policy

func (*PolicyService) List

List returns all the policies https://www.openpolicyagent.org/docs/latest/rest-api/#list-policies

type QueryAdHocOptions

type QueryAdHocOptions struct {
	Query    string                 `json:"query,omitempty"`
	Input    map[string]interface{} `json:"input,omitempty"`
	Unknowns []string               `json:"unknowns,omitempty"`
}

QueryAdHocOptions are the options available to the AdHoc

type QueryService

type QueryService struct {
	// contains filtered or unexported fields
}

QueryService is the service in charge of making Queries

func NewQueryService

func NewQueryService(c *Client) *QueryService

NewQueryService initializes a new QueryService

func (*QueryService) AdHoc

AdHoc makes a AdHoc query to the path p with the give opt https://www.openpolicyagent.org/docs/latest/rest-api/#execute-an-ad-hoc-query

func (*QueryService) Simple

func (qs *QueryService) Simple(ctx context.Context, p string, input map[string]interface{}) ([]byte, error)

Simple makes a simple query to the path p with the give input https://www.openpolicyagent.org/docs/latest/rest-api/#execute-a-simple-query

type Service

type Service interface {
	PolicyCreateOrUpdate(ctx context.Context, id string, policy []byte) (*types.PolicyPutResponseV1, error)
	PolicyList(ctx context.Context) (*types.PolicyListResponseV1, error)
	PolicyGet(ctx context.Context, id string) (*types.PolicyGetResponseV1, error)
	PolicyDelete(ctx context.Context, id string) (*types.PolicyDeleteResponseV1, error)

	DataCreateOrOverride(ctx context.Context, path string, data map[string]interface{}) error
	DataGet(ctx context.Context, path string) (*types.DataResponseV1, error)
	DataGetWithInput(ctx context.Context, path string, input map[string]interface{}) (*types.DataResponseV1, error)
	DataUpdate(ctx context.Context, path string, data map[string]interface{}) error
	DataDelete(ctx context.Context, path string) error

	QuerySimple(ctx context.Context, path string, input map[string]interface{}) ([]byte, error)
	QueryAdHoc(ctx context.Context, path string, opt QueryAdHocOptions) (*types.QueryResponseV1, error)
}

Service is the main interface that we support of OPA

Jump to

Keyboard shortcuts

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