Skip to content

Commit

Permalink
Merge pull request #36 from armosec/remove_getbyrange
Browse files Browse the repository at this point in the history
Remove getbyrange and pr.yaml
  • Loading branch information
kooomix authored Nov 22, 2023
2 parents 341d74c + 4aa45fa commit b2c4828
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: PR Checks

on:
pull_request:
branches: [ master , "*" ]

env:
GH_ACCESS_TOKEN: ${{ secrets.ARMOSEC_GITHUB_ACCESS_TOKEN }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ env.GH_ACCESS_TOKEN }}

- run: git config --global url.https://[email protected]/armosec/.insteadOf https://github.com/armosec/
- run: git config --global url.https://[email protected]/kubescape/.insteadOf https://github.com/kubescape/

- name: Set up Go

uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Get dependencies
run: go mod download

- name: Test
run: go test ./... -v --race -covermode=atomic -coverprofile=coverage.out

- name: Convert coverage count to lcov format
uses: jandelgado/gcov2lcov-action@v1

- name: Submit coverage tests to Coveralls
continue-on-error: true
uses: coverallsapp/github-action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: coverage.lcov
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Release-Tag
on:
push:
branches: [ master, "*" ]
branches: [ master ]

env:
GH_ACCESS_TOKEN: ${{ secrets.ARMOSEC_GITHUB_ACCESS_TOKEN }}
Expand All @@ -24,7 +24,7 @@ jobs:

- name: Test & coverage
id: unit-test
run: go test -v ./... -covermode=count -coverprofile=coverage.out
run: go test -v ./... -covermode=atomic -coverprofile=coverage.out

- name: Convert coverage count to lcov format
uses: jandelgado/gcov2lcov-action@v1
Expand Down
41 changes: 11 additions & 30 deletions s3connector/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type ObjectStorage interface {
StoreObject(objPath S3ObjectPath, value io.ReadSeeker) (S3ObjectPath, error)
DeleteObject(S3ObjectPath) error
GetObject(objPath S3ObjectPath) (io.ReadCloser, error)
GetByRange(objPath S3ObjectPath, rangeStart, rangeEnd int64) (io.ReadCloser, error)
GetBucket() string
}

Expand Down Expand Up @@ -113,6 +112,16 @@ func (s *s3ObjectStorage) DeleteObject(objPath S3ObjectPath) error {
}

func (s *s3ObjectStorage) GetObject(objPath S3ObjectPath) (io.ReadCloser, error) {

var objRange *string

if objPath.Range != nil {
if objPath.Range.Start < 0 || objPath.Range.End <= objPath.Range.Start {
return nil, fmt.Errorf("invalid range: start must be non-negative and end must be greater than start, ranges are: %v", objPath.Range)
}
objRange = aws.String(fmt.Sprintf("bytes=%d-%d", objPath.Range.Start, objPath.Range.End))
}

bucket := s.bucket
if objPath.Bucket != "" {
if err := s.BucketExists(objPath.Bucket); err != nil {
Expand All @@ -122,10 +131,10 @@ func (s *s3ObjectStorage) GetObject(objPath S3ObjectPath) (io.ReadCloser, error)
}

}

getObj := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objPath.Key),
Range: objRange,
}
if objPath.Range != nil && objPath.Range.Start > 0 && objPath.Range.End > 0 {
getObj.Range = aws.String(fmt.Sprintf("bytes=%d-%d", objPath.Range.Start, objPath.Range.End))
Expand All @@ -136,31 +145,3 @@ func (s *s3ObjectStorage) GetObject(objPath S3ObjectPath) (io.ReadCloser, error)
}
return awsObj.Body, nil
}

func (s *s3ObjectStorage) GetByRange(objPath S3ObjectPath, rangeStart, rangeEnd int64) (io.ReadCloser, error) {
if rangeStart < 0 || rangeEnd <= rangeStart {
return nil, fmt.Errorf("invalid range: start must be non-negative and end must be greater than start")
}

bucket := s.bucket
if objPath.Bucket != "" {
if err := s.BucketExists(objPath.Bucket); err != nil {
return nil, fmt.Errorf("failed to check if bucket exists: %w", err)
} else {
bucket = objPath.Bucket
}
}

getObjInput := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objPath.Key),
Range: aws.String(fmt.Sprintf("bytes=%d-%d", rangeStart, rangeEnd)),
}

result, err := s3.New(s.session).GetObject(getObjInput)
if err != nil {
return nil, fmt.Errorf("failed to get object by range: %w", err)
}

return result.Body, nil
}
8 changes: 6 additions & 2 deletions s3connector/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ func (suite *S3ObjectStorageSuite) TestGetByRange() {
_, err := suite.S3Localstack.GetLocalStack().StoreObject(S3ObjectPath{Key: key}, bytes.NewReader([]byte(fullContent)))
suite.NoError(err)

objPath := S3ObjectPath{
Key: key,
Range: &S3ObjectRange{Start: start, End: end},
}
// Perform the GetByRange operation
res, err := suite.S3Localstack.GetLocalStack().GetByRange(S3ObjectPath{Key: key}, start, end)
res, err := suite.S3Localstack.GetLocalStack().GetObject(objPath)
suite.NoError(err)
suite.NotNil(res)

Expand All @@ -103,7 +107,7 @@ func (suite *S3ObjectStorageSuite) TestGetByRange() {
suite.Equal(expectedContent, string(rangeContent))

// Clean up
objPath := S3ObjectPath{
objPath = S3ObjectPath{
Key: key,
Bucket: suite.S3Localstack.retStore.GetBucket(),
}
Expand Down

0 comments on commit b2c4828

Please sign in to comment.