Skip to content

Commit

Permalink
Merge pull request #19 from fireflycons/dev
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
fireflycons authored Jun 5, 2019
2 parents 4bcc5a3 + 57d576e commit 8726b82
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.14.1

* Fix a bug that arose today. Seems AWS have changed S3 URL format for urls with region in. Was s3-eu-west-1, now s3.eu-west-1. Either way, support both.

## 0.14.0

* New command `Compare-ATCFNStackResourceDrift`. Formats all resource drifts into two files that can be visually compared in the configured diff tool.
Expand Down
27 changes: 22 additions & 5 deletions aws-toolbox/Private/S3/Split-S3Url.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,30 @@ function Split-S3Url
[Uri]$S3Url
)

if (('https', 'http') -inotcontains $S3Url.Scheme -or -not $S3Url.Host.StartsWith('s3-'))
$url = $S3Url.ToString()

# Weird behaviour. PowerShell -match does not match these regexes
$virtualDomainRx = New-Object System.Text.RegularExpressions.Regex '^https://(?<bucket>.*?)\.s3([\.\-](?<region>[a-z]{2}-[a-z\-]+-\d))?\.amazonaws\.com/(?<key>.*)$'
$pathStyleRx = New-Object System.Text.RegularExpressions.Regex '^https://s3([\.\-](?<region>[a-z]{2}-[a-z\-]+-\d))?\.amazonaws\.com/(?<bucket>.*?)/(?<key>.*)$'

$m = $virtualDomainRx.Match($url)
if ($m.Success)
{
throw "$($S3Url): Not an S3 URL"
return New-Object PSObject -Property @{
BucketName = $m.Groups["bucket"].Value
Key = $m.Groups["key"].Value
}
}

New-Object PSObject -Property @{
BucketName = $S3Url.Segments[1].Trim('/')
Key = ($S3Url.Segments | Select-Object -Skip 2 ) -join [string]::Empty
$m = $pathStyleRx.Match($url)

if ($m.Success)
{
return New-Object PSObject -Property @{
BucketName = $m.Groups["bucket"].Value
Key = $m.Groups["key"].Value
}
}

throw "$($S3Url): Not an S3 URL"
}
2 changes: 1 addition & 1 deletion aws-toolbox/aws-toolbox.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'aws-toolbox.psm1'

# Version number of this module.
ModuleVersion = '0.14.0'
ModuleVersion = '0.14.1'

# ID used to uniquely identify this module
GUID = 'e3c04d58-4e7d-4572-9e81-3b3a93f1a518'
Expand Down
48 changes: 48 additions & 0 deletions tests/aws-toolbox.Private.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,54 @@ InModuleScope $ModuleName {
}
}

Context 'S3 Url Parsing - Path style' {

@(
'https://s3.amazonaws.com/jeffbarr-public/images/ritchie_and_thompson_pdp11.jpeg'
'https://s3-us-east-2.amazonaws.com/jeffbarr-public/images/ritchie_and_thompson_pdp11.jpeg'
'https://s3.us-east-2.amazonaws.com/jeffbarr-public/images/ritchie_and_thompson_pdp11.jpeg'
'https://s3.us-gov-east-1.amazonaws.com/jeffbarr-public/images/ritchie_and_thompson_pdp11.jpeg'
'https://s3-us-gov-east-1.amazonaws.com/jeffbarr-public/images/ritchie_and_thompson_pdp11.jpeg'
) |
Foreach-Object {

$uri = [Uri]$_

It "Should parse $uri" {

{ $uri | Split-S3Url } | Should Not throw

$loc = $uri | Split-S3Url
$loc.BucketName | Should -Be 'jeffbarr-public'
$loc.Key | Should -Be 'images/ritchie_and_thompson_pdp11.jpeg'
}
}
}

Context 'S3 Url Parsing - Virtual domain style' {

@(
'https://jeffbarr-public.s3.amazonaws.com/images/ritchie_and_thompson_pdp11.jpeg'
'https://jeffbarr-public.s3.amazonaws.com/images/ritchie_and_thompson_pdp11.jpeg'
'https://jeffbarr-public.s3.eu-west-1.amazonaws.com/images/ritchie_and_thompson_pdp11.jpeg'
'https://jeffbarr-public.s3-eu-west-1.amazonaws.com/images/ritchie_and_thompson_pdp11.jpeg'
'https://jeffbarr-public.s3-us-gov-east-1.amazonaws.com/images/ritchie_and_thompson_pdp11.jpeg'
) |
Foreach-Object {

$uri = [Uri]$_

It "Should parse $uri" {

{ $uri | Split-S3Url } | Should Not throw

$loc = $uri | Split-S3Url
$loc.BucketName | Should -Be 'jeffbarr-public'
$loc.Key | Should -Be 'images/ritchie_and_thompson_pdp11.jpeg'
}
}
}

Context 'S3 Workspace Bucket' {

Mock Get-STSCallerIdentity -MockWith {
Expand Down

0 comments on commit 8726b82

Please sign in to comment.