grpc_signature

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: GPL-3.0 Imports: 4 Imported by: 1

README

Library for signing grpc requests

Allows you to understand on the server side which service made the request. This is implemented through a pair of interceptors. The client writes the name of the application to the metadata, and the server reads this data and writes it to the context.

Usage

How to use interceptors
On client side
package main

import (
	"context"
	"github.com/tulzke/grpc_signature"
	"google.golang.org/grpc"
)

func main() {
	grpcClientDialOpts := []grpc.DialOption{
		grpc.WithChainUnaryInterceptor(
			grpc_signature.UnaryClientInterceptor("someApp"),
		),
		grpc.WithChainStreamInterceptor(
			grpc_signature.StreamClientInterceptor("someApp"),
		),
	}

	conn, err := grpc.DialContext(context.Background(), "", grpcClientDialOpts...)
}
On server side
package main

import (
    grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
	"github.com/tulzke/grpc_signature"
	"google.golang.org/grpc"
)

func main() {
	server := grpc.NewServer(
		grpc_middleware.WithUnaryServerChain(
			grpc_signature.UnaryServerInterceptor(),
		),
		grpc_middleware.WithStreamServerChain(
			grpc_signature.StreamServerInterceptor(),
		),
	)
}
How to get the client name on server side
package controller

import (
	"context"
	"examplev1"
	"github.com/tulzke/grpc_signature"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type Server struct{}

func (s *Server) Example(ctx context.Context, r *examplev1.Request) (*examplev1.Response, error) {
	// First way
	clientName := grpc_signature.ClientName(ctx)
	
	// Second Way
	clientName = ctx.Value(grpc_signature.ContextKey).(string)
}

Documentation

Index

Constants

View Source
const ContextKey = "client_name"
View Source
const MetadataKey = "client-name"
View Source
const UnknownClient = "unknown"

Variables

This section is empty.

Functions

func ClientName

func ClientName(ctx context.Context) string

func StreamClientInterceptor

func StreamClientInterceptor(clientName string) grpc.StreamClientInterceptor

func StreamServerInterceptor

func StreamServerInterceptor() grpc.StreamServerInterceptor

func UnaryClientInterceptor

func UnaryClientInterceptor(clientName string) grpc.UnaryClientInterceptor

func UnaryServerInterceptor

func UnaryServerInterceptor() grpc.UnaryServerInterceptor

Types

This section is empty.

Jump to

Keyboard shortcuts

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