forked from kyma-incubator/compass
/
client_fake.go
103 lines (83 loc) · 2.54 KB
/
client_fake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package lms
import (
"time"
"sync"
"errors"
"crypto/x509/pkix"
"github.com/google/uuid"
)
const (
FakeCaCertificate = "cert-ca-payload"
FakeSignedCertificate = "signed-cert-payload"
FakeLmsHost = "lms.localhost"
FakePrivateKey = "private-key"
)
// FakeClient implements the lms client interface but does not call real external system
type FakeClient struct {
mu sync.Mutex
data map[string]tenantInfo
timeToReady time.Duration
requestedCerts map[string]struct{}
}
// NewFakeClient creates lms fake client which response tenant ready after timeToReady duration
func NewFakeClient(timeToReady time.Duration) *FakeClient {
return &FakeClient{
data: make(map[string]tenantInfo, 0),
timeToReady: timeToReady,
requestedCerts: make(map[string]struct{}, 0),
}
}
type tenantInfo struct {
createdAt time.Time
}
func (f *FakeClient) CreateTenant(input CreateTenantInput) (o CreateTenantOutput, err error) {
f.mu.Lock()
defer f.mu.Unlock()
id, _ := uuid.NewRandom()
f.data[id.String()] = tenantInfo{
createdAt: time.Now(),
}
return CreateTenantOutput{
ID: id.String(),
}, nil
}
func (f *FakeClient) GetTenantStatus(tenantID string) (status TenantStatus, err error) {
ti, found := f.data[tenantID]
if !found {
return TenantStatus{}, errors.New("tenant not exists")
}
if time.Since(ti.createdAt) > f.timeToReady {
return TenantStatus{KibanaDNSResolves: true, ElasticsearchDNSResolves: true}, nil
} else {
return TenantStatus{KibanaDNSResolves: false, ElasticsearchDNSResolves: false}, nil
}
}
func (f *FakeClient) GetTenantInfo(tenantID string) (status TenantInfo, err error) {
_, found := f.data[tenantID]
if !found {
return TenantInfo{}, errors.New("tenant not exists")
}
return TenantInfo{DNS: FakeLmsHost}, nil
}
func (f *FakeClient) GetCACertificate(tenantID string) (cert string, found bool, err error) {
if !f.IsCertRequestedForTenant(tenantID) {
return "", false, errors.New("certificate not requested")
}
return FakeCaCertificate, true, nil
}
func (f *FakeClient) GetCertificateByURL(url string) (cert string, found bool, err error) {
return FakeSignedCertificate, true, nil
}
func (f *FakeClient) RequestCertificate(tenantID string, subj pkix.Name) (id string, privateKey []byte, err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.requestedCerts[tenantID] = struct{}{}
return "id-001", []byte(FakePrivateKey), nil
}
// assert methods
func (f *FakeClient) IsCertRequestedForTenant(tenantID string) bool {
f.mu.Lock()
defer f.mu.Unlock()
_, found := f.requestedCerts[tenantID]
return found
}