rpc

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

An RPC library to execute shell command with ed25519 authentication.

Index

Constants

This section is empty.

Variables

View Source
var ShellInterpreter = APPCommandInterpreter(func(ctx context.Context, command []string) (string, error) {
	cmd := exec.CommandContext(ctx, command[0], command[1:]...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", err
	}
	return string(out), nil
})

Functions

func FullFillRequestWithCallback

func FullFillRequestWithCallback(ctx *APPServerContext, request *APPRequest, done APPDoneFunction)

ASync call, its result will be populated into TaskDoneFn.

Types

type APPClient

type APPClient struct {
	// Options of the RPCClient, just for reference, do not modify.
	Option *APPClientOption
	// contains filtered or unexported fields
}

func CreateAPPClient

func CreateAPPClient(option APPClientOption) (*APPClient, error)

Creates an RPC netclient.

func CreateAPPClientFromEnvOrDie

func CreateAPPClientFromEnvOrDie() *APPClient

Creates an APPClient from environment variables or die. Utility function in docker images.

Environment Variables

  • YUKINO_SERVER_ADDRESS
  • YUKINO_SERVER_TOKEN
  • YUKINO_SERVER_TLS
  • (Optional) YUKINO_CLIENT_PRIVATE_KEY
  • (Optional) YUKINO_CLIENT_RPC_TIMEOUT_SEC

func (*APPClient) Invoke

func (client *APPClient) Invoke(TargetChannel string, TaskName string, TaskParams map[string]string) *APPResponse

Execute Task on given target.

type APPClientOption

type APPClientOption struct {
	// Options to initialize a ReidsProxynetclient.
	ClientOption netclient.NetClientOption
	// (Optional) Private key for authentication.
	PrivateKey string
	// Timeout of the RPC call.
	Timeout time.Duration
}

type APPCommand

type APPCommand struct {
	// Name of the task to be executed.
	TaskName string `json:"name"`
	// Paramters of the task.
	TaskParams map[string]string `json:"params"`
	// For security purpose, client will drop the request if the deadline is passed.
	Deadline time.Time `json:"expires"`

	// If specified, once the command is done.
	// `TaskResult` will be sent to `ReceiverChannel` with given `TaskID`.
	TaskID string `json:"taskID"`

	// Extra field for pre/post processing.
	Metadata string `json:"metadata"`
}

Stuctures to store a command to be executed.

type APPCommandInterpreter

type APPCommandInterpreter func(context.Context, []string) (string, error)

Callback to execute command with context deadline.

type APPDoneFunction

type APPDoneFunction func(*APPResponse)

Callback with TaskResponse as its parameter.

type APPMetadata

type APPMetadata struct {
	// If `TaskChannel` is not matched, client will reject the request.
	TaskChannel string `json:"channel"`
	// If not empty, response will be sent to this channel.
	ReceiverChannel string `json:"receiver"`
}

type APPRequest

type APPRequest struct {
	// Actual command to be executed.
	Command APPCommand `json:"command"`
	// (ed25519) The public key of the sender.
	SenderPubKey string `json:"sender"`
	// (ed25519) The signature of the data
	SenderSign string `json:"sign"`
}

Structure to store a task request.

func (*APPRequest) CheckPermission

func (request *APPRequest) CheckPermission(ACL map[string]bool) error

If `ACL` empty, permission check will pass. If `ACL` is not empty, this function will check: 1. request has a sender, and sender is in the ACL. 2. request has a timestamp, and it is not expired. 3. request has a valid ed25519 signature of `command` field.

func (*APPRequest) Sign

func (request *APPRequest) Sign(priv ed25519.PrivateKey) error

This function will populate `SenderPubKey` and `SenderSign` fields for `Command`. `Command` field will not be modified.

type APPResponse

type APPResponse struct {
	// Task Identifier, will be the same to the request.
	TaskID string `json:"taskID"`
	// The timestmap of the command to be done.
	Finish time.Time `json:"finish"`
	// Result of the task.
	Result string `json:"result"`
}

Structure to store a command execution result.

func FullFillRequest

func FullFillRequest(ctx *APPServerContext, request *APPRequest) *APPResponse

Blocking call. Initialize a permission check, parse commands and return its result into TaskResponse.

type APPServerContext

type APPServerContext struct {
	// Permission check ACL.
	ACL map[string]bool
	// Task name to real command mapping.
	Commands map[string]string
	// contains filtered or unexported fields
}

Structure to store the context of a task.

func CreateTaskContext

func CreateTaskContext(ACL []string, Commands map[string]string, MaxInflights int, Intepreter APPCommandInterpreter) APPServerContext

Initialize a TaskContext object. Must be called before any async calls.

func (*APPServerContext) Execute

func (ctx *APPServerContext) Execute(command *APPCommand) string

Blocking call. Executes the command wrapped in `TaskCommand`. Returns OK if succeed, otherwise an error message.

Jump to

Keyboard shortcuts

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