api

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2022 License: AGPL-3.0 Imports: 9 Imported by: 0

README

KIProtect - KIProtect API

KIProtect provides an API to the KIProtect tool, allowing users to transform data via a RESTful API.

Building

To build the API, run the following commands in the parent directory:

make
make install

Running the tests

Run the following in the parent directory:

make test-api

Running the API command

First, define the KODEX_SETTINGS environment variable. Then, run

kodex api run

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AccessTokenForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "token",
			Validators: []forms.Validator{
				forms.IsBytes{Encoding: "hex"},
			},
		},
		{
			Name: "scopes",
			Validators: []forms.Validator{
				forms.IsList{
					Validators: []forms.Validator{
						forms.IsString{},
					},
				},
			},
		},
	},
}
View Source
var BPObjectRoleForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "objectType",
			Validators: []forms.Validator{
				forms.IsString{},
			},
		},
		{
			Name: "organizationID",
			Validators: []forms.Validator{
				forms.IsBytes{Encoding: "hex"},
			},
		},
		{
			Name: "objectID",
			Validators: []forms.Validator{
				forms.IsBytes{Encoding: "hex"},
			},
		},
		{
			Name: "organizationRole",
			Validators: []forms.Validator{
				forms.IsString{},
			},
		},
		{
			Name: "organizationSource",
			Validators: []forms.Validator{
				forms.IsOptional{Default: "inMemory"},
				forms.IsString{},
			},
		},
		{
			Name: "objectRole",
			Validators: []forms.Validator{
				forms.IsString{},
			},
		},
	},
}
View Source
var BPOrganizationForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "source",
			Validators: []forms.Validator{
				forms.IsOptional{Default: "inMemory"},
				forms.IsIn{Choices: []interface{}{"inMemory"}},
			},
		},
		{
			Name: "name",
			Validators: []forms.Validator{
				forms.IsString{},
			},
		},
		{
			Name: "id",
			Validators: []forms.Validator{
				forms.IsBytes{Encoding: "hex"},
			},
		},
	},
}
View Source
var BlueprintConfigForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "users",
			Validators: []forms.Validator{
				forms.IsList{
					Validators: []forms.Validator{
						forms.IsStringMap{
							Form: &UserForm,
						},
					},
				},
			},
		},
		{
			Name: "roles",
			Validators: []forms.Validator{
				forms.IsList{
					Validators: []forms.Validator{
						forms.IsStringMap{
							Form: &BPObjectRoleForm,
						},
					},
				},
			},
		},
	},
}
View Source
var ObjectRoleForm = forms.Form{
	ErrorMsg: "invalid data encountered in the object role form",
	Fields: []forms.Field{
		{
			Name: "organization_role",
			Validators: []forms.Validator{
				forms.IsRequired{},
				forms.IsString{MinLength: 2, MaxLength: 100},
				forms.MatchesRegex{Regex: regexp.MustCompile(`^[\w\d\-\:\.]{2,100}$`)},
			},
		},
		{
			Name: "role",
			Validators: []forms.Validator{
				forms.IsRequired{},
				forms.IsString{},
				forms.IsIn{Choices: []interface{}{"superuser", "admin", "viewer"}},
			},
		},
	},
}
View Source
var OrganizationForm = forms.Form{
	ErrorMsg: "invalid data encountered in the organization form",
	Fields: []forms.Field{
		{
			Name: "name",
			Validators: append([]forms.Validator{
				forms.IsRequired{}}, kodex.NameValidators...),
		},
		{
			Name: "description",
			Validators: append([]forms.Validator{
				forms.IsOptional{Default: ""}}, kodex.DescriptionValidators...),
		},
		{
			Name:       "data",
			Validators: []forms.Validator{forms.IsOptional{}, forms.IsStringMap{}},
		},
	},
}
View Source
var RolesForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "roles",
			Validators: []forms.Validator{
				forms.IsStringList{},
			},
		},
		{
			Name: "organization",
			Validators: []forms.Validator{
				forms.IsStringMap{
					Form: &BPOrganizationForm,
				},
			},
		},
	},
}
View Source
var UserForm = forms.Form{
	Fields: []forms.Field{
		{
			Name: "source",
			Validators: []forms.Validator{
				forms.IsOptional{Default: "inMemory"},
				forms.IsIn{Choices: []interface{}{"inMemory"}},
			},
		},
		{
			Name: "email",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsString{},
			},
		},
		{
			Name: "displayName",
			Validators: []forms.Validator{
				forms.IsOptional{},
				forms.IsString{},
			},
		},
		{
			Name: "superuser",
			Validators: []forms.Validator{
				forms.IsOptional{Default: false},
				forms.IsBoolean{},
			},
		},
		{
			Name: "accessToken",
			Validators: []forms.Validator{
				forms.IsStringMap{
					Form: &AccessTokenForm,
				},
			},
		},
		{
			Name: "roles",
			Validators: []forms.Validator{
				forms.IsList{
					Validators: []forms.Validator{
						forms.IsStringMap{
							Form: &RolesForm,
						},
					},
				},
			},
		},
	},
}

Functions

func FormatError

func FormatError(err error) interface{}

func HandleError

func HandleError(c *gin.Context, code int, err error)

Types

type APIControllerDefinitions

type APIControllerDefinitions map[string]APIControllerMaker

type APIControllerMaker

type APIControllerMaker func(
	config map[string]interface{},
	baseController kodex.Controller,
	definitions *Definitions) (Controller, error)

type APIPlugin

type APIPlugin interface {
	InitializeAPI(*gin.RouterGroup, Controller, kodex.Meter) error
	InitializeAdaptors(map[string]ObjectAdaptor) error
}

type AccessToken

type AccessToken struct {
	Scopes []string `json:"scopes"`
	Token  []byte
}

func MakeAccessToken

func MakeAccessToken(scopes []string) *AccessToken

type AssociateAdaptor

type AssociateAdaptor interface {
	Associate(c *gin.Context, left, right kodex.Model) bool
	Dissociate(c *gin.Context, left, right kodex.Model) bool
	Get(c *gin.Context, left kodex.Model) interface{}
	LeftType() string
	RightType() string
}

type BaseController

type BaseController struct {
	Definitions_  *Definitions
	UserProvider_ UserProvider
	Self          Controller
}

func (*BaseController) APIDefinitions

func (b *BaseController) APIDefinitions() *Definitions

func (*BaseController) CanAccess

func (b *BaseController) CanAccess(user *User, object kodex.Model, objectRoles []string) (bool, error)

func (*BaseController) ObjectRolesForUser

func (b *BaseController) ObjectRolesForUser(objectType string, user *User) ([]ObjectRole, error)

func (*BaseController) RegisterAPIPlugin

func (b *BaseController) RegisterAPIPlugin(plugin APIPlugin) error

func (*BaseController) UserProvider

func (b *BaseController) UserProvider() UserProvider

type BaseObjectRole

type BaseObjectRole struct {
	Self ObjectRole
}

BaseObjectRole contains useful common functionality that should be shared by all implementations of the interface, such as validation.

func (*BaseObjectRole) Create

func (b *BaseObjectRole) Create(values map[string]interface{}) error

func (*BaseObjectRole) MarshalJSON

func (b *BaseObjectRole) MarshalJSON() ([]byte, error)

func (*BaseObjectRole) Type

func (b *BaseObjectRole) Type() string

func (*BaseObjectRole) Update

func (b *BaseObjectRole) Update(values map[string]interface{}) error

type BaseOrganization

type BaseOrganization struct {
	Self        Organization
	Controller_ Controller
}

func (*BaseOrganization) Controller

func (b *BaseOrganization) Controller() Controller

func (*BaseOrganization) Create

func (b *BaseOrganization) Create(values map[string]interface{}) error

func (*BaseOrganization) MarshalJSON

func (b *BaseOrganization) MarshalJSON() ([]byte, error)

func (*BaseOrganization) Type

func (b *BaseOrganization) Type() string

func (*BaseOrganization) Update

func (b *BaseOrganization) Update(values map[string]interface{}) error

type Blueprint

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

func MakeBlueprint

func MakeBlueprint(config map[string]interface{}) *Blueprint

func (*Blueprint) Create

func (b *Blueprint) Create(controller Controller) error

type Controller

type Controller interface {
	kodex.Controller

	KodexController() kodex.Controller
	RegisterAPIPlugin(APIPlugin) error
	APIDefinitions() *Definitions

	// User provider
	UserProvider() UserProvider

	// Object roles
	CanAccess(user *User, object kodex.Model, objectRoles []string) (bool, error)
	ObjectRole(id []byte) (ObjectRole, error)
	RolesForObject(object kodex.Model) ([]ObjectRole, error)
	ObjectRolesForUser(objectType string, user *User) ([]ObjectRole, error)
	ObjectRolesForOrganizationRoles(objectType string, organizationRoles []string, organizationID []byte) ([]ObjectRole, error)
	MakeObjectRole(object kodex.Model, organization Organization) ObjectRole

	// Organizations
	MakeOrganization() Organization
	Organization(source string, sourceID []byte) (Organization, error)
	Organizations(filters map[string]interface{}) ([]Organization, error)
}

type CreateObjectAdaptor

type CreateObjectAdaptor interface {
	MakeObject(*gin.Context) kodex.Model
}

type CreateUserProvider

type CreateUserProvider interface {
	Create(user *User) error
}

type Definitions

type Definitions struct {
	kodex.Definitions
	APIControllerDefinitions APIControllerDefinitions
	Routes                   []Routes
	ObjectAdaptors           map[string]ObjectAdaptor
	AssociateAdaptors        map[string]AssociateAdaptor
	UserProviders            map[string]UserProviderDefinition
}

func MergeDefinitions

func MergeDefinitions(a, b Definitions) Definitions

func (Definitions) Marshal

func (d Definitions) Marshal() map[string]interface{}

func (Definitions) MarshalJSON

func (d Definitions) MarshalJSON() ([]byte, error)

type In

type In struct {
	Values []interface{}
}

type ListAllObjectAdaptor

type ListAllObjectAdaptor interface {
	AllObjects(*gin.Context) []kodex.Model
}

type ObjectAdaptor

type ObjectAdaptor interface {
	// Returns the object for the given ID and its role object, if possible
	Get(Controller, *gin.Context, []byte) (kodex.Model, kodex.Model, error)
	Initialize(Controller, *gin.RouterGroup) error
	Objects(*gin.Context) []kodex.Model
	Type() string
	DependsOn() string
}

type ObjectRole

type ObjectRole interface {
	kodex.Model
	OrganizationID() []byte
	ObjectID() []byte
	OrganizationRole() string
	SetOrganizationRole(string) error
	ObjectRole() string
	SetObjectRole(string) error
	ObjectType() string
}

type ObjectRoleSpec

type ObjectRoleSpec struct {
	ObjectRole         string `json:"objectRole"`
	OrganizationRole   string `json:"organizationRole"`
	OrganizationID     []byte `json:"organizationID"`
	OrganizationSource string `json:"organizationSource"`
	ObjectID           []byte `json:"objectID"`
	ObjectType         string `json:"objectType"`
}

type Organization

type Organization interface {
	kodex.Model
	Source() string
	SourceID() []byte
	Name() string
	SetName(string) error
	Description() string
	SetDescription(string) error
	SetSource(string) error
	SetSourceID([]byte) error
	Data() interface{}
	SetData(interface{}) error
}

type OrganizationRoles

type OrganizationRoles struct {
	Roles        []string          `json:"roles"`
	Organization *UserOrganization `json:"organization"`
}

func MakeOrganizationRoles

func MakeOrganizationRoles(org *UserOrganization, roles []string) *OrganizationRoles

type Routes

type Routes func(*gin.RouterGroup, Controller, kodex.Meter) error

type Settings

type Settings struct {
	UserProvider *UserProviderSettings `json:"userProvider"`
}

type SettingsValidator

type SettingsValidator func(settings map[string]interface{}) (interface{}, error)

type UpdateObjectAdaptor

type UpdateObjectAdaptor interface {
	UpdateObject(kodex.Model, map[string]interface{}) (kodex.Model, error)
	SaveUpdated(updatedObject, object kodex.Model) error
}

type User

type User struct {
	Source      string                 `json:"source"`
	SourceID    []byte                 `json:"sourceID"`
	EMail       string                 `json:"email"`
	DisplayName string                 `json:"displayName"`
	Superuser   bool                   `json:"superuser"`
	AccessToken *AccessToken           `json:"accessToken"`
	Roles       []*OrganizationRoles   `json:"roles"`
	Limits      map[string]interface{} `json:"limits"`
}

func MakeUser

func MakeUser(source, email string, superuser bool, roles []*OrganizationRoles, limits map[string]interface{}, token *AccessToken) *User

type UserOrganization

type UserOrganization struct {
	Source      string `json:"source"`
	Name        string `json:"name"`
	Default     bool   `json:"default"`
	Description string `json:"description"`
	ID          []byte `json:"id"`
	// contains filtered or unexported fields
}

func MakeUserOrganization

func MakeUserOrganization(source, name, description string, id []byte) *UserOrganization

func (*UserOrganization) ApiOrganization

func (i *UserOrganization) ApiOrganization(controller Controller) (Organization, error)

type UserProvider

type UserProvider interface {
	Get(string) (*User, error)
	Start()
	Stop()
}

type UserProviderDefinition

type UserProviderDefinition struct {
	Name              string            `json:"name"`
	Description       string            `json:"description"`
	Maker             UserProviderMaker `json:"-"`
	SettingsValidator SettingsValidator `json:"-"`
}

type UserProviderDefinitions

type UserProviderDefinitions map[string]UserProviderDefinition

type UserProviderMaker

type UserProviderMaker func(settings kodex.Settings) (UserProvider, error)

type UserProviderSettings

type UserProviderSettings struct {
	Type     string      `json:"type"`
	Settings interface{} `json:"settings"`
}

type UsersAndRoles

type UsersAndRoles struct {
	Users []*User           `json:"users"`
	Roles []*ObjectRoleSpec `json:"roles"`
}

Directories

Path Synopsis
gin
v1
resources/pcap/pcapgo
Package pcapgo provides some native PCAP support, not requiring C libpcap to be installed.
Package pcapgo provides some native PCAP support, not requiring C libpcap to be installed.

Jump to

Keyboard shortcuts

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