generated from ConduitIO/conduit-connector-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add mTLS * fix generated files * Update paramgen_dest.go * address reviews * go mod tidy * address reviews 2
- Loading branch information
1 parent
8c3c5b4
commit 7bc051e
Showing
15 changed files
with
490 additions
and
41 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
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,85 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package grpcclient | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
sdk "github.com/conduitio/conduit-connector-sdk" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
// Config has the generic parameters needed for a gRPC client | ||
type Config struct { | ||
// url to gRPC server | ||
URL string `json:"url" validate:"required"` | ||
// the bandwidth limit in bytes/second, use "0" to disable rate limiting. | ||
RateLimit int `json:"rateLimit" default:"0" validate:"gt=-1"` | ||
// mTLS configurations. | ||
MTLS MTLSConfig `json:"mtls"` | ||
} | ||
|
||
type MTLSConfig struct { | ||
// the client certificate path. | ||
ClientCertPath string `json:"client.certPath"` | ||
// the client private key path. | ||
ClientKeyPath string `json:"client.keyPath"` | ||
// the root CA certificate path. | ||
CACertPath string `json:"ca.certPath"` | ||
// option to disable mTLS secure connection, set it to `true` for an insecure connection. | ||
Disabled bool `json:"disabled" default:"false"` | ||
} | ||
|
||
// ParseMTLSFiles parses and validates mTLS params values, returns the parsed client certificate, and CA certificate pool, | ||
// and an error if the parsing fails | ||
func (mc *MTLSConfig) ParseMTLSFiles() (tls.Certificate, *x509.CertPool, error) { | ||
err := mc.validateRequiredMTLSParams() | ||
if err != nil { | ||
return tls.Certificate{}, nil, fmt.Errorf("error validating \"mtls\": mTLS security is enabled and some"+ | ||
" configurations are missing, if you wish to disable mTLS, set the config option \"mtls.disabled\" to true: %w", err) | ||
} | ||
clientCert, err := tls.LoadX509KeyPair(mc.ClientCertPath, mc.ClientKeyPath) | ||
if err != nil { | ||
return tls.Certificate{}, nil, fmt.Errorf("failed to load client key pair: %w", err) | ||
} | ||
// Load CA certificate | ||
caCert, err := os.ReadFile(mc.CACertPath) | ||
if err != nil { | ||
return tls.Certificate{}, nil, fmt.Errorf("failed to read CA certificate: %w", err) | ||
} | ||
caCertPool := x509.NewCertPool() | ||
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok { | ||
return tls.Certificate{}, nil, errors.New("failed to append CA certs") | ||
} | ||
return clientCert, caCertPool, nil | ||
} | ||
|
||
func (mc *MTLSConfig) validateRequiredMTLSParams() error { | ||
var multiErr error | ||
if mc.CACertPath == "" { | ||
multiErr = multierr.Append(multiErr, fmt.Errorf("error validating \"mtls.ca.certPath\": %w", sdk.ErrRequiredParameterMissing)) | ||
} | ||
if mc.ClientCertPath == "" { | ||
multiErr = multierr.Append(multiErr, fmt.Errorf("error validating \"mtls.client.certPath\": %w", sdk.ErrRequiredParameterMissing)) | ||
} | ||
if mc.ClientKeyPath == "" { | ||
multiErr = multierr.Append(multiErr, fmt.Errorf("error validating \"mtls.client.keyPath\": %w", sdk.ErrRequiredParameterMissing)) | ||
} | ||
return multiErr | ||
} |
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,82 @@ | ||
// Copyright © 2023 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package grpcclient | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConfig_ParseMTLSFiles(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
config MTLSConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid paths", | ||
config: MTLSConfig{ | ||
ClientCertPath: "./test/certs/client.crt", | ||
ClientKeyPath: "./test/certs/client.key", | ||
CACertPath: "./test/certs/ca.crt", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "empty values", | ||
config: MTLSConfig{ | ||
ClientCertPath: "", | ||
ClientKeyPath: "", | ||
CACertPath: "", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid paths", | ||
config: MTLSConfig{ | ||
ClientCertPath: "not a file", | ||
ClientKeyPath: "not a file", | ||
CACertPath: "not a file", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "switched files", | ||
config: MTLSConfig{ | ||
ClientCertPath: "./test/certs/client.key", // switched with client crt | ||
ClientKeyPath: "./test/certs/client.crt", | ||
CACertPath: "./test/certs/ca.crt", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "wrong CA cert path", | ||
config: MTLSConfig{ | ||
ClientCertPath: "./test/certs/client.crt", | ||
ClientKeyPath: "./test/certs/client.key", | ||
CACertPath: "./test/certs/ca.key", // key instead of crt, should fail | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
_, _, err := tc.config.ParseMTLSFiles() | ||
if (err != nil) != tc.wantErr { | ||
t.Errorf("ParseMTLSFiles() error = %v, wantErr = %v", err, tc.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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.