-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwsIp.php
More file actions
722 lines (660 loc) · 20.5 KB
/
Copy pathAwsIp.php
File metadata and controls
722 lines (660 loc) · 20.5 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
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
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
400
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
<?php
namespace AwsIp;
class Lookup
{
/**
* @var string $alias_target
*/
private $alias_target;
/**
* @var string $aws
*/
private $aws;
/**
* @var string $domain
*/
private $domain;
/**
* @var string $elb_arn
*/
private $elb_arn;
/**
* @var string $elb_domain
*/
private $elb_domain;
/**
* @var string $elb_domain
*/
private $elb_id;
/**
* @var string $elb_name
*/
private $elb_name;
/**
* @var string $hosted_zone_id
*/
private $hosted_zone_id;
/**
* @var string $listener_arn
*/
private $listener_arn;
/**
* @var string $real_domain
*/
private $real_domain;
/**
* @var string $region
*/
private $region;
/**
* @var string $subdomain
*/
private $subdomain;
/**
* @var string $target_group_arn
*/
private $target_group_arn;
/**
* Constructor
*
* @param string $aws
* @param string $domain
* @param string $subdomain
*/
public function __construct($aws, $domain, $subdomain = null)
{
$this->aws = $aws;
$this->domain = $domain;
$this->subdomain = $subdomain;
// Build the real domain.
$this->real_domain = $this->domain;
if (isset($this->subdomain)) {
$this->real_domain = $this->subdomain . '.' . $this->domain;
}
}
/**
* Run.
*
* Handles the entire lookup process.
*/
public function run()
{
$start = microtime(true);
$this->message('Looking up Web Server IP\'s for "%s":', [$this->real_domain]);
$this->lookupHostedZone();
$this->lookupARecord();
$this->lookupElb();
$this->parseElb();
$this->lookupElbArn();
$this->lookupElbHttpsListener();
$this->lookupElbListenerRules();
$this->lookupTargetGroupInstances();
$this->lookupInstances();
$end = microtime(true);
$this->message('');
$this->message('Request processed in %s seconds.', [$end - $start]);
}
/**
* Error.
*
* Print the error message and stop execution.
*
* @param string $message
* @param array $params
*/
private function error($message, $params = [])
{
vprintf($message . PHP_EOL . PHP_EOL, $params);
exit;
}
/**
* Lookup A Record.
*
* Use the AWS CLI to lookup the A record for the hosted zone ID that was retrieved in lookupHostedZone().
*/
private function lookupARecord()
{
/**
* Sample output:
*
* [
* {
* "Name": "domain.com.",
* "Type": "A",
* "AliasTarget": {
* "HostedZoneId": "HostedZoneId",
* "DNSName": "DNSName.cloudfront.net.",
* "EvaluateTargetHealth": false
* }
* }
* ]
*/
$response = shell_exec(
vsprintf(
'%s %s %s --hosted-zone-id %s --query "ResourceRecordSets[?Name == \'%s.\']"',
[
$this->aws,
'route53',
'list-resource-record-sets',
$this->hosted_zone_id,
$this->real_domain,
]
)
);
$response = json_decode($response);
if (count($response) == 0) {
$this->error('No A records found for "%s"', [$this->real_domain]);
}
$this->alias_target = $response[0]->AliasTarget->DNSName;
$this->message('Looked up the A record for "%s":', [$this->real_domain], 1);
$this->message('Alias Target: %s', [$this->alias_target], 2);
}
/**
* Lookup ELB.
*
* Use the AWS CLI to lookup the ELB Domain for the alias target that was retrieved in lookupARecord().
*/
private function lookupElb()
{
if (preg_match('/.*\.cloudfront\.net\.$/i', $this->alias_target) === 1) {
/**
* Sample output (truncated):
*
* [
* {
* "Id": "Id",
* "ARN": "ARN",
* "Status": "Deployed",
* "LastModifiedTime": "2020-05-01T14:33:55.296000+00:00",
* "DomainName": "DomainName.cloudfront.net",
* ...
* "Origins": {
* "Quantity": 1,
* "Items": [
* {
* "Id": "Id",
* "DomainName": "DomainName",
* "OriginPath": "",
* "CustomHeaders": {
* "Quantity": 0
* },
* "CustomOriginConfig": {
* "HTTPPort": 80,
* "HTTPSPort": 443,
* "OriginProtocolPolicy": "match-viewer",
* "OriginSslProtocols": {
* "Quantity": 3,
* "Items": [
* "TLSv1",
* "TLSv1.1",
* "TLSv1.2"
* ]
* },
* "OriginReadTimeout": 30,
* "OriginKeepaliveTimeout": 5
* }
* }
* ]
* },
* ...
* }
* ]
*/
$response = shell_exec(
vsprintf(
'%s %s %s --query "DistributionList.Items[?DomainName == \'%s\']"',
[
$this->aws,
'cloudfront',
'list-distributions',
trim($this->alias_target, '.'),
]
)
);
$response = json_decode($response);
if (count($response) == 0) {
$this->error('No Cloudfront distribution found for "%s"', [$this->alias_target]);
}
$this->message('Cloudfront ID: %s', [$response[0]->Id], 2);
$this->elb_domain = $response[0]->Origins->Items[0]->DomainName;
} else {
$this->elb_domain = $this->alias_target;
}
$this->message('Looked up the ELB domain for Alias Target "%s":', [$this->real_domain], 1);
$this->message('ELB Domain: %s', [$this->elb_domain], 2);
}
/**
* Lookup ELB ARN.
*
* Use the AWS CLI to lookup the ELB ARN for the ELB name that was retrieved in lookupElb().
*/
private function lookupElbArn()
{
/**
* Sample output:
*
* {
* "LoadBalancers": [
* {
* "LoadBalancerArn": "LoadBalancerArn",
* "DNSName": "DNSName",
* "CanonicalHostedZoneId": "CanonicalHostedZoneId",
* "CreatedTime": "2020-02-12T19:14:17.100000+00:00",
* "LoadBalancerName": "LoadBalancerName",
* "Scheme": "internet-facing",
* "VpcId": "VpcId",
* "State": {
* "Code": "active"
* },
* "Type": "application",
* "AvailabilityZones": [
* {
* "ZoneName": "ZoneName",
* "SubnetId": "SubnetId",
* "LoadBalancerAddresses": []
* },
* {
* "ZoneName": "ZoneName",
* "SubnetId": "SubnetId",
* "LoadBalancerAddresses": []
* }
* ],
* "SecurityGroups": [
* "SecurityGroup1",
* "SecurityGroup2"
* ],
* "IpAddressType": "ipv4"
* }
* ]
* }
*/
$response = shell_exec(
vsprintf(
'%s %s %s --region %s --query "LoadBalancers[?DNSName == '\%s\']"',
[
$this->aws,
'elbv2',
'describe-load-balancers',
$this->region,
$this->elb_name . '-' . $this->elb_id . '.' . $this->region . '.elb.amazonaws.com',
]
)
);
$response = json_decode($response);
$this->elb_arn = $response[0]->LoadBalancerArn;
$this->message('Looked up the ELB ARN for ELB "%s":', [$this->elb_name], 1);
$this->message('ELB ARN: %s', [$this->elb_arn], 2);
}
/**
* Lookup ELB HTTPS Listener.
*
* Use the AWS CLI to lookup the Listener ARN for the ELB ARN that was retrieved in lookupElbArn().
*/
private function lookupElbHttpsListener()
{
/**
* Sample output (truncated):
*
* {
* "Listeners": [
* ...
* {
* "ListenerArn": "ListenerArn",
* "LoadBalancerArn": "LoadBalancerArn",
* "Port": 443,
* "Protocol": "HTTPS",
* "Certificates": [
* {
* "CertificateArn": "CertificateArn"
* }
* ],
* "SslPolicy": "ELBSecurityPolicy-2016-08",
* "DefaultActions": [
* {
* "Type": "forward",
* "TargetGroupArn": "TargetGroupArn",
* "ForwardConfig": {
* "TargetGroups": [
* {
* "TargetGroupArn": "TargetGroupArn",
* "Weight": 1
* }
* ],
* "TargetGroupStickinessConfig": {
* "Enabled": false
* }
* }
* }
* ]
* }
* ]
* }
*/
$response = shell_exec(
vsprintf(
'%s %s %s --region %s --load-balancer-arn %s',
[
$this->aws,
'elbv2',
'describe-listeners',
$this->region,
$this->elb_arn,
]
)
);
$response = json_decode($response);
if (count($response->Listeners) == 0) {
$this->error('No listeners found for "%s"', [$this->elb_arn]);
}
$found = false;
foreach ($response->Listeners as $listener) {
if ($listener->Protocol == 'HTTPS') {
$this->listener_arn = $listener->ListenerArn;
$found = true;
$this->message('Looked up the HTTPS Listener ARN for ELB "%s":', [$this->elb_name], 1);
$this->message('Listener ARN: %s', [$this->listener_arn], 2);
break;
}
}
if (!$found) {
$this->error('No listeners found for "%s"', [$this->elb_name]);
}
}
/**
* Lookup ELB Listener Rules.
*
* Use the AWS CLI to lookup the Target Group ARN for the Listener ARN that was retrieved in
* lookupElbHttpsListener().
*/
private function lookupElbListenerRules()
{
/**
* Sample output (truncated):
*
* {
* "Rules": [
* ...
* {
* "RuleArn": "RuleArn",
* "Priority": "3",
* "Conditions": [
* {
* "Field": "host-header",
* "Values": [
* "domain.com"
* ],
* "HostHeaderConfig": {
* "Values": [
* "domain.com"
* ]
* }
* }
* ],
* "Actions": [
* {
* "Type": "forward",
* "TargetGroupArn": "TargetGroupArn",
* "Order": 1,
* "ForwardConfig": {
* "TargetGroups": [
* {
* "TargetGroupArn": "TargetGroupArn",
* "Weight": 1
* }
* ],
* "TargetGroupStickinessConfig": {
* "Enabled": false
* }
* }
* }
* ],
* "IsDefault": false
* },
* ...
*/
$response = shell_exec(
vsprintf(
'%s %s %s --region %s --listener-arn %s',
[
$this->aws,
'elbv2',
'describe-rules',
$this->region,
$this->listener_arn,
]
)
);
$response = json_decode($response);
if (count($response->Rules) == 0) {
$this->error('No rules found for "%s"', [$this->listener_arn]);
}
// Lookup host-header rule.
$found = false;
foreach ($response->Rules as $rule) {
if (count($rule->Conditions)) {
foreach ($rule->Conditions as $condition) {
if ($condition->Field == 'host-header') {
foreach ($condition->Values as $value) {
if ($value == $this->real_domain) {
$this->target_group_arn = $rule->Actions[0]->TargetGroupArn;
$found = true;
break 3;
}
}
}
}
}
}
// Lookup default rule.
if (!$found) {
foreach ($response->Rules as $rule) {
if ($rule->IsDefault == 1) {
$this->target_group_arn = $rule->Actions[0]->TargetGroupArn;
$found = true;
break;
}
}
}
if ($found) {
$this->message('Looked up the Target Group ARN for Listener "%s":', [$this->listener_arn], 1);
$this->message('Target Group ARN: %s', [$this->target_group_arn], 2);
} else {
$this->error('No target groups found for "%s"', [$this->listener_arn]);
}
}
/**
* Lookup Hosted Zone.
*
* Use the AWS CLI to lookup the hosted zone ID from route 53 for the specified domain.
*/
private function lookupHostedZone()
{
/**
* Sample output
*
* {
* "HostedZones": [
* {
* "Id": "/hostedzone/ID",
* "Name": "domain.com.",
* "CallerReference": "CallerReference",
* "Config": {
* "PrivateZone": false
* },
* "ResourceRecordSetCount": 7
* }
* ],
* "DNSName": "domain.com",
* "IsTruncated": true,
* "NextDNSName": "nextdomain.com.",
* "NextHostedZoneId": "NextHostedZoneId",
* "MaxItems": "1"
* }
*/
$response = shell_exec(
vsprintf(
'%s %s %s --dns-name %s --max-items 1',
[
$this->aws,
'route53',
'list-hosted-zones-by-name',
$this->domain,
]
)
);
$response = json_decode($response);
if (!isset($response->HostedZones[0]->Name) ||
(isset($response->HostedZones[0]->Name) && $response->HostedZones[0]->Name !== $this->domain . '.')) {
$this->error('Error: Domain name not found!');
}
$hosted_zone = explode('/', trim($response->HostedZones[0]->Id));
$this->hosted_zone_id = array_pop($hosted_zone);
$this->message('Looked up the Hosted Zone ID for Domain "%s":', [$this->domain], 1);
$this->message('Hosted Zone ID: %s', [$this->hosted_zone_id], 2);
}
/**
* Lookup Target Group Instances.
*
* Use the AWS CLI to lookup the target group instance IDs that were retrieved from lookupElbListenerRules().
*/
private function lookupInstances()
{
/** Sample output (truncated):
* {
* "Reservations": [
* {
* "Groups": [],
* "Instances": [
* {
* "AmiLaunchIndex": 0,
* "ImageId": "ImageId",
* "InstanceId": "InstanceId",
* "InstanceType": "t3.xlarge",
* "KeyName": "KeyName",
* ...
* "PrivateIpAddress": "999.999.999.999",
* ...
* "Tags": [
* {
* "Key": "Key",
* "Value": "owned"
* },
* {
* "Key": "Name",
* "Value": "Value"
* }
* ],
* ...
* }
* ],
* "OwnerId": "OwnerId",
* "ReservationId": "ReservationId"
* }
* ]
* }
*/
$response = shell_exec(
vsprintf(
'%s %s %s --region %s --instance-ids %s',
[
$this->aws,
'ec2',
'describe-instances',
$this->region,
implode(' ', array_keys($this->instance_ids)),
]
)
);
$response = json_decode($response);
$server_ips = [];
foreach ($response->Reservations as $instance) {
$server_name = '';
foreach ($instance->Instances[0]->Tags as $tag) {
if ($tag->Key == 'Name') {
$server_name = $tag->Value;
}
}
$server_ips[$server_name] = $instance->Instances[0]->PrivateIpAddress;
}
ksort($server_ips);
$this->message('Looked up the Instance IP\'s:', [], 1);
foreach ($server_ips as $name => $ip) {
$this->message('%s = %s', [$ip, $name], 2);
}
}
/**
* Lookup Target Group Instances.
*
* Use the AWS CLI to lookup the target group instance IDs that were retrieved from lookupElbListenerRules().
*/
private function lookupTargetGroupInstances()
{
/**
* Sample output:
*
* {
* "TargetHealthDescriptions": [
* {
* "Target": {
* "Id": "Id",
* "Port": 80
* },
* "HealthCheckPort": "80",
* "TargetHealth": {
* "State": "healthy"
* }
* },
* ]
* }
*/
$response = shell_exec(
vsprintf(
'%s %s %s --region %s --target-group-arn %s',
[
$this->aws,
'elbv2',
'describe-target-health',
$this->region,
$this->target_group_arn,
]
)
);
$response = json_decode($response);
$this->instance_ids = [];
foreach ($response->TargetHealthDescriptions as $instance) {
$this->instance_ids[$instance->Target->Id] = $instance->Target->Port;
}
$this->message('Looked up the Instance Ids in the Target Group "%s":', [$this->target_group_arn], 1);
foreach ($this->instance_ids as $id => $port) {
$this->message('Instance ID: %s (Port %s)', [$id, $port], 2);
}
}
/**
* Message.
*
* Print an info message to the console.
*
* @param string $message
* @param array $params
* @param int $tabs
*/
private function message($message, $params = [], $tabs = 0)
{
for ($i = 0; $i < $tabs; $i++) {
print "\t";
}
vprintf($message . PHP_EOL, $params);
}
/**
* Parse ELB Domain.
*
* Parse the ELB Domain that was retrieved by lookupElb().
*/
private function parseElb()
{
preg_match('/([^\.]*)-([\d]*)\.([^\.]*)\.elb\.amazonaws\.com\.?$/i', $this->elb_domain, $matches);
$this->elb_name = $matches[1];
$this->elb_id = $matches[2];
$this->region = $matches[3];
}
}