istio

Форк
0
84 строки · 2.0 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package mock
16

17
import (
18
	"context"
19
	"fmt"
20
	"net"
21

22
	"google.golang.org/grpc"
23

24
	gcapb "istio.io/istio/security/proto/providers/google"
25
)
26

27
// CAService is a simple mocked Google CA Service.
28
type CAService struct {
29
	*gcapb.UnimplementedMeshCertificateServiceServer
30
	Certs []string
31
	Err   error
32
}
33

34
// CreateCertificate is a mocked function for the Google Mesh CA API.
35
func (ca *CAService) CreateCertificate(ctx context.Context, in *gcapb.MeshCertificateRequest) (
36
	*gcapb.MeshCertificateResponse, error,
37
) {
38
	if ca.Err == nil {
39
		return &gcapb.MeshCertificateResponse{CertChain: ca.Certs}, nil
40
	}
41
	return nil, ca.Err
42
}
43

44
// CAServer is the mocked Mesh CA server.
45
type CAServer struct {
46
	Server  *grpc.Server
47
	Address string
48
}
49

50
// CreateServer creates a mocked local Google CA server and runs it in a separate thread.
51
// nolint: interfacer
52
func CreateServer(addr string, service *CAService) (*CAServer, error) {
53
	// create a local grpc server
54
	s := &CAServer{
55
		Server: grpc.NewServer(),
56
	}
57

58
	lis, err := net.Listen("tcp", addr)
59
	if err != nil {
60
		return nil, fmt.Errorf("failed to listen on the TCP address: %v", err)
61
	}
62
	s.Address = lis.Addr().String()
63

64
	var serveErr error
65
	gcapb.RegisterMeshCertificateServiceServer(s.Server, service)
66
	go func() {
67
		if err := s.Server.Serve(lis); err != nil {
68
			serveErr = err
69
		}
70
	}()
71

72
	if serveErr != nil {
73
		return nil, err
74
	}
75

76
	return s, nil
77
}
78

79
// Stop stops the Mock Mesh CA server.
80
func (s *CAServer) Stop() {
81
	if s.Server != nil {
82
		s.Server.Stop()
83
	}
84
}
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.