-
Notifications
You must be signed in to change notification settings - Fork 1
/
lowkey-vault-example_test.go
239 lines (222 loc) · 8.06 KB
/
lowkey-vault-example_test.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package src
import (
"context"
"crypto/elliptic"
"crypto/tls"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azcertificates"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"log"
"net/http"
"testing"
"time"
)
func TestSecret(t *testing.T) {
//given
httpClient := PrepareClient()
secretDatabase := "database"
secretUsername := "username"
secretPassword := "password"
database := "db"
username := "admin"
password := "s3cr3t"
client, _ := azsecrets.NewClient("https://localhost:8443",
&FakeCredential{},
&azsecrets.ClientOptions{ClientOptions: struct {
APIVersion string
Cloud cloud.Configuration
InsecureAllowCredentialWithHTTP bool
Logging policy.LogOptions
Retry policy.RetryOptions
Telemetry policy.TelemetryOptions
TracingProvider tracing.Provider
Transport policy.Transporter
PerCallPolicies []policy.Policy
PerRetryPolicies []policy.Policy
}{Transport: &httpClient}, DisableChallengeResourceVerification: true})
SetSecret(client, secretDatabase, database)
SetSecret(client, secretUsername, username)
SetSecret(client, secretPassword, password)
//when
gotDatabase, err := Secret(client, secretDatabase)
if err != nil {
log.Panicf("failed to get the secret %s", err.Error())
}
gotUsername, err := Secret(client, secretUsername)
if err != nil {
log.Panicf("failed to get the secret %s", err.Error())
}
gotPassword, err := Secret(client, secretPassword)
if err != nil {
log.Panicf("failed to get the secret %s", err.Error())
}
//then
if gotDatabase != database {
t.Errorf("got %q, wanted %q", gotDatabase, database)
}
if gotUsername != username {
t.Errorf("got %q, wanted %q", gotUsername, username)
}
if gotPassword != password {
t.Errorf("got %q, wanted %q", gotPassword, password)
}
}
func TestKey(t *testing.T) {
//given
httpClient := PrepareClient()
secretMessage := "a secret message"
keyName := "rsa-key"
client, _ := azkeys.NewClient("https://localhost:8443",
&FakeCredential{},
&azkeys.ClientOptions{ClientOptions: struct {
APIVersion string
Cloud cloud.Configuration
InsecureAllowCredentialWithHTTP bool
Logging policy.LogOptions
Retry policy.RetryOptions
Telemetry policy.TelemetryOptions
TracingProvider tracing.Provider
Transport policy.Transporter
PerCallPolicies []policy.Policy
PerRetryPolicies []policy.Policy
}{Transport: &httpClient}, DisableChallengeResourceVerification: true})
CreateKey(client, keyName)
//when
gotEncrypted, err := Encrypt(client, keyName, secretMessage)
if err != nil {
log.Panicf("failed to encrypt: %v", err)
}
gotDecrypted, err := Decrypt(client, keyName, gotEncrypted)
if err != nil {
log.Panicf("failed to decrypt: %v", err)
}
//then
if gotDecrypted != secretMessage {
t.Errorf("got %q, wanted %q", gotDecrypted, secretMessage)
}
}
func TestCertificate(t *testing.T) {
//given
httpClient := PrepareClient()
certificateName := "certificate"
subject := "CN=example.com"
secretClient, _ := azsecrets.NewClient("https://localhost:8443",
&FakeCredential{},
&azsecrets.ClientOptions{ClientOptions: struct {
APIVersion string
Cloud cloud.Configuration
InsecureAllowCredentialWithHTTP bool
Logging policy.LogOptions
Retry policy.RetryOptions
Telemetry policy.TelemetryOptions
TracingProvider tracing.Provider
Transport policy.Transporter
PerCallPolicies []policy.Policy
PerRetryPolicies []policy.Policy
}{Transport: &httpClient}, DisableChallengeResourceVerification: true})
certificateClient, _ := azcertificates.NewClient("https://localhost:8443",
&FakeCredential{},
&azcertificates.ClientOptions{ClientOptions: struct {
APIVersion string
Cloud cloud.Configuration
InsecureAllowCredentialWithHTTP bool
Logging policy.LogOptions
Retry policy.RetryOptions
Telemetry policy.TelemetryOptions
TracingProvider tracing.Provider
Transport policy.Transporter
PerCallPolicies []policy.Policy
PerRetryPolicies []policy.Policy
}{Transport: &httpClient}, DisableChallengeResourceVerification: true})
CreateCertificate(certificateClient, certificateName, subject)
//when
gotCertificate, err := Certificate(secretClient, certificateName)
if err != nil {
log.Panicf("failed to get certificate: %v", err)
}
gotKey, err := PrivateKey(secretClient, certificateName)
if err != nil {
log.Panicf("failed to get private key: %v", err)
}
//then
if gotCertificate.Subject.String() != subject {
t.Errorf("got %q, wanted %q", gotCertificate.Subject.CommonName, subject)
}
//check key curve of the obtained key
if gotKey.Curve != elliptic.P256() {
t.Errorf("got %q, wanted %q", gotKey.Curve, elliptic.P256())
}
}
func SetSecret(client *azsecrets.Client, name string, value string) {
_, err := client.SetSecret(context.TODO(), name, azsecrets.SetSecretParameters{Value: &value}, nil)
if err != nil {
log.Fatalf("failed to create a secret: %v", err)
}
}
func CreateKey(client *azkeys.Client, name string) {
rsaKeyParams := azkeys.CreateKeyParameters{
Kty: to.Ptr(azkeys.KeyTypeRSA),
KeySize: to.Ptr(int32(2048)),
KeyOps: KeyOperations(),
}
_, err := client.CreateKey(context.TODO(), name, rsaKeyParams, nil)
if err != nil {
log.Fatalf("failed to create a key: %v", err)
}
}
func CreateCertificate(client *azcertificates.Client, name string, subject string) {
_, err := client.CreateCertificate(context.TODO(), name, azcertificates.CreateCertificateParameters{
CertificatePolicy: &azcertificates.CertificatePolicy{
IssuerParameters: &azcertificates.IssuerParameters{
Name: to.Ptr("Self"),
},
KeyProperties: &azcertificates.KeyProperties{
Curve: to.Ptr(azcertificates.CurveNameP256),
KeyType: to.Ptr(azcertificates.KeyTypeEC),
ReuseKey: to.Ptr(true),
},
SecretProperties: &azcertificates.SecretProperties{
ContentType: to.Ptr("application/x-pkcs12"),
},
X509CertificateProperties: &azcertificates.X509CertificateProperties{
Subject: &subject,
SubjectAlternativeNames: &azcertificates.SubjectAlternativeNames{
DNSNames: []*string{to.Ptr("localhost")},
},
ValidityInMonths: to.Ptr(int32(12)),
},
},
}, nil)
if err != nil {
log.Fatalf("failed to create a certificate: %v", err)
}
}
/*
Ignore SSL error caused by the self-signed certificate.
*/
func PrepareClient() http.Client {
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
return http.Client{Transport: customTransport}
}
func KeyOperations() []*azkeys.KeyOperation {
return []*azkeys.KeyOperation{
to.Ptr(azkeys.KeyOperationDecrypt),
to.Ptr(azkeys.KeyOperationEncrypt),
to.Ptr(azkeys.KeyOperationUnwrapKey),
to.Ptr(azkeys.KeyOperationWrapKey),
}
}
/*
Fake token used for bypassing the fake authentication of Lowkey Vault
*/
type FakeCredential struct{}
//goland:noinspection GoUnusedParameter
func (f *FakeCredential) GetToken(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error) {
return azcore.AccessToken{Token: "faketoken", ExpiresOn: time.Now().Add(time.Hour).UTC()}, nil
}