github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/target/nats_tls_contrib_test.go (about)

     1  /*
     2   * MinIO Object Storage (c) 2021-2023 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package target
    18  
    19  import (
    20  	"path"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	xnet "github.com/minio/pkg/v2/net"
    25  	natsserver "github.com/nats-io/nats-server/v2/test"
    26  )
    27  
    28  func TestNatsConnTLSCustomCA(t *testing.T) {
    29  	s, opts := natsserver.RunServerWithConfig(filepath.Join("testdata", "contrib", "nats_tls.conf"))
    30  	defer s.Shutdown()
    31  
    32  	clientConfig := &NATSArgs{
    33  		Enable: true,
    34  		Address: xnet.Host{
    35  			Name:      "localhost",
    36  			Port:      (xnet.Port(opts.Port)),
    37  			IsPortSet: true,
    38  		},
    39  		Subject:       "test",
    40  		Secure:        true,
    41  		CertAuthority: path.Join("testdata", "contrib", "certs", "root_ca_cert.pem"),
    42  	}
    43  
    44  	con, err := clientConfig.connectNats()
    45  	if err != nil {
    46  		t.Errorf("Could not connect to nats: %v", err)
    47  	}
    48  	defer con.Close()
    49  }
    50  
    51  func TestNatsConnTLSClientAuthorization(t *testing.T) {
    52  	s, opts := natsserver.RunServerWithConfig(filepath.Join("testdata", "contrib", "nats_tls_client_cert.conf"))
    53  	defer s.Shutdown()
    54  
    55  	clientConfig := &NATSArgs{
    56  		Enable: true,
    57  		Address: xnet.Host{
    58  			Name:      "localhost",
    59  			Port:      (xnet.Port(opts.Port)),
    60  			IsPortSet: true,
    61  		},
    62  		Subject:       "test",
    63  		Secure:        true,
    64  		CertAuthority: path.Join("testdata", "contrib", "certs", "root_ca_cert.pem"),
    65  		ClientCert:    path.Join("testdata", "contrib", "certs", "nats_client_cert.pem"),
    66  		ClientKey:     path.Join("testdata", "contrib", "certs", "nats_client_key.pem"),
    67  	}
    68  
    69  	con, err := clientConfig.connectNats()
    70  	if err != nil {
    71  		t.Errorf("Could not connect to nats: %v", err)
    72  	}
    73  	defer con.Close()
    74  }