forked from VITObelgium/fakes3pp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing: provide test coverage for backend selection
Add a scenario where we mimic a full setup and where we verify which backend is reached.
- Loading branch information
Peter Van Bouwel
committed
Nov 20, 2024
1 parent
fff9409
commit 6c42e42
Showing
11 changed files
with
385 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
**__debug_bin* | ||
**/.idea | ||
|
||
**/*.private | ||
**/*.private | ||
|
||
testing/venv/** | ||
testing/**.err | ||
testing/**.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
|
||
const testRegion1 = "tst-1" | ||
const testRegion2 = "eu-test-2" | ||
var backendTestRegions = []string{testRegion1, testRegion2} | ||
|
||
var testingBackendsConfig = []byte(fmt.Sprintf(` | ||
# This is a test file check backend-config.yaml if you want to create a configuration | ||
s3backends: | ||
- region: %s | ||
credentials: | ||
file: ../etc/creds/cfc_creds.yaml | ||
endpoint: http://localhost:5000 | ||
- region: %s | ||
credentials: | ||
file: ../etc/creds/otc_creds.yaml | ||
endpoint: http://localhost:5001 | ||
default: %s | ||
`, testRegion1, testRegion2, testRegion2)) | ||
|
||
|
||
//Set the configurations as expected for the testingbackends | ||
//See testing/README.md for details on testing setup | ||
func setTestingBackendsConfig(t *testing.T) { | ||
cfg, err := getBackendsConfigFromBytes(testingBackendsConfig) | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
globalBackendsConfig = cfg | ||
} | ||
|
||
//This is the testing fixture. It starts an sts and s3 proxy which | ||
//are configured with the S3 backends detailed in testing/README.md. | ||
func testingFixture(t *testing.T) (tearDown func ()(), getToken func(subject string, d time.Duration, tags AWSSessionTags) string){ | ||
//Configure backends to be the testing S3 backends | ||
setTestingBackendsConfig(t) | ||
//Given valid server config | ||
teardownSuiteSTS := setupSuiteProxySTS(t) | ||
teardownSuiteS3 := setupSuiteProxyS3(t, justProxied) | ||
|
||
//function to stop the setup of the fixture | ||
tearDownProxies := func () { | ||
teardownSuiteSTS(t) | ||
teardownSuiteS3(t) | ||
} | ||
|
||
_, err := loadOidcConfig([]byte(testConfigFakeTesting)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
signingKey, err := getTestSigningKey() | ||
if err != nil { | ||
t.Error("Could not get test signing key") | ||
t.FailNow() | ||
} | ||
|
||
//function to get a valid token that can be exchanged for credentials | ||
getSignedToken := func(subject string, d time.Duration, tags AWSSessionTags) string { | ||
token, err := CreateSignedToken(createRS256PolicyTokenWithSessionTags(testFakeIssuer, subject, d, tags), signingKey) | ||
if err != nil { | ||
t.Errorf("Could create signed token with subject %s and tags %v: %s", subject, tags, err) | ||
t.FailNow() | ||
} | ||
return token | ||
} | ||
|
||
|
||
return tearDownProxies, getSignedToken | ||
} | ||
|
||
func getCredentialsFromTestStsProxy(t *testing.T, token, sessionName, roleArn string) aws.Credentials { | ||
result, err := assumeRoleWithWebIdentityAgainstTestStsProxy(t, token, sessionName, roleArn) | ||
if err != nil { | ||
t.Errorf("encountered error when assuming role: %s", err) | ||
} | ||
creds := result.Credentials | ||
awsCreds := aws.Credentials{ | ||
AccessKeyID: *creds.AccessKeyId, | ||
SecretAccessKey: *creds.SecretAccessKey, | ||
SessionToken: *creds.SessionToken, | ||
Expires: *creds.Expiration, | ||
CanExpire: true, | ||
} | ||
return awsCreds | ||
} | ||
|
||
//region object is setup in the backends and matches the region name of the backend | ||
func getRegionObjectContent(t *testing.T, region string, creds aws.Credentials) string{ | ||
client := getS3ClientAgainstS3Proxy(t, region, creds) | ||
|
||
max1Sec, cancel := context.WithTimeout(context.Background(), 1000 * time.Second) | ||
var bucketName = "backenddetails" | ||
var objectKey = "region.txt" | ||
input := s3.GetObjectInput{ | ||
Bucket: &bucketName, | ||
Key: &objectKey, | ||
} | ||
defer cancel() | ||
s3ObjectOutput, err := client.GetObject(max1Sec, &input) | ||
if err != nil { | ||
t.Errorf("encountered error getting region file for %s: %s", region, err) | ||
} | ||
bytes, err := io.ReadAll(s3ObjectOutput.Body) | ||
if err != nil { | ||
t.Errorf("encountered error reading region file content for %s: %s", region, err) | ||
} | ||
return string(bytes) | ||
} | ||
|
||
|
||
//Backend selection is done by chosing a region. The enpdoint we use is fixed | ||
//to our testing S3Proxy and therefore the hostname is the same. In each backend | ||
//we have a bucket with the same name and region.txt which holds the actual region | ||
//name which we can use to validate that our request went to the right backend. | ||
func TestMakeSureCorrectBackendIsSelected(t *testing.T) { | ||
tearDown, getSignedToken := testingFixture(t) | ||
defer tearDown() | ||
token := getSignedToken("mySubject", time.Minute * 20, AWSSessionTags{PrincipalTags: map[string][]string{"org": {"a"}}}) | ||
print(token) | ||
//Given the policy Manager that has roleArn for the testARN | ||
pm = *NewTestPolicyManagerAllowAll() | ||
//Given credentials for that role | ||
creds := getCredentialsFromTestStsProxy(t, token, "my-session", testPolicyAllowAllARN) | ||
|
||
|
||
for _, backendRegion := range backendTestRegions { | ||
regionContent := getRegionObjectContent(t, backendRegion, creds) | ||
if regionContent != backendRegion { | ||
t.Errorf("when retrieving region file for %s we got %s", backendRegion, regionContent) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# testing setup | ||
|
||
In order to allow more extensive testing we run basic implementations of S3 servers as part of testing and | ||
allow using them as part of the tests. For simplicity there are not separate hostnames so they do run on | ||
localhost but they get distinguished by port number. | ||
|
||
The downside of this is that we make assumptions on the development environment. This directory should help | ||
developers setup their local environment to have the S3 servers running. | ||
|
||
## Overview | ||
|
||
From code it might be harder to understand what we are trying to simulate. But we are just trying to simulate | ||
S3 servers which corresponds to different regions and thus 2 separate S3 stacks. These regions do not share any state. | ||
|
||
In every region we create a bucket "backenddetails" that contains a file region.txt with the region name. | ||
|
||
Currently we bootstrap the following regions: | ||
- tst-1 : available on port 5000 | ||
- eu-test-2 : available on port 5001` | ||
|
||
|
||
## Dependencies | ||
|
||
|
||
### Dependencies bootstrap | ||
Assumed dependencies are to have a modern Python3 runtime which supports virtual environments and pip. | ||
By executing `make setup-test-dependencies` the required packages get downloaded and installed in the virtual | ||
environment. | ||
|
||
### Dependencies runtimes | ||
In order to run the S3 servers and have them populated with the test files run `make start-test-s3-servers` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
eu-test-2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tst-1 |
Oops, something went wrong.