Skip to content

Commit ced2c7b

Browse files
committed
Merge pull request #39 from unity-sds/lifecycle-health-check-update
updated policy to set prefix
2 parents d1992c4 + f71a8fb commit ced2c7b

File tree

1 file changed

+58
-9
lines changed
  • backend/internal/aws

1 file changed

+58
-9
lines changed

backend/internal/aws/s3.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package aws
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"math/rand"
8+
"strconv"
9+
"time"
10+
611
"github.com/aws/aws-sdk-go-v2/aws"
712
"github.com/aws/aws-sdk-go-v2/config"
813
awsconfig "github.com/aws/aws-sdk-go-v2/config"
@@ -11,10 +16,6 @@ import (
1116
log "github.com/sirupsen/logrus"
1217
"github.com/spf13/viper"
1318
appconfig "github.com/unity-sds/unity-management-console/backend/internal/application/config"
14-
"io"
15-
"math/rand"
16-
"time"
17-
"strconv"
1819
)
1920

2021
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
@@ -28,6 +29,7 @@ type S3BucketAPI interface {
2829
ListObjectsV2(ctx context.Context, params *s3.ListObjectsV2Input) (*s3.ListObjectsV2Output, error)
2930
PutBucketVersioning(ctx context.Context, params *s3.PutBucketVersioningInput) (*s3.PutBucketVersioningOutput, error)
3031
PutBucketLifecycleConfiguration(ctx context.Context, params *s3.PutBucketLifecycleConfigurationInput) (*s3.PutBucketLifecycleConfigurationOutput, error)
32+
PutBucketPolicy(ctx context.Context, params *s3.PutBucketPolicyInput) (*s3.PutBucketPolicyOutput, error)
3133
}
3234

3335
type AWSS3Client struct {
@@ -64,6 +66,10 @@ func (a *AWSS3Client) PutBucketLifecycleConfiguration(ctx context.Context, param
6466
return a.Client.PutBucketLifecycleConfiguration(ctx, params)
6567
}
6668

69+
func (a *AWSS3Client) PutBucketPolicy(ctx context.Context, params *s3.PutBucketPolicyInput) (*s3.PutBucketPolicyOutput, error) {
70+
return a.Client.PutBucketPolicy(ctx, params)
71+
}
72+
6773
func CreateBucketFromS3(ctx context.Context, api S3BucketAPI, params *s3.CreateBucketInput) (*s3.CreateBucketOutput, error) {
6874
resp, berr := api.CreateBucket(ctx, params)
6975
return resp, berr
@@ -91,6 +97,33 @@ func PutBucketLifecycleConfiguration(ctx context.Context, api S3BucketAPI, param
9197
return api.PutBucketLifecycleConfiguration(ctx, params)
9298
}
9399

100+
func PutBucketPolicy(ctx context.Context, api S3BucketAPI, params *s3.PutBucketPolicyInput) (*s3.PutBucketPolicyOutput, error) {
101+
return api.PutBucketPolicy(ctx, params)
102+
}
103+
104+
func createTLSOnlyBucketPolicy(bucketName string) string {
105+
return fmt.Sprintf(`{
106+
"Version": "2012-10-17",
107+
"Statement": [
108+
{
109+
"Sid": "DenyNonTLSRequests",
110+
"Effect": "Deny",
111+
"Principal": "*",
112+
"Action": "s3:*",
113+
"Resource": [
114+
"arn:aws:s3:::%s",
115+
"arn:aws:s3:::%s/*"
116+
],
117+
"Condition": {
118+
"Bool": {
119+
"aws:SecureTransport": "false"
120+
}
121+
}
122+
}
123+
]
124+
}`, bucketName, bucketName)
125+
}
126+
94127
func InitS3Client(conf *appconfig.AppConfig) S3BucketAPI {
95128
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(conf.AWSRegion))
96129
if err != nil {
@@ -176,14 +209,27 @@ func CreateBucket(s3client S3BucketAPI, conf *appconfig.AppConfig) {
176209
}
177210
}
178211

179-
// Set bucket lifecycle length
180-
log.Printf("Setting lifecycle length on bucket: %s", bucket)
181-
berr = SetBucketLifecycleLength(s3client, conf, bucket, bucketLifecycleInDays)
212+
// Set bucket health_check object lifecycle policy
213+
log.Printf("Setting health_check object lifecycle policy on bucket: %s", bucket)
214+
berr = SetBucketHealthCheckLifecyclePolicy(s3client, conf, bucket, bucketLifecycleInDays)
182215

183216
if berr != nil {
184217
log.Errorf("Error setting lifecycle length on bucket: %v", berr)
185218
return
186219
}
220+
221+
// Apply TLS-only bucket policy
222+
policyInput := &s3.PutBucketPolicyInput{
223+
Bucket: aws.String(bucket),
224+
Policy: aws.String(createTLSOnlyBucketPolicy(bucket)),
225+
}
226+
227+
_, perr = PutBucketPolicy(context.TODO(), s3client, policyInput)
228+
if perr != nil {
229+
log.Errorf("Error setting TLS-only policy on bucket: %v", perr)
230+
return
231+
}
232+
log.Infof("Applied TLS-only policy to bucket %s", bucket)
187233
} else {
188234
log.Infof("Bucket %s exists", bucket)
189235
}
@@ -342,16 +388,19 @@ func EnableBucketVersioning(s3client S3BucketAPI, conf *appconfig.AppConfig, buc
342388
return nil
343389
}
344390

345-
func SetBucketLifecycleLength(s3client S3BucketAPI, conf *appconfig.AppConfig, bucketName string, lifecycleInDays int32) error {
391+
func SetBucketHealthCheckLifecyclePolicy(s3client S3BucketAPI, conf *appconfig.AppConfig, bucketName string, lifecycleInDays int32) error {
346392
if s3client == nil {
347393
s3client = InitS3Client(conf)
348394
}
349395

350396
lifecycleRule := &types.LifecycleRule{
397+
ID: aws.String("delete old health checks"),
351398
Expiration: &types.LifecycleExpiration{
352399
Days: lifecycleInDays,
353400
},
354-
Filter: &types.LifecycleRuleFilterMemberPrefix{},
401+
Filter: &types.LifecycleRuleFilterMemberPrefix{
402+
Value: "health_check",
403+
},
355404
Status: types.ExpirationStatusEnabled,
356405
}
357406

0 commit comments

Comments
 (0)