sudoclient

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Overview

Package sudoclient 封装sudo-api生成的gRPC client。

SudoClient 自动处理token的刷新,支持连接池配置,简化gRPC接口使用。

Client 使用

下面是创建SudoClient并使用sudo-api生成的gRPC client接口的example。

func ExampleNewSudoClient() {
	client, err := NewSudoClient(context.TODO(),
		WithGrpcEndpoint("localhost:8071"),
		WithHTTPEndpoint("http://localhost:7857"),
		WithAccount("admin", "sudosudo"))
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()

	// 使用 client
	_, err = client.ListVtables(context.TODO(), &vtable.ListVtablesRequest{})
	if err != nil {
		// TODO: Handle error.
	}
}

Context 使用

传入NewSudoClient的context会用于请求token和创建底层连接。 client的每个方法调用可能会触发token刷新。刷新token也是gRPC接口调用,用于token刷新的grpc接口调用会使用方法传入的context。

Index

Examples

Constants

This section is empty.

Variables

View Source
var UploadURL = "/upload"

文件上传URL

Functions

func RandPath added in v2.2.0

func RandPath(n int) string

func UnmarshalJSONToProto added in v2.2.0

func UnmarshalJSONToProto(data []byte, dest proto.Message) error

Types

type BasicSudoClient

type BasicSudoClient struct {
	basiccommon.CommonClient
	basicfurnace.FurnaceClient
	// contains filtered or unexported fields
}

BasicSudoClient 封装基础版sudo-api生成的 gRPC client。

func NewBasicSudoClient

func NewBasicSudoClient(ctx context.Context, opts ...DialOption) (*BasicSudoClient, error)

NewBasicSudoClient 创建基础版SudoClient,其中仅包含基础版sudo-api提供的gRPC client。 client使用后需要关闭,从而释放其中的连接资源。

func (*BasicSudoClient) Close

func (client *BasicSudoClient) Close() error

Close 关闭其中的连接资源。

func (*BasicSudoClient) CreateVtableFromDB

func (client *BasicSudoClient) CreateVtableFromDB(
	ctx context.Context,
	dataSrcName, dbName, tableName string,
) (uint64, error)

CreateVtableFromDB 指定数据表创建vtable,返回vtableID。 dataSource需要在数牍隐私计算平台提前配置,dataSource名称会被校验是否存在。 重复创建相同的数据表会直接返回之前的vtableID。

Example
package main

import (
	"context"

	"github.com/sudo-privacy/sudo-sdk-go/v2/pkg/sudoclient"
)

func main() {
	client, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("localhost:8071"),
		sudoclient.WithHTTPEndpoint("http://localhost:7857"),
		sudoclient.WithAccount("admin", "sudosudo"))
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()
	// 指定MariaDB-server数据源,furnace_a数据库,behavior_record数据表。
	_, err = client.CreateVtableFromDB(context.TODO(), "MariaDB-server", "furnace_a", "behavior_record")
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*BasicSudoClient) CreateVtableFromLocalFile

func (client *BasicSudoClient) CreateVtableFromLocalFile(ctx context.Context, tableFilePath string) (uint64, error)

CreateVtableFromLocalFile 从本地文件创建vtable,返回vtableID。 重复上传相同的文件会直接返回之前的vtableID。

Example
package main

import (
	"context"

	"github.com/sudo-privacy/sudo-sdk-go/v2/pkg/sudoclient"
)

func main() {
	// 创建 SudoClient
	client, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("localhost:8071"),
		sudoclient.WithHTTPEndpoint("http://localhost:7857"),
		sudoclient.WithAccount("admin", "sudosudo"))
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()
	// 从本地文件创建vtable,这里指定的是根路径下的csv表文件。
	_, err = client.CreateVtableFromLocalFile(context.TODO(), "/psi_server.csv")
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

type ConnPool

type ConnPool interface {
	// Conn returns a ClientConn from the pool.
	//
	// Conns aren't returned to the pool.
	Conn() *grpc.ClientConn

	// Num returns the number of connections in the pool.
	//
	// It will always return the same value.
	Num() int

	// Close closes every ClientConn in the pool.
	//
	// The error returned by Close may be a single error or multiple errors.
	Close() error

	// ConnPool implements grpc.ClientConnInterface to enable it to be used directly with generated proto stubs.
	grpc.ClientConnInterface
}

ConnPool is a pool of grpc.ClientConns.

type DialOption

type DialOption interface {
	Apply(*dialOptions)
}

DialOption 设置连接相关配置,比如 endpoint、账号、密码。

func WithAccount

func WithAccount(account, password string) DialOption

WithAccount 设置账号、密码。

func WithConnPool

func WithConnPool(grpcConnPool ConnPool) DialOption

WithConnPool 设置连接池。

func WithGrpcClientInterceptor

func WithGrpcClientInterceptor(interceptor grpc.UnaryClientInterceptor) DialOption

WithGrpcClientInterceptor 增加gRPC拦截器。

func WithGrpcDialOption

func WithGrpcDialOption(grpcOpt grpc.DialOption) DialOption

WithGrpcDialOption 增加grpc.DialOption 设置

func WithGrpcEndpoint

func WithGrpcEndpoint(endPoint string) DialOption

WithGrpcEndpoint 设置gRPC服务endpoint。

func WithHTTPEndpoint

func WithHTTPEndpoint(endpoint string) DialOption

WithGrpcEndpoint 设置HTTP endpoint。

func WithHTTPTransport

func WithHTTPTransport(transport http.RoundTripper) DialOption

WithHTTPTransport 修改http Client Transport 设置

func WithTokenSource

func WithTokenSource(tokenSource TokenSource) DialOption

WithTokenSource 设置tokenSource。

type SudoClient

type SudoClient struct {
	BasicSudoClient
	common.CommonClient
	furnace.FurnaceClient
	// contains filtered or unexported fields
}

SudoClient 封装标准版sudo-api生成的 gRPC client。

func NewSudoClient

func NewSudoClient(ctx context.Context, opts ...DialOption) (*SudoClient, error)

NewSudoClient 创建标准版sudo-api gRPC client。SudoClient继承了BasicSudoClient的全部功能。 client使用后需要关闭,从而释放其中的连接资源。

Example
package main

import (
	"context"

	"github.com/sudo-privacy/sudo-sdk-go/v2/pkg/sudoclient"
	"github.com/sudo-privacy/sudo-sdk-go/v2/protobuf/virtualservice/platformpb/vtable"
)

func main() {
	// ==========本地无tls连接==========
	client, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("localhost:8071"),
		sudoclient.WithHTTPEndpoint("http://localhost:7857"),
		sudoclient.WithAccount("admin", "sudosudo"))
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()
	// 使用client调用gRPC接口
	_, err = client.ListVtables(context.TODO(), &vtable.ListVtablesRequest{})
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

Example (Tls_appendCert)
package main

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"io/ioutil"
	"net/http"

	"github.com/sudo-privacy/sudo-sdk-go/v2/pkg/sudoclient"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

func main() {
	// ==========使用tls连接,添加自签证书到信任==========
	rootCAs, err := x509.SystemCertPool()
	if err != nil {
		// TODO: Handle error.
	}
	perm, err := ioutil.ReadFile("/home/we/dist-lyy-f1.crt")
	if err != nil {
		// TODO: Handle error.
	}
	ok := rootCAs.AppendCertsFromPEM(perm)
	if !ok {
		// TODO: Handle error.
	}
	tlsCfg := tls.Config{
		RootCAs: rootCAs,
	}
	transportCred := credentials.NewTLS(&tlsCfg)
	grpcTransportOpt := grpc.WithTransportCredentials(transportCred)
	httpTransport := &http.Transport{
		TLSClientConfig: &tlsCfg,
	}
	client, err := sudoclient.NewSudoClient(
		context.TODO(),
		sudoclient.WithGrpcEndpoint("grpc.dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithAccount("admin", "sudosudo"),
		sudoclient.WithHTTPEndpoint("https://dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithGrpcDialOption(grpcTransportOpt),
		sudoclient.WithHTTPTransport(httpTransport),
	)
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()
}
Output:

Example (Tls_skipVerify)
package main

import (
	"context"
	"crypto/tls"
	"net/http"

	"github.com/sudo-privacy/sudo-sdk-go/v2/pkg/sudoclient"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

func main() {
	// ==========使用tls连接,忽略证书校验==========
	tlsCfg := tls.Config{InsecureSkipVerify: true}

	transportCred := credentials.NewTLS(&tlsCfg)
	grpcTransportOpt := grpc.WithTransportCredentials(transportCred)

	httpTransport := &http.Transport{
		TLSClientConfig: &tlsCfg,
	}

	client, err := sudoclient.NewSudoClient(
		context.TODO(),
		sudoclient.WithGrpcEndpoint("grpc.dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithAccount("admin", "sudosudo"),
		sudoclient.WithHTTPEndpoint("https://dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithGrpcDialOption(grpcTransportOpt),
		sudoclient.WithHTTPTransport(httpTransport),
	)
	if err != nil {
		// TODO: Handle error.
	}
	defer client.Close()
}
Output:

func (*SudoClient) Close

func (client *SudoClient) Close() error

Close 关闭其中的连接资源。

type SudoHTTPClient

type SudoHTTPClient struct {
	http.Client
	// contains filtered or unexported fields
}

SudoHTTPClient 封装HTTP Client和tokenSource,通过http接口提供上传文件功能。

func NewHTTPClient

func NewHTTPClient(ctx context.Context, opts ...DialOption) (*SudoHTTPClient, error)

NewHTTPClient 返回封装后的HTTPClient。 endpoint和token通过opts配置。

func (*SudoHTTPClient) UploadVtableFile

func (client *SudoHTTPClient) UploadVtableFile(ctx context.Context, filePath, identityName string) (string, error)

UploadVtableFile 上传文件。

type TokenSource

type TokenSource interface {
	credentials.PerRPCCredentials
	Close() error
}

TokenSource 是对 credentials.PerRPCCredentials 的简单封装,每次访问可能触发token刷新。

func NewUserAccountToken

func NewUserAccountToken(ctx context.Context, opts ...DialOption) (TokenSource, error)

NewUserAccountToken 根据用户名、密码创建tokenSource,endpoint、账号等需要通过opts配置。

Jump to

Keyboard shortcuts

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