-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath_role_test.go
More file actions
183 lines (153 loc) · 5.65 KB
/
Copy pathpath_role_test.go
File metadata and controls
183 lines (153 loc) · 5.65 KB
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
/*
Copyright © 2024 Keyfactor
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 ejbca
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
)
const (
testRoleName = "test-role"
)
func TestRole(t *testing.T) {
b, reqStorage := getTestBackend(t)
err := testConfigCreate(t, b, reqStorage, map[string]interface{}{
"client_cert": clientCert,
"client_key": clientKey,
"ca_cert": caCert,
"hostname": hostname,
"default_ca": _defaultCaName,
"default_end_entity_profile": defaultEndEntityProfile,
"default_certificate_profile": defaultCertificateProfile,
})
assert.NoError(t, err)
maxTTL, _ := time.ParseDuration("1h")
notBeforeDuration, _ := time.ParseDuration("15m")
var testRole = map[string]interface{}{
"max_ttl": int(maxTTL.Seconds()),
"ttl": int(notBeforeDuration.Seconds()),
"allow_localhost": true,
"allowed_domains": []string{"example.com", "example.org"},
"allow_bare_domains": true,
"allow_subdomains": true,
"allow_glob_domains": true,
"allow_any_name": false,
"allowed_uri_sans_template": false,
"enforce_hostnames": true,
"allow_ip_sans": true,
"allowed_uri_sans": []string{"urn:example"},
"server_flag": true,
"client_flag": false,
"code_signing_flag": false,
"email_protection_flag": true,
"key_type": "rsa",
"key_bits": 2048,
"signature_bits": 256,
"use_pss": true,
"use_csr_common_name": true,
"use_csr_sans": false,
"key_usage": []string{"Digital Signature", "Key Encipherment"},
"ext_key_usage": []string{"Server Auth", "Client Auth"},
"ext_key_usage_oids": []string{"1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.2"},
"ou": []string{"Engineering"},
"organization": []string{"Example Co."},
"country": []string{"US"},
"locality": []string{"San Francisco"},
"province": []string{"California"},
"street_address": []string{"123 Example St."},
"postal_code": []string{"94101"},
"no_store": true,
"require_cn": true,
"cn_validations": []string{"disabled"},
"allowed_serial_numbers": []string{"123456789"},
"allowed_user_ids": []string{"userid1", "userid2"},
"basic_constraints_valid_for_non_ca": true,
"not_before_duration": int(notBeforeDuration.Seconds()),
"not_after": "2024-05-18T00:00:00Z",
"issuer_ref": "ManagementCA",
"end_entity_profile_name": "End Entity Profile Name",
"certificate_profile_name": "Certificate Profile Name",
"account_binding_id": "Account Binding ID",
}
t.Run("Test Create Role", func(t *testing.T) {
err := testRoleCreate(t, b, reqStorage, testRole)
assert.NoError(t, err)
})
t.Run("Test Read Role", func(t *testing.T) {
err := testRoleRead(t, b, reqStorage, testRole)
assert.NoError(t, err)
})
t.Run("Test Delete Role", func(t *testing.T) {
err := testRoleDelete(t, b, reqStorage)
assert.NoError(t, err)
})
}
func testRoleCreate(_ *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: roleStoragePath + testRoleName,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}
func testRoleRead(_ *testing.T, b logical.Backend, s logical.Storage, expected map[string]interface{}) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: roleStoragePath + testRoleName,
Storage: s,
})
if err != nil {
return err
}
if resp.IsError() {
return resp.Error()
}
if resp == nil {
return fmt.Errorf("resp is nil")
}
// Find the values in expected that are not in resp.Data
diff := map[string]interface{}{}
for k := range expected {
if _, ok := resp.Data[k]; !ok {
diff[k] = nil
}
}
if len(diff) > 0 {
return fmt.Errorf("read data mismatch (unexpected keys: %v)", diff)
}
return nil
}
func testRoleDelete(_ *testing.T, b logical.Backend, s logical.Storage) error {
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: roleStoragePath + testRoleName,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}