pir

package
v2.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package pir 简化pir服务的部署和使用,基于 sudoclient.SudoClient 封装的gRPC接口实现。

ServerClient 分别封装了pir server,pir client相关的gRPC接口调用。

部署 Pir Server Example

下面是创建并部署pir server service的example。

clientF1, err := sudoclient.NewSudoClient(context.TODO(),
	sudoclient.WithGrpcEndpoint("localhost:8071"),
	sudoclient.WithHTTPEndpoint("http://localhost:7857"),
	sudoclient.WithAccount("admin", "sudosudo"),
)
if err != nil {
	// TODO: Handle error.
}
factoryF1 := NewPirFactory(clientF1)
// 创建并等待pir server service部署完成
pirServerF1, err := factoryF1.NewPirServer(context.TODO(),
	"IDENTITY_NAME_HERE",
	enums.SVCType_PIR,
	&protopir.DataModeParams{
		Params: &protopir.DataModeParams_VtableParams{
			VtableParams: &protopir.VtableModeParams{
				VtableId: 7,
			},
		},
	},
	[]string{"ID"},
	[]string{"age"},
	10,
	true,
)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*sudoclient.SudoClient
	// contains filtered or unexported fields
}

Client 封装了关于pir client service的gRPC接口,可用于client service管理和pir查询。

func (*Client) Delete

func (c *Client) Delete(ctx context.Context) error

Delete 终结并删除service。

func (*Client) Deploy

func (c *Client) Deploy(ctx context.Context, blocking bool) error

Deploy 部署pir service。 如果blocking为true,阻塞等待直到服务部署完成。

func (*Client) Factor3SVerify

Factor3SVerify 三要素查询,grpc接口的简单封装,query参数不用设置subPath字段。

Example
package main

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

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"

	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/pir"
	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/sudoclient"
	"github.com/lvyangyang/sudo-sdk-go/v2/protobuf/basic/protobuf/service/enums"

	protopir "github.com/lvyangyang/sudo-sdk-go/v2/protobuf/basic/protobuf/virtualservice/platformpb/pir"
	"github.com/lvyangyang/sudo-sdk-go/v2/protobuf/online_service"
)

func main() {
	tlsCfg := tls.Config{InsecureSkipVerify: true}

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

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

	//=================server side====================

	// prepare server side sudoClient
	client1, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("grpc.dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithHTTPEndpoint("https://dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithAccount("admin", "sudosudo"),
		sudoclient.WithGrpcDialOption(grpcTransportOpt),
		sudoclient.WithHTTPTransport(httpTransport),
	)
	if err != nil {
		// TODO: Handle error.
	}
	factoryF1 := pir.NewPirFactory(client1)
	// New pir server(blocking,idempotent by identityName)
	server, err := factoryF1.NewPirServer(context.TODO(),
		"lyy-server-test11",
		enums.SVCType_FACTOR3s,
		&protopir.DataModeParams{
			Params: &protopir.DataModeParams_ApiParams{
				ApiParams: &protopir.APIModeParams{
					Url:  "http://10.0.1.40:9123",
					Path: "/api/mock/data",
					AuthParams: &protopir.AuthParams{
						AuthMethod: enums.AuthMethod_NO_AUTH,
					},
				},
			},
		},
		[]string{"phone_md5"},
		[]string{"id_md5", "name_md5"},
		10,
		true,
	)
	if err != nil {
		// TODO: Handle error.
	}
	// prepare client token
	token, err := server.NewClientToken(context.TODO(), "dist-lyy-f2")
	if err != nil {
		// TODO: Handle error.
	}
	serviceID := server.GetServiceID()

	//=================client side====================

	// prepare client side sudoClient
	clientF2, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("grpc.dist-lyy-f2.dist.sudoprivacy.cn:30443"),
		sudoclient.WithHTTPEndpoint("https://dist-lyy-f2.dist.sudoprivacy.cn:30443"),
		sudoclient.WithAccount("admin", "sudosudo"),
		sudoclient.WithGrpcDialOption(grpcTransportOpt),
		sudoclient.WithHTTPTransport(httpTransport),
	)
	if err != nil {
		// TODO: Handle error.
	}
	factoryF2 := pir.NewPirFactory(clientF2)
	// new client(blocking,idempotent by token)
	client, err := factoryF2.NewPirClient(context.TODO(),
		"dist-lyy-f1",
		"wss://dist-lyy-f1.dist.sudoprivacy.cn:30443/ws", // 如果是 http 则配置为ws
		fmt.Sprintf("%d", serviceID),
		token,
		enums.SVCType_FACTOR3s,
		true,
	)
	if err != nil {
		// TODO: Handle error.
	}
	// 三要素查询
	query := &online_service.Factor3SVerifyRequest{
		PhoneMd5: "63b78922bef3d161d2e617080e233011",
		IdMd5:    "4c28b1a6e8bb7cdc6d6050c61a748802",
		NameMd5:  "3a30b24a583acdb203ab1092c793cdc2",
	}
	_, err = client.Factor3SVerify(context.TODO(), query)
	if err != nil {
		// TODO: Handle error.
	}

	// ============release all resoure===============
	err = client.Delete(context.TODO())
	err = server.Delete(context.TODO())
	factoryF1.Close()
	factoryF2.Close()
}
Output:

func (*Client) ID

func (c *Client) ID() uint64

ID 返回id属性。

func (*Client) IsActive

func (c *Client) IsActive(ctx context.Context) (bool, error)

IsActive 检查service 是否就绪可用。

func (*Client) Name

func (c *Client) Name() string

Name 返回name属性

func (*Client) Pir

func (c *Client) Pir(ctx context.Context, queries []*pir.PirRequest_KeyColumn) (*pir.PirResponse, error)

Pir 查询。

Example
package main

import (
	"context"
	"fmt"

	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/pir"
	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/sudoclient"
	"github.com/lvyangyang/sudo-sdk-go/v2/protobuf/basic/protobuf/service/enums"

	protopir "github.com/lvyangyang/sudo-sdk-go/v2/protobuf/basic/protobuf/virtualservice/platformpb/pir"
)

func main() {
	//=================server side====================

	// prepare server side sudoClient
	sudoClientF1, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("localhost:8071"),
		sudoclient.WithHTTPEndpoint("http://localhost:7857"),
		sudoclient.WithAccount("admin", "sudosudo"),
	)
	if err != nil {
		// TODO: Handle error.
	}
	factoryF1 := pir.NewPirFactory(sudoClientF1)
	// New pir pirServerF1(blocking,idempotent by identityName)
	pirServerF1, err := factoryF1.NewPirServer(context.TODO(),
		"IDENTITY_NAME_HERE",
		enums.SVCType_PIR,
		&protopir.DataModeParams{
			Params: &protopir.DataModeParams_VtableParams{
				VtableParams: &protopir.VtableModeParams{
					VtableId: 7,
				},
			},
		},
		[]string{"ID"},
		[]string{"age"},
		10,
		true,
	)
	// prepare client token
	token, err := pirServerF1.NewClientToken(context.TODO(), "party-f2")
	serviceID := pirServerF1.GetServiceID()

	//=================client side====================

	// prepare client side sudoClient
	sudoClientF2, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("localhost:18071"),
		sudoclient.WithHTTPEndpoint("http://localhost:17857"),
		sudoclient.WithAccount("admin", "sudosudo"),
	)
	if err != nil {
		// TODO: Handle error.
	}
	factoryF2 := pir.NewPirFactory(sudoClientF2)
	// new pirClientF2(blocking,idempotent by token)
	pirClientF2, err := factoryF2.NewPirClient(context.TODO(),
		"party-f1",
		"ws://localhost:7857/ws", // 服务方nginx地址,https入口需配置为wss
		fmt.Sprintf("%d", serviceID),
		token,
		enums.SVCType_PIR,
		true,
	)
	if err != nil {
		// TODO: Handle error.
	}

	query := []*protopir.PirRequest_KeyColumn{}
	query = append(query, &protopir.PirRequest_KeyColumn{
		Values: []string{"13380"},
	})
	query = append(query, &protopir.PirRequest_KeyColumn{
		Values: []string{"17816"},
	})
	// do pir request
	_, err = pirClientF2.Pir(context.TODO(), query)
	if err != nil {
		// TODO: Handle error.
	}

	// ============release all resoure===============
	err = pirClientF2.Delete(context.TODO())
	err = pirServerF1.Delete(context.TODO())
	factoryF1.Close()
	factoryF2.Close()
}
Output:

func (*Client) ServiceID

func (c *Client) ServiceID() string

ServiceID 返回serviceID属性

func (*Client) Status

func (c *Client) Status(ctx context.Context) (enums.PirService_Status, error)

Status 查询并返回状态。

func (*Client) TerminateService

func (c *Client) TerminateService(ctx context.Context) error

TerminateService 仅终结service。service未删除,之后还可以重新部署。

func (*Client) Token

func (c *Client) Token() string

Token 返回token属性。

func (*Client) WaitForReady

func (c *Client) WaitForReady(ctx context.Context) error

WaitForReady 阻塞等待直到 service 部署完成,如果检查到部署失败会直接返回错误。

type Factory

type Factory struct {
	*sudoclient.SudoClient
}

Factory 是对 sudoclient.SudoClient 的简单封装,提供创建pir Server/Client方法。

func NewPirFactory

func NewPirFactory(client *sudoclient.SudoClient) *Factory

NewPirFactory 返回一个简单封装 sudoclient.SudoClientFactory

func (*Factory) CreatePirClient

func (f *Factory) CreatePirClient(
	ctx context.Context,
	serverParty, serverPath, serverServiceID, tokenStr string, svcType enums.SVCType,
) (*Client, error)

CreatePirClient 创建pir client。 参数配置参考 Factory.NewPirClient

func (*Factory) CreatePirServer

func (f *Factory) CreatePirServer(
	ctx context.Context,
	identityName string,
	svcType enums.SVCType,
	dataParam *pir.DataModeParams,
	keyColumns []string,
	labelColumns []string,
	indiscernibilityDegree uint64,
) (*Server, error)

CreatePirServer 创建pir server。 参数配置参考 Factory.NewPirServer

func (*Factory) NewPirClient

func (f *Factory) NewPirClient(
	ctx context.Context,
	serverParty, serverPath, serverServiceID, tokenStr string,
	svcType enums.SVCType,
	blocking bool,
) (*Client, error)

NewPirClient 创建并部署client service。

  • 如果设置blocking,阻塞等待直到service部署完成。

  • serverParty 设置pir服务端partyID。

  • serverPath 设置服务端url地址。比如"ws://localhost:7857/ws"。

  • serverServiceID 设置服务端serviceID,需要从服务端pir server读取。

  • tokenStr 设置服务端提供的token,需要服务端pir server提前为客户端party创建,参考 Server.NewClientToken

  • svcType 设置 pir 或三要素服务。

func (*Factory) NewPirServer

func (f *Factory) NewPirServer(
	ctx context.Context,
	identityName string,
	svcType enums.SVCType,
	dataParam *pir.DataModeParams,
	keyColumns []string,
	labelColumns []string,
	indiscernibilityDegree uint64,
	blocking bool,
) (*Server, error)

NewPirServer 创建并部署 pir server。

type Manager

type Manager struct {
	Factory
}

Manager 是对 Factory 的简单封装,封装管控相关接口。

func NewManager

func NewManager(factory Factory) *Manager

NewManager 返回一个简单封装 sudoclient.SudoClientManager

func (*Manager) GetClients

func (m *Manager) GetClients(ctx context.Context, query *pir.GetPirClientServicesRequst) ([]*Client, error)

GetClients 查询 Client , 根据输入的过滤参数,可能返回0、1个 或多个 Client。

func (*Manager) GetServers

func (m *Manager) GetServers(ctx context.Context, query *pir.GetServerServicesRequest) ([]*Server, error)

GetServers 查询 Server ,根据输入的过滤参数,可能返回0个、1个 或多个 Server。

func (*Manager) ListAPIUsages

ListAPIUsages 是对grpc接口的简单封装。

func (*Manager) UpsertUsageNotifyReceiver

func (m *Manager) UpsertUsageNotifyReceiver(
	ctx context.Context,
	req *basic_api_usage.UpsertAPIUsageReceiverRequest,
) error

UpsertUsageNotifyReceiver 是对 grpc 接口的简单封装。

type Server

type Server struct {
	*sudoclient.SudoClient
	// contains filtered or unexported fields
}

Server 封装了关于pir server service的gRPC接口。 用于service的部署、终结,和token创建、停用等操作。

func (*Server) Delete

func (s *Server) Delete(ctx context.Context) error

Delete 终结并删除service。

func (*Server) Deploy

func (s *Server) Deploy(ctx context.Context, blocking bool) error

Deploy 部署service. 如果设置blocking,阻塞等待直到服务就绪。

func (*Server) DisableClientToken

func (s *Server) DisableClientToken(ctx context.Context, tokenStr string) error

DisableClientToken 停用token。

func (*Server) EnableClientToken

func (s *Server) EnableClientToken(ctx context.Context, tokenStr string) error

EnableClientToken 启用token。

func (*Server) GetServiceID

func (s *Server) GetServiceID() uint64

获取serviceID,serviceID是pir server service的属性。

func (*Server) ID

func (s *Server) ID() uint64

ID 返回id属性。

func (*Server) IsActive

func (s *Server) IsActive(ctx context.Context) (bool, error)

IsActive 检查服务是否就绪。

func (*Server) Name

func (s *Server) Name() string

Name 返回name属性

func (*Server) NewClientToken

func (s *Server) NewClientToken(ctx context.Context, partyID string) (string, error)

New ClientToken 为客户端参与方创建token。 token用于部署client端服务。 允许为同一party创建多个token,不同token的client端service的pir查询计量会区别统计。 token创建后默认处于启用状态。

func (*Server) ServiceID

func (s *Server) ServiceID() uint64

ServiceID 返回serviceID属性

func (*Server) Status

func (s *Server) Status(ctx context.Context) (enums.PirService_Status, error)

Status 查询并返回状态。

func (*Server) TerminateService

func (s *Server) TerminateService(ctx context.Context) error

TerminateService 仅终结service。

func (*Server) UpdateServerData

func (s *Server) UpdateServerData(ctx context.Context, dataParam *pir.DataModeParams) error

UpdateServerData 更新server service数据,grpc 接口的简单封装,更新过程中原来的数据正常提供服务。 更新状态可通过 Server.Status 查询。

Example
package main

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

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"

	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/pir"
	"github.com/lvyangyang/sudo-sdk-go/v2/pkg/sudoclient"

	protopir "github.com/lvyangyang/sudo-sdk-go/v2/protobuf/basic/protobuf/virtualservice/platformpb/pir"
)

func main() {
	tlsCfg := tls.Config{InsecureSkipVerify: true}

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

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

	client1, err := sudoclient.NewSudoClient(context.TODO(),
		sudoclient.WithGrpcEndpoint("grpc.dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithHTTPEndpoint("https://dist-lyy-f1.dist.sudoprivacy.cn:30443"),
		sudoclient.WithAccount("admin", "sudosudo"),
		sudoclient.WithGrpcDialOption(grpcTransportOpt),
		sudoclient.WithHTTPTransport(httpTransport),
	)
	if err != nil {
		// TODO: Handle error.
	}
	factory1 := pir.NewPirFactory(client1)
	manager := pir.NewManager(*factory1)
	servers, err := manager.GetServers(context.TODO(), &protopir.GetServerServicesRequest{
		Query: &protopir.ServerQueryOption{
			Id: 14,
		},
	})
	if err != nil {
		// TODO: Handle error.
	}
	err = servers[0].UpdateServerData(context.TODO(), &protopir.DataModeParams{
		Params: &protopir.DataModeParams_VtableParams{
			VtableParams: &protopir.VtableModeParams{
				// new Vtable
				VtableId: 3,
			},
		},
	})
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func (*Server) WaitForReady

func (s *Server) WaitForReady(ctx context.Context, interval time.Duration) error

WaitForReady 阻塞等待,直到服务就绪。如果检查到部署失败会直接返回错误。

Jump to

Keyboard shortcuts

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