Add method for creating in-memory certificates
This commit is contained in:
committed by
Ralf Haferkamp
parent
a4f5682851
commit
cbe41fb85f
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||||
|
mtls "go-micro.dev/v4/util/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -50,6 +52,16 @@ func GenCert(certName string, keyName string, l log.Logger) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenTempCertForAddr generates temporary TLS-Certificates in memory.
|
||||||
|
func GenTempCertForAddr(addr string) (tls.Certificate, error) {
|
||||||
|
subjects := defaultHosts
|
||||||
|
|
||||||
|
if host, _, err := net.SplitHostPort(addr); err == nil && host != "" {
|
||||||
|
subjects = []string{host}
|
||||||
|
}
|
||||||
|
return mtls.Certificate(subjects...)
|
||||||
|
}
|
||||||
|
|
||||||
// persistCertificate generates a certificate using pk as private key and proceeds to store it into a file named certName.
|
// persistCertificate generates a certificate using pk as private key and proceeds to store it into a file named certName.
|
||||||
func persistCertificate(certName string, l log.Logger, pk interface{}) error {
|
func persistCertificate(certName string, l log.Logger, pk interface{}) error {
|
||||||
if err := ensureExistsDir(certName); err != nil {
|
if err := ensureExistsDir(certName); err != nil {
|
||||||
|
|||||||
@@ -3,17 +3,16 @@ package grpc
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mgrpcs "github.com/go-micro/plugins/v4/server/grpc"
|
mgrpcs "github.com/go-micro/plugins/v4/server/grpc"
|
||||||
"github.com/go-micro/plugins/v4/wrapper/monitoring/prometheus"
|
"github.com/go-micro/plugins/v4/wrapper/monitoring/prometheus"
|
||||||
"github.com/go-micro/plugins/v4/wrapper/trace/opencensus"
|
"github.com/go-micro/plugins/v4/wrapper/trace/opencensus"
|
||||||
|
ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto"
|
||||||
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
|
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
|
||||||
"go-micro.dev/v4"
|
"go-micro.dev/v4"
|
||||||
"go-micro.dev/v4/server"
|
"go-micro.dev/v4/server"
|
||||||
mtls "go-micro.dev/v4/util/tls"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service simply wraps the go-micro grpc service.
|
// Service simply wraps the go-micro grpc service.
|
||||||
@@ -38,15 +37,7 @@ func NewService(opts ...Option) (Service, error) {
|
|||||||
} else {
|
} else {
|
||||||
// Generate a self-signed server certificate on the fly. This requires the clients
|
// Generate a self-signed server certificate on the fly. This requires the clients
|
||||||
// to connect with InsecureSkipVerify.
|
// to connect with InsecureSkipVerify.
|
||||||
subj := []string{sopts.Address}
|
cert, err = ociscrypto.GenTempCertForAddr(sopts.Address)
|
||||||
if host, _, err := net.SplitHostPort(sopts.Address); err == nil && host != "" {
|
|
||||||
subj = []string{host}
|
|
||||||
}
|
|
||||||
|
|
||||||
sopts.Logger.Warn().Str("address", sopts.Address).
|
|
||||||
Msg("GRPC: No server certificate configured. Generating a temporary self-signed certificate")
|
|
||||||
|
|
||||||
cert, err = mtls.Certificate(subj...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Service{}, fmt.Errorf("grpc service error creating temporary self-signed certificate: %w", err)
|
return Service{}, fmt.Errorf("grpc service error creating temporary self-signed certificate: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user