Skip to content

Commit

Permalink
Minor code and comment improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Sep 18, 2024
1 parent b2cd261 commit 489dcd8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cmd/tusd/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ func ParseFlags() {
f.IntVar(&Flags.GrpcHooksRetry, "hooks-grpc-retry", 3, "Number of times to retry on a server error or network timeout")
f.DurationVar(&Flags.GrpcHooksBackoff, "hooks-grpc-backoff", 1*time.Second, "Wait period before retrying each retry")
f.BoolVar(&Flags.GrpcHooksSecure, "hooks-grpc-secure", false, "Enables secure connection via TLS certificates to the specified gRPC endpoint")
f.StringVar(&Flags.GrpcHooksServerTLSCertFile, "hooks-grpc-server-tls-certificate", "", "Path to the file containing the TLS certificate of the remote gRPC server. This is used in order to add the gRPC server as trusted.")
f.StringVar(&Flags.GrpcHooksClientTLSCertFile, "hooks-grpc-client-tls-certificate", "", "Path to the file containing TLS certificate to be used as client.")
f.StringVar(&Flags.GrpcHooksClientTLSKeyFile, "hooks-grpc-client-tls-key", "", "Path to the file containing the key for the Client TLS certificate.")
f.StringVar(&Flags.GrpcHooksServerTLSCertFile, "hooks-grpc-server-tls-certificate", "", "Path to the file containing the TLS certificate of the remote gRPC server")
f.StringVar(&Flags.GrpcHooksClientTLSCertFile, "hooks-grpc-client-tls-certificate", "", "Path to the file containing the client certificate for mTLS")
f.StringVar(&Flags.GrpcHooksClientTLSKeyFile, "hooks-grpc-client-tls-key", "", "Path to the file containing the client key for mTLS")
})

fs.AddGroup("Plugin hook options", func(f *flag.FlagSet) {
Expand Down
50 changes: 25 additions & 25 deletions pkg/hooks/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,38 @@ func (g *GrpcHook) Setup() error {
grpcOpts := []grpc.DialOption{}

if g.Secure {
// Load the server's TLS certificate if provided
if g.ServerTLSCertificateFilePath != "" {
serverCert, err := os.ReadFile(g.ServerTLSCertificateFilePath)
if err != nil {
return err
}
if g.ServerTLSCertificateFilePath == "" {
return errors.New("hooks-grpc-secure was set to true but no gRPC server TLS certificate file was provided. A value for hooks-grpc-server-tls-certificate is missing")
}

// Create a certificate pool and add the server's certificate
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(serverCert)
// Load the server's TLS certificate if provided
serverCert, err := os.ReadFile(g.ServerTLSCertificateFilePath)
if err != nil {
return err
}

// Create TLS configuration with the server's CA certificate
tlsConfig := &tls.Config{
RootCAs: certPool,
}
// Create a certificate pool and add the server's certificate
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(serverCert)

// If client's TLS certificate and key file paths are provided, use mutual TLS
if g.ClientTLSCertificateFilePath != "" && g.ClientTLSCertificateKeyFilePath != "" {
// Load the client's TLS certificate and private key
clientCert, err := tls.LoadX509KeyPair(g.ClientTLSCertificateFilePath, g.ClientTLSCertificateKeyFilePath)
if err != nil {
return err
}
// Create TLS configuration with the server's CA certificate
tlsConfig := &tls.Config{
RootCAs: certPool,
}

// Append client certificate to the TLS configuration
tlsConfig.Certificates = append(tlsConfig.Certificates, clientCert)
// If client's TLS certificate and key file paths are provided, use mutual TLS
if g.ClientTLSCertificateFilePath != "" && g.ClientTLSCertificateKeyFilePath != "" {
// Load the client's TLS certificate and private key
clientCert, err := tls.LoadX509KeyPair(g.ClientTLSCertificateFilePath, g.ClientTLSCertificateKeyFilePath)
if err != nil {
return err
}

grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
return errors.New("hooks-grpc-secure was set to true but no gRPC server TLS certificate file was provided. A value for hooks-grpc-server-tls-certificate is missing")
// Append client certificate to the TLS configuration
tlsConfig.Certificates = append(tlsConfig.Certificates, clientCert)
}

grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
Expand Down

0 comments on commit 489dcd8

Please sign in to comment.