Class: CfnVpn::Templates::Vpn

Inherits:
CfnDsl::CloudFormationTemplate
  • Object
show all
Defined in:
lib/cfnvpn/templates/vpn.rb

Instance Method Summary collapse

Constructor Details

#initializeVpn

Returns a new instance of Vpn.



9
10
11
# File 'lib/cfnvpn/templates/vpn.rb', line 9

def initialize
  super
end

Instance Method Details

#auto_route_populator(name, config) ⇒ Object



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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/cfnvpn/templates/vpn.rb', line 274

def auto_route_populator(name, config)
  IAM_Role(:CfnVpnAutoRoutePopulatorRole) {
    AssumeRolePolicyDocument({
      Version: '2012-10-17',
      Statement: [{
        Effect: 'Allow',
        Principal: { Service: [ 'lambda.amazonaws.com' ] },
        Action: [ 'sts:AssumeRole' ]
      }]
    })
    Path '/cfnvpn/'
    Policies([
      {
        PolicyName: 'client-vpn',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [ 
              'ec2:AuthorizeClientVpnIngress',
              'ec2:RevokeClientVpnIngress',
              'ec2:DescribeClientVpnAuthorizationRules',
              'ec2:DescribeClientVpnEndpoints',
              'ec2:DescribeClientVpnRoutes',
              'ec2:CreateClientVpnRoute',
              'ec2:DeleteClientVpnRoute'
            ],
            Resource: '*'
          }]
        }
      },
      {
        PolicyName: 'logging',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [
              'logs:DescribeLogGroups',
              'logs:CreateLogGroup',
              'logs:CreateLogStream',
              'logs:DescribeLogStreams',
              'logs:PutLogEvents'
            ],
            Resource: '*'
          }]
        }
      },
      {
        PolicyName: 'vpn-quotas',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [
            {
              Effect: 'Allow',
              Action: [
                'servicequotas:ListRequestedServiceQuotaChangeHistoryByQuota',
              ],
              Resource: '*'
            },
            {
              Effect: 'Allow',
              Action: [
                'servicequotas:RequestServiceQuotaIncrease'
              ],
              Resource: [
                FnSub('arn:aws:servicequotas:${AWS::Region}:${AWS::AccountId}:ec2/L-401D78F7'),
                FnSub('arn:aws:servicequotas:${AWS::Region}:${AWS::AccountId}:ec2/L-9A1BC94B')
              ]
            }
          ]
        }
      }
    ])
    Tags([
      { Key: 'Name', Value: "#{name}-cfnvpn-auto-route-populator-role" },
      { Key: 'Environment', Value: 'cfnvpn' }
    ])
  }

  s3_key = CfnVpn::Templates::Lambdas.package_lambda(
    name: name,
    bucket: config[:bucket],
    func: 'auto_route_populator',
    files: [
      'auto_route_populator/app.py',
      'auto_route_populator/lookup.py',
      'auto_route_populator/quotas.py',
      'lib/slack.py',
      'auto_route_populator/states.py'
    ]
  )
  
  Lambda_Function(:CfnVpnAutoRoutePopulator) {
    Runtime 'python3.8'
    Role FnGetAtt(:CfnVpnAutoRoutePopulatorRole, :Arn)
    MemorySize '128'
    Handler 'app.handler'
    Timeout 60
    Code({
      S3Bucket: config[:bucket],
      S3Key: s3_key
    })
    Environment({
      Variables: {
        SLACK_URL: config[:slack_webhook_url] || '',
        AUTO_LIMIT_INCREASE: config[:auto_limit_increase] || true
      }
    })
    Tags([
      { Key: 'Name', Value: "#{name}-cfnvpn-auto-route-populator" },
      { Key: 'Environment', Value: 'cfnvpn' }
    ])
  }

  Logs_LogGroup(:CfnVpnAutoRoutePopulatorLogGroup) {
    LogGroupName FnSub("/aws/lambda/${CfnVpnAutoRoutePopulator}")
    RetentionInDays 30
  }

  Lambda_Permission(:CfnVpnAutoRoutePopulatorFunctionPermissions) {
    FunctionName Ref(:CfnVpnAutoRoutePopulator)
    Action 'lambda:InvokeFunction'
    Principal 'events.amazonaws.com'
  }
end

#output(name, value) ⇒ Object



270
271
272
# File 'lib/cfnvpn/templates/vpn.rb', line 270

def output(name, value)
  Output(name) { Value value }
end

#render(name, config) ⇒ Object



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
# File 'lib/cfnvpn/templates/vpn.rb', line 13

def render(name, config)
  Description "cfnvpn #{name} AWS Client-VPN"
  Parameter(:AssociateSubnets) {
    Type 'String'
    Default 'true'
    AllowedValues ['true', 'false']
    Description 'Toggle to false to disassociate all Client VPN subnet associations'
  }

  Condition(:EnableSubnetAssociation, FnEquals(Ref(:AssociateSubnets), 'true'))

  Logs_LogGroup(:ClientVpnLogGroup) {
    LogGroupName FnSub("#{name}-ClientVpn")
    RetentionInDays 30
  }

  EC2_ClientVpnEndpoint(:ClientVpnEndpoint) {
    Description FnSub("cfnvpn #{name} AWS Client-VPN")
    AuthenticationOptions([
    if config[:type] == 'federated'
      {
        FederatedAuthentication: {
          SAMLProviderArn: config[:saml_arn],
          SelfServiceSAMLProviderArn: config[:saml_self_service_arn].nil? ? config[:saml_arn] : config[:saml_self_service_arn]
        },
        Type: 'federated-authentication'
      }
    elsif config[:type] == 'active-directory'
      {
        ActiveDirectory: {
          DirectoryId: config[:directory_id]
        },
        Type: 'directory-service-authentication'
      }
    else
      {
        MutualAuthentication: {
          ClientRootCertificateChainArn: config[:client_cert_arn]
        },
        Type: 'certificate-authentication'
      }
    end
    ])
    ServerCertificateArn config[:server_cert_arn]
    ClientCidrBlock config[:cidr]
    ConnectionLogOptions({
      CloudwatchLogGroup: Ref(:ClientVpnLogGroup),
      Enabled: true
    })
    DnsServers config[:dns_servers].any? ? config[:dns_servers] : Ref('AWS::NoValue')
    TagSpecifications([{
      ResourceType: "client-vpn-endpoint",
      Tags: [
        { Key: 'Name', Value: name }
      ]
    }])
    TransportProtocol config[:protocol]
    SplitTunnel config[:split_tunnel]
  }

  network_assoc_dependson = []
  config[:subnet_ids].each_with_index do |subnet, index|
    suffix = index == 0 ? "" : "For#{subnet.resource_safe}"

    EC2_ClientVpnTargetNetworkAssociation(:"ClientVpnTargetNetworkAssociation#{suffix}") {
      Condition(:EnableSubnetAssociation)
      ClientVpnEndpointId Ref(:ClientVpnEndpoint)
      SubnetId subnet
    }

    network_assoc_dependson << "ClientVpnTargetNetworkAssociation#{suffix}"
  end

  if config[:default_groups].any?
    config[:default_groups].each do |group|
      EC2_ClientVpnAuthorizationRule(:"TargetNetworkAuthorizationRule#{group.resource_safe}"[0..255]) {
        Condition(:EnableSubnetAssociation)
        DependsOn network_assoc_dependson if network_assoc_dependson.any?
        Description FnSub("#{name} client-vpn auth rule for subnet association")
        AccessGroupId group
        ClientVpnEndpointId Ref(:ClientVpnEndpoint)
        TargetNetworkCidr CfnVpn::Templates::Helper.get_auth_cidr(config[:region], config[:subnet_ids].first)
      }
    end
  else
    EC2_ClientVpnAuthorizationRule(:"TargetNetworkAuthorizationRule") {
      Condition(:EnableSubnetAssociation)
      DependsOn network_assoc_dependson if network_assoc_dependson.any?
      Description FnSub("#{name} client-vpn auth rule for subnet association")
      AuthorizeAllGroups true
      ClientVpnEndpointId Ref(:ClientVpnEndpoint)
      TargetNetworkCidr CfnVpn::Templates::Helper.get_auth_cidr(config[:region], config[:subnet_ids].first)
    }
  end

  if !config[:internet_route].nil?
    EC2_ClientVpnRoute(:RouteToInternet) {
      Condition(:EnableSubnetAssociation)
      DependsOn network_assoc_dependson if network_assoc_dependson.any?
      Description "Route to the internet through subnet #{config[:internet_route]}"
      ClientVpnEndpointId Ref(:ClientVpnEndpoint)
      DestinationCidrBlock '0.0.0.0/0'
      TargetVpcSubnetId config[:internet_route]
    }
  
    EC2_ClientVpnAuthorizationRule(:RouteToInternetAuthorizationRule) {
      Condition(:EnableSubnetAssociation)
      DependsOn network_assoc_dependson if network_assoc_dependson.any?
      Description "Internet route authorization from subnet #{config[:internet_route]}"
      AuthorizeAllGroups true
      ClientVpnEndpointId Ref(:ClientVpnEndpoint)
      TargetNetworkCidr '0.0.0.0/0'
    }

    output(:InternetRoute, config[:internet_route])
  end

  create_auto_route_populator = config[:routes].detect { |route| route.has_key?(:dns) || route.has_key?(:cloud)}

  if create_auto_route_populator
    auto_route_populator(name, config)
  end

  config[:routes].each do |route|
    
    if route[:subnets]
      target_subnets = route[:subnets]
    elsif config[:subnet_ids].include?(route[:subnet])
      target_subnets = config[:subnet_ids]
    else
      target_subnets = [*route[:subnet]]
    end

    if route.has_key?(:dns)
      input = { 
        Record: route[:dns],
        ClientVpnEndpointId: "${ClientVpnEndpoint}",
        TargetSubnets: target_subnets,
        Description: route[:desc]
      }
      
      if !route[:groups].nil? && route[:groups].any?
        input[:Groups] = route[:groups]
      end

      Events_Rule(:"CfnVpnAutoRoutePopulatorEvent#{route[:dns].resource_safe}"[0..255]) {
        Condition(:EnableSubnetAssociation)
        DependsOn network_assoc_dependson if network_assoc_dependson.any?
        State 'ENABLED'
        Description "cfnvpn auto route populator schedule for #{route[:dns]}"
        ScheduleExpression route.fetch(:schedule, 'rate(5 minutes)')
        Targets([
          { 
            Arn: FnGetAtt(:CfnVpnAutoRoutePopulator, :Arn),
            Id: "dns-auto-route-populator",
            Input: FnSub(input.to_json)
          }
        ])
      }
    
    elsif route.has_key?(:cloud)
      input = { 
        Cloud: route[:cloud],
        ClientVpnEndpointId: "${ClientVpnEndpoint}",
        TargetSubnets: target_subnets,
        Description: route[:desc]
      }
      
      if !route[:groups].nil? && route[:groups].any?
        input[:Groups] = route[:groups]
      end

      if !route[:filters].nil? && route[:filters].any?
        input[:Filters] = route[:filters]
      end          

      Events_Rule(:"CfnVpnAutoRoutePopulatorEvent#{route[:cloud].resource_safe}"[0..255]) {
        Condition(:EnableSubnetAssociation)
        DependsOn network_assoc_dependson if network_assoc_dependson.any?
        State 'ENABLED'
        Description "cfnvpn auto route populator schedule for cloud #{route[:cloud]} lookup"
        ScheduleExpression route.fetch(:schedule, 'rate(5 minutes)')
        Targets([
          { 
            Arn: FnGetAtt(:CfnVpnAutoRoutePopulator, :Arn),
            Id: "cloud-auto-route-populator",
            Input: FnSub(input.to_json)
          }
        ])
      }

    elsif route.has_key?(:cidr)
      target_subnets.each do |subnet|
        EC2_ClientVpnRoute(:"#{route[:cidr].resource_safe}VpnRouteTo#{subnet.resource_safe}"[0..255]) {
          Description "cfnvpn static route for #{route[:cidr]}. #{route[:desc]}".strip
          ClientVpnEndpointId Ref(:ClientVpnEndpoint)
          DestinationCidrBlock route[:cidr]
          TargetVpcSubnetId subnet
        }
      end

      if route[:groups].any?
        route[:groups].each do |group|
          EC2_ClientVpnAuthorizationRule(:"#{route[:cidr].resource_safe}AuthorizationRule#{group.resource_safe}"[0..255]) {
            Description "cfnvpn static authorization rule for group #{group} to route #{route[:cidr]}. #{route[:desc]}".strip
            AccessGroupId group
            ClientVpnEndpointId Ref(:ClientVpnEndpoint)
            TargetNetworkCidr route[:cidr]
          }
        end
      else
        EC2_ClientVpnAuthorizationRule(:"#{route[:cidr].resource_safe}AllowAllAuthorizationRule") {
          Description "cfnvpn static allow all authorization rule to route #{route[:cidr]}. #{route[:desc]}".strip
          AuthorizeAllGroups true
          ClientVpnEndpointId Ref(:ClientVpnEndpoint)
          TargetNetworkCidr route[:cidr]
        }
      end

    end
  end
  
  SSM_Parameter(:CfnVpnConfig) {
    Description "#{name} cfnvpn config"
    Name "/cfnvpn/config/#{name}"
    Tier 'Intelligent-Tiering'
    Type 'String'
    Value config.to_json
    Tags({
      Name: "#{name}-cfnvpn-config",
      Environment: 'cfnvpn'
    })
  }

  if config[:start] || config[:stop]
    scheduler(name, config)
    output(:Start, config[:start]) if config[:start]
    output(:Stop, config[:stop]) if config[:stop]
  end

  output(:ServerCertArn, config[:server_cert_arn])
  output(:Cidr, config[:cidr])
  output(:DnsServers, config.fetch(:dns_servers, []).join(','))
  output(:SubnetIds, config[:subnet_ids].join(','))
  output(:SplitTunnel, config[:split_tunnel])
  output(:Protocol, config[:protocol])
  output(:Type, config[:type])

  if config[:type] == 'federated'
    output(:SamlArn, config[:saml_arn])
  elsif config[:type] == 'active-directory'
    output(:DirectoryId, config[:directory_id])
  else
    output(:ClientCertArn, config[:client_cert_arn])
  end
end

#scheduler(name, config) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/cfnvpn/templates/vpn.rb', line 401

def scheduler(name, config)
  IAM_Role(:ClientVpnSchedulerRole) {
    AssumeRolePolicyDocument({
      Version: '2012-10-17',
      Statement: [{
        Effect: 'Allow',
        Principal: { Service: [ 'lambda.amazonaws.com' ] },
        Action: [ 'sts:AssumeRole' ]
      }]
    })
    Path '/cfnvpn/'
    Policies([
      {
        PolicyName: 'cloudformation',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [
              'cloudformation:UpdateStack'
            ],
            Resource: FnSub("arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/#{name}-cfnvpn/*")
          }]
        }
      },
      {
        PolicyName: 'client-vpn',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [ 
              'ec2:AssociateClientVpnTargetNetwork',
              'ec2:DisassociateClientVpnTargetNetwork',
              'ec2:DescribeClientVpnTargetNetworks',
              'ec2:AuthorizeClientVpnIngress',
              'ec2:RevokeClientVpnIngress',
              'ec2:DescribeClientVpnAuthorizationRules',
              'ec2:DescribeClientVpnEndpoints',
              'ec2:DescribeClientVpnConnections',
              'ec2:TerminateClientVpnConnections',
              'ec2:DescribeClientVpnRoutes',
              'ec2:CreateClientVpnRoute',
              'ec2:DeleteClientVpnRoute'
            ],
            Resource: '*'
          }]
        }
      },
      {
        PolicyName: 'route-populator-events',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [
              'events:PutRule',
              'events:PutTargets',
              'events:DeleteRule',
              'events:DescribeRule',
              'events:DisableRule',
              'events:EnableRule',
              'events:RemoveTargets'
            ],
            Resource: FnSub("arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/#{name}-cfnvpn-CfnVpnAutoRoutePopulator*")
          }]
        }
      },
      {
        PolicyName: 'logging',
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [{
            Effect: 'Allow',
            Action: [
              'logs:DescribeLogGroups',
              'logs:CreateLogGroup',
              'logs:CreateLogStream',
              'logs:DescribeLogStreams',
              'logs:PutLogEvents'
            ],
            Resource: '*'
          }]
        }
      }
    ])
    Tags([
      { Key: 'Name', Value: "#{name}-cfnvpn-scheduler-role" },
      { Key: 'Environment', Value: 'cfnvpn' }
    ])
  }

  s3_key = CfnVpn::Templates::Lambdas.package_lambda(name: name, bucket: config[:bucket], func: 'scheduler', files: ['scheduler/app.py', 'lib/slack.py', 'scheduler/states.py'])

  Lambda_Function(:ClientVpnSchedulerFunction) {
    Runtime 'python3.8'
    Role FnGetAtt(:ClientVpnSchedulerRole, :Arn)
    MemorySize '128'
    Handler 'app.handler'
    Timeout 60
    Environment({
      Variables: {
        SLACK_URL: config[:slack_webhook_url] || ''
      }
    })
    Code({
      S3Bucket: config[:bucket],
      S3Key: s3_key
    })
    Tags([
      { Key: 'Name', Value: "#{name}-cfnvpn-scheduler-function" },
      { Key: 'Environment', Value: 'cfnvpn' }
    ])
  }

  Logs_LogGroup(:ClientVpnSchedulerLogGroup) {
    LogGroupName FnSub("/aws/lambda/${ClientVpnSchedulerFunction}")
    RetentionInDays 30
  }

  Lambda_Permission(:ClientVpnSchedulerFunctionPermissions) {
    FunctionName Ref(:ClientVpnSchedulerFunction)
    Action 'lambda:InvokeFunction'
    Principal 'events.amazonaws.com'
  }

  if config[:start]
    Events_Rule(:ClientVpnSchedulerStart) {
      State 'ENABLED'
      Description "cfnvpn start schedule"
      ScheduleExpression "cron(#{config[:start]})"
      Targets([
        { 
          Arn: FnGetAtt(:ClientVpnSchedulerFunction, :Arn),
          Id: 'cfnvpnschedulerstart',
          Input: FnSub({ StackName: "#{name}-cfnvpn", AssociateSubnets: 'true', ClientVpnEndpointId: "${ClientVpnEndpoint}" }.to_json)
        }
      ])
    }
  end

  if config[:stop]
    Events_Rule(:ClientVpnSchedulerStop) {
      State 'ENABLED'
      Description "cfnvpn stop schedule"
      ScheduleExpression "cron(#{config[:stop]})"
      Targets([
        { 
          Arn: FnGetAtt(:ClientVpnSchedulerFunction, :Arn),
          Id: 'cfnvpnschedulerstop',
          Input: FnSub({ StackName: "#{name}-cfnvpn", AssociateSubnets: 'false', ClientVpnEndpointId: "${ClientVpnEndpoint}" }.to_json)
        }
      ])
    }
  end

end