-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource_security_policy_tunnel_pair_policy.go
365 lines (325 loc) · 12.4 KB
/
resource_security_policy_tunnel_pair_policy.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package junos
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type policyPairPolicyOptions struct {
zoneA string
zoneB string
policyAtoB string
policyBtoA string
}
func resourceSecurityPolicyTunnelPairPolicy() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecurityPolicyTunnelPairPolicyCreate,
ReadContext: resourceSecurityPolicyTunnelPairPolicyRead,
DeleteContext: resourceSecurityPolicyTunnelPairPolicyDelete,
Importer: &schema.ResourceImporter{
State: resourceSecurityPolicyTunnelPairPolicyImport,
},
Schema: map[string]*schema.Schema{
"zone_a": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
"zone_b": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
"policy_a_to_b": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
"policy_b_to_a": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateNameObjectJunos([]string{}, 64, formatDefault),
},
},
}
}
func resourceSecurityPolicyTunnelPairPolicyCreate(
ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setSecurityPolicyTunnelPairPolicy(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId(d.Get("zone_a").(string) + idSeparator + d.Get("policy_a_to_b").(string) +
idSeparator + d.Get("zone_b").(string) + idSeparator + d.Get("policy_b_to_a").(string))
return nil
}
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if !checkCompatibilitySecurity(jnprSess) {
return diag.FromErr(fmt.Errorf("security policy tunnel pair policy not compatible with Junos device %s",
jnprSess.SystemInformation.HardwareModel))
}
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
securityPolicyExists, err := checkSecurityPolicyExists(d.Get("zone_a").(string), d.Get("zone_b").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if !securityPolicyExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("security policy from %v to %v not exists",
d.Get("zone_a").(string), d.Get("zone_b").(string)))...)
}
securityPolicyExists, err = checkSecurityPolicyExists(d.Get("zone_b").(string), d.Get("zone_a").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if !securityPolicyExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("security policy from %v to %v not exists",
d.Get("zone_b").(string), d.Get("zone_a").(string)))...)
}
pairPolicyExists, err := checkSecurityPolicyPairExists(d.Get("zone_a").(string), d.Get("policy_a_to_b").(string),
d.Get("zone_b").(string), d.Get("policy_b_to_a").(string), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if pairPolicyExists {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("security policy pair policy %v(%v) / %v(%v) already exists",
d.Get("zone_a").(string), d.Get("policy_a_to_b").(string),
d.Get("zone_b").(string), d.Get("policy_b_to_a").(string)))...)
}
if err := setSecurityPolicyTunnelPairPolicy(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_security_policy_tunnel_pair_policy", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
pairPolicyExists, err = checkSecurityPolicyPairExists(d.Get("zone_a").(string), d.Get("policy_a_to_b").(string),
d.Get("zone_b").(string), d.Get("policy_b_to_a").(string), m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if pairPolicyExists {
d.SetId(d.Get("zone_a").(string) + idSeparator + d.Get("policy_a_to_b").(string) +
idSeparator + d.Get("zone_b").(string) + idSeparator + d.Get("policy_b_to_a").(string))
} else {
return append(diagWarns, diag.FromErr(fmt.Errorf("security policy pair policy not exists after commit "+
"=> check your config"))...)
}
return append(diagWarns, resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d, m, jnprSess)...)
}
func resourceSecurityPolicyTunnelPairPolicyRead(
ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
return resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(d, m, jnprSess)
}
func resourceSecurityPolicyTunnelPairPolicyReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy(d.Get("zone_a").(string)+idSeparator+
d.Get("policy_a_to_b").(string)+idSeparator+
d.Get("zone_b").(string)+idSeparator+
d.Get("policy_b_to_a").(string), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if policyPairPolicyOptions.policyAtoB == "" && policyPairPolicyOptions.policyBtoA == "" {
d.SetId("")
} else {
fillSecurityPolicyPairData(d, policyPairPolicyOptions)
}
return nil
}
func resourceSecurityPolicyTunnelPairPolicyDelete(
ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := delSecurityPolicyTunnelPairPolicy(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("delete resource junos_security_policy_tunnel_pair_policy", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceSecurityPolicyTunnelPairPolicyImport(d *schema.ResourceData,
m interface{}) ([]*schema.ResourceData, error) {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return nil, err
}
defer sess.closeSession(jnprSess)
result := make([]*schema.ResourceData, 1)
idList := strings.Split(d.Id(), idSeparator)
if len(idList) < 4 {
return nil, fmt.Errorf("missing element(s) in id with separator %v", idSeparator)
}
poliyPairPolicyExists, err := checkSecurityPolicyPairExists(idList[0], idList[1], idList[2], idList[3], m, jnprSess)
if err != nil {
return nil, err
}
if !poliyPairPolicyExists {
return nil, fmt.Errorf("don't find policy pair policy with id %v "+
"(id must be <zone_a>"+idSeparator+"<policy_a_to_b>"+idSeparator+
"<zone_b>"+idSeparator+"<policy_b_to_a>)", d.Id())
}
policyPairPolicyOptions, err := readSecurityPolicyTunnelPairPolicy(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
fillSecurityPolicyPairData(d, policyPairPolicyOptions)
result[0] = d
return result, nil
}
func checkSecurityPolicyPairExists(zoneA, policyAtoB, zoneB, policyBtoA string,
m interface{}, jnprSess *NetconfObject) (bool, error) {
sess := m.(*Session)
pairAtoBConfig, err := sess.command("show configuration"+
" security policies from-zone "+zoneA+" to-zone "+zoneB+" policy "+policyAtoB+
" then permit tunnel pair-policy | display set", jnprSess)
if err != nil {
return false, err
}
pairBtoAConfig, err := sess.command("show configuration"+
" security policies from-zone "+zoneB+" to-zone "+zoneA+" policy "+policyBtoA+
" then permit tunnel pair-policy | display set", jnprSess)
if err != nil {
return false, err
}
if pairAtoBConfig == emptyWord && pairBtoAConfig == emptyWord {
return false, nil
}
return true, nil
}
func setSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 2)
configSet = append(configSet, "set security policies from-zone "+
d.Get("zone_a").(string)+" to-zone "+d.Get("zone_b").(string)+
" policy "+d.Get("policy_a_to_b").(string)+
" then permit tunnel pair-policy "+d.Get("policy_b_to_a").(string))
configSet = append(configSet, "set security policies from-zone "+
d.Get("zone_b").(string)+" to-zone "+d.Get("zone_a").(string)+
" policy "+d.Get("policy_b_to_a").(string)+
" then permit tunnel pair-policy "+d.Get("policy_a_to_b").(string))
return sess.configSet(configSet, jnprSess)
}
func readSecurityPolicyTunnelPairPolicy(idRessource string,
m interface{}, jnprSess *NetconfObject) (policyPairPolicyOptions, error) {
zone := strings.Split(idRessource, idSeparator)
zoneA := zone[0]
policyAtoB := zone[1]
zoneB := zone[2]
policyBtoA := zone[3]
sess := m.(*Session)
var confRead policyPairPolicyOptions
policyPairConfig, err := sess.command("show configuration"+
" security policies from-zone "+zoneA+" to-zone "+zoneB+" policy "+policyAtoB+
" then permit tunnel pair-policy | display set", jnprSess)
if err != nil {
return confRead, err
}
if policyPairConfig != emptyWord {
confRead.zoneA = zoneA
confRead.zoneB = zoneB
for _, item := range strings.Split(policyPairConfig, "\n") {
if strings.Contains(item, "<configuration-output>") {
continue
}
if strings.Contains(item, "</configuration-output>") {
break
}
if strings.Contains(item, " tunnel pair-policy ") {
confRead.policyBtoA = strings.TrimPrefix(item,
"set security policies from-zone "+zoneA+" to-zone "+zoneB+
" policy "+policyAtoB+" then permit tunnel pair-policy ")
}
}
}
policyPairConfig, err = sess.command("show configuration"+
" security policies from-zone "+zoneB+" to-zone "+zoneA+" policy "+policyBtoA+
" then permit tunnel pair-policy | display set", jnprSess)
if err != nil {
return confRead, err
}
if policyPairConfig != emptyWord {
confRead.zoneA = zoneA
confRead.zoneB = zoneB
for _, item := range strings.Split(policyPairConfig, "\n") {
if strings.Contains(item, "<configuration-output>") {
continue
}
if strings.Contains(item, "</configuration-output>") {
break
}
if strings.Contains(item, " tunnel pair-policy ") {
confRead.policyAtoB = strings.TrimPrefix(item,
"set security policies from-zone "+zoneB+" to-zone "+zoneA+
" policy "+policyBtoA+" then permit tunnel pair-policy ")
}
}
}
return confRead, nil
}
func delSecurityPolicyTunnelPairPolicy(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
configSet := make([]string, 0, 2)
configSet = append(configSet, "delete security policies"+
" from-zone "+d.Get("zone_a").(string)+" to-zone "+d.Get("zone_b").(string)+
" policy "+d.Get("policy_a_to_b").(string)+
" then permit tunnel pair-policy "+d.Get("policy_b_to_a").(string))
configSet = append(configSet, "delete security policies"+
" from-zone "+d.Get("zone_b").(string)+" to-zone "+d.Get("zone_a").(string)+
" policy "+d.Get("policy_b_to_a").(string)+
" then permit tunnel pair-policy "+d.Get("policy_a_to_b").(string))
return sess.configSet(configSet, jnprSess)
}
func fillSecurityPolicyPairData(d *schema.ResourceData, policyPairPolicyOptions policyPairPolicyOptions) {
if tfErr := d.Set("zone_a", policyPairPolicyOptions.zoneA); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("zone_b", policyPairPolicyOptions.zoneB); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("policy_a_to_b", policyPairPolicyOptions.policyAtoB); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("policy_b_to_a", policyPairPolicyOptions.policyBtoA); tfErr != nil {
panic(tfErr)
}
}