-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce TLS Settings for go-micro based grpc services and clients
TLS for the services can be configure by setting the OCIS_MICRO_GRPC_TLS_ENABLED" "OCIS_MICRO_GRPC_TLS_CERTIFICATE" and "OCIS_MICRO_GRPC_TLS_KEY" enviroment variables. TLS for the clients can configured by setting the "OCIS_MICRO_GRPC_CLIENT_TLS_MODE" and "OCIS_MICRO_GRPC_CLIENT_TLS_CACERT" variables. By default TLS is disabled.
- Loading branch information
Showing
39 changed files
with
426 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
Enhancement: Allow to setup TLS for the reva grpc services | ||
Enhancement: Allow to setup TLS for grpc services | ||
|
||
We added config options to allow enabling TLS encrption for all reva backed | ||
We added config options to allow enabling TLS encrption for all reva and go-micro backed | ||
grpc services. | ||
|
||
https://github.com/owncloud/ocis/pull/4798 | ||
https://github.com/owncloud/ocis/pull/4901 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package grpc | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"io/ioutil" | ||
"sync" | ||
|
||
mgrpcc "github.com/go-micro/plugins/v4/client/grpc" | ||
mbreaker "github.com/go-micro/plugins/v4/wrapper/breaker/gobreaker" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/registry" | ||
"github.com/owncloud/ocis/v2/ocis-pkg/shared" | ||
"go-micro.dev/v4/client" | ||
) | ||
|
||
var ( | ||
defaultClient client.Client | ||
once sync.Once | ||
) | ||
|
||
// ClientOptions represent options (e.g. tls settings) for the grpc clients | ||
type ClientOptions struct { | ||
tlsMode string | ||
caCert string | ||
} | ||
|
||
// Option is used to pass client options | ||
type ClientOption func(opts *ClientOptions) | ||
|
||
// WithTLSMode allows to set the TLSMode option for grpc clients | ||
func WithTLSMode(v string) ClientOption { | ||
return func(o *ClientOptions) { | ||
o.tlsMode = v | ||
} | ||
} | ||
|
||
// WithTLSCACert allows to set the CA Certificate for grpc clients | ||
func WithTLSCACert(v string) ClientOption { | ||
return func(o *ClientOptions) { | ||
o.caCert = v | ||
} | ||
} | ||
|
||
// Configure configures the default oOCIS grpc client (e.g. TLS settings) | ||
func Configure(opts ...ClientOption) error { | ||
var options ClientOptions | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
|
||
var outerr error | ||
once.Do(func() { | ||
reg := registry.GetRegistry() | ||
var tlsConfig *tls.Config | ||
cOpts := []client.Option{ | ||
client.Registry(reg), | ||
client.Wrap(mbreaker.NewClientWrapper()), | ||
} | ||
switch options.tlsMode { | ||
case "insecure": | ||
tlsConfig = &tls.Config{ | ||
InsecureSkipVerify: true, | ||
} | ||
cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig)) | ||
case "on": | ||
tlsConfig = &tls.Config{} | ||
if options.caCert != "" { | ||
certs := x509.NewCertPool() | ||
pemData, err := ioutil.ReadFile(options.caCert) | ||
if err != nil { | ||
outerr = err | ||
return | ||
} | ||
if !certs.AppendCertsFromPEM(pemData) { | ||
outerr = errors.New("Error initializing LDAP Backend. Adding CA cert failed") | ||
return | ||
} | ||
tlsConfig.RootCAs = certs | ||
cOpts = append(cOpts, mgrpcc.AuthTLS(tlsConfig)) | ||
} | ||
} | ||
|
||
defaultClient = mgrpcc.NewClient(cOpts...) | ||
}) | ||
return outerr | ||
} | ||
|
||
// DefaultClient returns a custom oCIS grpc configured client. | ||
func DefaultClient() client.Client { | ||
return defaultClient | ||
} | ||
|
||
func GetClientOptions(mc *shared.MicroGRPCClient) []ClientOption { | ||
opts := []ClientOption{ | ||
WithTLSMode(mc.TLSMode), | ||
WithTLSCACert(mc.TLSCACert), | ||
} | ||
return opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.