grpctls

package module
v0.0.0-...-e99d668 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2019 License: MIT Imports: 12 Imported by: 0

README

grpctls

GRPCTLS provides helper structs and functions for grpc dialing with TLS options.

Usage

func main() {
	uri := "tcp://0.0.0.0:8069"
	cfg := grpctls.ServerCfg{
		CertFile:   "server.crt",
		KeyFile:    "server.key",
		CACert:     "ca.crt",
		ClientAuth: true,
	}

	var err error
	var creds credentials.TransportCredentials
	if cfg.UseTLS() {
		creds, err = grpctls.Creds(cfg)
		if err != nil {
			log.Fatalf("initializing TLS: %v\n", err)
		}
	}
	lis, err := grpctls.Listener(uri)
	if err != nil {
		log.Fatalf("can't listen server: %v\n", err)
	}
	grpcs := grpc.NewServer(grpc.Creds(creds))
	grpcs.Serve(lis)
}

See examples directory for common use.

Documentation

Overview

Package grpctls provides helper structs and functions for grpc dialing with TLS options. See examples directory for common use.

This package is a work in progress and makes no API stability promises.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Creds

Creds creates a transport credentials for the configuration

func Dial

func Dial(uri string, cfg ClientCfg, grpcOpts ...grpc.DialOption) (*grpc.ClientConn, error)

Dial is used for grpc client dialing

func DialContext

func DialContext(ctx context.Context, uri string, cfg ClientCfg, grpcOpts ...grpc.DialOption) (*grpc.ClientConn, error)

DialContext is used for grpc client dialing with context

func Listener

func Listener(uri string) (net.Listener, error)

Listener returns a valid listener server from an URI

func ParseURI

func ParseURI(s string) (proto string, addr string, err error)

ParseURI parses uri strings used in structs, it returns protocol and address

Types

type ClientCfg

type ClientCfg struct {
	// CertFile path to the client certificate
	CertFile string `json:"certfile,omitempty"`
	// KeyFile path to the private key
	KeyFile string `json:"keyfile,omitempty"`
	// ServerName is used for server check validation
	ServerName string `json:"servername,omitempty"`
	// ServerCert path to the server certificate
	ServerCert string `json:"servercert,omitempty"`
	// CACert path to certification authority certificate
	CACert string `json:"cacert,omitempty"`
	// UseSystemCAs if client uses system wide CA certs
	UseSystemCAs bool `json:"systemca"`
}

ClientCfg defines configuration for a client

func (ClientCfg) Empty

func (cfg ClientCfg) Empty() bool

Empty returns true if configuration values are empty

func (ClientCfg) UseTLS

func (cfg ClientCfg) UseTLS() bool

UseTLS returns true if TLS configuration is set

func (ClientCfg) Validate

func (cfg ClientCfg) Validate() error

Validate if configuration is ok

type ServerCfg

type ServerCfg struct {
	// CertFile path to the server certificate
	CertFile string
	// KeyFile path to the private key
	KeyFile string
	// CACert path to certification authority certificate
	CACert string
	// ClientAuth if client certificate is required
	ClientAuth bool
}

ServerCfg defines configuration for a server

func (ServerCfg) UseTLS

func (cfg ServerCfg) UseTLS() bool

UseTLS returns true if TLS configuration is set

func (ServerCfg) Validate

func (cfg ServerCfg) Validate() error

Validate if configuration is ok

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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