-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSDevTools.ps1
1031 lines (925 loc) · 29.7 KB
/
AWSDevTools.ps1
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$awsSource = @"
using System;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
namespace Amazon.DevTools
{
public class AWSUser
{
public string AccessKey
{
get;
set;
}
public string SecretKey
{
get;
set;
}
protected internal void Validate()
{
if (string.IsNullOrEmpty(this.AccessKey))
{
throw new InvalidOperationException("[AccessKey]");
}
if (string.IsNullOrEmpty(this.SecretKey))
{
throw new InvalidOperationException("[SecretKey]");
}
}
}
}
namespace Amazon.DevTools
{
public abstract class AWSDevToolsRequest
{
protected const string METHOD = "GIT";
protected const string SERVICE = "devtools";
DateTime dateTime;
public AWSDevToolsRequest()
: this(DateTime.UtcNow)
{
}
public AWSDevToolsRequest(DateTime dateTime)
{
if (dateTime == null)
{
throw new ArgumentNullException("dateTime");
}
this.dateTime = dateTime.ToUniversalTime();
}
public string DateStamp
{
get
{
return this.dateTime.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
}
}
public string DateTimeStamp
{
get
{
return this.dateTime.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture);
}
}
public abstract string DerivePath();
protected internal abstract string DeriveRequest();
public string Host
{
get;
set;
}
public string Region
{
get;
set;
}
public string Service
{
get
{
return AWSDevToolsRequest.SERVICE;
}
}
protected internal virtual void Validate()
{
if (string.IsNullOrEmpty(this.Host))
{
throw new InvalidOperationException("[Host]");
}
if (string.IsNullOrEmpty(this.Region))
{
throw new InvalidOperationException("[Region]");
}
}
}
}
namespace Amazon.DevTools
{
public class AWSElasticBeanstalkRequest : AWSDevToolsRequest
{
public AWSElasticBeanstalkRequest()
: base()
{
}
public AWSElasticBeanstalkRequest(DateTime dateTime)
: base(dateTime)
{
}
public string Application
{
get;
set;
}
public override string DerivePath()
{
this.Validate();
string path = null;
if (string.IsNullOrEmpty(this.Environment))
{
path = string.Format("/v1/repos/{0}/commitid/{1}"
, this.Encode(this.Application)
, this.Encode(this.CommitId));
}
else
{
path = string.Format("/v1/repos/{0}/commitid/{1}/environment/{2}"
, this.Encode(this.Application)
, this.Encode(this.CommitId)
, this.Encode(this.Environment));
}
return path;
}
protected internal override string DeriveRequest()
{
this.Validate();
string path = this.DerivePath();
string request = string.Format("{0}\n{1}\n\nhost:{2}\n\nhost\n", AWSDevToolsRequest.METHOD, path, this.Host);
return request;
}
public string Environment
{
get;
set;
}
public string CommitId
{
get;
set;
}
protected internal override void Validate()
{
base.Validate();
if (string.IsNullOrEmpty(this.Application))
{
throw new InvalidOperationException("[Application]");
}
if (string.IsNullOrEmpty(this.Host))
{
throw new InvalidOperationException("[Host]");
}
}
protected internal string Encode(string plaintext)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in new UTF8Encoding().GetBytes(plaintext))
{
sb.Append(b.ToString("x2", CultureInfo.InvariantCulture));
}
return sb.ToString();
}
}
}
namespace Amazon.DevTools
{
public class AWSDevToolsAuth
{
const string AWS_ALGORITHM = "HMAC-SHA256";
const string HASH_ALGORITHM = "SHA-256";
const string HMAC_ALGORITHM = "HMACSHA256";
const string SCHEME = "AWS4";
const string TERMINATOR = "aws4_request";
AWSUser user;
AWSDevToolsRequest request;
public AWSDevToolsAuth(AWSUser user, AWSDevToolsRequest request)
{
this.user = user;
this.request = request;
}
static byte[] DeriveKey(AWSUser user, AWSDevToolsRequest request)
{
string secret = string.Format("{0}{1}", AWSDevToolsAuth.SCHEME, user.SecretKey);
byte[] kSecret = Encoding.UTF8.GetBytes(secret);
byte[] kDate = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HMAC_ALGORITHM, kSecret, Encoding.UTF8.GetBytes(request.DateStamp));
byte[] kRegion = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HMAC_ALGORITHM, kDate, Encoding.UTF8.GetBytes(request.Region));
byte[] kService = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HMAC_ALGORITHM, kRegion, Encoding.UTF8.GetBytes(request.Service));
byte[] key = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HMAC_ALGORITHM, kService, Encoding.UTF8.GetBytes(AWSDevToolsAuth.TERMINATOR));
return key;
}
public string DerivePassword()
{
this.user.Validate();
this.request.Validate();
string signature = AWSDevToolsAuth.SignRequest(this.user, this.request);
string password = string.Format("{0}Z{1}", this.request.DateTimeStamp, signature);
return password;
}
public Uri DeriveRemote()
{
this.request.Validate();
string path = this.request.DerivePath();
string password = this.DerivePassword();
string username = this.DeriveUserName();
UriBuilder remote = new UriBuilder()
{
Host = this.request.Host,
Path = path,
Password = password,
Scheme = "https",
UserName = username,
};
return remote.Uri;
}
public string DeriveUserName()
{
this.user.Validate();
return this.user.AccessKey;
}
static byte[] Hash(string algorithm, byte[] message)
{
HashAlgorithm hash = HashAlgorithm.Create(algorithm);
byte[] digest = hash.ComputeHash(message);
return digest;
}
static byte[] Hash(string algorithm, byte[] key, byte[] message)
{
KeyedHashAlgorithm hash = KeyedHashAlgorithm.Create(algorithm);
hash.Key = key;
byte[] digest = hash.ComputeHash(message);
return digest;
}
static string SignRequest(AWSUser user, AWSDevToolsRequest request)
{
string scope = string.Format("{0}/{1}/{2}/{3}", request.DateStamp, request.Region, request.Service, AWSDevToolsAuth.TERMINATOR);
StringBuilder stringToSign = new StringBuilder();
stringToSign.AppendFormat("{0}-{1}\n{2}\n{3}\n", AWSDevToolsAuth.SCHEME, AWSDevToolsAuth.AWS_ALGORITHM, request.DateTimeStamp, scope);
byte[] requestBytes = Encoding.UTF8.GetBytes(request.DeriveRequest());
byte[] requestDigest = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HASH_ALGORITHM, requestBytes);
stringToSign.Append(AWSDevToolsAuth.ToHex(requestDigest));
byte[] key = AWSDevToolsAuth.DeriveKey(user, request);
byte[] digest = AWSDevToolsAuth.Hash(AWSDevToolsAuth.HMAC_ALGORITHM, key, Encoding.UTF8.GetBytes(stringToSign.ToString()));
string signature = AWSDevToolsAuth.ToHex(digest);
return signature;
}
static string ToHex(byte[] data)
{
StringBuilder hex = new StringBuilder();
foreach (byte b in data)
{
hex.Append(b.ToString("x2", CultureInfo.InvariantCulture));
}
return hex.ToString();
}
}
}
"@
Add-Type -Language CSharpVersion3 -TypeDefinition $awsSource
# -*-powershell-*-
#
# Sets the AWS.push configuration values
#
# Will read values from the pipeline input if none are present the values are read from the console input instead.
#
function Edit-AWSElasticBeanstalkRemote
{
$data=@($input)
$used=0
$config = Read-Config $False $True
$awsAccessKey = Lookup-Setting $config "global" "AWSAccessKeyId" ("cred","git")
if (!$awsAccessKey -and (ShouldWrite-Credentials $config $false))
{
$awsAccessKeyInput = ($data[$used++] | Input-Data "AWS Access Key")
}
if ($awsAccessKeyInput)
{
$config = Write-Setting $config "cred" "global" "AWSAccessKeyId" $awsAccessKeyInput
}
$awsSecretKey = Lookup-Setting $config "global" "AWSSecretKey" ("cred","git")
if (!$awsSecretKey -and (ShouldWrite-Credentials $config $false))
{
$awsSecretKeyInput = ($data[$used++] | Input-Data "AWS Secret Key")
}
if ($awsSecretKeyInput)
{
$config = Write-Setting $config "cred" "global" "AWSSecretKey" $awsSecretKeyInput
}
$awsRegion = Lookup-Setting $config "global" "Region" ("eb","git")
if (-not $awsRegion)
{
$awsRegion = "us-east-1"
$config = Write-Setting $config "eb" "global" "Region" $awsRegion
}
$awsRegionInput = ($data[$used++] | Input-Data "AWS Region [default to $($awsRegion)]")
if ($awsRegionInput)
{
$awsRegion = $awsRegionInput
$config = Write-Setting $config "eb" "global" "Region" $awsRegionInput
}
$awsHost = Get-Endpoint $awsRegion
if ($awsHost)
{
$config = Write-Setting $config "eb" "global" "DevToolsEndpoint" $awsHost
}
else
{
$awsHostInput = ($data[$used++] | Input-Data "AWS Host [default to git.elasticbeanstalk.us-east-1.amazonaws.com]")
if ($awsHostInput)
{
$config = Write-Setting $config "eb" "global" "DevToolsEndpoint" $awsHostInput
}
else
{
$config = Write-Setting $config "eb" "global" "DevToolsEndpoint" "git.elasticbeanstalk.us-east-1.amazonaws.com"
}
}
$awsApplication = Lookup-Setting $config "global" "ApplicationName" ("eb","git")
if ($awsApplication)
{
$awsApplicationInput = ($data[$used++] | Input-Data "AWS Elastic Beanstalk Application [default to $($awsApplication)]")
}
else
{
$awsApplicationInput = ($data[$used++] | Input-Data "AWS Elastic Beanstalk Application")
}
if ($awsApplicationInput)
{
$config = Write-Setting $config "eb" "global" "ApplicationName" $awsApplicationInput
}
$awsEnvironment = Lookup-Setting $config "global" "EnvironmentName" ("eb","git")
if ($awsEnvironment)
{
$awsEnvironmentInput = ($data[$used++] | Input-Data "AWS Elastic Beanstalk Environment [default to $($awsEnvironment)]")
}
else
{
$awsEnvironmentInput = ($data[$used++] | Input-Data "AWS Elastic Beanstalk Environment")
}
if ($awsEnvironmentInput)
{
$config = Write-Setting $config "eb" "global" "EnvironmentName" $awsEnvironmentInput
}
Write-Config $config
}
#
# Looks up the endpoint for a region
#
# @params $region
# @return The endpoint
#
function Get-Endpoint
{
Param($region)
switch ($region)
{
"us-east-1" { $endpoint = "git.elasticbeanstalk.us-east-1.amazonaws.com" }
"ap-northeast-1" { $endpoint = "git.elasticbeanstalk.ap-northeast-1.amazonaws.com" }
"ap-southeast-1" { $endpoint = "git.elasticbeanstalk.ap-southeast-1.amazonaws.com" }
"ap-southeast-2" { $endpoint = "git.elasticbeanstalk.ap-southeast-2.amazonaws.com" }
"eu-west-1" { $endpoint = "git.elasticbeanstalk.eu-west-1.amazonaws.com" }
"sa-east-1" { $endpoint = "git.elasticbeanstalk.sa-east-1.amazonaws.com" }
"us-west-1" { $endpoint = "git.elasticbeanstalk.us-west-1.amazonaws.com" }
"us-west-2" { $endpoint = "git.elasticbeanstalk.us-west-2.amazonaws.com" }
}
$endpoint
}
#
# Gets the remote URL used for AWS.push
#
# @param $e environment that is deployed too.
# @param $c commit to deploy
# @return The URL
#
function Get-AWSElasticBeanstalkRemote
{
Param([string] $e,
[string] $c,
[bool] $toPush = $FALSE )
trap [System.Management.Automation.MethodInvocationException]
{
if ($_.Exception -and $_.Exception.InnerException)
{
$awsOption = $_.Exception.InnerException.Message
switch ($awsOption)
{
"[AccessKey]" { $awsOption = "aws.accesskey" }
"[Application]" { $awsOption = "aws.elasticbeanstalk.application" }
"[Host]" { $awsOption = "aws.elasticbeanstalk.host" }
"[Region]" { $awsOption = "aws.region" }
"[SecretKey]" { $awsOption = "aws.secretkey" }
}
Write-Host "Missing configuration setting for: $($awsOption)"
}
else
{
Write-Host "An unknown error occurred while computing your temporary password."
}
Write-Host "`nTry running 'git aws.config' to update your repository configuration."
Exit
}
$config = Read-Config
$awsAccessKey = Lookup-Setting $config "global" "AWSAccessKeyId" ("cred","git")
$awsSecretKey = Lookup-Setting $config "global" "AWSSecretKey" ("cred","git")
$awsRegion = Lookup-Setting $config "global" "Region" ("eb","git")
$awsHost = Lookup-Setting $config "global" "DevToolsEndpoint" ("eb","git")
$awsApplication = Lookup-Setting $config "global" "ApplicationName" ("eb","git")
if ($e)
{
$awsEnvironment = $e
}
else
{
$branchName = &git rev-parse --abbrev-ref HEAD
$defaultEnv = Lookup-Setting $config "branches" $branchName ("eb")
if ($defaultEnv)
{
$awsEnvironment = $defaultEnv
}
else
{
$awsEnvironment = Lookup-Setting $config "global" "EnvironmentName" ("eb","git")
}
}
$gitCommitId = $c
$awsUser = New-Object -TypeName Amazon.DevTools.AWSUser
$awsUser.AccessKey = $awsAccessKey
$awsUser.SecretKey = $awsSecretKey
$awsRequest = New-Object -TypeName Amazon.DevTools.AWSElasticBeanstalkRequest
$awsRequest.Region = $awsRegion
$awsRequest.Host = $awsHost
$awsRequest.Application = $awsApplication
$awsRequest.Environment = $awsEnvironment
$awsRequest.CommitId = $gitCommitId
$awsAuth = New-Object -TypeName Amazon.DevTools.AWSDevToolsAuth $awsUser,$awsRequest
$awsRemote = $awsAuth.DeriveRemote()
if($toPush) {
Write-Host "Pushing to environment: $awsEnvironment"
}
return $awsRemote.ToString()
}
#
# Performs the aws.push
#
# @param $e environment that is deployed too.
# @param $c commit to deploy
#
function Invoke-AWSElasticBeanstalkPush
{
Param([string] $e, [string] $c)
$remote = Get-AWSElasticBeanstalkRemote $e $c $TRUE
$src = $c
$dst = "refs/heads/master"
$commit = $src + ":" + $dst
&git push -f $remote $commit
}
#
# Adds the git aliases for aws.push and aws.config to the git repository.
#
function Initialize-AWSElasticBeanstalkRepository
{
$command = 'Import-Module AWSDevTools; $e, $c = Get-Options $args; Get-AWSElasticBeanstalkRemote $e $c'
&git config alias.aws.elasticbeanstalk.remote "!powershell -noprofile -executionpolicy bypass -command '& { $command }'"
$command = 'Import-Module AWSDevTools; $e, $c = Get-Options $args; Invoke-AWSElasticBeanstalkPush $e $c'
&git config alias.aws.push "!powershell -noprofile -executionpolicy bypass -command '& { $command }'"
$command = 'Import-Module AWSDevTools; Edit-AWSElasticBeanstalkRemote'
&git config alias.aws.config "!powershell -noprofile -executionpolicy bypass -command '& { $command }'"
}
#
# Read in data
#
# Will used pipeline data if present, otherwise reads from the console
#
# @param $message The text to display as a prompt
# @return The data collected
#
function Input-Data
{
Param([string] $message)
Write-Host -NoNewline "$($message): "
if (($input.MoveNext()) -and ($input.Current))
{
Write-Host $input.Current
$input.Current
}
else
{
[Console]::In.ReadLine()
}
}
#
# Gets the values for the aws.push and aws.config command options
#
# @param $arr The command line options passed to the command
# @return The options values
#
function Get-Options
{
Param([string[]] $arr)
$e = $null;
$c = $null;
$optionmappings = @{
'--environment' = 'environment';
'-e' = 'environment';
'--commit' = 'commit';
'-c' = 'commit';
'--help' = 'help';
'-h' = 'help'
}
$options = @{}
for ($i=0; $i -lt $arr.count; $i++)
{
$optname = $arr[$i]
$mappedoption = $optionmappings[$optname]
if (!$mappedoption) {
Write-Host("Unknown Option: {0}" -f $arr[$i])
Write-Help
Exit
}
if ($mappedoption -eq "help") {
Write-Help
Exit
}
$value = $arr[++$i]
if (($value -eq $null) -or $optionmappings[$value]) {
Write-Host("You must provide a value for {0}" -f $optname)
Write-Help
Exit
}
if ($options[$mappedoption]) {
Write-Host("--{0} specified twice" -f $mappedoption)
Exit
}
$options[$mappedoption] = $value
}
$e = $options["environment"]
$c = $options["commit"]
if ($c -eq $null) {
$c = "HEAD"
}
$c = Parse-CommitOption $c
$result = $e, $c
$result
}
#
# Looks up a commit to get the id and verifies it is a commit object.
#
# @param $c The commit to get the id of.
# @return The commit id
#
function Parse-CommitOption
{
Param([string] $c)
$parsed = &"git" "rev-parse" $c
if ($LastExitCode -ne 0) {
Exit
}
$type = &"git" "cat-file" "-t" $parsed
if ($type -ne "commit") {
Write-Host("{0} is a {1}, and the value of --commit must refer to a commit" -f
$c, $type)
Write-Help
Exit
}
$parsed
}
#
# Display the usage of aws.push
#
function Write-Help
{
Write-Host @'
Usage: git aws.push
Options:
--environment ENVIRONMENT, -e ENVIRONMENT
ENVIRONMENT is the name of an AWS Elastic Beanstalk environment. When this
option is used, the command updates the named environment instead of
the default one. The default can be set by running "git aws.config".
--commit COMMIT, -c COMMIT
COMMIT identifies a commit in the repository -- for example, "HEAD" identifies
the commit that is currently checked out, or a SHA1 (possibly abbreviated) can
be used to identify a specific commit from the history. When this option is used,
the command uses the named commit instead of HEAD to create the version to be
deployed to your environment. See the help for "git rev-parse" for a description
of all the supported formats for identifying commits.
'@
}
#
# Reads in the config values
#
# @param $verboseOutput = display the config values found.
# @param $credentialOutput = display the credential file location and values found.
# @return The config read
#
function Read-Config($verboseOutput = $FALSE, $credentialOutput = $FALSE)
{
$gitSettings = @{}
$gitSettings["global"] = @{}
$gitSettings["global"]["AWSAccessKeyId"] = &git config --get aws.accesskey
$gitSettings["global"]["AWSSecretKey"] = &git config --get aws.secretkey
$gitSettings["global"]["Region"] = &git config --get aws.region
$gitSettings["global"]["DevToolsEndpoint"] = &git config --get aws.elasticbeanstalk.host
$gitSettings["global"]["ApplicationName"] = &git config --get aws.elasticbeanstalk.application
$gitSettings["global"]["EnvironmentName"] = &git config --get aws.elasticbeanstalk.environment
if ($verboseOutput)
{
foreach ($key in $gitSettings["global"].keys)
{
if ($gitSettings["global"][$key])
{
Write-Host "Read setting in git.config for $($key) of $($gitSettings["global"][$key])"
}
}
}
$ebConfigFile = Get-Location
$ebConfigFile = Join-Path $ebConfigFile ".elasticbeanstalk"
$ebConfigFile = Join-Path $ebConfigFile "config"
$ebSettings = @{}
if (Test-Path $ebConfigFile)
{
$ebSettings = Read-IniFile $ebConfigFile
}
if ((($ebSettings["global"]) -and ($ebSettings["global"]["Region"])) -and (-not($ebSettings["global"]["DevToolsEndpoint"])))
{
$endpoint = Get-Endpoint $ebSettings["global"]["Region"]
if ($endpoint)
{
$ebSettings["global"]["DevToolsEndpoint"] = $endpoint
}
}
if ($verboseOutput)
{
foreach ($section in $ebSettings.keys)
{
foreach ($key in $ebSettings[$section].keys)
{
Write-Host "Read setting in $($ebConfigFile) in $($section) for $($key) of $($ebSettings[$section][$key])"
}
}
}
if (($ebsettings) -and ($ebsettings["global"]))
{
$credentialFile = $ebsettings["global"]["AwsCredentialFile"]
}
if ((-not($credentialFile -and (Test-Path $credentialFile ))) -and (Test-Path env:USERPROFILE))
{
$credentialFile = (Get-Item env:USERPROFILE).value
$credentialFile = Join-Path $credentialFile ".elasticbeanstalk"
$credentialFile = Join-Path $credentialFile "aws_credential_file"
}
if (Test-Path env:AWS_CREDENTIAL_FILE)
{
$envVar = Get-Item env:AWS_CREDENTIAL_FILE
$credentialFile = $envVar.Value
}
$credentials = @{}
if (($credentialFile) -and (Test-Path $credentialFile))
{
try
{
$credentials = Read-IniFile $credentialFile
}
catch {}
}
if ($verboseOutput)
{
foreach ($section in $credentials.keys)
{
foreach ($key in $credentials[$section].keys)
{
Write-Host "Read setting in $($credentialFile) in $($section) for $($key) of $($credentials[$section][$key])"
}
}
}
if ($credentialOutput)
{
if ($credentialFile)
{
Write-Host @"
Reading Credentials from $($credentialFile).
"@
if (Test-Path $credentialFile)
{
try
{
$temp = Get-Item $credentialFile -Force
if ($credentials["global"] -and ($credentials["global"]["AWSAccessKeyId"] -and $credentials["global"]["AWSSecretKey"]))
{
Write-Host @"
You can supply different credentials by editing that file or editing .elasticbeanstalk/config to reference a different file.
"@
}
else
{
Write-Host @"
This file doesn't contain a full set of credentials. You can fix the problem by editing the file or by editing .elasticbeanstalk/config to reference a different file.
"@
}
}
catch
{
Write-Host @"
The file is not readable. You can fix the problem by granting read permissions to the file or by editing .elasticbeanstalk/config to reference a different file.
"@
}
}
else
{
Write-Host @"
The file does not exist. You can supply credentials by creating the file or editing .elasticbeanstalk/config to reference a different file.
"@
}
}
else
{
Write-Host @"
No credential file location found.
You can specify the credential file location by editting .elasticbeanstalk/config and adding the following line:
AwsCredentialFile=<path_to_your_credential_file>
"@
}
Write-Host @"
The credential file should have the following format:
AWSAccessKeyId=your key
AWSSecretKey=your secret
"@
}
@{git=$gitSettings;eb=$ebSettings;cred=$credentials}
}
#
# Reads an ini file into a hashmap
#
# @param $path The location of the file to read
# @return The ini read
#
function Read-IniFile
{
Param([string] $path)
$ini = @{}
$section = "global"
$ini[$section] = @{}
switch -regex -file $path
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
if (!$ini[$section])
{
$ini[$section] = @{}
}
continue
}
"^(;.*)" # Comment
{
continue
}
"\s*(.+?)\s*=\s*(.*?)\s*$" # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
continue
}
}
return $ini
}
#
# Looks up a value from the config
#
# @param $config The config to look in
# @param $section The section of the value being looked for.
# @param $key The key for the value being looked for.
# @param $locations An in order array of parts of the config to look in.
# @return The value looked up
#
function Lookup-Setting($config, $section, $key, $locations)
{
foreach ($loc in $locations)
{
if (((($config) -and ($config[$loc])) -and ($config[$loc][$section])) -and ($config[$loc][$section][$key]))
{
$setting = $config[$loc][$section][$key]
break;
}
}
$setting
}
#
# Sets a value in a config
#
# @param $config The config to set the value in
# @param $file The part of the config to set the value in
# @param $section The section the value should be in
# @param $key The key for the value being set
# @param $value The value that is being set
# @return The config with the value set
#
function Write-Setting($config, $file, $section, $key, $value)
{
if (!$config)
{
$config = @{}
}
if (!$config[$file])
{
$config[$file] = @{}
}
if (!$config[$file][$section])
{
$config[$file][$section] = @{}
}
$config[$file][$section][$key] = $value
$config
}
#
# Write out the config
#
# @param $config The config to write
#
function Write-Config($config)
{
foreach ($var in ("Region", "DevToolsEndpoint", "ApplicationName", "EnvironmentName"))
{
if ((!(Lookup-Setting $config "global" $var ("eb"))) -and (Lookup-Setting $config "global" $var ("git")))
{
$config = Write-Setting $config "eb" "global" $var (Lookup-Setting $config "global" $var ("git"))
}
}
foreach ($var in ("AWSAccessKeyId", "AWSSecretKey"))
{
if ((!(Lookup-Setting $config "global" $var ("cred"))) -and (Lookup-Setting $config "global" $var ("git")))
{
$config = Write-Setting $config "cred" "global" $var (Lookup-Setting $config "global" $var ("git"))
}
}
$credentialFile = ShouldWrite-Credentials $config
if ($credentialFile)
{
Write-IniFile $config["cred"] $credentialFile
$config = Write-Setting $config "eb" "global" "AwsCredentialFile" $credentialFile
}
if (($config) -and (($config["eb"]) -and (@($config["eb"].keys).length -ne 0)))
{
$ebConfigFile = Get-Location
$ebConfigFile = Join-Path $ebConfigFile ".elasticbeanstalk"
$ebConfigFile = Join-Path $ebConfigFile "config"
Write-IniFile $config["eb"] $ebConfigFile
}
}
#
# ShouldWrite-Credentials
#
# @param $config The config to we will be writing
# @return The location to write to
#
function ShouldWrite-Credentials($config, $checkForCreds = $True)
{
if ((!$checkForCreds) -or (($config) -and (($config["cred"]) -and (@($config["cred"].keys).length -ne 0))))
{
if (-not((Test-Path env:AWS_CREDENTIAL_FILE) -or (Lookup-Setting $config "global" "AwsCredentialFile" ("eb"))))
{
if (Test-Path env:USERPROFILE)
{
$credentialFile = (Get-Item env:USERPROFILE).value
$credentialFile = Join-Path $credentialFile ".elasticbeanstalk"
$credentialFile = Join-Path $credentialFile "aws_credential_file"
if (Test-Path $credentialFile)
{
$credentialFile = $null
}
}
}
}
$credentialFile
}
#
# Write an ini file
#
# @param $ini The ini that should be written to the file
# @param $path The location of the ini to write
#
function Write-IniFile($ini, $path)
{
$iniFile = New-Item -ItemType file -Path $path -Force
if (($ini["global"]) -and ($ini["global"].keys.count -gt 0))
{
Add-Content -Path $iniFile -Value "[global]"
foreach ($i in $ini["global"].keys | Sort-Object)