mirror of
https://github.com/storytold/irsa-manager.git
synced 2026-10-09 00:09:43 +00:00
upload S3 object (not updated)
This commit is contained in:
+37
-2
@@ -26,11 +26,12 @@ type AwsIamAPI interface {
|
||||
|
||||
type AwsS3API interface {
|
||||
CreateBucket(ctx context.Context, params *s3.CreateBucketInput, optFns ...func(*s3.Options)) (*s3.CreateBucketOutput, error)
|
||||
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
|
||||
DeletePublicAccessBlock(ctx context.Context, params *s3.DeletePublicAccessBlockInput, optFns ...func(*s3.Options)) (*s3.DeletePublicAccessBlockOutput, error)
|
||||
PutBucketOwnershipControls(ctx context.Context, params *s3.PutBucketOwnershipControlsInput, optFns ...func(*s3.Options)) (*s3.PutBucketOwnershipControlsOutput, error)
|
||||
DeleteBucket(ctx context.Context, params *s3.DeleteBucketInput, optFns ...func(*s3.Options)) (*s3.DeleteBucketOutput, error)
|
||||
DeleteObjects(ctx context.Context, params *s3.DeleteObjectsInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectsOutput, error)
|
||||
HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
|
||||
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
|
||||
PutBucketOwnershipControls(ctx context.Context, params *s3.PutBucketOwnershipControlsInput, optFns ...func(*s3.Options)) (*s3.PutBucketOwnershipControlsOutput, error)
|
||||
}
|
||||
|
||||
type AwsClient interface {
|
||||
@@ -68,6 +69,40 @@ type AwsS3Client struct {
|
||||
bucketName string
|
||||
}
|
||||
|
||||
// CheckObjectExists checks if a specific object exists in the given bucket.
|
||||
func (a *AwsS3Client) CheckObjectExists(ctx context.Context, key string) (bool, error) {
|
||||
_, err := a.Client.HeadObject(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(a.bucketName),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
var nfe *types.NotFound
|
||||
if errors.As(err, &nfe) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CreateObjectPublic creates a file to an S3 bucket and sets its access level to public read.
|
||||
// This means the file can be read by anyone on the internet.
|
||||
func (a *AwsS3Client) CreateObjectPublic(ctx context.Context, key string, body []byte) error {
|
||||
exists, err := a.CheckObjectExists(ctx, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
log.Printf("skipped to create bucket object %s \n", key)
|
||||
} else {
|
||||
err := a.PutObjectPublic(ctx, key, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutObjectPublic uploads a file to an S3 bucket and sets its access level to public read.
|
||||
// This means the file can be read by anyone on the internet.
|
||||
func (a *AwsS3Client) PutObjectPublic(ctx context.Context, key string, body []byte) error {
|
||||
|
||||
@@ -108,7 +108,6 @@ var _ = Describe("IRSASetup Controller", func() {
|
||||
for _, expect := range expected {
|
||||
checkNoExist(expect, newSecret)
|
||||
}
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -144,6 +143,10 @@ func (m *mockAwsS3API) PutObject(ctx context.Context, params *s3.PutObjectInput,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *mockAwsS3API) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *mockAwsS3API) DeletePublicAccessBlock(ctx context.Context, params *s3.DeletePublicAccessBlockInput, optFns ...func(*s3.Options)) (*s3.DeletePublicAccessBlockOutput, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func (s *S3IdPDiscovery) Upload(ctx context.Context, o selfhosted.OIDCIdPDiscove
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
err = s.s3Client.PutObjectPublic(ctx,
|
||||
err = s.s3Client.CreateObjectPublic(ctx,
|
||||
CONFIGURATION_PATH,
|
||||
discovery,
|
||||
)
|
||||
@@ -50,7 +50,7 @@ func (s *S3IdPDiscovery) Upload(ctx context.Context, o selfhosted.OIDCIdPDiscove
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
err = s.s3Client.PutObjectPublic(ctx,
|
||||
err = s.s3Client.CreateObjectPublic(ctx,
|
||||
o.JWKsFileName(),
|
||||
jwk,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user